mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
fix(Postgres Node): Account for JSON expressions (#12012)
Co-authored-by: Shireen Missi <94372015+ShireenMissi@users.noreply.github.com>
This commit is contained in:
@@ -14,6 +14,7 @@ import * as select from '../../v2/actions/database/select.operation';
|
||||
import * as update from '../../v2/actions/database/update.operation';
|
||||
import * as upsert from '../../v2/actions/database/upsert.operation';
|
||||
import type { ColumnInfo, PgpDatabase, QueriesRunner } from '../../v2/helpers/interfaces';
|
||||
import * as utils from '../../v2/helpers/utils';
|
||||
|
||||
const runQueries: QueriesRunner = jest.fn();
|
||||
|
||||
@@ -360,6 +361,99 @@ describe('Test PostgresV2, executeQuery operation', () => {
|
||||
nodeOptions,
|
||||
);
|
||||
});
|
||||
|
||||
it('should execute queries with multiple json key/value pairs', async () => {
|
||||
const nodeParameters: IDataObject = {
|
||||
operation: 'executeQuery',
|
||||
query: 'SELECT *\nFROM users\nWHERE username IN ($1, $2, $3)',
|
||||
options: {
|
||||
queryReplacement:
|
||||
'={{ JSON.stringify({id: "7",id2: "848da11d-e72e-44c5-yyyy-c6fb9f17d366"}) }}',
|
||||
},
|
||||
};
|
||||
const nodeOptions = nodeParameters.options as IDataObject;
|
||||
|
||||
expect(async () => {
|
||||
await executeQuery.execute.call(
|
||||
createMockExecuteFunction(nodeParameters),
|
||||
runQueries,
|
||||
items,
|
||||
nodeOptions,
|
||||
);
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
it('should execute queries with single json key/value pair', async () => {
|
||||
const nodeParameters: IDataObject = {
|
||||
operation: 'executeQuery',
|
||||
query: 'SELECT *\nFROM users\nWHERE username IN ($1, $2, $3)',
|
||||
options: {
|
||||
queryReplacement: '={{ {"id": "7"} }}',
|
||||
},
|
||||
};
|
||||
const nodeOptions = nodeParameters.options as IDataObject;
|
||||
|
||||
expect(async () => {
|
||||
await executeQuery.execute.call(
|
||||
createMockExecuteFunction(nodeParameters),
|
||||
runQueries,
|
||||
items,
|
||||
nodeOptions,
|
||||
);
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
it('should not parse out expressions if there are valid JSON query parameters', async () => {
|
||||
const query = 'SELECT *\nFROM users\nWHERE username IN ($1, $2, $3)';
|
||||
const nodeParameters: IDataObject = {
|
||||
operation: 'executeQuery',
|
||||
query,
|
||||
options: {
|
||||
queryReplacement: '={{ {"id": "7"} }}',
|
||||
nodeVersion: 2.6,
|
||||
},
|
||||
};
|
||||
const nodeOptions = nodeParameters.options as IDataObject;
|
||||
|
||||
jest.spyOn(utils, 'isJSON');
|
||||
jest.spyOn(utils, 'stringToArray');
|
||||
|
||||
await executeQuery.execute.call(
|
||||
createMockExecuteFunction(nodeParameters),
|
||||
runQueries,
|
||||
items,
|
||||
nodeOptions,
|
||||
);
|
||||
|
||||
expect(utils.isJSON).toHaveBeenCalledTimes(1);
|
||||
expect(utils.stringToArray).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
||||
it('should parse out expressions if is invalid JSON in query parameters', async () => {
|
||||
const query = 'SELECT *\nFROM users\nWHERE username IN ($1, $2, $3)';
|
||||
const nodeParameters: IDataObject = {
|
||||
operation: 'executeQuery',
|
||||
query,
|
||||
options: {
|
||||
queryReplacement: '={{ JSON.stringify({"id": "7"}}) }}',
|
||||
nodeVersion: 2.6,
|
||||
},
|
||||
};
|
||||
const nodeOptions = nodeParameters.options as IDataObject;
|
||||
|
||||
jest.spyOn(utils, 'isJSON');
|
||||
jest.spyOn(utils, 'stringToArray');
|
||||
|
||||
await executeQuery.execute.call(
|
||||
createMockExecuteFunction(nodeParameters),
|
||||
runQueries,
|
||||
items,
|
||||
nodeOptions,
|
||||
);
|
||||
|
||||
expect(utils.isJSON).toHaveBeenCalledTimes(1);
|
||||
expect(utils.stringToArray).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Test PostgresV2, insert operation', () => {
|
||||
|
||||
Reference in New Issue
Block a user