mirror of
https://github.com/n8n-io/n8n.git
synced 2025-02-02 07:01:30 -08:00
test(Write Binary File Node): Unit tests (no-changelog) (#5440)
* ✅ add write binary file test * 🎨 cleanup --------- Co-authored-by: Michael Kret <michael.k@radency.com>
This commit is contained in:
parent
136b74de5e
commit
5b1e3a3fd3
|
@ -0,0 +1,70 @@
|
||||||
|
/* eslint-disable @typescript-eslint/no-loop-func */
|
||||||
|
import * as Helpers from '../../../test/nodes/Helpers';
|
||||||
|
import type { WorkflowTestData } from '../../../test/nodes/types';
|
||||||
|
import { executeWorkflow } from '../../../test/nodes/ExecuteWorkflow';
|
||||||
|
import path from 'path';
|
||||||
|
|
||||||
|
describe('Test Write Binary File Node', () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
await Helpers.initBinaryDataManager();
|
||||||
|
});
|
||||||
|
|
||||||
|
const temporaryDir = Helpers.createTemporaryDir();
|
||||||
|
const workflow = Helpers.readJsonFileSync(
|
||||||
|
'nodes/WriteBinaryFile/test/WriteBinaryFile.workflow.json',
|
||||||
|
);
|
||||||
|
const readFileNode = workflow.nodes.find((n: any) => n.name === 'Read Binary File');
|
||||||
|
readFileNode.parameters.filePath = path.join(__dirname, 'image.jpg');
|
||||||
|
|
||||||
|
const writeFileNode = workflow.nodes.find((n: any) => n.name === 'Write Binary File');
|
||||||
|
const writeFilePath = path.join(temporaryDir, 'image-written.jpg');
|
||||||
|
writeFileNode.parameters.fileName = writeFilePath;
|
||||||
|
|
||||||
|
const tests: WorkflowTestData[] = [
|
||||||
|
{
|
||||||
|
description: 'nodes/WriteBinaryFile/test/WriteBinaryFile.workflow.json',
|
||||||
|
input: {
|
||||||
|
workflowData: workflow,
|
||||||
|
},
|
||||||
|
output: {
|
||||||
|
nodeData: {
|
||||||
|
'Write Binary File': [
|
||||||
|
[
|
||||||
|
{
|
||||||
|
json: {
|
||||||
|
fileName: writeFilePath,
|
||||||
|
},
|
||||||
|
binary: {
|
||||||
|
data: {
|
||||||
|
mimeType: 'image/jpeg',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ut0iSeWoOAKAdO27NCqUN8oQrcHDqccDrQDpyRNPdRwEKcsx7CobIebPLORwThc0inGMF724jagNpxG4OOM1dIDAgjIPBpkqUOxnR2pmh85pW3nJB9KkNi4yqTssZ6rSNXNX0ehHFfusYDLuI7+tXY4I40ChQcdzQRKcL7Fb7PcQO32cqUY5we1XqZPtH11KsFoFDGYK7sckkZxVqgTnJlEQXMBZYGUoTkZ7VeoH7RvcqwWaIh80K7k5JIq1QJzkyhbMtvdSxMdqnlc1amgjmx5i5I70inNSVpFdrmaWRltkBVerHvUW57B2AUNGxyOaC+VW9xXLVrcGbcjrtkXqKZZxvveeTAL9APSgiooq1ty3RTMj//2Q==',
|
||||||
|
directory: __dirname,
|
||||||
|
fileName: 'image.jpg',
|
||||||
|
fileSize: '1.04 kB',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const nodeTypes = Helpers.setup(tests);
|
||||||
|
|
||||||
|
for (const testData of tests) {
|
||||||
|
test(testData.description, async () => {
|
||||||
|
const { result } = await executeWorkflow(testData, nodeTypes);
|
||||||
|
|
||||||
|
const resultNodeData = Helpers.getResultNodeData(result, testData);
|
||||||
|
resultNodeData.forEach(({ nodeName, resultData }) =>
|
||||||
|
expect(resultData).toEqual(testData.output.nodeData[nodeName]),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result.finished).toEqual(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
|
@ -0,0 +1,76 @@
|
||||||
|
{
|
||||||
|
"name": "Write Binary File unit test",
|
||||||
|
"nodes": [
|
||||||
|
{
|
||||||
|
"parameters": {},
|
||||||
|
"id": "b319a78c-ff34-4888-b412-e08609bd84e4",
|
||||||
|
"name": "When clicking \"Execute Workflow\"",
|
||||||
|
"type": "n8n-nodes-base.manualTrigger",
|
||||||
|
"typeVersion": 1,
|
||||||
|
"position": [
|
||||||
|
1040,
|
||||||
|
640
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"parameters": {
|
||||||
|
"fileName": "C:\\Test\\image-written.jpg",
|
||||||
|
"options": {}
|
||||||
|
},
|
||||||
|
"id": "804f7422-7414-4557-bf3b-0aa0e8f4885e",
|
||||||
|
"name": "Write Binary File",
|
||||||
|
"type": "n8n-nodes-base.writeBinaryFile",
|
||||||
|
"typeVersion": 1,
|
||||||
|
"position": [
|
||||||
|
1540,
|
||||||
|
640
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"parameters": {
|
||||||
|
"filePath": "C:\\Test\\image.jpg"
|
||||||
|
},
|
||||||
|
"id": "7bed9527-c0a9-4b03-8f2f-d96d805f7fbc",
|
||||||
|
"name": "Read Binary File",
|
||||||
|
"type": "n8n-nodes-base.readBinaryFile",
|
||||||
|
"typeVersion": 1,
|
||||||
|
"position": [
|
||||||
|
1300,
|
||||||
|
640
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"pinData": {},
|
||||||
|
"connections": {
|
||||||
|
"When clicking \"Execute Workflow\"": {
|
||||||
|
"main": [
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"node": "Read Binary File",
|
||||||
|
"type": "main",
|
||||||
|
"index": 0
|
||||||
|
}
|
||||||
|
]
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"Read Binary File": {
|
||||||
|
"main": [
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"node": "Write Binary File",
|
||||||
|
"type": "main",
|
||||||
|
"index": 0
|
||||||
|
}
|
||||||
|
]
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"active": false,
|
||||||
|
"settings": {},
|
||||||
|
"versionId": "eb891093-ce90-44d8-9325-b2130e59819d",
|
||||||
|
"id": "185",
|
||||||
|
"meta": {
|
||||||
|
"instanceId": "104a4d08d8897b8bdeb38aaca515021075e0bd8544c983c2bb8c86e6a8e6081c"
|
||||||
|
},
|
||||||
|
"tags": []
|
||||||
|
}
|
BIN
packages/nodes-base/nodes/WriteBinaryFile/test/image.jpg
Normal file
BIN
packages/nodes-base/nodes/WriteBinaryFile/test/image.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 1 KiB |
|
@ -158,8 +158,12 @@ const loadKnownNodes = (): Record<string, LoadingDetails> => {
|
||||||
return knownNodes!;
|
return knownNodes!;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export function createTemporaryDir(prefix: string = 'n8n') {
|
||||||
|
return mkdtempSync(path.join(tmpdir(), prefix));
|
||||||
|
}
|
||||||
|
|
||||||
export async function initBinaryDataManager(mode: 'default' | 'filesystem' = 'default') {
|
export async function initBinaryDataManager(mode: 'default' | 'filesystem' = 'default') {
|
||||||
const temporaryDir = mkdtempSync(path.join(tmpdir(), 'n8n'));
|
const temporaryDir = createTemporaryDir();
|
||||||
await BinaryDataManager.init({
|
await BinaryDataManager.init({
|
||||||
mode,
|
mode,
|
||||||
availableModes: mode,
|
availableModes: mode,
|
||||||
|
@ -167,6 +171,7 @@ export async function initBinaryDataManager(mode: 'default' | 'filesystem' = 'de
|
||||||
binaryDataTTL: 1,
|
binaryDataTTL: 1,
|
||||||
persistedBinaryDataTTL: 1,
|
persistedBinaryDataTTL: 1,
|
||||||
});
|
});
|
||||||
|
return temporaryDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function setup(testData: Array<WorkflowTestData> | WorkflowTestData) {
|
export function setup(testData: Array<WorkflowTestData> | WorkflowTestData) {
|
||||||
|
|
Loading…
Reference in a new issue