-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd03ff0
commit 78f650f
Showing
14 changed files
with
445 additions
and
0 deletions.
There are no files selected for viewing
250 changes: 250 additions & 0 deletions
250
libs/extensions/tabular/exec/src/lib/csv-to-table-interpreter-executor.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,250 @@ | ||
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
import { readFile } from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
|
||
import * as R from '@jvalue/jayvee-execution'; | ||
import { getTestExecutionContext } from '@jvalue/jayvee-execution/test'; | ||
import { | ||
type BlockDefinition, | ||
IOType, | ||
type JayveeServices, | ||
createJayveeServices, | ||
} from '@jvalue/jayvee-language-server'; | ||
import { | ||
type ParseHelperOptions, | ||
expectNoParserAndLexerErrors, | ||
loadTestExtensions, | ||
parseHelper, | ||
readJvTestAssetHelper, | ||
} from '@jvalue/jayvee-language-server/test'; | ||
import { | ||
type AstNode, | ||
type AstNodeLocator, | ||
type LangiumDocument, | ||
} from 'langium'; | ||
import { NodeFileSystem } from 'langium/node'; | ||
|
||
import { CSVToTableInterpreterExecutor } from './csv-to-table-interpreter-executor'; | ||
|
||
describe('Validation of CSVToTableInterpreterExecutor', () => { | ||
let parse: ( | ||
input: string, | ||
options?: ParseHelperOptions, | ||
) => Promise<LangiumDocument<AstNode>>; | ||
|
||
let locator: AstNodeLocator; | ||
let services: JayveeServices; | ||
|
||
const readJvTestAsset = readJvTestAssetHelper( | ||
__dirname, | ||
'../../test/assets/csv-to-table-interpreter-executor/', | ||
); | ||
|
||
async function readTestCSV(fileName: string): Promise<R.BinaryFile> { | ||
const absoluteFileName = path.resolve( | ||
__dirname, | ||
'../../test/assets/csv-to-table-interpreter-executor/', | ||
fileName, | ||
); | ||
|
||
const content = await readFile(absoluteFileName); | ||
|
||
return new R.BinaryFile( | ||
fileName, | ||
R.FileExtension.CSV, | ||
R.MimeType.TEXT_CSV, | ||
content, | ||
); | ||
} | ||
|
||
async function parseAndExecuteExecutor( | ||
input: string, | ||
IOInput: R.BinaryFile, | ||
): Promise<R.Result<R.Table>> { | ||
const document = await parse(input, { validation: true }); | ||
expectNoParserAndLexerErrors(document); | ||
|
||
const block = locator.getAstNode<BlockDefinition>( | ||
document.parseResult.value, | ||
'pipelines@0/blocks@1', | ||
) as BlockDefinition; | ||
|
||
return new CSVToTableInterpreterExecutor().doExecute( | ||
IOInput, | ||
getTestExecutionContext(locator, document, services, [block]), | ||
); | ||
} | ||
|
||
beforeAll(async () => { | ||
// Create language services | ||
services = createJayveeServices(NodeFileSystem).Jayvee; | ||
await loadTestExtensions(services, [ | ||
path.resolve(__dirname, '../../test/test-extension/TestBlockTypes.jv'), | ||
]); | ||
locator = services.workspace.AstNodeLocator; | ||
// Parse function for Jayvee (without validation) | ||
parse = parseHelper(services); | ||
}); | ||
|
||
describe('validation of sheet with header', () => { | ||
it('should diagnose no error on valid sheet', async () => { | ||
const text = readJvTestAsset('valid-with-header.jv'); | ||
|
||
const testFile = await readTestCSV('test-with-header.csv'); | ||
const result = await parseAndExecuteExecutor(text, testFile); | ||
|
||
expect(R.isErr(result)).toEqual(false); | ||
if (R.isOk(result)) { | ||
expect(result.right.ioType).toEqual(IOType.TABLE); | ||
expect(result.right.getNumberOfColumns()).toEqual(3); | ||
expect(result.right.getNumberOfRows()).toEqual(16); | ||
expect(result.right.getColumn('index')).toEqual( | ||
expect.objectContaining({ | ||
values: expect.arrayContaining([0, 1, 2, 15]) as number[], | ||
}), | ||
); | ||
expect(result.right.getColumn('name')).toEqual( | ||
expect.objectContaining({ | ||
values: expect.arrayContaining(['Test']) as string[], | ||
}), | ||
); | ||
expect(result.right.getColumn('flag')).toEqual( | ||
expect.objectContaining({ | ||
values: expect.arrayContaining([true, false]) as boolean[], | ||
}), | ||
); | ||
} | ||
}); | ||
|
||
it('should diagnose empty table on empty column parameter', async () => { | ||
const text = readJvTestAsset('valid-empty-columns-with-header.jv'); | ||
|
||
const testFile = await readTestCSV('test-with-header.csv'); | ||
const result = await parseAndExecuteExecutor(text, testFile); | ||
|
||
expect(R.isErr(result)).toEqual(false); | ||
if (R.isOk(result)) { | ||
expect(result.right.ioType).toEqual(IOType.TABLE); | ||
expect(result.right.getNumberOfColumns()).toEqual(0); | ||
expect(result.right.getNumberOfRows()).toEqual(0); | ||
} | ||
}); | ||
|
||
it('should diagnose skipping row on wrong cell value type', async () => { | ||
const text = readJvTestAsset('valid-wrong-value-type-with-header.jv'); | ||
|
||
const testFile = await readTestCSV('test-with-header.csv'); | ||
const result = await parseAndExecuteExecutor(text, testFile); | ||
|
||
expect(R.isErr(result)).toEqual(false); | ||
if (R.isOk(result)) { | ||
expect(result.right.ioType).toEqual(IOType.TABLE); | ||
expect(result.right.getNumberOfColumns()).toEqual(3); | ||
expect(result.right.getNumberOfRows()).toEqual(0); | ||
} | ||
}); | ||
}); | ||
|
||
describe('validation of sheet without header', () => { | ||
it('should diagnose no error on valid sheet', async () => { | ||
const text = readJvTestAsset('valid-without-header.jv'); | ||
|
||
const testFile = await readTestCSV('test-without-header.csv'); | ||
const result = await parseAndExecuteExecutor(text, testFile); | ||
|
||
expect(R.isErr(result)).toEqual(false); | ||
if (R.isOk(result)) { | ||
expect(result.right.ioType).toEqual(IOType.TABLE); | ||
expect(result.right.getNumberOfColumns()).toEqual(3); | ||
expect(result.right.getNumberOfRows()).toEqual(16); | ||
expect(result.right.getColumn('index')).toEqual( | ||
expect.objectContaining({ | ||
values: expect.arrayContaining([0, 1, 2, 15]) as number[], | ||
}), | ||
); | ||
expect(result.right.getColumn('name')).toEqual( | ||
expect.objectContaining({ | ||
values: expect.arrayContaining(['Test']) as string[], | ||
}), | ||
); | ||
expect(result.right.getColumn('flag')).toEqual( | ||
expect.objectContaining({ | ||
values: expect.arrayContaining([true, false]) as boolean[], | ||
}), | ||
); | ||
} | ||
}); | ||
|
||
it('should diagnose no error on valid sheet with header', async () => { | ||
const text = readJvTestAsset('valid-without-header.jv'); | ||
|
||
const testFile = await readTestCSV('test-with-header.csv'); | ||
const result = await parseAndExecuteExecutor(text, testFile); | ||
|
||
expect(R.isErr(result)).toEqual(false); | ||
if (R.isOk(result)) { | ||
expect(result.right.ioType).toEqual(IOType.TABLE); | ||
expect(result.right.getNumberOfColumns()).toEqual(3); | ||
expect(result.right.getNumberOfRows()).toEqual(16); | ||
expect(result.right.getColumn('index')).toEqual( | ||
expect.objectContaining({ | ||
values: expect.arrayContaining([0, 1, 2, 15]) as number[], | ||
}), | ||
); | ||
expect(result.right.getColumn('name')).toEqual( | ||
expect.objectContaining({ | ||
values: expect.arrayContaining(['Test']) as string[], | ||
}), | ||
); | ||
expect(result.right.getColumn('flag')).toEqual( | ||
expect.objectContaining({ | ||
values: expect.arrayContaining([true, false]) as boolean[], | ||
}), | ||
); | ||
} | ||
}); | ||
|
||
it('should diagnose empty table on empty column parameter', async () => { | ||
const text = readJvTestAsset('valid-empty-columns-without-header.jv'); | ||
|
||
const testFile = await readTestCSV('test-without-header.csv'); | ||
const result = await parseAndExecuteExecutor(text, testFile); | ||
|
||
expect(R.isErr(result)).toEqual(false); | ||
if (R.isOk(result)) { | ||
expect(result.right.ioType).toEqual(IOType.TABLE); | ||
expect(result.right.getNumberOfColumns()).toEqual(0); | ||
expect(result.right.getNumberOfRows()).toEqual(0); | ||
} | ||
}); | ||
|
||
it('should diagnose empty table on empty sheet', async () => { | ||
const text = readJvTestAsset('valid-without-header.jv'); | ||
|
||
const testFile = await readTestCSV('test-empty.csv'); | ||
const result = await parseAndExecuteExecutor(text, testFile); | ||
|
||
expect(R.isErr(result)).toEqual(false); | ||
if (R.isOk(result)) { | ||
expect(result.right.isEmpty()).toEqual(true); | ||
} | ||
}); | ||
|
||
it('should diagnose skipping row on wrong cell value type', async () => { | ||
const text = readJvTestAsset('valid-wrong-value-type-without-header.jv'); | ||
|
||
const testFile = await readTestCSV('test-without-header.csv'); | ||
const result = await parseAndExecuteExecutor(text, testFile); | ||
|
||
expect(R.isErr(result)).toEqual(false); | ||
if (R.isOk(result)) { | ||
expect(result.right.ioType).toEqual(IOType.TABLE); | ||
expect(result.right.getNumberOfColumns()).toEqual(3); | ||
expect(result.right.getNumberOfRows()).toEqual(0); | ||
} | ||
}); | ||
}); | ||
}); |
Empty file.
3 changes: 3 additions & 0 deletions
3
...ensions/tabular/exec/test/assets/csv-to-table-interpreter-executor/test-empty.csv.license
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
SPDX-FileCopyrightText: 2024 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
|
||
SPDX-License-Identifier: AGPL-3.0-only |
17 changes: 17 additions & 0 deletions
17
...xtensions/tabular/exec/test/assets/csv-to-table-interpreter-executor/test-with-header.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
index,name,flag | ||
0,Test,true | ||
1,Test,false | ||
2,Test,false | ||
3,Test,true | ||
4,Test,true | ||
5,Test,false | ||
6,Test,false | ||
7,Test,false | ||
8,Test,false | ||
9,Test,true | ||
10,Test,false | ||
11,Test,false | ||
12,Test,true | ||
13,Test,true | ||
14,Test,true | ||
15,Test,true |
3 changes: 3 additions & 0 deletions
3
...s/tabular/exec/test/assets/csv-to-table-interpreter-executor/test-with-header.csv.license
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
SPDX-FileCopyrightText: 2024 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
|
||
SPDX-License-Identifier: AGPL-3.0-only |
16 changes: 16 additions & 0 deletions
16
...nsions/tabular/exec/test/assets/csv-to-table-interpreter-executor/test-without-header.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
0,Test,true | ||
1,Test,false | ||
2,Test,false | ||
3,Test,true | ||
4,Test,true | ||
5,Test,false | ||
6,Test,false | ||
7,Test,false | ||
8,Test,false | ||
9,Test,true | ||
10,Test,false | ||
11,Test,false | ||
12,Test,true | ||
13,Test,true | ||
14,Test,true | ||
15,Test,true |
3 changes: 3 additions & 0 deletions
3
...abular/exec/test/assets/csv-to-table-interpreter-executor/test-without-header.csv.license
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
SPDX-FileCopyrightText: 2024 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
|
||
SPDX-License-Identifier: AGPL-3.0-only |
19 changes: 19 additions & 0 deletions
19
...lar/exec/test/assets/csv-to-table-interpreter-executor/valid-empty-columns-with-header.jv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
pipeline TestPipeline { | ||
|
||
block TestExtractor oftype TestSheetExtractor { | ||
} | ||
|
||
block TestBlock oftype CSVToTableInterpreter { | ||
header: true; | ||
columns: []; | ||
} | ||
|
||
block TestLoader oftype TestTableLoader { | ||
} | ||
|
||
TestExtractor -> TestBlock -> TestLoader; | ||
} |
19 changes: 19 additions & 0 deletions
19
.../exec/test/assets/csv-to-table-interpreter-executor/valid-empty-columns-without-header.jv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
pipeline TestPipeline { | ||
|
||
block TestExtractor oftype TestSheetExtractor { | ||
} | ||
|
||
block TestBlock oftype CSVToTableInterpreter { | ||
header: false; | ||
columns: []; | ||
} | ||
|
||
block TestLoader oftype TestTableLoader { | ||
} | ||
|
||
TestExtractor -> TestBlock -> TestLoader; | ||
} |
23 changes: 23 additions & 0 deletions
23
...bular/exec/test/assets/csv-to-table-interpreter-executor/valid-with-capitalized-header.jv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
pipeline TestPipeline { | ||
|
||
block TestExtractor oftype TestSheetExtractor { | ||
} | ||
|
||
block TestBlock oftype CSVToTableInterpreter { | ||
header: true; | ||
columns: [ | ||
"Index" oftype integer, | ||
"Name" oftype text, | ||
"Flag" oftype boolean | ||
]; | ||
} | ||
|
||
block TestLoader oftype TestTableLoader { | ||
} | ||
|
||
TestExtractor -> TestBlock -> TestLoader; | ||
} |
23 changes: 23 additions & 0 deletions
23
...xtensions/tabular/exec/test/assets/csv-to-table-interpreter-executor/valid-with-header.jv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SPDX-FileCopyrightText: 2023 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
pipeline TestPipeline { | ||
|
||
block TestExtractor oftype TestFileExtractor { | ||
} | ||
|
||
block TestBlock oftype CSVToTableInterpreter { | ||
header: true; | ||
columns: [ | ||
"index" oftype integer, | ||
"name" oftype text, | ||
"flag" oftype boolean | ||
]; | ||
} | ||
|
||
block TestLoader oftype TestTableLoader { | ||
} | ||
|
||
TestExtractor -> TestBlock -> TestLoader; | ||
} |
Oops, something went wrong.