node-query/tests/query-builder-base.js

517 lines
12 KiB
JavaScript
Raw Normal View History

2014-10-23 10:53:16 -04:00
'use strict';
var helpers = require('../lib/helpers');
module.exports = (function QueryBuilderTestBase() {
// That 'new' keyword is annoying
if ( ! (this instanceof QueryBuilderTestBase)) return new QueryBuilderTestBase();
2014-10-23 10:53:16 -04:00
var base = {};
/**
* Inject the appropriate driver and adapter for the test suite
*
* @param {Object} qb - The adapter-specific query builder object
* @param {Function} callback - The test callback
* @return void
*/
this._setUp = function(qb, callback) {
2014-10-23 10:53:16 -04:00
base.qb = qb;
base.testCallback = callback;
this.qb = base.qb;
2014-10-23 10:53:16 -04:00
};
/**
* Generic query builder tests
*/
this.tests = {
// ! Get tests
2014-10-23 10:53:16 -04:00
'Get tests' : {
'Get with function': function(test) {
test.expect(1);
base.qb.select('id, COUNT(id) as count')
.from('create_test')
.groupBy('id')
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
},
2014-10-23 10:53:16 -04:00
'Basic select all get': function(test) {
test.expect(1);
2014-10-27 15:46:54 -04:00
base.qb.get('create_test', base.testCallback.bind(this, test));
2014-10-23 10:53:16 -04:00
},
'Basic select all with from': function(test) {
test.expect(1);
2014-10-23 10:53:16 -04:00
base.qb.from('create_test')
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
},
'Get with limit': function(test) {
test.expect(1);
2014-10-27 15:46:54 -04:00
base.qb.get('create_test', 2, base.testCallback.bind(this, test));
},
'Get with limit and offset': function(test) {
test.expect(1);
2014-10-27 15:46:54 -04:00
base.qb.get('create_test', 2, 1, base.testCallback.bind(this, test));
2014-10-23 15:33:20 -04:00
},
'Test get with having': function(test) {
test.expect(1);
2014-10-23 15:33:20 -04:00
base.qb.select('id')
.from('create_test')
.groupBy('id')
.having({'id >':1})
.having('id !=', 3)
.having('id', 900)
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
2014-10-23 15:33:20 -04:00
},
"Test get with 'orHaving'": function(test) {
test.expect(1);
2014-10-23 15:33:20 -04:00
base.qb.select('id')
.from('create_test')
.groupBy('id')
.having({'id >':1})
.orHaving('id !=', 3)
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
2014-10-23 10:53:16 -04:00
}
},
// ! Select tests
2014-10-23 10:53:16 -04:00
'Select tests' : {
2014-10-23 15:33:20 -04:00
'Select where get': function(test) {
test.expect(1);
base.qb.select(['id', 'key as k', 'val'])
.where('id >', 1)
.where('id <', 900)
2014-10-27 15:46:54 -04:00
.get('create_test', 2, 1, base.testCallback.bind(this, test));
},
'Select where get 2': function(test) {
test.expect(1);
2014-10-23 15:33:20 -04:00
base.qb.select('id, key as k, val')
.where('id !=', 1)
2014-10-27 15:46:54 -04:00
.get('create_test', 2, 1, base.testCallback.bind(this, test));
},
'Multi Order By': function(test) {
test.expect(1);
base.qb.from('create_test')
.orderBy('id, key')
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
2014-10-24 10:30:54 -04:00
},
'Select get': function(test) {
test.expect(1);
2014-10-24 10:30:54 -04:00
base.qb.select('id, key as k, val')
2014-10-27 15:46:54 -04:00
.get('create_test', 2, 1, base.testCallback.bind(this, test));
2014-10-24 10:30:54 -04:00
},
'Select from get': function(test) {
test.expect(1);
2014-10-24 10:30:54 -04:00
base.qb.select('id, key as k, val')
.from('create_test ct')
.where('id >', 1)
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
2014-10-24 10:30:54 -04:00
},
'Select from limit get': function(test) {
test.expect(1);
2014-10-24 10:30:54 -04:00
base.qb.select('id, key as k, val')
.from('create_test ct')
.where('id >', 1)
.limit(3)
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
2014-10-23 15:33:20 -04:00
}
2014-10-23 10:53:16 -04:00
},
// ! Grouping tests
2014-10-23 10:53:16 -04:00
'Grouping tests' : {
2014-10-24 10:30:54 -04:00
'Using grouping method': function(test) {
test.expect(1);
2014-10-24 10:30:54 -04:00
base.qb.select('id, key as k, val')
.from('create_test')
.groupStart()
.where('id >', 1)
.where('id <', 900)
.groupEnd()
.limit(2, 1)
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
2014-10-24 10:30:54 -04:00
},
'Using where first grouping': function(test) {
test.expect(1);
base.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(base.testCallback.bind(this, test));
},
2014-10-24 10:30:54 -04:00
'Using or grouping method': function(test) {
test.expect(1);
2014-10-24 10:30:54 -04:00
base.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)
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
2014-10-24 10:30:54 -04:00
},
'Using or not grouping method': function(test) {
test.expect(1);
2014-10-24 10:30:54 -04:00
base.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)
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
2014-10-24 10:30:54 -04:00
}
2014-10-23 10:53:16 -04:00
},
// ! Where in tests
2014-10-23 10:53:16 -04:00
'Where in tests' : {
2014-10-24 10:30:54 -04:00
'Where in': function(test) {
test.expect(1);
2014-10-24 10:30:54 -04:00
base.qb.from('create_test')
.whereIn('id', [0, 6, 56, 563, 341])
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
2014-10-24 10:30:54 -04:00
},
'Or Where in': function(test) {
test.expect(1);
2014-10-24 10:30:54 -04:00
base.qb.from('create_test')
.where('key', 'false')
.orWhereIn('id', [0, 6, 56, 563, 341])
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
2014-10-24 10:30:54 -04:00
},
'Where Not in': function(test) {
test.expect(1);
2014-10-24 10:30:54 -04:00
base.qb.from('create_test')
.where('key', 'false')
.whereNotIn('id', [0, 6, 56, 563, 341])
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
2014-10-24 10:30:54 -04:00
},
'Or Where Not in': function(test) {
test.expect(1);
2014-10-24 10:30:54 -04:00
base.qb.from('create_test')
.where('key', 'false')
.orWhereNotIn('id', [0, 6, 56, 563, 341])
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
2014-10-24 10:30:54 -04:00
}
2014-10-23 10:53:16 -04:00
},
2014-10-27 15:46:54 -04:00
// ! Query modifier tests
2014-10-23 10:53:16 -04:00
'Query modifier tests': {
2014-10-24 10:30:54 -04:00
'Order By': function(test) {
test.expect(1);
2014-10-24 10:30:54 -04:00
base.qb.select('id, key as k, val')
.from('create_test')
.where('id >', 0)
.where('id <', 9000)
.orderBy('id', 'DESC')
.orderBy('k', "ASC")
.limit(5, 2)
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
2014-10-24 10:30:54 -04:00
},
'Group by': function(test) {
test.expect(1);
2014-10-24 10:30:54 -04:00
base.qb.select('id, key as k, val')
.from('create_test')
.where('id >', 0)
.where('id <', 9000)
.groupBy('k')
.groupBy(['id', 'val'])
2014-10-24 10:30:54 -04:00
.orderBy('id', 'DESC')
.orderBy('k', "ASC")
.limit(5, 2)
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
2014-10-24 10:30:54 -04:00
},
'Or Where': function(test) {
test.expect(1);
2014-10-24 10:30:54 -04:00
base.qb.select('id, key as k, val')
.from('create_test')
.where(' id ', 1)
.orWhere('key >', 0)
.limit(2, 1)
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
2014-10-24 10:30:54 -04:00
},
'Like' : function(test) {
test.expect(1);
2014-10-24 10:30:54 -04:00
base.qb.from('create_test')
.like('key', 'og')
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
2014-10-24 10:30:54 -04:00
},
'Or Like': function(test) {
test.expect(1);
2014-10-24 10:30:54 -04:00
base.qb.from('create_test')
.like('key', 'og')
.orLike('key', 'val')
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
},
'Not Like': function(test) {
test.expect(1);
base.qb.from('create_test')
.like('key', 'og', 'before')
.notLike('key', 'val')
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
},
'Or Not Like': function(test) {
test.expect(1);
base.qb.from('create_test')
.like('key', 'og', 'before')
.orNotLike('key', 'val')
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
},
'Like Before': function(test) {
test.expect(1);
base.qb.from('create_test')
.like('key', 'og', 'before')
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
},
'Like After': function(test) {
test.expect(1);
base.qb.from('create_test')
.like('key', 'og', 'after')
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
2014-10-27 10:35:16 -04:00
},
'Basic Join': function(test) {
test.expect(1);
base.qb.from('create_test ct')
2014-10-27 10:35:16 -04:00
.join('create_join cj', 'cj.id=ct.id')
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
2014-10-27 10:35:16 -04:00
},
'Left Join': function(test) {
test.expect(1);
2014-10-27 10:35:16 -04:00
base.qb.from('create_test ct')
.join('create_join cj', 'cj.id=ct.id', 'left')
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
2014-10-27 10:35:16 -04:00
},
'InnerJoin': function(test) {
test.expect(1);
2014-10-27 10:35:16 -04:00
base.qb.from('create_test ct')
.join('create_join cj', 'cj.id=ct.id', 'inner')
2014-10-27 15:46:54 -04:00
.get(base.testCallback.bind(this, test));
2014-10-27 10:35:16 -04:00
}
2014-10-23 10:53:16 -04:00
},
2014-10-27 15:46:54 -04:00
// ! DB Update test
2014-10-23 10:53:16 -04:00
'DB update tests' : {
setUp: function(callback) {
var sql = base.qb.driver.truncate('create_test');
base.qb.adapter.execute(sql, function(err, result) {
if (err) {
throw new Error(err);
}
callback();
});
},
tearDown: function(callback) {
callback();
},
'Test Insert': function(test) {
test.expect(1);
2014-10-27 10:35:16 -04:00
base.qb.set('id', 98)
.set('key', "84")
.set('val', new Buffer("120"))
2014-10-27 15:46:54 -04:00
.insert('create_test', base.testCallback.bind(this, test));
2014-10-27 10:35:16 -04:00
},
'Test Insert Object': function(test) {
test.expect(1);
2014-10-27 10:35:16 -04:00
base.qb.insert('create_test', {
id: 587,
key: 1,
val: new Buffer('2')
2014-10-27 15:46:54 -04:00
}, base.testCallback.bind(this, test));
},
2014-10-30 09:48:03 -04:00
'Test Insert Batch': function(test) {
test.expect(1);
var data = [{
id: 544,
key: 3,
val: new Buffer('7')
2014-10-30 09:48:03 -04:00
}, {
id: 89,
key: 34,
val: new Buffer("10 o'clock")
2014-10-30 09:48:03 -04:00
}, {
id: 48,
key: 403,
val: new Buffer('97')
2014-10-30 09:48:03 -04:00
}];
base.qb.insertBatch('create_test', data, base.testCallback.bind(this, test));
},
'Test Update': function(test) {
test.expect(1);
base.qb.where('id', 7)
.update('create_test', {
id: 7,
key: 'gogle',
val: new Buffer('non-word')
2014-10-27 15:46:54 -04:00
}, base.testCallback.bind(this, test));
},
'Test set Array Update': function(test) {
test.expect(1);
var object = {
id: 22,
key: 'gogle',
val: new Buffer('non-word')
};
2014-10-23 10:53:16 -04:00
base.qb.set(object)
.where('id', 22)
2014-10-27 15:46:54 -04:00
.update('create_test', base.testCallback.bind(this, test));
},
'Test where set update': function(test) {
test.expect(1);
base.qb.where('id', 36)
.set('id', 36)
.set('key', 'gogle')
.set('val', new Buffer('non-word'))
2014-10-27 15:46:54 -04:00
.update('create_test', base.testCallback.bind(this, test));
},
'Test delete': function(test) {
test.expect(1);
2014-10-27 15:46:54 -04:00
base.qb.delete('create_test', {id: 5}, base.testCallback.bind(this, test));
2014-10-30 09:48:03 -04:00
},
'delete with where': function(test) {
test.expect(1);
base.qb.where('id', 5)
.delete('create_test', base.testCallback.bind(this, test));
},
'Delete multiple where values': function(test) {
test.expect(1);
base.qb.delete('create_test', {
id: 5,
key: 'gogle'
}, base.testCallback.bind(this, test));
2014-10-30 09:48:03 -04:00
}
},
// ! Get compiled tests
'Get compiled tests' : {
'select': function(test) {
test.expect(1);
var string = base.qb.select('id')
.from('create_test')
.getCompiledSelect(true);
test.equal(true, helpers.isString(string));
test.done();
},
'select from': function(test) {
test.expect(1);
var string = base.qb.select('id')
.getCompiledSelect('create_test', true);
test.equal(true, helpers.isString(string));
test.done();
},
'insert': function(test) {
test.expect(1);
var string = base.qb.set('id', 3)
.getCompiledInsert('create_test');
test.equal(true, helpers.isString(string));
test.done();
},
'update': function(test) {
test.expect(1);
var string = base.qb.set('id', 3)
.where('id', 5)
.getCompiledUpdate('create_test');
test.equal(true, helpers.isString(string));
test.done();
},
'delete': function(test) {
test.expect(1);
var string = base.qb.where('id', 5)
.getCompiledDelete('create_test');
test.equal(true, helpers.isString(string));
test.done();
}
2014-10-23 10:53:16 -04:00
},
// ! Misc tests
'Misc tests' : {
'Get State': function(test) {
test.expect(1);
base.qb.select('foo')
.from('bar')
.where('baz', 'foobar');
var state = {
// Arrays/Maps
queryMap: [],
values: [],
whereValues: [],
setArrayKeys: [],
orderArray: [],
groupArray: [],
havingMap: [],
whereMap: {},
2014-10-23 10:53:16 -04:00
// Partials
selectString: '',
fromString: '',
setString: '',
orderString: '',
groupString: '',
// Other various values
limit: null,
offset: null
};
test.notDeepEqual(JSON.stringify(state), JSON.stringify(base.qb.getState()));
test.done();
},
'Reset State': function(test) {
test.expect(1);
base.qb.select('foo')
.from('bar')
.where('baz', 'foobar');
base.qb.resetQuery();
var state = {
// Arrays/Maps
queryMap: [],
values: [],
whereValues: [],
setArrayKeys: [],
orderArray: [],
groupArray: [],
havingMap: [],
whereMap: {},
// Partials
selectString: '',
fromString: '',
setString: '',
orderString: '',
groupString: '',
// Other various values
limit: null,
offset: null
};
test.deepEqual(state, base.qb.getState());
test.done();
}
2014-10-23 10:53:16 -04:00
}
};
return this;
2014-10-23 10:53:16 -04:00
}());