knex/test/unit/dialects/postgres.js

149 lines
3.9 KiB
JavaScript
Raw Normal View History

const knex = require('../../../knex');
const expect = require('chai').expect;
const sinon = require('sinon');
2019-07-10 22:48:43 +01:00
const pgDialect = require('../../../lib/dialects/postgres/index.js');
const _ = require('lodash');
const { isFunction } = require('../../../lib/util/is');
2020-04-19 00:40:23 +02:00
describe('Postgres Unit Tests', function () {
let checkVersionStub, querySpy;
before(() => {
const fakeConnection = {
query: (...args) => {
const cb = args.find((arg) => {
return isFunction(arg);
});
cb();
},
on: _.noop,
};
querySpy = sinon.spy(fakeConnection, 'query');
checkVersionStub = sinon
.stub(pgDialect.prototype, 'checkVersion')
2020-04-19 00:40:23 +02:00
.callsFake(function () {
return Promise.resolve('9.6');
});
2021-09-06 09:04:23 -04:00
sinon
.stub(pgDialect.prototype, '_acquireOnlyConnection')
.returns(Promise.resolve(fakeConnection));
});
afterEach(() => {
querySpy.resetHistory();
});
after(() => {
sinon.restore();
});
2021-09-06 09:04:23 -04:00
it('does not resolve client version if specified explicitly', () => {
const knexInstance = knex({
2021-09-06 09:04:23 -04:00
client: 'postgres',
version: '10.5',
connection: {
pool: {},
},
});
2021-09-06 09:04:23 -04:00
return knexInstance.raw('select 1 as 1').then((result) => {
expect(checkVersionStub.notCalled).to.equal(true);
knexInstance.destroy();
});
});
2019-10-06 20:21:32 +02:00
it('escape statements correctly', async () => {
const knexInstance = knex({
client: 'postgresql',
version: '10.5',
connection: {
pool: {},
},
});
const sql = knexInstance('projects')
.where('id = 1 UNION SELECT 1, version();', 1)
.toSQL();
expect(sql.sql).to.equal(
'select * from "projects" where "id = 1 UNION SELECT 1, version();" = ?'
);
const sql2 = knexInstance('projects')
.where('id = 1" UNION SELECT 1, version();', 1)
.toSQL();
expect(sql2.sql).to.equal(
'select * from "projects" where "id = 1"" UNION SELECT 1, version();" = ?'
);
});
2021-09-06 09:04:23 -04:00
it('resolve client version if not specified explicitly', () => {
const knexInstance = knex({
client: 'postgresql',
connection: {
pool: {},
},
});
2021-09-06 09:04:23 -04:00
return knexInstance.raw('select 1 as 1').then((result) => {
expect(checkVersionStub.calledOnce).to.equal(true);
knexInstance.destroy();
});
});
2020-04-19 00:40:23 +02:00
it('Validates searchPath as Array/String', function () {
const knexInstance = knex({
client: 'pg',
});
2020-04-19 00:40:23 +02:00
expect(function () {
knexInstance.client.setSchemaSearchPath(null, {});
}).to.throw(TypeError);
2020-04-19 00:40:23 +02:00
expect(function () {
knexInstance.client.setSchemaSearchPath(null, 4);
}).to.throw(TypeError);
2020-04-19 00:40:23 +02:00
const fakeQueryFn = function (expectedSearchPath) {
return {
2020-04-19 00:40:23 +02:00
query: function (sql, callback) {
try {
expect(sql).to.equal('set search_path to ' + expectedSearchPath);
callback(null);
} catch (error) {
callback(error);
}
},
};
};
return knexInstance.client
.setSchemaSearchPath(fakeQueryFn('"public,knex"'), 'public,knex')
2020-04-19 00:40:23 +02:00
.then(function () {
return knexInstance.client.setSchemaSearchPath(
fakeQueryFn('"public","knex"'),
['public', 'knex']
);
})
2020-04-19 00:40:23 +02:00
.then(function () {
return knexInstance.client.setSchemaSearchPath(
fakeQueryFn('"public"'),
'public'
);
});
});
2021-09-06 09:04:23 -04:00
it('Uses documented query config as param when providing bindings', () => {
const knexInstance = knex({
client: 'postgresql',
connection: {},
});
2021-09-06 09:04:23 -04:00
return knexInstance.raw('select 1 as ?', ['foo']).then((result) => {
sinon.assert.calledOnce(querySpy);
sinon.assert.calledWithExactly(
querySpy,
{
text: 'select 1 as $1',
values: ['foo'],
},
sinon.match.func
);
knexInstance.destroy();
});
});
});