n8n/packages/nodes-base/nodes/MoveBinaryData/test/MoveBinaryData.test.ts
Iván Ovejero 6d6e2488c6
refactor(core): Generalize binary data manager interface (no-changelog) (#7164)
Depends on: #7092 | Story:
[PAY-768](https://linear.app/n8n/issue/PAY-768)

This PR: 
- Generalizes the `IBinaryDataManager` interface.
- Adjusts `Filesystem.ts` to satisfy the interface.
- Sets up an S3 client stub to be filled in in the next PR.
- Turns `BinaryDataManager` into an injectable service.
- Adjusts the config schema and adds new validators.

Note that the PR looks large but all the main changes are in
`packages/core/src/binaryData`.

Out of scope:
- `BinaryDataManager` (now `BinaryDataService`) and `Filesystem.ts` (now
`fs.client.ts`) were slightly refactored for maintainability, but fully
overhauling them is **not** the focus of this PR, which is meant to
clear the way for the S3 implementation. Future improvements for these
two should include setting up a backwards-compatible dir structure that
makes it easier to locate binary data files to delete, removing
duplication, simplifying cloning methods, using integers for binary data
size instead of `prettyBytes()`, writing tests for existing binary data
logic, etc.

---------

Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
2023-09-22 17:22:12 +02:00

131 lines
3.1 KiB
TypeScript

/* 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 Move Binary Data Node', () => {
beforeEach(async () => {
await Helpers.initBinaryDataService();
});
const workflow = Helpers.readJsonFileSync(
'nodes/MoveBinaryData/test/MoveBinaryData.workflow.json',
);
const node = workflow.nodes.find((n: any) => n.name === 'Read Binary File');
node.parameters.filePath = path.join(__dirname, 'data', 'sample.json');
const tests: WorkflowTestData[] = [
{
description: 'nodes/MoveBinaryData/test/MoveBinaryData.workflow.json',
input: {
workflowData: workflow,
},
output: {
nodeData: {
'Binary to JSON': [
[
{
json: {
id: 1,
title: 'My Title',
description: 'Lorem Ipsum ...',
},
},
],
],
'Binary to JSON - No Set All Data': [
[
{
json: {
data: '{\n\t"id": 1,\n\t"title": "My Title",\n\t"description": "Lorem Ipsum ..."\n}',
},
},
],
],
'Binary to JSON - No Set All Data + Json Parse': [
[
{
json: {
data: {
id: 1,
title: 'My Title',
description: 'Lorem Ipsum ...',
},
},
},
],
],
'Binary to JSON - No Set All Data + Encoding base64': [
[
{
json: {
data: 'ewoJImlkIjogMSwKCSJ0aXRsZSI6ICJNeSBUaXRsZSIsCgkiZGVzY3JpcHRpb24iOiAiTG9yZW0gSXBzdW0gLi4uIgp9',
},
},
],
],
'Binary to JSON - Keep as Base64': [
[
{
json: {
data: 'ewoJImlkIjogMSwKCSJ0aXRsZSI6ICJNeSBUaXRsZSIsCgkiZGVzY3JpcHRpb24iOiAiTG9yZW0gSXBzdW0gLi4uIgp9',
},
},
],
],
'JSON to Binary': [
[
{
json: {},
binary: {
data: {
data: 'eyJpZCI6MSwidGl0bGUiOiJNeSBUaXRsZSIsImRlc2NyaXB0aW9uIjoiTG9yZW0gSXBzdW0gLi4uIn0=',
mimeType: 'application/json',
fileType: 'json',
fileSize: '59 B',
},
},
},
],
],
'JSON to Binary - No Convert All Data': [
[
{
json: {
id: 1,
description: 'Lorem Ipsum ...',
},
binary: {
data: {
data: 'Ik15IFRpdGxlIg==',
mimeType: 'application/json',
fileType: 'json',
fileSize: '10 B',
fileName: 'example.json',
},
},
},
],
],
},
},
},
];
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);
});
}
});