Skip to content

Commit

Permalink
Write test for passing in arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
dana-gill committed Jan 3, 2025
1 parent 37ba7df commit 4005bec
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
93 changes: 93 additions & 0 deletions packages/nodes-base/nodes/Postgres/test/v2/operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
INode,
INodeParameters,
} from 'n8n-workflow';
import pgPromise from 'pg-promise';

import * as deleteTable from '../../v2/actions/database/deleteTable.operation';
import * as executeQuery from '../../v2/actions/database/executeQuery.operation';
Expand Down Expand Up @@ -506,6 +507,7 @@ describe('Test PostgresV2, insert operation', () => {
items,
nodeOptions,
createMockDb(columnsInfo),
pgPromise(),
);

expect(runQueries).toHaveBeenCalledWith(
Expand Down Expand Up @@ -579,6 +581,7 @@ describe('Test PostgresV2, insert operation', () => {
inputItems,
nodeOptions,
createMockDb(columnsInfo),
pgPromise(),
);

expect(runQueries).toHaveBeenCalledWith(
Expand All @@ -600,6 +603,96 @@ describe('Test PostgresV2, insert operation', () => {
nodeOptions,
);
});

it('dataMode: define, should accept an array with values if column is of type json', async () => {
const convertValuesToJsonWithPgpSpy = jest.spyOn(utils, 'convertValuesToJsonWithPgp');
const hasJsonDataTypeInSchemaSpy = jest.spyOn(utils, 'hasJsonDataTypeInSchema');

const values = [
{ value: { id: 1, json: [], foo: 'data 1' }, expected: { id: 1, json: '[]', foo: 'data 1' } },
{
value: {
id: 2,
json: [0, 1],
foo: 'data 2',
},
expected: {
id: 2,
json: '[0,1]',
foo: 'data 2',
},
},
{
value: {
id: 2,
json: [0],
foo: 'data 2',
},
expected: {
id: 2,
json: '[0]',
foo: 'data 2',
},
},
];

values.forEach(async (value) => {
const valuePassedIn = value.value;
const nodeParameters: IDataObject = {
schema: {
__rl: true,
mode: 'list',
value: 'public',
},
table: {
__rl: true,
value: 'my_table',
mode: 'list',
},
columns: {
mappingMode: 'defineBelow',
value: valuePassedIn,
},
options: { nodeVersion: 2.5 },
};
const columnsInfo: ColumnInfo[] = [
{ column_name: 'id', data_type: 'integer', is_nullable: 'NO', udt_name: '' },
{ column_name: 'json', data_type: 'json', is_nullable: 'NO', udt_name: '' },
{ column_name: 'foo', data_type: 'text', is_nullable: 'NO', udt_name: '' },
];

const inputItems = [
{
json: valuePassedIn,
},
];

const nodeOptions = nodeParameters.options as IDataObject;
const pg = pgPromise();

await insert.execute.call(
createMockExecuteFunction(nodeParameters),
runQueries,
inputItems,
nodeOptions,
createMockDb(columnsInfo),
pg,
);

expect(runQueries).toHaveBeenCalledWith(
[
{
query: 'INSERT INTO $1:name.$2:name($3:name) VALUES($3:csv) RETURNING *',
values: ['public', 'my_table', value.expected],
},
],
inputItems,
nodeOptions,
);
expect(convertValuesToJsonWithPgpSpy).toHaveBeenCalledWith(pg, columnsInfo, valuePassedIn);
expect(hasJsonDataTypeInSchemaSpy).toHaveBeenCalledWith(columnsInfo);
});
});
});

describe('Test PostgresV2, select operation', () => {
Expand Down
54 changes: 54 additions & 0 deletions packages/nodes-base/nodes/Postgres/test/v2/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { IDataObject, INode } from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import pgPromise from 'pg-promise';

import type { ColumnInfo } from '../../v2/helpers/interfaces';
import {
Expand All @@ -14,6 +15,8 @@ import {
wrapData,
convertArraysToPostgresFormat,
isJSON,
convertValuesToJsonWithPgp,
hasJsonDataTypeInSchema,
} from '../../v2/helpers/utils';

const node: INode = {
Expand Down Expand Up @@ -387,6 +390,57 @@ describe('Test PostgresV2, checkItemAgainstSchema', () => {
});
});

describe('Test PostgresV2, hasJsonDataType', () => {
it('returns true if there are columns which are of type json', () => {
const schema: ColumnInfo[] = [
{ column_name: 'data', data_type: 'json', is_nullable: 'YES' },
{ column_name: 'id', data_type: 'integer', is_nullable: 'NO' },
];

expect(hasJsonDataTypeInSchema(schema)).toEqual(true);
});

it('returns false if there are columns which are of type json', () => {
const schema: ColumnInfo[] = [{ column_name: 'id', data_type: 'integer', is_nullable: 'NO' }];

expect(hasJsonDataTypeInSchema(schema)).toEqual(false);
});
});

describe('Test PostgresV2, convertValuesToJsonWithPgp', () => {
it('should use pgp to properly convert values to JSON', () => {
const pgp = pgPromise();
const pgpJsonSpy = jest.spyOn(pgp.as, 'json');

const schema: ColumnInfo[] = [
{ column_name: 'data', data_type: 'json', is_nullable: 'YES' },
{ column_name: 'id', data_type: 'integer', is_nullable: 'NO' },
];
const values = [
{
value: { data: [], id: 1 },
expected: { data: '[]', id: 1 },
},
{
value: { data: [0], id: 1 },
expected: { data: '[0]', id: 1 },
},
{
value: { data: { key: 2 }, id: 1 },
expected: { data: '{"key":2}', id: 1 },
},
];

values.forEach((value) => {
const data = value.value.data;

expect(convertValuesToJsonWithPgp(pgp, schema, value.value)).toEqual(value.expected);
expect(value.value).toEqual(value.expected);
expect(pgpJsonSpy).toHaveBeenCalledWith(data, true);
});
});
});

describe('Test PostgresV2, convertArraysToPostgresFormat', () => {
it('should convert js arrays to postgres format', () => {
const item = {
Expand Down

0 comments on commit 4005bec

Please sign in to comment.