node-query/test/adapters/adapterTestBase.js

479 lines
9.9 KiB
JavaScript
Raw Normal View History

2015-12-07 17:03:36 -05:00
'use strict';
2015-12-07 12:03:42 -05:00
module.exports.tests = {
'Get tests': {
'Get with function': {
select: ['id, COUNT(id) as count'],
from: ['create_test'],
groupBy: ['id'],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 12:03:42 -05:00
},
'Basic select all get': {
2015-12-07 17:03:36 -05:00
get: ['create_test'],
2015-12-07 12:03:42 -05:00
},
'Basic select all with from': {
from: ['create_test'],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 12:03:42 -05:00
},
'Get with limit': {
2015-12-07 17:03:36 -05:00
get: ['create_test', 2],
2015-12-07 12:03:42 -05:00
},
'Get with limit and offset': {
2015-12-07 17:03:36 -05:00
get: ['create_test', 2, 1],
2015-12-07 12:03:42 -05:00
},
'Get with having': {
select: ['id'],
from: ['create_test'],
groupBy: ['id'],
having: [
'multiple',
[{'id >': 1}],
['id !=', 3],
['id', 900],
],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 12:03:42 -05:00
},
'Get with orHaving': {
select: ['id'],
from: ['create_test'],
groupBy: ['id'],
having: [{'id >': 1}],
orHaving: ['id !=', 3],
2015-12-07 17:03:36 -05:00
get: [],
},
2015-12-07 12:03:42 -05:00
},
'Select tests': {
'Select where get': {
select: [['id', 'key as k', 'val']],
where: [
'multiple',
['id >', 1],
2015-12-07 17:03:36 -05:00
['id <', 900],
2015-12-07 12:03:42 -05:00
],
2015-12-07 17:03:36 -05:00
get: ['create_test', 2, 1],
2015-12-07 12:03:42 -05:00
},
'Select where get 2': {
select: ['id, key as k, val'],
where: ['id !=', 1],
2015-12-07 17:03:36 -05:00
get: ['create_test', 2, 1],
2015-12-07 12:03:42 -05:00
},
'Multi Order By': {
from: ['create_test'],
orderBy: ['id, key'],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 12:03:42 -05:00
},
'Select get': {
select: ['id, key as k, val'],
2015-12-07 17:03:36 -05:00
get: ['create_test', 2, 1],
2015-12-07 12:03:42 -05:00
},
'Select from get': {
select: ['id, key as k, val'],
from: ['create_test ct'],
where: ['id >', 1],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 12:03:42 -05:00
},
'Select from limit get': {
select: ['id, key as k, val'],
from: ['create_test ct'],
where: ['id >', 1],
limit: [3],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 12:03:42 -05:00
},
'Select where IS NOT NULL': {
select: ['id', 'key as k', 'val'],
from: ['create_test ct'],
whereIsNotNull: ['id'],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 12:03:42 -05:00
},
'Select where IS NULL': {
select: ['id', 'key as k', 'val'],
from: ['create_test ct'],
whereIsNull: ['id'],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 12:03:42 -05:00
},
'Select where OR IS NOT NULL': {
select: ['id', 'key as k', 'val'],
from: ['create_test ct'],
whereIsNull: ['id'],
orWhereIsNotNull: ['id'],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 12:03:42 -05:00
},
'Select where OR IS NULL': {
select: ['id', 'key as k', 'val'],
from: ['create_test ct'],
where: ['id', 3],
orWhereIsNull: ['id'],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 12:03:42 -05:00
},
'Select with string where value': {
select: ['id', 'key as k', 'val'],
from: ['create_test ct'],
where: ['id > 3'],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 12:03:42 -05:00
},
'Select with function and argument in WHERE clause': {
select: ['id'],
from: ['create_test ct'],
where: ['id', 'CEILING(SQRT(88))'],
2015-12-07 17:03:36 -05:00
get: [],
},
2015-12-07 14:33:48 -05:00
},
'Where in tests': {
'Where in': {
from: ['create_test'],
whereIn: ['id', [0, 6, 56, 563, 341]],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 14:33:48 -05:00
},
'Or Where in': {
from: ['create_test'],
where: ['key', 'false'],
orWhereIn: ['id', [0, 6, 56, 563, 341]],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 14:33:48 -05:00
},
'Where Not in': {
from: ['create_test'],
where: ['key', 'false'],
whereNotIn: ['id', [0, 6, 56, 563, 341]],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 14:33:48 -05:00
},
'Or Where Not in': {
from: ['create_test'],
where: ['key', 'false'],
orWhereNotIn: ['id', [0, 6, 56, 563, 341]],
2015-12-07 17:03:36 -05:00
get: [],
},
2015-12-07 14:33:48 -05:00
},
'Query modifier tests': {
'Order By': {
select: ['id, key as k, val'],
from: ['create_test'],
where: [
'multiple',
['id >', 0],
2015-12-07 17:03:36 -05:00
['id <', 9000],
2015-12-07 14:33:48 -05:00
],
orderBy: [
'multiple',
['id', 'DESC'],
2015-12-07 17:03:36 -05:00
['k', 'ASC'],
2015-12-07 14:33:48 -05:00
],
limit: [5, 2],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 14:33:48 -05:00
},
'Group By': {
select: ['id, key as k, val'],
from: ['create_test'],
where: [
'multiple',
['id >', 0],
2015-12-07 17:03:36 -05:00
['id <', 9000],
2015-12-07 14:33:48 -05:00
],
groupBy: [
'multiple',
['k'],
2015-12-07 17:03:36 -05:00
[['id', 'val']],
2015-12-07 14:33:48 -05:00
],
orderBy: [
'multiple',
['id', 'DESC'],
2015-12-07 17:03:36 -05:00
['k', 'ASC'],
2015-12-07 14:33:48 -05:00
],
2015-12-07 17:03:36 -05:00
limit: [5, 2],
get: [],
2015-12-07 14:33:48 -05:00
},
'Or Where': {
select: ['id, key as k, val'],
from: ['create_test'],
where: [' id ', 1],
orWhere: ['key > ', 0],
limit: [2, 1],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 14:33:48 -05:00
},
2015-12-07 17:03:36 -05:00
Like: {
2015-12-07 14:33:48 -05:00
from: ['create_test'],
like: ['key', 'og'],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 14:33:48 -05:00
},
'Or Like': {
from: ['create_test'],
like: ['key', 'og'],
orLike: ['key', 'val'],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 14:33:48 -05:00
},
'Not Like': {
from: ['create_test'],
like: ['key', 'og', 'before'],
notLike: ['key', 'val'],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 14:33:48 -05:00
},
'Or Not Like': {
from: ['create_test'],
like: ['key', 'og', 'before'],
orNotLike: ['key', 'val'],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 14:33:48 -05:00
},
'Like Before': {
from: ['create_test'],
like: ['key', 'og', 'before'],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 14:33:48 -05:00
},
'Like After': {
from: ['create_test'],
like: ['key', 'og', 'after'],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 14:33:48 -05:00
},
'Basic Join': {
from: ['create_test ct'],
join: ['create_join cj', 'cj.id=ct.id'],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 14:33:48 -05:00
},
'Left Join': {
from: ['create_test ct'],
join: ['create_join cj', 'cj.id=ct.id', 'left'],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 14:33:48 -05:00
},
'Inner Join': {
from: ['create_test ct'],
join: ['create_join cj', 'cj.id=ct.id', 'inner'],
2015-12-07 17:03:36 -05:00
get: [],
2015-12-07 14:33:48 -05:00
},
'Join with multiple where values': {
from: ['create_test ct'],
join: ['create_join cj', 'cj.id=ct.id', 'inner'],
2015-12-07 17:03:36 -05:00
where: [
{
'ct.id < ': 3,
'ct.key ': 'foo',
},
],
get: [],
},
},
2015-12-07 12:03:42 -05:00
};
2015-12-07 14:33:48 -05:00
let expect = require('chai').expect,
helpers = require('../../lib/helpers'),
State = require('../../lib/State');
2015-12-07 12:03:42 -05:00
module.exports.runner = (tests, qb, callback) => {
Object.keys(tests).forEach(suiteName => {
suite(suiteName, () => {
let currentSuite = tests[suiteName];
Object.keys(currentSuite).forEach(testDesc => {
test(testDesc, done => {
let methodObj = currentSuite[testDesc];
let methodNames = Object.keys(methodObj);
let lastMethodIndex = methodNames[methodNames.length - 1];
methodObj[lastMethodIndex].push((err, rows) => {
callback(err, done);
});
methodNames.forEach(name => {
let args = methodObj[name],
method = qb[name];
if (args[0] === 'multiple') {
args.shift();
args.forEach(argSet => {
method.apply(qb, argSet);
});
} else {
method.apply(qb, args);
}
});
});
});
});
});
2015-12-07 14:33:48 -05:00
suite('DB update tests', () => {
setup(done => {
let sql = qb.driver.truncate('create_test');
qb.adapter.execute(sql, (err, res) => {
done();
});
});
test('Test Insert', done => {
qb.set('id', 98)
2015-12-07 17:03:36 -05:00
.set('key', '84')
.set('val', new Buffer('120'))
2015-12-07 14:33:48 -05:00
.insert('create_test', (err, rows) => {
return callback(err, done);
});
});
test('Test Insert Object', done => {
qb.insert('create_test', {
id: 587,
key: 1,
2015-12-07 17:03:36 -05:00
val: new Buffer('2'),
2015-12-07 14:33:48 -05:00
}, (err, rows) => {
return callback(err, done);
});
});
test('Test Update', done => {
qb.where('id', 7)
.update('create_test', {
id: 7,
key: 'gogle',
2015-12-07 17:03:36 -05:00
val: new Buffer('non-word'),
2015-12-07 14:33:48 -05:00
}, (err, rows) => {
return callback(err, done);
});
});
test('Test set Array Update', done => {
let object = {
id: 22,
key: 'gogle',
2015-12-07 17:03:36 -05:00
val: new Buffer('non-word'),
2015-12-07 14:33:48 -05:00
};
qb.set(object)
.where('id', 22)
.update('create_test', (err, rows) => {
return callback(err, done);
});
});
test('Test where set update', done => {
qb.where('id', 36)
.set('id', 36)
.set('key', 'gogle')
.set('val', new Buffer('non-word'))
.update('create_test', (err, rows) => {
return callback(err, done);
});
});
test('Test delete', done => {
qb.delete('create_test', {id: 5}, (err, rows) => {
return callback(err, done);
});
});
test('Delete with where', done => {
qb.where('id', 5)
.delete('create_test', (err, rows) => {
return callback(err, done);
});
});
test('Delete multiple where values', done => {
qb.delete('create_test', {
id: 5,
2015-12-07 17:03:36 -05:00
key: 'gogle',
2015-12-07 14:33:48 -05:00
}, (err, rows) => {
return callback(err, done);
});
});
});
suite('Grouping tests', () => {
test('Using grouping method', done => {
qb.select('id, key as k, val')
.from('create_test')
.groupStart()
.where('id >', 1)
.where('id <', 900)
.groupEnd()
.limit(2, 1)
.get((err, rows) => {
return callback(err, done);
});
});
test('Using where first grouping', done => {
qb.select('id, key as k, val')
.from('create_test')
.where('id !=', 5)
.groupStart()
.where('id >', 1)
.where('id <', 900)
.groupEnd()
.limit(2, 1)
.get((err, rows) => {
return callback(err, done);
});
});
test('Using or grouping method', done => {
qb.select('id, key as k, val')
.from('create_test')
.groupStart()
.where('id >', 1)
.where('id <', 900)
.groupEnd()
.orGroupStart()
.where('id', 0)
.groupEnd()
.limit(2, 1)
.get((err, rows) => {
return callback(err, done);
});
});
test('Using or not grouping method', done => {
qb.select('id, key as k, val')
.from('create_test')
.groupStart()
.where('id >', 1)
.where('id <', 900)
.groupEnd()
.orNotGroupStart()
.where('id', 0)
.groupEnd()
.limit(2, 1)
.get((err, rows) => {
return callback(err, done);
});
});
});
suite('Get compiled tests', () => {
test('select', () => {
let sql = qb.select('id')
.from('create_test')
.getCompiledSelect(true);
expect(helpers.isString(sql)).to.be.true;
});
test('select from', () => {
let sql = qb.select('id')
.getCompiledSelect('create_test', true);
expect(helpers.isString(sql)).to.be.true;
});
test('insert', () => {
let sql = qb.set('id', 3)
.getCompiledInsert('create_test');
expect(helpers.isString(sql)).to.be.true;
});
test('update', () => {
let sql = qb.set('id', 3)
.where('id', 5)
.getCompiledUpdate('create_test');
expect(helpers.isString(sql)).to.be.true;
});
test('delete', () => {
let sql = qb.where('id', 5)
.getCompiledDelete('create_test');
expect(helpers.isString(sql)).to.be.true;
});
});
suite('Misc tests', () => {
test('Get State', () => {
qb.select('foo')
.from('bar')
.where('baz', 'foobar');
let state = new State();
expect(JSON.stringify(state)).to.not.be.deep.equal(qb.getState());
});
test('Reset State', () => {
qb.select('foo')
.from('bar')
.where('baz', 'foobar');
qb.resetQuery();
let state = new State();
expect(qb.getState()).to.be.deep.equal(state);
2015-12-07 17:03:36 -05:00
});
2015-12-07 14:33:48 -05:00
});
2015-12-07 12:03:42 -05:00
};