2023-10-18 07:57:37 -07:00
|
|
|
import path from 'path';
|
2024-05-23 06:04:46 -07:00
|
|
|
import { readFileSync } from 'fs';
|
2023-10-18 07:57:37 -07:00
|
|
|
import type { IWorkflowBase } from 'n8n-workflow';
|
2023-07-12 02:15:38 -07:00
|
|
|
import * as Helpers from '@test/nodes/Helpers';
|
|
|
|
import type { WorkflowTestData } from '@test/nodes/types';
|
|
|
|
import { executeWorkflow } from '@test/nodes/ExecuteWorkflow';
|
2023-02-09 00:00:29 -08:00
|
|
|
|
|
|
|
describe('Execute Spreadsheet File Node', () => {
|
|
|
|
beforeEach(async () => {
|
2023-09-22 08:22:12 -07:00
|
|
|
await Helpers.initBinaryDataService();
|
2023-02-09 00:00:29 -08:00
|
|
|
});
|
|
|
|
|
2024-05-23 06:04:46 -07:00
|
|
|
const readBinaryFile = (fileName: string) =>
|
|
|
|
readFileSync(path.resolve(__dirname, fileName), 'base64');
|
|
|
|
|
2023-10-18 07:57:37 -07:00
|
|
|
const loadWorkflow = (fileName: string, csvName: string) => {
|
|
|
|
const workflow = Helpers.readJsonFileSync<IWorkflowBase>(
|
|
|
|
`nodes/SpreadsheetFile/test/${fileName}`,
|
|
|
|
);
|
|
|
|
const node = workflow.nodes.find((n) => n.name === 'Read Binary File');
|
|
|
|
node!.parameters.fileSelector = path.join(__dirname, csvName);
|
|
|
|
return workflow;
|
|
|
|
};
|
2023-02-09 00:00:29 -08:00
|
|
|
|
|
|
|
const tests: WorkflowTestData[] = [
|
|
|
|
{
|
|
|
|
description: 'execute workflow.json',
|
|
|
|
input: {
|
2023-10-18 07:57:37 -07:00
|
|
|
workflowData: loadWorkflow('workflow.json', 'spreadsheet.csv'),
|
2023-02-09 00:00:29 -08:00
|
|
|
},
|
|
|
|
output: {
|
|
|
|
nodeData: {
|
|
|
|
'Read From File': [
|
|
|
|
[
|
|
|
|
{
|
|
|
|
json: { A: 1, B: 2, C: 3 },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
json: { A: 4, B: 5, C: 6 },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'Read From File Range': [
|
|
|
|
[
|
|
|
|
{
|
|
|
|
json: { '1': 4, '2': 5 },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'Read From File no Header Row': [
|
|
|
|
[
|
|
|
|
{
|
|
|
|
json: {
|
|
|
|
row: ['A', 'B', 'C'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
json: {
|
|
|
|
row: [1, 2, 3],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
json: {
|
|
|
|
row: [4, 5, 6],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'Read From File Raw Data': [
|
|
|
|
[
|
|
|
|
{
|
|
|
|
json: { A: '1', B: '2', C: '3' },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
json: { A: '4', B: '5', C: '6' },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'Read From File Read as String': [
|
|
|
|
[
|
|
|
|
{
|
|
|
|
json: { A: 1, B: 2, C: 3 },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
json: { A: 4, B: 5, C: 6 },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
2023-10-18 07:57:37 -07:00
|
|
|
'Read CSV with Row Limit': [[{ json: { A: '1', B: '2', C: '3' } }]],
|
2023-02-09 00:00:29 -08:00
|
|
|
'Write To File CSV': [
|
|
|
|
[
|
|
|
|
{
|
|
|
|
json: {},
|
|
|
|
binary: {
|
|
|
|
data: {
|
|
|
|
mimeType: 'text/csv',
|
|
|
|
fileType: 'text',
|
|
|
|
fileExtension: 'csv',
|
2023-05-04 08:10:54 -07:00
|
|
|
data: '77u/QSxCLEMKMSwyLDMKNCw1LDY=',
|
2023-02-09 00:00:29 -08:00
|
|
|
fileName: 'spreadsheet.csv',
|
2023-05-04 08:10:54 -07:00
|
|
|
fileSize: '20 B',
|
2023-02-09 00:00:29 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'Write To File HTML': [
|
|
|
|
[
|
|
|
|
{
|
|
|
|
json: {},
|
|
|
|
binary: {
|
|
|
|
data: {
|
|
|
|
mimeType: 'text/html',
|
2023-10-11 03:09:19 -07:00
|
|
|
fileType: 'html',
|
2023-02-09 00:00:29 -08:00
|
|
|
fileExtension: 'html',
|
2024-05-23 06:04:46 -07:00
|
|
|
data: readBinaryFile('spreadsheet.html'),
|
2023-02-09 00:00:29 -08:00
|
|
|
fileName: 'spreadsheet.html',
|
|
|
|
fileSize: '535 B',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
// ODS file has slight differences every time it's created
|
|
|
|
//
|
|
|
|
'Write To File RTF': [
|
|
|
|
[
|
|
|
|
{
|
|
|
|
json: {},
|
|
|
|
binary: {
|
|
|
|
data: {
|
|
|
|
mimeType: 'application/rtf',
|
|
|
|
fileExtension: 'rtf',
|
2024-05-23 06:04:46 -07:00
|
|
|
data: readBinaryFile('spreadsheet.rtf'),
|
2023-02-09 00:00:29 -08:00
|
|
|
fileName: 'spreadsheet.rtf',
|
|
|
|
fileSize: '267 B',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'Write To File XLS': [
|
|
|
|
[
|
|
|
|
{
|
|
|
|
json: {},
|
|
|
|
binary: {
|
|
|
|
data: {
|
|
|
|
mimeType: 'application/vnd.ms-excel',
|
|
|
|
fileExtension: 'xls',
|
2024-05-23 06:04:46 -07:00
|
|
|
data: readBinaryFile('spreadsheet.xls'),
|
2023-02-09 00:00:29 -08:00
|
|
|
fileName: 'spreadsheet.xls',
|
|
|
|
fileSize: '3.58 kB',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-10-18 07:57:37 -07:00
|
|
|
{
|
|
|
|
description: 'execute workflow.bom.json',
|
|
|
|
input: {
|
|
|
|
workflowData: loadWorkflow('workflow.bom.json', 'bom.csv'),
|
|
|
|
},
|
|
|
|
output: {
|
|
|
|
nodeData: {
|
|
|
|
'Edit with BOM included': [[{ json: { X: null } }]],
|
|
|
|
'Edit with BOM excluded': [[{ json: { X: '1' } }]],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-10-24 06:22:45 -07:00
|
|
|
{
|
|
|
|
description: 'execute includeempty.json',
|
|
|
|
input: {
|
|
|
|
workflowData: loadWorkflow('workflow.empty.json', 'includeempty.csv'),
|
|
|
|
},
|
|
|
|
output: {
|
|
|
|
nodeData: {
|
|
|
|
'Include Empty': [[{ json: { A: '1', B: '', C: '3' } }]],
|
|
|
|
'Ignore Empty': [[{ json: { A: '1', C: '3' } }]],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-11-14 02:27:37 -08:00
|
|
|
{
|
|
|
|
description: 'execute utf8.json',
|
|
|
|
input: {
|
|
|
|
workflowData: loadWorkflow('workflow.utf8.json', 'utf8.csv'),
|
|
|
|
},
|
|
|
|
output: {
|
|
|
|
nodeData: {
|
|
|
|
'Parse UTF8 v1': [
|
|
|
|
[{ json: { A: 1, B: '株式会社', C: 3 } }, { json: { A: 4, B: 5, C: '🐛' } }],
|
|
|
|
],
|
|
|
|
'Parse UTF8 v2': [
|
|
|
|
[{ json: { A: '1', B: '株式会社', C: '3' } }, { json: { A: '4', B: '5', C: '🐛' } }],
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-02-09 00:00:29 -08:00
|
|
|
];
|
|
|
|
|
|
|
|
const nodeTypes = Helpers.setup(tests);
|
|
|
|
|
|
|
|
for (const testData of tests) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-loop-func
|
|
|
|
test(testData.description, async () => {
|
|
|
|
// execute workflow
|
|
|
|
const { result } = await executeWorkflow(testData, nodeTypes);
|
|
|
|
|
|
|
|
// check if result node data matches expected test data
|
|
|
|
const resultNodeData = Helpers.getResultNodeData(result, testData);
|
|
|
|
resultNodeData.forEach(({ nodeName, resultData }) =>
|
|
|
|
expect(resultData).toEqual(testData.output.nodeData[nodeName]),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(result.finished).toEqual(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|