mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
refactor: Overhaul nodes-testing setup - Part 1 (no-changelog) (#14303)
This commit is contained in:
committed by
GitHub
parent
f85b851851
commit
73e8d76e13
@@ -0,0 +1,162 @@
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import pgPromise from 'pg-promise';
|
||||
|
||||
import * as PostgresFun from '../v1/genericFunctions';
|
||||
import type { PgpDatabase } from '../v2/helpers/interfaces';
|
||||
|
||||
type NodeParams = Record<string, string | {}>;
|
||||
|
||||
const pgp = pgPromise();
|
||||
const db = mock<PgpDatabase>();
|
||||
|
||||
describe('pgUpdate', () => {
|
||||
it('runs query to update db', async () => {
|
||||
const updateItem = { id: 1234, name: 'test' };
|
||||
const nodeParams: NodeParams = {
|
||||
table: 'mytable',
|
||||
schema: 'myschema',
|
||||
updateKey: 'id',
|
||||
columns: 'id,name',
|
||||
additionalFields: {},
|
||||
returnFields: '*',
|
||||
};
|
||||
const getNodeParam = (key: string) => nodeParams[key];
|
||||
|
||||
const items = [
|
||||
{
|
||||
json: updateItem,
|
||||
},
|
||||
];
|
||||
|
||||
await PostgresFun.pgUpdate(getNodeParam, pgp, db, items);
|
||||
|
||||
expect(db.any).toHaveBeenCalledWith(
|
||||
'update "myschema"."mytable" as t set "id"=v."id","name"=v."name" from (values(1234,\'test\')) as v("id","name") WHERE v."id" = t."id" RETURNING *',
|
||||
);
|
||||
});
|
||||
|
||||
it('runs query to update db if updateKey is not in columns', async () => {
|
||||
const updateItem = { id: 1234, name: 'test' };
|
||||
const nodeParams: NodeParams = {
|
||||
table: 'mytable',
|
||||
schema: 'myschema',
|
||||
updateKey: 'id',
|
||||
columns: 'name',
|
||||
additionalFields: {},
|
||||
returnFields: '*',
|
||||
};
|
||||
const getNodeParam = (key: string) => nodeParams[key];
|
||||
|
||||
const items = [
|
||||
{
|
||||
json: updateItem,
|
||||
},
|
||||
];
|
||||
|
||||
await PostgresFun.pgUpdate(getNodeParam, pgp, db, items);
|
||||
|
||||
expect(db.any).toHaveBeenCalledWith(
|
||||
'update "myschema"."mytable" as t set "id"=v."id","name"=v."name" from (values(1234,\'test\')) as v("id","name") WHERE v."id" = t."id" RETURNING *',
|
||||
);
|
||||
});
|
||||
|
||||
it('runs query to update db with cast as updateKey', async () => {
|
||||
const updateItem = { id: '1234', name: 'test' };
|
||||
const nodeParams: NodeParams = {
|
||||
table: 'mytable',
|
||||
schema: 'myschema',
|
||||
updateKey: 'id:uuid',
|
||||
columns: 'name',
|
||||
additionalFields: {},
|
||||
returnFields: '*',
|
||||
};
|
||||
const getNodeParam = (key: string) => nodeParams[key];
|
||||
|
||||
const items = [
|
||||
{
|
||||
json: updateItem,
|
||||
},
|
||||
];
|
||||
|
||||
await PostgresFun.pgUpdate(getNodeParam, pgp, db, items);
|
||||
|
||||
expect(db.any).toHaveBeenCalledWith(
|
||||
'update "myschema"."mytable" as t set "id"=v."id","name"=v."name" from (values(\'1234\'::uuid,\'test\')) as v("id","name") WHERE v."id" = t."id" RETURNING *',
|
||||
);
|
||||
});
|
||||
|
||||
it('runs query to update db with cast in target columns', async () => {
|
||||
const updateItem = { id: '1234', name: 'test' };
|
||||
const nodeParams: NodeParams = {
|
||||
table: 'mytable',
|
||||
schema: 'myschema',
|
||||
updateKey: 'id',
|
||||
columns: 'id:uuid,name',
|
||||
additionalFields: {},
|
||||
returnFields: '*',
|
||||
};
|
||||
const getNodeParam = (key: string) => nodeParams[key];
|
||||
|
||||
const items = [
|
||||
{
|
||||
json: updateItem,
|
||||
},
|
||||
];
|
||||
|
||||
await PostgresFun.pgUpdate(getNodeParam, pgp, db, items);
|
||||
|
||||
expect(db.any).toHaveBeenCalledWith(
|
||||
'update "myschema"."mytable" as t set "id"=v."id","name"=v."name" from (values(\'1234\'::uuid,\'test\')) as v("id","name") WHERE v."id" = t."id" RETURNING *',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('pgInsert', () => {
|
||||
it('runs query to insert', async () => {
|
||||
const insertItem = { id: 1234, name: 'test', age: 34 };
|
||||
const nodeParams: NodeParams = {
|
||||
table: 'mytable',
|
||||
schema: 'myschema',
|
||||
columns: 'id,name,age',
|
||||
returnFields: '*',
|
||||
additionalFields: {},
|
||||
};
|
||||
const getNodeParam = (key: string) => nodeParams[key];
|
||||
|
||||
const items = [
|
||||
{
|
||||
json: insertItem,
|
||||
},
|
||||
];
|
||||
|
||||
await PostgresFun.pgInsert(getNodeParam, pgp, db, items, false);
|
||||
|
||||
expect(db.any).toHaveBeenCalledWith(
|
||||
'insert into "myschema"."mytable"("id","name","age") values(1234,\'test\',34) RETURNING *',
|
||||
);
|
||||
});
|
||||
|
||||
it('runs query to insert with type casting', async () => {
|
||||
const insertItem = { id: 1234, name: 'test', age: 34 };
|
||||
const nodeParams: NodeParams = {
|
||||
table: 'mytable',
|
||||
schema: 'myschema',
|
||||
columns: 'id:int,name:text,age',
|
||||
returnFields: '*',
|
||||
additionalFields: {},
|
||||
};
|
||||
const getNodeParam = (key: string) => nodeParams[key];
|
||||
|
||||
const items = [
|
||||
{
|
||||
json: insertItem,
|
||||
},
|
||||
];
|
||||
|
||||
await PostgresFun.pgInsert(getNodeParam, pgp, db, items, false);
|
||||
|
||||
expect(db.any).toHaveBeenCalledWith(
|
||||
'insert into "myschema"."mytable"("id","name","age") values(1234::int,\'test\'::text,34) RETURNING *',
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -5,6 +5,8 @@ import type pg from 'pg-promise/typescript/pg-subset';
|
||||
|
||||
import { getResolvables } from '@utils/utilities';
|
||||
|
||||
import type { PgpDatabase } from '../v2/helpers/interfaces';
|
||||
|
||||
/**
|
||||
* Returns of a shallow copy of the items which only contains the json data and
|
||||
* of that only the define properties
|
||||
@@ -87,14 +89,14 @@ export function wrapData(data: IDataObject[]): INodeExecutionData[] {
|
||||
*
|
||||
* @param {Function} getNodeParam The getter for the Node's parameters
|
||||
* @param {pgPromise.IMain<{}, pg.IClient>} pgp The pgPromise instance
|
||||
* @param {pgPromise.IDatabase<{}, pg.IClient>} db The pgPromise database connection
|
||||
* @param {PgpDatabase} db The pgPromise database connection
|
||||
* @param {input[]} input The Node's input data
|
||||
*/
|
||||
export async function pgQuery(
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
getNodeParam: Function,
|
||||
pgp: pgPromise.IMain<{}, pg.IClient>,
|
||||
db: pgPromise.IDatabase<{}, pg.IClient>,
|
||||
db: PgpDatabase,
|
||||
items: INodeExecutionData[],
|
||||
continueOnFail: boolean,
|
||||
overrideMode?: string,
|
||||
@@ -170,7 +172,7 @@ export async function pgQuery(
|
||||
export async function pgQueryV2(
|
||||
this: IExecuteFunctions,
|
||||
pgp: pgPromise.IMain<{}, pg.IClient>,
|
||||
db: pgPromise.IDatabase<{}, pg.IClient>,
|
||||
db: PgpDatabase,
|
||||
items: INodeExecutionData[],
|
||||
continueOnFail: boolean,
|
||||
options?: {
|
||||
@@ -273,14 +275,14 @@ export async function pgQueryV2(
|
||||
*
|
||||
* @param {Function} getNodeParam The getter for the Node's parameters
|
||||
* @param {pgPromise.IMain<{}, pg.IClient>} pgp The pgPromise instance
|
||||
* @param {pgPromise.IDatabase<{}, pg.IClient>} db The pgPromise database connection
|
||||
* @param {PgpDatabase} db The pgPromise database connection
|
||||
* @param {INodeExecutionData[]} items The items to be inserted
|
||||
*/
|
||||
export async function pgInsert(
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
getNodeParam: Function,
|
||||
pgp: pgPromise.IMain<{}, pg.IClient>,
|
||||
db: pgPromise.IDatabase<{}, pg.IClient>,
|
||||
db: PgpDatabase,
|
||||
items: INodeExecutionData[],
|
||||
continueOnFail: boolean,
|
||||
overrideMode?: string,
|
||||
@@ -364,13 +366,13 @@ export async function pgInsert(
|
||||
*
|
||||
* @param {Function} getNodeParam The getter for the Node's parameters
|
||||
* @param {pgPromise.IMain<{}, pg.IClient>} pgp The pgPromise instance
|
||||
* @param {pgPromise.IDatabase<{}, pg.IClient>} db`` The pgPromise database connection
|
||||
* @param {PgpDatabase} db`` The pgPromise database connection
|
||||
* @param {INodeExecutionData[]} items The items to be inserted
|
||||
*/
|
||||
export async function pgInsertV2(
|
||||
this: IExecuteFunctions,
|
||||
pgp: pgPromise.IMain<{}, pg.IClient>,
|
||||
db: pgPromise.IDatabase<{}, pg.IClient>,
|
||||
db: PgpDatabase,
|
||||
items: INodeExecutionData[],
|
||||
continueOnFail: boolean,
|
||||
overrideMode?: string,
|
||||
@@ -474,14 +476,14 @@ export async function pgInsertV2(
|
||||
*
|
||||
* @param {Function} getNodeParam The getter for the Node's parameters
|
||||
* @param {pgPromise.IMain<{}, pg.IClient>} pgp The pgPromise instance
|
||||
* @param {pgPromise.IDatabase<{}, pg.IClient>} db The pgPromise database connection
|
||||
* @param {PgpDatabase} db The pgPromise database connection
|
||||
* @param {INodeExecutionData[]} items The items to be updated
|
||||
*/
|
||||
export async function pgUpdate(
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
getNodeParam: Function,
|
||||
pgp: pgPromise.IMain<{}, pg.IClient>,
|
||||
db: pgPromise.IDatabase<{}, pg.IClient>,
|
||||
db: PgpDatabase,
|
||||
items: INodeExecutionData[],
|
||||
continueOnFail = false,
|
||||
): Promise<IDataObject[]> {
|
||||
@@ -604,13 +606,13 @@ export async function pgUpdate(
|
||||
*
|
||||
* @param {Function} getNodeParam The getter for the Node's parameters
|
||||
* @param {pgPromise.IMain<{}, pg.IClient>} pgp The pgPromise instance
|
||||
* @param {pgPromise.IDatabase<{}, pg.IClient>} db The pgPromise database connection
|
||||
* @param {PgpDatabase} db The pgPromise database connection
|
||||
* @param {INodeExecutionData[]} items The items to be updated
|
||||
*/
|
||||
export async function pgUpdateV2(
|
||||
this: IExecuteFunctions,
|
||||
pgp: pgPromise.IMain<{}, pg.IClient>,
|
||||
db: pgPromise.IDatabase<{}, pg.IClient>,
|
||||
db: PgpDatabase,
|
||||
items: INodeExecutionData[],
|
||||
continueOnFail = false,
|
||||
): Promise<IDataObject[]> {
|
||||
|
||||
Reference in New Issue
Block a user