Skip to content

Commit

Permalink
Add tests for block specific hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
TungstnBallon committed Jan 14, 2025
1 parent f5831c1 commit 2eabbd0
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 3 deletions.
6 changes: 4 additions & 2 deletions libs/execution/src/lib/blocks/block-execution-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ export async function executeBlocks(

executionContext.enterNode(block);

await executionContext.executeHooks(inputValue); // FIXME #634: Pass the blocktype to also execute block specific hooks
await executionContext.executeHooks(inputValue);

const executionResult = await executeBlock(
inputValue,
block,
executionContext,
);
await executionContext.executeHooks(inputValue, executionResult); // FIXME #634: Pass the blocktype to also execute block specific hooks
await executionContext.executeHooks(inputValue, executionResult);

if (R.isErr(executionResult)) {
return executionResult;
}
Expand Down
1 change: 0 additions & 1 deletion libs/execution/src/lib/hooks/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export interface HookOptions {
/** Whether the pipeline should await the hooks completion. `false` if omitted.*/
blocking?: boolean;
/** Optionally specify one or more blocks to limit this hook to. If omitted, the hook will be executed on all blocks*/
// FIXME #634: Add `BlockExecutor[]` variant
blocktypes?: string | string[];
}

Expand Down
57 changes: 57 additions & 0 deletions libs/interpreter-lib/src/interpreter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,63 @@ describe('Interpreter', () => {
expect(spy).toHaveBeenCalledTimes(6);
});

it('should execute a block specific hook only on that blocktype', async () => {
const exampleFilePath =
'libs/interpreter-lib/test/assets/hooks/valid-builtin-and-composite-blocks.jv';
const model = readJvTestAsset(exampleFilePath);

const interpreter = new DefaultJayveeInterpreter({
pipelineMatcher: () => true,
debug: true,
debugGranularity: 'peek',
debugTarget: 'all',
env: new Map(),
});

const program = await interpreter.parseModel(
async (services, loggerFactory) =>
await extractAstNodeFromString<JayveeModel>(
model,
services,
loggerFactory.createLogger(),
),
);
expect(program).toBeDefined();
assert(program !== undefined);

const sqlite_spy = vi
.fn<unknown[], Promise<undefined>>()
.mockResolvedValue(undefined);

program.addHook(
'preBlock',
async (blocktype) => {
return sqlite_spy(blocktype);
},
{ blocking: true, blocktypes: 'SQLiteLoader' },
);

const interpreter_spy = vi
.fn<unknown[], Promise<undefined>>()
.mockResolvedValue(undefined);

program.addHook(
'postBlock',
async (blocktype) => {
return interpreter_spy(blocktype);
},
{ blocking: true, blocktypes: 'CSVFileInterpreter' },
);

const exitCode = await interpreter.interpretProgram(program);
expect(exitCode).toEqual(ExitCode.SUCCESS);

expect(sqlite_spy).toHaveBeenCalledTimes(1);
expect(sqlite_spy).toHaveBeenCalledWith('SQLiteLoader');
expect(interpreter_spy).toHaveBeenCalledTimes(1);
expect(interpreter_spy).toHaveBeenCalledWith('CSVFileInterpreter');
});

it('should not wait for non-blocking hooks', async () => {
const exampleFilePath = 'example/cars.jv';
const model = readJvTestAsset(exampleFilePath);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
pipeline CarsPipeline {

CarsExtractor
-> CarsInterpreter
-> NameHeaderWriter
-> CarsTableInterpreter
-> CarsLoader;


block CarsExtractor oftype HttpExtractor {
url: "https://gist.githubusercontent.com/noamross/e5d3e859aa0c794be10b/raw/b999fb4425b54c63cab088c0ce2c0d6ce961a563/cars.csv";
}

block CarsInterpreter oftype CSVFileInterpreter {
enclosing: '"';
}

block NameHeaderWriter oftype CellWriter {
at: cell A1;

write: [
"name"
];
}

block CarsTableInterpreter oftype TableInterpreter {
header: true;
columns: [
"name" oftype text,
"mpg" oftype decimal,
"cyl" oftype integer,
"disp" oftype decimal,
"hp" oftype integer,
"drat" oftype decimal,
"wt" oftype decimal,
"qsec" oftype decimal,
"vs" oftype integer,
"am" oftype integer,
"gear" oftype integer,
"carb" oftype integer
];
}

block CarsLoader oftype SQLiteLoader {
table: "Cars";
file: "./cars.sqlite";
}
}

0 comments on commit 2eabbd0

Please sign in to comment.