Skip to content

Commit

Permalink
Make tests use handwritten falsy Jayvee model
Browse files Browse the repository at this point in the history
  • Loading branch information
joluj committed Jan 9, 2024
1 parent 400b2b4 commit fa661e0
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 26 deletions.
35 changes: 9 additions & 26 deletions apps/interpreter/src/parse-only.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
//
// SPDX-License-Identifier: AGPL-3.0-only

import * as fs from 'node:fs/promises';
import * as os from 'os';
import * as fs from 'node:fs';
import * as path from 'path';
import * as process from 'process';

Expand All @@ -20,7 +19,7 @@ import {
import { runAction } from './run-action';

jest.mock('@jvalue/jayvee-interpreter-lib', () => {
const original: object = jest.requireActual('@jvalue/jayvee-interpreter-lib'); // Step 2.
const original: object = jest.requireActual('@jvalue/jayvee-interpreter-lib');
return {
...original,
interpretModel: jest.fn(),
Expand All @@ -29,8 +28,11 @@ jest.mock('@jvalue/jayvee-interpreter-lib', () => {
});

describe('Parse Only', () => {
const baseDir = path.resolve(__dirname, '../../../example/');
const pathToValidModel = path.resolve(baseDir, 'cars.jv');
const pathToValidModel = path.resolve(__dirname, '../../../example/cars.jv');
const pathToInvalidModel = path.resolve(
__dirname,
'../test/assets/broken-model.jv',
);

const defaultOptions: RunOptions = {
env: new Map<string, string>(),
Expand All @@ -39,16 +41,6 @@ describe('Parse Only', () => {
debugTarget: undefined,
};

let tempFile: string | undefined = undefined;

afterEach(async () => {
if (tempFile != null) {
await fs.rm(tempFile);
// eslint-disable-next-line require-atomic-updates
tempFile = undefined;
}
});

afterEach(() => {
// Assert that model is not executed
expect(interpretString).not.toBeCalled();
Expand Down Expand Up @@ -79,19 +71,10 @@ describe('Parse Only', () => {
});

it('should exit with 1 on error', async () => {
const validModel = (await fs.readFile(pathToValidModel)).toString();

tempFile = path.resolve(
os.tmpdir(),
// E.g. "0.gn6v6ra9575" -> "gn6v6ra9575.jv"
Math.random().toString(36).substring(2) + '.jv',
);

// Write a partial valid model in that file
await fs.writeFile(tempFile, validModel.substring(validModel.length / 2));
expect(fs.existsSync(pathToInvalidModel)).toBe(true);

await expect(
runAction(tempFile, {
runAction(pathToInvalidModel, {
...defaultOptions,
parseOnly: true,
}),
Expand Down
50 changes: 50 additions & 0 deletions apps/interpreter/test/assets/broken-model.jv
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
pipeline CarsPipeline {
// Try using a CoolCarsExtractor although we only have normal cars.
// This fill result in an error during parsing.
CoolCarsExtractor -> CarsTextFileInterpreter;

CarsTextFileInterpreter
-> CarsCSVInterpreter
-> NameHeaderWriter
-> CarsTableInterpreter
-> CarsLoader;

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

block CarsTextFileInterpreter oftype TextFileInterpreter { }

block CarsCSVInterpreter oftype CSVInterpreter {
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 fa661e0

Please sign in to comment.