2023-07-12 02:15:38 -07:00
|
|
|
import type { IDataObject, IExecuteFunctions, INode } from 'n8n-workflow';
|
2023-04-03 08:18:01 -07:00
|
|
|
|
|
|
|
import pgPromise from 'pg-promise';
|
2023-07-12 02:15:38 -07:00
|
|
|
import { mock } from 'jest-mock-extended';
|
2023-10-27 05:15:02 -07:00
|
|
|
import type { PgpDatabase } from '../../v2/helpers/interfaces';
|
|
|
|
import { configureQueryRunner } from '../../v2/helpers/utils';
|
2023-04-03 08:18:01 -07:00
|
|
|
|
|
|
|
const node: INode = {
|
|
|
|
id: '1',
|
|
|
|
name: 'Postgres node',
|
|
|
|
typeVersion: 2,
|
|
|
|
type: 'n8n-nodes-base.postgres',
|
|
|
|
position: [60, 760],
|
|
|
|
parameters: {
|
|
|
|
operation: 'executeQuery',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const createMockDb = (returnData: IDataObject | IDataObject[]) => {
|
|
|
|
return {
|
|
|
|
async any() {
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
async multi() {
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
async tx() {
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
async task() {
|
|
|
|
return returnData;
|
|
|
|
},
|
|
|
|
} as unknown as PgpDatabase;
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('Test PostgresV2, runQueries', () => {
|
|
|
|
it('should execute, should return success true', async () => {
|
|
|
|
const pgp = pgPromise();
|
|
|
|
const db = createMockDb([]);
|
|
|
|
|
|
|
|
const dbMultiSpy = jest.spyOn(db, 'multi');
|
|
|
|
|
2023-07-12 02:15:38 -07:00
|
|
|
const thisArg = mock<IExecuteFunctions>();
|
|
|
|
const runQueries = configureQueryRunner.call(thisArg, node, false, pgp, db);
|
2023-04-03 08:18:01 -07:00
|
|
|
|
2023-08-25 08:38:09 -07:00
|
|
|
const result = await runQueries([{ query: 'SELECT * FROM table', values: [] }], [], {
|
|
|
|
nodeVersion: 2.2,
|
|
|
|
});
|
2023-04-03 08:18:01 -07:00
|
|
|
|
|
|
|
expect(result).toBeDefined();
|
|
|
|
expect(result).toHaveLength(1);
|
2023-10-10 08:36:20 -07:00
|
|
|
expect(result).toEqual([{ json: { success: true }, pairedItem: [{ item: 0 }] }]);
|
2023-04-03 08:18:01 -07:00
|
|
|
expect(dbMultiSpy).toHaveBeenCalledWith('SELECT * FROM table');
|
|
|
|
});
|
|
|
|
});
|