2016-03-02 17:07:05 +01:00
|
|
|
/*global expect, d*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var _ = require('lodash')
|
|
|
|
|
|
|
|
module.exports = function(knex) {
|
|
|
|
|
|
|
|
var client = knex.client;
|
|
|
|
|
2018-07-08 14:10:51 +03:00
|
|
|
// allowed driver name of a client
|
|
|
|
const allowedClients = ['pg', 'mssql', 'mysql', 'mysql2', 'oracledb', 'pg-redshift', 'sqlite3'];
|
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
function compareBindings(gotBindings, wantedBindings) {
|
|
|
|
if (Array.isArray(wantedBindings)) {
|
|
|
|
wantedBindings.forEach(function (wantedBinding, index) {
|
|
|
|
if (typeof wantedBinding === 'function') {
|
2018-07-08 14:10:51 +03:00
|
|
|
expect(wantedBinding(gotBindings[index]), "binding cheker function failed got: " + gotBindings).to.equal(true);
|
2016-03-02 17:07:05 +01:00
|
|
|
} else {
|
|
|
|
expect(wantedBinding).to.eql(gotBindings[index]);
|
|
|
|
}
|
|
|
|
});
|
2018-07-08 14:10:51 +03:00
|
|
|
expect(gotBindings.length, "length doesn't match got: " + gotBindings).to.equal(wantedBindings.length);
|
2016-03-02 17:07:05 +01:00
|
|
|
} else {
|
|
|
|
expect(gotBindings).to.eql(wantedBindings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Useful in cases where we want to just test the sql for both PG and SQLite3
|
|
|
|
function testSqlTester(qb, driverName, statement, bindings, returnval) {
|
|
|
|
|
|
|
|
if (Array.isArray(driverName)) {
|
|
|
|
driverName.forEach(function(val) {
|
|
|
|
testSqlTester(qb, val, statement, bindings, returnval);
|
|
|
|
});
|
|
|
|
} else if (client.driverName === driverName) {
|
|
|
|
var sql = qb.toSQL();
|
|
|
|
|
|
|
|
if (statement) {
|
|
|
|
if (Array.isArray(sql)) {
|
|
|
|
expect(_.map(sql, 'sql')).to.eql(statement);
|
|
|
|
} else {
|
|
|
|
expect(sql.sql).to.equal(statement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bindings) {
|
|
|
|
if (Array.isArray(sql)) {
|
2016-03-02 17:25:07 +01:00
|
|
|
compareBindings(_.map(sql, 'bindings'), bindings);
|
2016-03-02 17:07:05 +01:00
|
|
|
} else {
|
|
|
|
compareBindings(sql.bindings, bindings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (returnval !== undefined && returnval !== null) {
|
|
|
|
var oldThen = qb.then;
|
|
|
|
qb.then = function() {
|
|
|
|
var promise = oldThen.apply(this, []);
|
|
|
|
promise = promise.tap(function(resp) {
|
|
|
|
if (typeof returnval === 'function') {
|
|
|
|
expect(!!returnval(resp)).to.equal(true);
|
|
|
|
} else {
|
|
|
|
expect(stripDates(resp)).to.eql(returnval);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return promise.then.apply(promise, arguments);
|
|
|
|
};
|
|
|
|
}
|
2018-07-08 14:10:51 +03:00
|
|
|
} else {
|
|
|
|
if (!allowedClients.includes(driverName)) {
|
|
|
|
throw new Error('Invalid client name: ' + driverName + ' Should be one of: ' + allowedClients.join(','));
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function stripDates(resp) {
|
|
|
|
if (!_.isObject(resp[0])) return resp;
|
|
|
|
return _.map(resp, function(val) {
|
|
|
|
return _.reduce(val, function(memo, val, key) {
|
2016-03-02 17:10:07 +01:00
|
|
|
if (_.includes(['created_at', 'updated_at'], key)) {
|
2016-03-02 17:07:05 +01:00
|
|
|
memo[key] = d;
|
|
|
|
} else {
|
|
|
|
memo[key] = val;
|
|
|
|
}
|
|
|
|
return memo;
|
|
|
|
}, {});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-13 08:15:58 -04:00
|
|
|
function makeTestSQL(builder) {
|
2016-09-13 09:50:28 -04:00
|
|
|
var tester = testSqlTester.bind(null, builder)
|
2016-09-13 08:15:58 -04:00
|
|
|
return function(handler) {
|
|
|
|
handler(tester)
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 09:50:28 -04:00
|
|
|
var originalRaw = client.raw
|
|
|
|
var originalQueryBuilder = client.queryBuilder
|
|
|
|
var originalSchemaBuilder = client.schemaBuilder
|
2016-09-13 08:15:58 -04:00
|
|
|
client.raw = function() {
|
2016-09-13 09:50:28 -04:00
|
|
|
var raw = originalRaw.apply(this, arguments)
|
2016-09-13 08:15:58 -04:00
|
|
|
raw.testSql = makeTestSQL(raw)
|
|
|
|
return raw
|
|
|
|
}
|
|
|
|
client.queryBuilder = function() {
|
2016-09-13 09:50:28 -04:00
|
|
|
var qb = originalQueryBuilder.apply(this, arguments)
|
2016-09-13 08:15:58 -04:00
|
|
|
qb.testSql = makeTestSQL(qb)
|
|
|
|
return qb
|
|
|
|
}
|
|
|
|
client.schemaBuilder = function() {
|
2016-09-13 09:50:28 -04:00
|
|
|
var sb = originalSchemaBuilder.apply(this, arguments)
|
2016-09-13 08:15:58 -04:00
|
|
|
sb.testSql = makeTestSQL(sb)
|
|
|
|
return sb
|
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
return knex;
|
|
|
|
}
|