2016-03-02 17:07:05 +01:00
|
|
|
/*global expect, d*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2018-07-09 08:10:34 -04:00
|
|
|
var _ = require('lodash');
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
module.exports = function(knex) {
|
2018-07-09 08:10:34 -04:00
|
|
|
var client = knex.client;
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2018-07-08 14:10:51 +03:00
|
|
|
// allowed driver name of a client
|
2018-07-09 08:10:34 -04:00
|
|
|
const allowedClients = [
|
|
|
|
'pg',
|
|
|
|
'mssql',
|
|
|
|
'mysql',
|
|
|
|
'mysql2',
|
|
|
|
'oracledb',
|
|
|
|
'pg-redshift',
|
|
|
|
'sqlite3',
|
|
|
|
];
|
2018-07-08 14:10:51 +03:00
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
function compareBindings(gotBindings, wantedBindings) {
|
|
|
|
if (Array.isArray(wantedBindings)) {
|
2018-07-09 08:10:34 -04:00
|
|
|
wantedBindings.forEach(function(wantedBinding, index) {
|
2016-03-02 17:07:05 +01:00
|
|
|
if (typeof wantedBinding === 'function') {
|
2018-07-09 08:10:34 -04: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-09 08:10:34 -04: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)) {
|
2018-07-09 08:10:34 -04:00
|
|
|
throw new Error(
|
|
|
|
'Invalid client name: ' +
|
|
|
|
driverName +
|
|
|
|
' Should be one of: ' +
|
|
|
|
allowedClients.join(',')
|
|
|
|
);
|
2018-07-08 14:10:51 +03:00
|
|
|
}
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function stripDates(resp) {
|
|
|
|
if (!_.isObject(resp[0])) return resp;
|
|
|
|
return _.map(resp, function(val) {
|
2018-07-09 08:10:34 -04:00
|
|
|
return _.reduce(
|
|
|
|
val,
|
|
|
|
function(memo, val, key) {
|
|
|
|
if (_.includes(['created_at', 'updated_at'], key)) {
|
|
|
|
memo[key] = d;
|
|
|
|
} else {
|
|
|
|
memo[key] = val;
|
|
|
|
}
|
|
|
|
return memo;
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
);
|
2016-03-02 17:07:05 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-13 08:15:58 -04:00
|
|
|
function makeTestSQL(builder) {
|
2018-07-09 08:10:34 -04:00
|
|
|
var tester = testSqlTester.bind(null, builder);
|
2016-09-13 08:15:58 -04:00
|
|
|
return function(handler) {
|
2018-07-09 08:10:34 -04:00
|
|
|
handler(tester);
|
|
|
|
return this;
|
|
|
|
};
|
2016-09-13 08:15:58 -04:00
|
|
|
}
|
|
|
|
|
2018-07-09 08:10:34 -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() {
|
2018-07-09 08:10:34 -04:00
|
|
|
var raw = originalRaw.apply(this, arguments);
|
|
|
|
raw.testSql = makeTestSQL(raw);
|
|
|
|
return raw;
|
|
|
|
};
|
2016-09-13 08:15:58 -04:00
|
|
|
client.queryBuilder = function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
var qb = originalQueryBuilder.apply(this, arguments);
|
|
|
|
qb.testSql = makeTestSQL(qb);
|
|
|
|
return qb;
|
|
|
|
};
|
2016-09-13 08:15:58 -04:00
|
|
|
client.schemaBuilder = function() {
|
2018-07-09 08:10:34 -04:00
|
|
|
var sb = originalSchemaBuilder.apply(this, arguments);
|
|
|
|
sb.testSql = makeTestSQL(sb);
|
|
|
|
return sb;
|
|
|
|
};
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
return knex;
|
2018-07-09 08:10:34 -04:00
|
|
|
};
|