2014-10-23 10:53:16 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
module.exports = (function() {
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
base._setUp = function(qb, callback) {
|
|
|
|
base.qb = qb;
|
|
|
|
base.testCallback = callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic query builder tests
|
|
|
|
*/
|
|
|
|
base.tests = {
|
2014-10-27 13:36:10 -04:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
},
|
2014-10-24 14:26:31 -04:00
|
|
|
// ! Get tests
|
2014-10-23 10:53:16 -04:00
|
|
|
'Get tests' : {
|
2014-10-23 11:59:42 -04:00
|
|
|
'Get with function': function(test) {
|
|
|
|
base.qb.select('id, COUNT(id) as count')
|
|
|
|
.from('create_test')
|
|
|
|
.groupBy('id')
|
2014-10-24 14:26:31 -04:00
|
|
|
.get(base.testCallback.bind(null, test));
|
2014-10-23 11:59:42 -04:00
|
|
|
},
|
2014-10-23 10:53:16 -04:00
|
|
|
'Basic select all get': function(test) {
|
2014-10-24 14:26:31 -04:00
|
|
|
base.qb.get('create_test', base.testCallback.bind(null, test));
|
2014-10-23 10:53:16 -04:00
|
|
|
},
|
|
|
|
'Basic select all with from': function(test) {
|
|
|
|
base.qb.from('create_test')
|
2014-10-24 14:26:31 -04:00
|
|
|
.get(base.testCallback.bind(null, test));
|
2014-10-23 13:50:11 -04:00
|
|
|
},
|
|
|
|
'Get with limit': function(test) {
|
2014-10-24 14:26:31 -04:00
|
|
|
base.qb.get('create_test', 2, base.testCallback.bind(null, test));
|
2014-10-23 13:50:11 -04:00
|
|
|
},
|
|
|
|
'Get with limit and offset': function(test) {
|
2014-10-24 14:26:31 -04:00
|
|
|
base.qb.get('create_test', 2, 1, base.testCallback.bind(null, test));
|
2014-10-23 15:33:20 -04:00
|
|
|
},
|
|
|
|
'Test get with having': function(test) {
|
|
|
|
base.qb.select('id')
|
|
|
|
.from('create_test')
|
|
|
|
.groupBy('id')
|
|
|
|
.having({'id >':1})
|
|
|
|
.having('id !=', 3)
|
2014-10-24 14:26:31 -04:00
|
|
|
.get(base.testCallback.bind(null, test));
|
2014-10-23 15:33:20 -04:00
|
|
|
},
|
|
|
|
"Test get with 'orHaving'": function(test) {
|
|
|
|
base.qb.select('id')
|
|
|
|
.from('create_test')
|
|
|
|
.groupBy('id')
|
|
|
|
.having({'id >':1})
|
|
|
|
.orHaving('id !=', 3)
|
2014-10-24 14:26:31 -04:00
|
|
|
.get(base.testCallback.bind(null, test));
|
2014-10-23 10:53:16 -04:00
|
|
|
}
|
|
|
|
},
|
2014-10-24 14:26:31 -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) {
|
2014-10-23 15:49:17 -04:00
|
|
|
base.qb.select(['id', 'key as k', 'val'])
|
|
|
|
.where('id >', 1)
|
|
|
|
.where('id <', 900)
|
2014-10-24 14:26:31 -04:00
|
|
|
.get('create_test', 2, 1, base.testCallback.bind(null, test));
|
|
|
|
},
|
|
|
|
'Select where get 2': function(test) {
|
2014-10-23 15:33:20 -04:00
|
|
|
base.qb.select('id, key as k, val')
|
|
|
|
.where('id !=', 1)
|
2014-10-24 14:26:31 -04:00
|
|
|
.get('create_test', 2, 1, base.testCallback.bind(null, test));
|
2014-10-23 15:49:17 -04:00
|
|
|
},
|
|
|
|
'Multi Order By': function(test) {
|
|
|
|
base.qb.from('create_test')
|
|
|
|
.orderBy('id, key')
|
2014-10-24 14:26:31 -04:00
|
|
|
.get(base.testCallback.bind(null, test));
|
2014-10-24 10:30:54 -04:00
|
|
|
},
|
|
|
|
'Select get': function(test) {
|
|
|
|
base.qb.select('id, key as k, val')
|
2014-10-24 14:26:31 -04:00
|
|
|
.get('create_test', 2, 1, base.testCallback.bind(null, test));
|
2014-10-24 10:30:54 -04:00
|
|
|
},
|
|
|
|
'Select from get': function(test) {
|
|
|
|
base.qb.select('id, key as k, val')
|
|
|
|
.from('create_test ct')
|
|
|
|
.where('id >', 1)
|
2014-10-24 14:26:31 -04:00
|
|
|
.get(base.testCallback.bind(null, test));
|
2014-10-24 10:30:54 -04:00
|
|
|
},
|
|
|
|
'Select from limit get': function(test) {
|
|
|
|
base.qb.select('id, key as k, val')
|
|
|
|
.from('create_test ct')
|
|
|
|
.where('id >', 1)
|
|
|
|
.limit(3)
|
2014-10-24 14:26:31 -04:00
|
|
|
.get(base.testCallback.bind(null, test));
|
2014-10-23 15:33:20 -04:00
|
|
|
}
|
2014-10-23 10:53:16 -04:00
|
|
|
},
|
2014-10-24 14:26:31 -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) {
|
|
|
|
base.qb.select('id, key as k, val')
|
|
|
|
.from('create_test')
|
|
|
|
.groupStart()
|
|
|
|
.where('id >', 1)
|
|
|
|
.where('id <', 900)
|
|
|
|
.groupEnd()
|
|
|
|
.limit(2, 1)
|
2014-10-24 14:26:31 -04:00
|
|
|
.get(base.testCallback.bind(null, test));
|
2014-10-24 10:30:54 -04:00
|
|
|
},
|
|
|
|
'Using or grouping method': function(test) {
|
|
|
|
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-24 14:26:31 -04:00
|
|
|
.get(base.testCallback.bind(null, test));
|
2014-10-24 10:30:54 -04:00
|
|
|
},
|
|
|
|
'Using or not grouping method': function(test) {
|
|
|
|
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-24 14:26:31 -04:00
|
|
|
.get(base.testCallback.bind(null, test));
|
2014-10-24 10:30:54 -04:00
|
|
|
}
|
2014-10-23 10:53:16 -04:00
|
|
|
},
|
2014-10-24 14:26:31 -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) {
|
|
|
|
base.qb.from('create_test')
|
|
|
|
.whereIn('id', [0, 6, 56, 563, 341])
|
2014-10-24 14:26:31 -04:00
|
|
|
.get(base.testCallback.bind(null, test));
|
2014-10-24 10:30:54 -04:00
|
|
|
},
|
|
|
|
'Or Where in': function(test) {
|
|
|
|
base.qb.from('create_test')
|
|
|
|
.where('key', 'false')
|
|
|
|
.orWhereIn('id', [0, 6, 56, 563, 341])
|
2014-10-24 14:26:31 -04:00
|
|
|
.get(base.testCallback.bind(null, test));
|
2014-10-24 10:30:54 -04:00
|
|
|
},
|
|
|
|
'Where Not in': function(test) {
|
|
|
|
base.qb.from('create_test')
|
|
|
|
.where('key', 'false')
|
|
|
|
.whereNotIn('id', [0, 6, 56, 563, 341])
|
2014-10-24 14:26:31 -04:00
|
|
|
.get(base.testCallback.bind(null, test));
|
2014-10-24 10:30:54 -04:00
|
|
|
},
|
|
|
|
'Or Where Not in': function(test) {
|
|
|
|
base.qb.from('create_test')
|
|
|
|
.where('key', 'false')
|
|
|
|
.orWhereNotIn('id', [0, 6, 56, 563, 341])
|
2014-10-24 14:26:31 -04:00
|
|
|
.get(base.testCallback.bind(null, test));
|
2014-10-24 10:30:54 -04:00
|
|
|
}
|
2014-10-23 10:53:16 -04:00
|
|
|
},
|
2014-10-24 14:26:31 -04:00
|
|
|
// ! Query modifier testss
|
2014-10-23 10:53:16 -04:00
|
|
|
'Query modifier tests': {
|
2014-10-24 10:30:54 -04:00
|
|
|
'Order By': function(test) {
|
|
|
|
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-24 14:26:31 -04:00
|
|
|
.get(base.testCallback.bind(null, test));
|
2014-10-24 10:30:54 -04:00
|
|
|
},
|
|
|
|
'Group by': function(test) {
|
|
|
|
base.qb.select('id, key as k, val')
|
|
|
|
.from('create_test')
|
|
|
|
.where('id >', 0)
|
|
|
|
.where('id <', 9000)
|
|
|
|
.groupBy('k')
|
|
|
|
.groupBy(['id', 'val'])
|
|
|
|
.orderBy('id', 'DESC')
|
|
|
|
.orderBy('k', "ASC")
|
|
|
|
.limit(5, 2)
|
2014-10-24 14:26:31 -04:00
|
|
|
.get(base.testCallback.bind(null, test));
|
2014-10-24 10:30:54 -04:00
|
|
|
},
|
|
|
|
'Or Where': function(test) {
|
|
|
|
base.qb.select('id, key as k, val')
|
|
|
|
.from('create_test')
|
|
|
|
.where(' id ', 1)
|
|
|
|
.orWhere('key >', 0)
|
|
|
|
.limit(2, 1)
|
2014-10-24 14:26:31 -04:00
|
|
|
.get(base.testCallback.bind(null, test));
|
2014-10-24 10:30:54 -04:00
|
|
|
},
|
|
|
|
'Like' : function(test) {
|
|
|
|
base.qb.from('create_test')
|
|
|
|
.like('key', 'og')
|
2014-10-24 14:26:31 -04:00
|
|
|
.get(base.testCallback.bind(null, test));
|
2014-10-24 10:30:54 -04:00
|
|
|
},
|
|
|
|
'Or Like': function(test) {
|
|
|
|
base.qb.from('create_test')
|
|
|
|
.like('key', 'og')
|
|
|
|
.orLike('key', 'val')
|
2014-10-24 14:26:31 -04:00
|
|
|
.get(base.testCallback.bind(null, test));
|
|
|
|
},
|
|
|
|
'Not Like': function(test) {
|
|
|
|
base.qb.from('create_test')
|
|
|
|
.like('key', 'og', 'before')
|
|
|
|
.notLike('key', 'val')
|
|
|
|
.get(base.testCallback.bind(null, test));
|
|
|
|
},
|
|
|
|
'Or Not Like': function(test) {
|
|
|
|
base.qb.from('create_test')
|
|
|
|
.like('key', 'og', 'before')
|
|
|
|
.orNotLike('key', 'val')
|
|
|
|
.get(base.testCallback.bind(null, test));
|
|
|
|
},
|
|
|
|
'Like Before': function(test) {
|
|
|
|
base.qb.from('create_test')
|
|
|
|
.like('key', 'og', 'before')
|
|
|
|
.get(base.testCallback.bind(null, test));
|
|
|
|
},
|
|
|
|
'Like After': function(test) {
|
|
|
|
base.qb.from('create_test')
|
|
|
|
.like('key', 'og', 'after')
|
|
|
|
.get(base.testCallback.bind(null, test));
|
2014-10-27 10:35:16 -04:00
|
|
|
},
|
2014-10-24 14:26:31 -04:00
|
|
|
'Basic Join': function(test) {
|
|
|
|
base.qb.from('create_test ct')
|
2014-10-27 10:35:16 -04:00
|
|
|
.join('create_join cj', 'cj.id=ct.id')
|
2014-10-24 14:26:31 -04:00
|
|
|
.get(base.testCallback.bind(null, test));
|
2014-10-27 10:35:16 -04:00
|
|
|
},
|
|
|
|
'Left Join': function(test) {
|
|
|
|
base.qb.from('create_test ct')
|
|
|
|
.join('create_join cj', 'cj.id=ct.id', 'left')
|
|
|
|
.get(base.testCallback.bind(null, test));
|
|
|
|
},
|
|
|
|
'InnerJoin': function(test) {
|
|
|
|
base.qb.from('create_test ct')
|
|
|
|
.join('create_join cj', 'cj.id=ct.id', 'inner')
|
|
|
|
.get(base.testCallback.bind(null, test));
|
|
|
|
}
|
2014-10-23 10:53:16 -04:00
|
|
|
},
|
|
|
|
'DB update tests' : {
|
2014-10-27 13:36:10 -04:00
|
|
|
'Test Insert': function(test) {
|
2014-10-27 10:35:16 -04:00
|
|
|
base.qb.set('id', 98)
|
|
|
|
.set('key', 84)
|
|
|
|
.set('val', 120)
|
|
|
|
.insert('create_test', base.testCallback.bind(null, test));
|
|
|
|
},
|
|
|
|
'Test Insert Object': function(test) {
|
|
|
|
base.qb.insert('create_test', {
|
|
|
|
id: 587,
|
|
|
|
key: 1,
|
|
|
|
val: 2
|
|
|
|
}, base.testCallback.bind(null, test));
|
2014-10-27 13:36:10 -04:00
|
|
|
},
|
|
|
|
'Test Update': function(test) {
|
|
|
|
base.qb.where('id', 7)
|
|
|
|
.update('create_test', {
|
|
|
|
id: 7,
|
|
|
|
key: 'gogle',
|
|
|
|
val: 'non-word'
|
|
|
|
}, base.testCallback.bind(null, test));
|
|
|
|
},
|
|
|
|
'Test set Array Update': function(test) {
|
|
|
|
var object = {
|
|
|
|
id: 22,
|
|
|
|
key: 'gogle',
|
|
|
|
val: 'non-word'
|
|
|
|
};
|
2014-10-23 10:53:16 -04:00
|
|
|
|
2014-10-27 13:36:10 -04:00
|
|
|
base.qb.set(object)
|
|
|
|
.where('id', 22)
|
|
|
|
.update('create_test', base.testCallback.bind(null, test));
|
|
|
|
},
|
|
|
|
'Test where set update': function(test) {
|
|
|
|
base.qb.where('id', 36)
|
|
|
|
.set('id', 36)
|
|
|
|
.set('key', 'gogle')
|
|
|
|
.set('val', 'non-word')
|
|
|
|
.update('create_test', base.testCallback.bind(null, test));
|
|
|
|
},
|
|
|
|
'Test delete': function(test) {
|
|
|
|
base.qb.delete('create_test', {id: 5}, base.testCallback.bind(null, test));
|
|
|
|
}
|
2014-10-23 10:53:16 -04:00
|
|
|
},
|
2014-10-27 13:36:10 -04:00
|
|
|
'Compiled query tests' : {
|
2014-10-23 10:53:16 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return base;
|
|
|
|
}());
|