knex/test/integration/logger.js

144 lines
4.1 KiB
JavaScript
Raw Normal View History

2016-03-02 17:07:05 +01:00
'use strict';
const { expect } = require('chai');
const _ = require('lodash');
2021-03-08 07:16:07 -05:00
const { DRIVER_NAMES } = require('../../lib/constants');
const { isObject } = require('../../lib/util/is');
2016-03-02 17:07:05 +01:00
const { TEST_TIMESTAMP, TEST_ID } = require('../util/constants');
2020-04-19 00:40:23 +02:00
module.exports = function (knex) {
const client = knex.client;
2016-03-02 17:07:05 +01:00
// allowed driver name of a client
2021-03-08 07:16:07 -05:00
const allowedClients = Object.values(DRIVER_NAMES);
2016-03-02 17:07:05 +01:00
function compareBindings(gotBindings, wantedBindings) {
if (Array.isArray(wantedBindings)) {
2020-04-19 00:40:23 +02:00
wantedBindings.forEach(function (wantedBinding, index) {
2016-03-02 17:07:05 +01:00
if (typeof wantedBinding === 'function') {
expect(
wantedBinding(gotBindings[index]),
'binding cheker function failed got: ' + gotBindings
).to.equal(true);
2016-03-02 17:07:05 +01:00
} else {
2021-10-13 01:19:56 +03:00
expect(gotBindings[index]).to.eql(wantedBinding);
2016-03-02 17:07:05 +01: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)) {
2020-04-19 00:40:23 +02:00
driverName.forEach(function (val) {
2016-03-02 17:07:05 +01:00
testSqlTester(qb, val, statement, bindings, returnval);
});
} else if (client.driverName === driverName) {
const sql = qb.toSQL();
2016-03-02 17:07:05 +01:00
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) {
const oldThen = qb.then;
2020-04-19 00:40:23 +02:00
qb.then = function () {
let promise = oldThen.apply(this, []);
2020-04-19 00:40:23 +02:00
promise = promise.then(function (resp) {
2016-03-02 17:07:05 +01:00
if (typeof returnval === 'function') {
expect(!!returnval(resp)).to.equal(true);
} else {
try {
expect(stripDates(resp)).to.eql(returnval);
} catch (err) {
console.log('Actual:');
console.log(JSON.stringify(resp));
console.log('Expected:');
console.log(JSON.stringify(returnval));
throw err;
}
2016-03-02 17:07:05 +01:00
}
return resp;
2016-03-02 17:07:05 +01:00
});
return promise.then.apply(promise, arguments);
};
}
} 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;
2020-04-19 00:40:23 +02:00
return _.map(resp, function (val) {
return _.reduce(
val,
2020-04-19 00:40:23 +02:00
function (memo, val, key) {
if (_.includes(['created_at', 'updated_at'], key)) {
memo[key] = TEST_TIMESTAMP;
} else if (_.includes(['dummy_id'], key)) {
memo[key] = TEST_ID;
} 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) {
const tester = testSqlTester.bind(null, builder);
2020-04-19 00:40:23 +02:00
return function (handler) {
handler(tester);
return this;
};
2016-09-13 08:15:58 -04:00
}
const originalRaw = client.raw;
const originalQueryBuilder = client.queryBuilder;
const originalSchemaBuilder = client.schemaBuilder;
2020-04-19 00:40:23 +02:00
client.raw = function () {
const raw = originalRaw.apply(this, arguments);
raw.testSql = makeTestSQL(raw);
return raw;
};
2020-04-19 00:40:23 +02:00
client.queryBuilder = function () {
const qb = originalQueryBuilder.apply(this, arguments);
qb.testSql = makeTestSQL(qb);
return qb;
};
2020-04-19 00:40:23 +02:00
client.schemaBuilder = function () {
const sb = originalSchemaBuilder.apply(this, arguments);
sb.testSql = makeTestSQL(sb);
return sb;
};
2016-03-02 17:07:05 +01:00
return knex;
};