2018-08-29 17:13:16 +02:00
|
|
|
const Knex = require('../../lib/index');
|
|
|
|
const { expect } = require('chai');
|
2018-09-26 22:27:59 +02:00
|
|
|
const Promise = require('bluebird');
|
|
|
|
const sqliteConfig = require('../knexfile').sqlite3;
|
2018-09-27 00:08:18 +02:00
|
|
|
const sqlite3 = require('sqlite3');
|
2018-11-19 12:55:50 +01:00
|
|
|
const { noop } = require('lodash');
|
2018-08-29 17:13:16 +02:00
|
|
|
|
|
|
|
describe('knex', () => {
|
2018-09-27 00:08:18 +02:00
|
|
|
describe('supports passing existing connection', () => {
|
|
|
|
let connection;
|
|
|
|
beforeEach(() => {
|
|
|
|
connection = new sqlite3.Database(':memory:');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
connection.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('happy path', (done) => {
|
|
|
|
const knex = Knex({ client: 'sqlite3' });
|
|
|
|
knex
|
|
|
|
.connection(connection)
|
|
|
|
.select(knex.raw('"0" as value'))
|
|
|
|
.then((result) => {
|
|
|
|
expect(result[0].value).to.equal('0');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-08-29 17:13:16 +02:00
|
|
|
it('throws error on unsupported config client value', () => {
|
|
|
|
expect(() => {
|
|
|
|
Knex({
|
|
|
|
client: 'dummy',
|
|
|
|
});
|
|
|
|
}).to.throw(
|
|
|
|
/Unknown configuration option 'client' value dummy. Note that it is case-sensitive, check documentation for supported values/
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('accepts supported config client value', () => {
|
|
|
|
expect(() => {
|
|
|
|
Knex({
|
|
|
|
client: 'mysql',
|
|
|
|
});
|
|
|
|
}).not.to.throw();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('accepts supported config client value alias', () => {
|
|
|
|
expect(() => {
|
|
|
|
Knex({
|
|
|
|
client: 'sqlite',
|
|
|
|
});
|
|
|
|
}).not.to.throw();
|
|
|
|
});
|
2018-09-26 22:27:59 +02:00
|
|
|
|
|
|
|
it('supports creating copy with userParams', () => {
|
|
|
|
const knex = Knex({
|
|
|
|
client: 'sqlite',
|
|
|
|
});
|
|
|
|
|
|
|
|
const knexWithParams = knex.withUserParams({ userParam: '451' });
|
|
|
|
|
|
|
|
expect(knexWithParams).to.be.a('function');
|
|
|
|
expect(knexWithParams.userParams).to.deep.equal({ userParam: '451' });
|
|
|
|
expect(knexWithParams.client.config.client).to.equal('sqlite');
|
|
|
|
expect(knexWithParams.migrate).to.be.a('object');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports passing user params in config', () => {
|
|
|
|
const knexWithParams = Knex({
|
|
|
|
client: 'sqlite',
|
|
|
|
userParams: {
|
|
|
|
userParam: '451',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(knexWithParams).to.be.a('function');
|
|
|
|
expect(knexWithParams.userParams).to.deep.equal({ userParam: '451' });
|
|
|
|
expect(knexWithParams.client.config.client).to.equal('sqlite');
|
|
|
|
expect(knexWithParams.migrate).to.be.a('object');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('migrator of a copy with userParams has reference to correct Knex', () => {
|
|
|
|
const knex = Knex({
|
|
|
|
client: 'sqlite',
|
|
|
|
});
|
|
|
|
|
|
|
|
const knexWithParams = knex.withUserParams({ userParam: '451' });
|
|
|
|
|
|
|
|
expect(knexWithParams.migrate.knex.userParams).to.deep.equal({
|
|
|
|
userParam: '451',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-11-19 12:55:50 +01:00
|
|
|
it('sets correct postProcessResponse for builders instantiated from clone', () => {
|
|
|
|
const knex = Knex({
|
|
|
|
client: 'sqlite',
|
|
|
|
postProcessResponse: noop,
|
|
|
|
});
|
|
|
|
|
|
|
|
const knexWithParams = knex.withUserParams();
|
|
|
|
knexWithParams.client.config.postProcessResponse = null;
|
|
|
|
const builderForTable = knex('tableName');
|
|
|
|
const builderWithParamsForTable = knexWithParams('tableName');
|
|
|
|
|
|
|
|
expect(knex.client.config.postProcessResponse).to.equal(noop);
|
|
|
|
expect(knexWithParams.client.config.postProcessResponse).to.equal(null);
|
|
|
|
expect(builderForTable.client.config.postProcessResponse).to.equal(noop);
|
|
|
|
expect(
|
|
|
|
builderWithParamsForTable.client.config.postProcessResponse
|
|
|
|
).to.equal(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets correct postProcessResponse for chained builders', () => {
|
|
|
|
const knex = Knex({
|
|
|
|
client: 'sqlite',
|
|
|
|
postProcessResponse: noop,
|
|
|
|
});
|
|
|
|
|
|
|
|
const knexWithParams = knex.withUserParams();
|
|
|
|
knexWithParams.client.config.postProcessResponse = null;
|
|
|
|
const builderForTable = knex('tableName').where('1 = 1');
|
|
|
|
const builderWithParamsForTable = knexWithParams('tableName').where(
|
|
|
|
'1 = 1'
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(knex.client.config.postProcessResponse).to.equal(noop);
|
|
|
|
expect(knexWithParams.client.config.postProcessResponse).to.equal(null);
|
|
|
|
expect(builderForTable.client.config.postProcessResponse).to.equal(noop);
|
|
|
|
expect(
|
|
|
|
builderWithParamsForTable.client.config.postProcessResponse
|
|
|
|
).to.equal(null);
|
|
|
|
});
|
|
|
|
|
2018-09-26 22:27:59 +02:00
|
|
|
it('transaction of a copy with userParams retains userparams', (done) => {
|
|
|
|
const knex = Knex(sqliteConfig);
|
|
|
|
|
|
|
|
const knexWithParams = knex.withUserParams({ userParam: '451' });
|
|
|
|
|
|
|
|
knexWithParams.transaction((trx) => {
|
|
|
|
expect(trx.userParams).to.deep.equal({
|
|
|
|
userParam: '451',
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
return Promise.resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('creating transaction copy with user params should throw an error', (done) => {
|
|
|
|
const knex = Knex(sqliteConfig);
|
|
|
|
|
|
|
|
knex.transaction((trx) => {
|
|
|
|
expect(() => {
|
|
|
|
trx.withUserParams({ userParam: '451' });
|
|
|
|
}).to.throw(
|
|
|
|
/Cannot set user params on a transaction - it can only inherit params from main knex instance/
|
|
|
|
);
|
|
|
|
done();
|
|
|
|
return Promise.resolve();
|
|
|
|
});
|
|
|
|
});
|
2018-10-08 16:04:28 +11:00
|
|
|
|
|
|
|
it('throws if client module has not been installed', () => {
|
2018-10-15 22:29:53 -04:00
|
|
|
expect(Knex({ client: 'oracle' })).to.throw(
|
2018-10-08 16:04:28 +11:00
|
|
|
/Knex: run\n$ npm install oracle/
|
|
|
|
);
|
|
|
|
});
|
2018-11-19 12:55:50 +01:00
|
|
|
|
|
|
|
describe('async stack traces', () => {
|
|
|
|
it('should capture stack trace on query builder instantiation', () => {
|
|
|
|
const knex = Knex({
|
|
|
|
...sqliteConfig,
|
|
|
|
asyncStackTraces: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
return knex('some_nonexisten_table')
|
|
|
|
.select()
|
|
|
|
.catch((err) => {
|
|
|
|
expect(err.stack.split('\n')[1]).to.match(/at createQueryBuilder \(/); // the index 1 might need adjustment if the code is refactored
|
|
|
|
expect(typeof err.originalStack).to.equal('string');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-08-29 17:13:16 +02:00
|
|
|
});
|