2016-01-26 19:29:12 -05:00
|
|
|
// Load the test base
|
|
|
|
const reload = require('require-reload')(require);
|
2016-09-14 16:50:32 -04:00
|
|
|
const tests = reload('../base/tests');
|
2016-01-26 19:29:12 -05:00
|
|
|
|
2016-09-14 16:50:32 -04:00
|
|
|
module.exports = function promiseTestRunner (qb) {
|
2017-02-28 15:47:29 -05:00
|
|
|
Object.keys(tests).forEach(describeName => {
|
2018-02-02 11:50:29 -05:00
|
|
|
describe(describeName, async () => {
|
2017-02-28 15:47:29 -05:00
|
|
|
let currentSuite = tests[describeName];
|
2016-01-26 19:29:12 -05:00
|
|
|
Object.keys(currentSuite).forEach(testDesc => {
|
2017-02-28 15:47:29 -05:00
|
|
|
it(testDesc, done => {
|
2016-09-14 16:50:32 -04:00
|
|
|
const methodObj = currentSuite[testDesc];
|
|
|
|
const methodNames = Object.keys(methodObj);
|
2016-01-26 19:29:12 -05:00
|
|
|
let results = [];
|
|
|
|
|
|
|
|
methodNames.forEach(name => {
|
2016-09-14 16:50:32 -04:00
|
|
|
const args = methodObj[name];
|
|
|
|
const method = qb[name];
|
2016-01-26 19:29:12 -05:00
|
|
|
|
|
|
|
if (args[0] === 'multiple') {
|
|
|
|
args.shift();
|
|
|
|
args.forEach(argSet => {
|
|
|
|
results.push(method.apply(qb, argSet));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
results.push(method.apply(qb, args));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-02-02 11:50:29 -05:00
|
|
|
const promise = results.pop();
|
2016-11-18 21:59:22 -05:00
|
|
|
promise.then(result => {
|
2018-02-02 11:50:29 -05:00
|
|
|
// expect(result.rows).is.an('array');
|
|
|
|
expect(result.rowCount()).toEqual(expect.anything());
|
|
|
|
expect(result.columnCount()).toEqual(expect.anything());
|
2016-11-18 21:59:22 -05:00
|
|
|
return done();
|
|
|
|
}).catch(e => done(e));
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-02-09 17:29:26 -05:00
|
|
|
|
|
|
|
describe('DB update tests -', () => {
|
2017-02-28 15:47:29 -05:00
|
|
|
beforeAll(done => {
|
2016-01-26 19:29:12 -05:00
|
|
|
let sql = qb.driver.truncate('create_test');
|
2018-02-09 17:29:26 -05:00
|
|
|
qb.query(sql).then(() => done())
|
2016-03-15 15:37:24 -04:00
|
|
|
.catch(err => done(err));
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
2018-02-09 17:29:26 -05:00
|
|
|
it('Test Insert', async () => {
|
2018-02-02 11:50:29 -05:00
|
|
|
const promise = await qb.set('id', 98)
|
2016-01-26 19:29:12 -05:00
|
|
|
.set('key', '84')
|
2016-11-10 22:10:45 -05:00
|
|
|
.set('val', Buffer.from('120'))
|
2016-01-26 19:29:12 -05:00
|
|
|
.insert('create_test');
|
|
|
|
|
2018-02-02 11:50:29 -05:00
|
|
|
expect(promise).toEqual(expect.anything());
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
2018-02-09 17:29:26 -05:00
|
|
|
it('Test Insert Object', async () => {
|
2018-02-02 11:50:29 -05:00
|
|
|
const promise = await qb.insert('create_test', {
|
2016-01-26 19:29:12 -05:00
|
|
|
id: 587,
|
|
|
|
key: 1,
|
2016-11-10 22:10:45 -05:00
|
|
|
val: Buffer.from('2')
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
|
|
|
|
2018-02-02 11:50:29 -05:00
|
|
|
expect(promise).toEqual(expect.anything());
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
2018-02-09 17:29:26 -05:00
|
|
|
it('Test Update', async () => {
|
2018-02-02 11:50:29 -05:00
|
|
|
const promise = await qb.where('id', 7)
|
2016-01-26 19:29:12 -05:00
|
|
|
.update('create_test', {
|
|
|
|
id: 7,
|
|
|
|
key: 'gogle',
|
2016-11-10 22:10:45 -05:00
|
|
|
val: Buffer.from('non-word')
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
|
|
|
|
2018-02-02 11:50:29 -05:00
|
|
|
expect(promise).toEqual(expect.anything());
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
2018-02-09 17:29:26 -05:00
|
|
|
it('Test set Array Update', async () => {
|
2016-01-26 19:29:12 -05:00
|
|
|
let object = {
|
|
|
|
id: 22,
|
|
|
|
key: 'gogle',
|
2016-11-10 22:10:45 -05:00
|
|
|
val: Buffer.from('non-word')
|
2016-01-26 19:29:12 -05:00
|
|
|
};
|
|
|
|
|
2018-02-02 11:50:29 -05:00
|
|
|
const promise = await qb.set(object)
|
2016-01-26 19:29:12 -05:00
|
|
|
.where('id', 22)
|
|
|
|
.update('create_test');
|
|
|
|
|
2018-02-02 11:50:29 -05:00
|
|
|
expect(promise).toEqual(expect.anything());
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
2018-02-09 17:29:26 -05:00
|
|
|
it('Test where set update', async () => {
|
2018-02-02 11:50:29 -05:00
|
|
|
const promise = await qb.where('id', 36)
|
2016-01-26 19:29:12 -05:00
|
|
|
.set('id', 36)
|
|
|
|
.set('key', 'gogle')
|
2016-11-10 22:10:45 -05:00
|
|
|
.set('val', Buffer.from('non-word'))
|
2016-01-26 19:29:12 -05:00
|
|
|
.update('create_test');
|
|
|
|
|
2018-02-02 11:50:29 -05:00
|
|
|
expect(promise).toEqual(expect.anything());
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
2018-02-09 17:29:26 -05:00
|
|
|
it('Test delete', async () => {
|
2018-02-02 11:50:29 -05:00
|
|
|
const promise = await qb.delete('create_test', {id: 5});
|
|
|
|
expect(promise).toEqual(expect.anything());
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
2018-02-09 17:29:26 -05:00
|
|
|
it('Delete with where', async () => {
|
2018-02-02 11:50:29 -05:00
|
|
|
const promise = await qb.where('id', 5)
|
2016-01-26 19:29:12 -05:00
|
|
|
.delete('create_test');
|
|
|
|
|
2018-02-02 11:50:29 -05:00
|
|
|
expect(promise).toEqual(expect.anything());
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
2018-02-09 17:29:26 -05:00
|
|
|
it('Delete multiple where values', async () => {
|
2018-02-02 11:50:29 -05:00
|
|
|
const promise = await qb.delete('create_test', {
|
2016-01-26 19:29:12 -05:00
|
|
|
id: 5,
|
2016-09-14 16:50:32 -04:00
|
|
|
key: 'gogle'
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
|
|
|
|
2018-02-02 11:50:29 -05:00
|
|
|
expect(promise).toEqual(expect.anything());
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
|
|
|
});
|
2018-02-09 17:29:26 -05:00
|
|
|
|
|
|
|
describe('Batch tests -', () => {
|
|
|
|
it('Test Insert Batch', async () => {
|
|
|
|
const data = [
|
|
|
|
{
|
|
|
|
id: 544,
|
|
|
|
key: 3,
|
|
|
|
val: Buffer.from('7')
|
|
|
|
}, {
|
|
|
|
id: 89,
|
|
|
|
key: 34,
|
|
|
|
val: Buffer.from('10 o\'clock')
|
|
|
|
}, {
|
|
|
|
id: 48,
|
|
|
|
key: 403,
|
|
|
|
val: Buffer.from('97')
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
const promise = await qb.insertBatch('create_test', data);
|
|
|
|
expect(promise).toEqual(expect.anything());
|
|
|
|
});
|
|
|
|
it('Test Update Batch', async () => {
|
|
|
|
const data = [{
|
|
|
|
id: 480,
|
|
|
|
key: 49,
|
|
|
|
val: '7x7'
|
|
|
|
}, {
|
|
|
|
id: 890,
|
|
|
|
key: 100,
|
|
|
|
val: '10x10'
|
|
|
|
}];
|
|
|
|
|
|
|
|
const affectedRows = qb.updateBatch('create_test', data, 'id');
|
|
|
|
expect(affectedRows).toBe(2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Grouping tests -', () => {
|
|
|
|
it('Using grouping method', async () => {
|
2018-02-02 11:50:29 -05:00
|
|
|
const promise = await qb.select('id, key as k, val')
|
2016-01-26 19:29:12 -05:00
|
|
|
.from('create_test')
|
|
|
|
.groupStart()
|
|
|
|
.where('id >', 1)
|
|
|
|
.where('id <', 900)
|
|
|
|
.groupEnd()
|
|
|
|
.limit(2, 1)
|
|
|
|
.get();
|
|
|
|
|
2018-02-02 11:50:29 -05:00
|
|
|
expect(promise).toEqual(expect.anything());
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
2018-02-09 17:29:26 -05:00
|
|
|
it('Using where first grouping', async () => {
|
2018-02-02 11:50:29 -05:00
|
|
|
const promise = await qb.select('id, key as k, val')
|
2016-01-26 19:29:12 -05:00
|
|
|
.from('create_test')
|
|
|
|
.where('id !=', 5)
|
|
|
|
.groupStart()
|
|
|
|
.where('id >', 1)
|
|
|
|
.where('id <', 900)
|
|
|
|
.groupEnd()
|
|
|
|
.limit(2, 1)
|
|
|
|
.get();
|
|
|
|
|
2018-02-02 11:50:29 -05:00
|
|
|
expect(promise).toEqual(expect.anything());
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
2018-02-09 17:29:26 -05:00
|
|
|
it('Using or grouping method', async () => {
|
2018-02-02 11:50:29 -05:00
|
|
|
const promise = await qb.select('id, key as k, val')
|
2016-01-26 19:29:12 -05:00
|
|
|
.from('create_test')
|
|
|
|
.groupStart()
|
|
|
|
.where('id >', 1)
|
|
|
|
.where('id <', 900)
|
|
|
|
.groupEnd()
|
|
|
|
.orGroupStart()
|
|
|
|
.where('id', 0)
|
|
|
|
.groupEnd()
|
|
|
|
.limit(2, 1)
|
|
|
|
.get();
|
|
|
|
|
2018-02-02 11:50:29 -05:00
|
|
|
expect(promise).toEqual(expect.anything());
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
2018-02-09 17:29:26 -05:00
|
|
|
it('Using or not grouping method', async () => {
|
2018-02-02 11:50:29 -05:00
|
|
|
const promise = await qb.select('id, key as k, val')
|
2016-01-26 19:29:12 -05:00
|
|
|
.from('create_test')
|
|
|
|
.groupStart()
|
|
|
|
.where('id >', 1)
|
|
|
|
.where('id <', 900)
|
|
|
|
.groupEnd()
|
|
|
|
.orNotGroupStart()
|
|
|
|
.where('id', 0)
|
|
|
|
.groupEnd()
|
|
|
|
.limit(2, 1)
|
|
|
|
.get();
|
|
|
|
|
2018-02-02 11:50:29 -05:00
|
|
|
expect(promise).toEqual(expect.anything());
|
2016-01-26 19:29:12 -05:00
|
|
|
});
|
|
|
|
});
|
2018-02-09 17:29:26 -05:00
|
|
|
describe('Get compiled - ', () => {
|
|
|
|
it('getCompiledSelect', () => {
|
|
|
|
const sql = qb.select('id, key as k, val')
|
|
|
|
.from('create_test')
|
|
|
|
.groupStart()
|
|
|
|
.where('id >', 1)
|
|
|
|
.where('id <', 900)
|
|
|
|
.groupEnd()
|
|
|
|
.limit(2, 1)
|
|
|
|
.getCompiledSelect();
|
|
|
|
|
|
|
|
expect(sql).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
it('getCompiledSelect 2', () => {
|
|
|
|
const sql = qb.select('id, key as k, val')
|
|
|
|
.groupStart()
|
|
|
|
.where('id >', 1)
|
|
|
|
.where('id <', 900)
|
|
|
|
.groupEnd()
|
|
|
|
.limit(2, 1)
|
|
|
|
.getCompiledSelect('create_test');
|
|
|
|
|
|
|
|
expect(sql).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
it('getCompiledInsert', () => {
|
|
|
|
const sql = qb.set({
|
|
|
|
id: 587,
|
|
|
|
key: 1,
|
|
|
|
val: Buffer.from('2')
|
|
|
|
}).getCompiledInsert('create_test');
|
|
|
|
|
|
|
|
expect(sql).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
it('getCompiledUpdate', () => {
|
|
|
|
const sql = qb.where('id', 36)
|
|
|
|
.set('id', 36)
|
|
|
|
.set('key', 'gogle')
|
|
|
|
.set('val', Buffer.from('non-word'))
|
|
|
|
.getCompiledUpdate('create_test');
|
|
|
|
|
|
|
|
expect(sql).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
it('getCompiledDelete', () => {
|
|
|
|
const sql = qb.where({id: 5}).getCompiledDelete('create_test');
|
|
|
|
expect(sql).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
2016-09-14 16:50:32 -04:00
|
|
|
};
|