2015-05-01 14:55:53 -04:00
|
|
|
'use strict';
|
2015-05-01 14:49:18 -04:00
|
|
|
|
2019-07-10 22:48:43 +01:00
|
|
|
const knex = require('../../lib/index');
|
2018-10-15 22:29:53 -04:00
|
|
|
const test = require('tape');
|
2021-04-21 23:50:03 +02:00
|
|
|
const fs = require('fs');
|
2015-05-01 14:49:18 -04:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
test('it should parse the connection string', function (t) {
|
2018-07-09 08:10:34 -04:00
|
|
|
t.plan(1);
|
2018-10-15 22:29:53 -04:00
|
|
|
const knexObj = knex({
|
2015-05-01 14:49:18 -04:00
|
|
|
client: 'mysql',
|
2018-07-09 08:10:34 -04:00
|
|
|
connection: 'mysql://user:password@localhost/dbname',
|
|
|
|
});
|
2016-03-08 08:41:13 +01:00
|
|
|
t.deepEqual(knexObj.client.config.connection, {
|
|
|
|
database: 'dbname',
|
2016-09-13 18:12:23 -04:00
|
|
|
host: 'localhost',
|
2016-03-08 08:41:13 +01:00
|
|
|
password: 'password',
|
2018-07-09 08:10:34 -04:00
|
|
|
user: 'user',
|
|
|
|
});
|
|
|
|
knexObj.destroy();
|
|
|
|
});
|
2016-05-19 12:18:11 +03:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
test('it should allow to use proprietary dialect', function (t) {
|
2018-07-09 08:10:34 -04:00
|
|
|
t.plan(2);
|
2019-07-10 22:48:43 +01:00
|
|
|
const Client = require('../../lib/dialects/mysql');
|
2018-10-15 22:29:53 -04:00
|
|
|
const knexObj = knex({
|
2016-05-19 12:18:11 +03:00
|
|
|
client: Client,
|
2016-05-19 13:32:07 +03:00
|
|
|
connection: {
|
|
|
|
database: 'dbname',
|
2016-09-13 18:12:23 -04:00
|
|
|
host: 'localhost',
|
2016-05-19 13:32:07 +03:00
|
|
|
password: 'password',
|
2018-07-09 08:10:34 -04:00
|
|
|
user: 'user',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
t.ok(knexObj.client instanceof Client);
|
2016-05-19 13:32:07 +03:00
|
|
|
t.deepEqual(knexObj.client.config, {
|
2016-05-19 12:18:11 +03:00
|
|
|
client: Client,
|
2016-05-19 13:32:07 +03:00
|
|
|
connection: {
|
|
|
|
database: 'dbname',
|
2016-09-13 18:12:23 -04:00
|
|
|
host: 'localhost',
|
2016-05-19 13:32:07 +03:00
|
|
|
password: 'password',
|
2018-07-09 08:10:34 -04:00
|
|
|
user: 'user',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
knexObj.destroy();
|
|
|
|
});
|
2016-05-19 12:18:11 +03:00
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
test('it should use knex supported dialect', function (t) {
|
2018-07-09 08:10:34 -04:00
|
|
|
t.plan(1);
|
2018-10-15 22:29:53 -04:00
|
|
|
const knexObj = knex({
|
2016-05-19 12:18:11 +03:00
|
|
|
client: 'postgres',
|
2016-05-19 13:32:07 +03:00
|
|
|
connection: {
|
|
|
|
database: 'dbname',
|
2016-09-13 18:12:23 -04:00
|
|
|
host: 'localhost',
|
2016-05-19 13:32:07 +03:00
|
|
|
password: 'password',
|
2018-07-09 08:10:34 -04:00
|
|
|
user: 'user',
|
|
|
|
},
|
|
|
|
});
|
2016-05-19 13:32:07 +03:00
|
|
|
t.deepEqual(knexObj.client.config, {
|
2016-05-19 12:18:11 +03:00
|
|
|
client: 'postgres',
|
2016-05-19 13:32:07 +03:00
|
|
|
connection: {
|
|
|
|
database: 'dbname',
|
2016-09-13 18:12:23 -04:00
|
|
|
host: 'localhost',
|
2016-05-19 13:32:07 +03:00
|
|
|
password: 'password',
|
2018-07-09 08:10:34 -04:00
|
|
|
user: 'user',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
knexObj.destroy();
|
|
|
|
});
|
2016-12-11 17:23:44 +01:00
|
|
|
|
2021-04-21 23:50:03 +02:00
|
|
|
test('it should support open flags selection for sqlite3', function (t) {
|
|
|
|
t.plan(1);
|
|
|
|
const knexObj = knex({
|
|
|
|
client: 'sqlite3',
|
|
|
|
connection: {
|
|
|
|
filename: 'file:memdb-test?mode=memory',
|
|
|
|
// allow the filename to be interpreted as a URI
|
|
|
|
flags: ['OPEN_URI'],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
// run a query so a connection is created
|
|
|
|
knexObj
|
|
|
|
.select(knexObj.raw('"0"'))
|
|
|
|
.then(() => {
|
|
|
|
// if the filename was interpreted as a URI, no file should have been created
|
|
|
|
t.equal(fs.existsSync('./file:memdb-test?mode=memory'), false);
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
knexObj.destroy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('it should error when invalid open flags are selected for sqlite3', function (t) {
|
|
|
|
t.plan(2);
|
|
|
|
|
|
|
|
// Test invalid flags
|
|
|
|
let knexObj = knex({
|
|
|
|
client: 'sqlite3',
|
|
|
|
connection: {
|
|
|
|
filename: ':memory:',
|
|
|
|
flags: ['NON_EXISTING'],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
// run a query so a connection is created
|
|
|
|
knexObj
|
|
|
|
.select(knexObj.raw('"0"'))
|
|
|
|
.then(() => {
|
|
|
|
t.fail('Should not get here');
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
t.equal(err.message, 'flag NON_EXISTING not supported by node-sqlite3');
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
knexObj.destroy();
|
|
|
|
});
|
|
|
|
|
|
|
|
// test invalid config
|
|
|
|
knexObj = knex({
|
|
|
|
client: 'sqlite3',
|
|
|
|
connection: {
|
|
|
|
filename: ':memory:',
|
|
|
|
flags: 'OPEN_URI',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
// run a query so a connection is created
|
|
|
|
knexObj
|
|
|
|
.select(knexObj.raw('"0"'))
|
|
|
|
.then(() => {
|
|
|
|
t.fail('Should not get here');
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
t.equal(err.message, 'flags must be an array of strings');
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
knexObj.destroy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-19 00:40:23 +02:00
|
|
|
test('it should throw error if client is omitted in config', function (t) {
|
2016-12-11 17:23:44 +01:00
|
|
|
t.plan(1);
|
|
|
|
try {
|
2018-10-15 22:29:53 -04:00
|
|
|
knex({});
|
2016-12-11 17:23:44 +01:00
|
|
|
t.deepEqual(true, false); //Don't reach this point
|
2018-07-09 08:10:34 -04:00
|
|
|
} catch (error) {
|
|
|
|
t.deepEqual(
|
|
|
|
error.message,
|
|
|
|
"knex: Required configuration option 'client' is missing."
|
|
|
|
);
|
2016-12-11 17:23:44 +01:00
|
|
|
}
|
2018-07-09 08:10:34 -04:00
|
|
|
});
|