2013-05-08 20:16:39 -04:00
|
|
|
var When = require('when');
|
2013-05-09 03:47:03 -04:00
|
|
|
module.exports = function(Knex, dbName, resolver) {
|
2013-05-02 00:21:49 -04:00
|
|
|
|
2013-05-04 18:19:33 -04:00
|
|
|
describe(dbName, function() {
|
2013-05-02 00:21:49 -04:00
|
|
|
|
|
|
|
it('runs with no conditions', function(ok) {
|
2013-05-09 03:47:03 -04:00
|
|
|
Knex('accounts').select().then(resolver(ok), ok);
|
2013-05-02 00:21:49 -04:00
|
|
|
});
|
2013-05-04 20:36:02 -04:00
|
|
|
|
|
|
|
it('uses `orderBy`', function(ok) {
|
|
|
|
Knex('accounts')
|
|
|
|
.select()
|
2013-05-09 03:47:03 -04:00
|
|
|
.orderBy('id', 'asc').then(resolver(ok), ok);
|
2013-05-04 20:36:02 -04:00
|
|
|
}),
|
2013-05-02 00:21:49 -04:00
|
|
|
|
|
|
|
it('does simple "where" cases', function(ok) {
|
2013-05-08 20:16:39 -04:00
|
|
|
When.all([
|
2013-05-04 02:57:12 -04:00
|
|
|
Knex('accounts').where('id', 1).select('first_name', 'last_name'),
|
|
|
|
Knex('accounts').where('id', '>', 1).select(['email', 'logins']),
|
|
|
|
Knex('accounts').where({'id': 1}).select('*'),
|
|
|
|
Knex('accounts').where({'id': void 0}).select('*'),
|
|
|
|
Knex('accounts').where({'id': null}).select('first_name', 'email'),
|
|
|
|
Knex('accounts').where({'id': 0}).select()
|
2013-05-09 03:47:03 -04:00
|
|
|
]).then(resolver(ok, true), ok);
|
2013-05-02 00:21:49 -04:00
|
|
|
});
|
|
|
|
|
2013-05-04 19:13:07 -04:00
|
|
|
it('has a "distinct" clause', function(ok) {
|
2013-05-08 20:16:39 -04:00
|
|
|
When.all([
|
2013-05-09 03:47:03 -04:00
|
|
|
Knex('accounts').select().distinct('email').where('logins', 2).orderBy('email'),
|
|
|
|
Knex('accounts').distinct('email').select().orderBy('email')
|
|
|
|
]).then(resolver(ok, true), ok);
|
2013-05-04 19:13:07 -04:00
|
|
|
});
|
|
|
|
|
2013-05-02 00:21:49 -04:00
|
|
|
it('does "orWhere" cases', function(ok) {
|
2013-05-08 20:16:39 -04:00
|
|
|
When.all([
|
2013-05-04 02:57:12 -04:00
|
|
|
Knex('accounts').where('id', 1).orWhere('id', '>', 2).select('first_name', 'last_name')
|
2013-05-02 00:21:49 -04:00
|
|
|
// More tests can be added here.
|
2013-05-09 03:47:03 -04:00
|
|
|
]).then(resolver(ok, true), ok);
|
2013-05-02 00:21:49 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('does "andWhere" cases', function(ok) {
|
2013-05-08 20:16:39 -04:00
|
|
|
When.all([
|
2013-05-04 02:57:12 -04:00
|
|
|
Knex('accounts').select('first_name', 'last_name', 'about').where('id', 1).andWhere('email', 'test@example.com')
|
2013-05-09 03:47:03 -04:00
|
|
|
]).then(resolver(ok, true), ok);
|
2013-05-02 00:21:49 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('takes a function to wrap nested where statements', function(ok) {
|
2013-05-08 20:16:39 -04:00
|
|
|
When.all([
|
2013-05-04 02:57:12 -04:00
|
|
|
Knex('accounts').where(function() {
|
|
|
|
this.where('id', 2);
|
|
|
|
this.orWhere('id', 3);
|
2013-05-02 00:21:49 -04:00
|
|
|
}).select('*')
|
2013-05-09 03:47:03 -04:00
|
|
|
]).then(resolver(ok, true), ok);
|
2013-05-02 00:21:49 -04:00
|
|
|
});
|
|
|
|
|
2013-05-02 00:50:54 -04:00
|
|
|
it('handles "where in" cases', function(ok) {
|
2013-05-08 20:16:39 -04:00
|
|
|
When.all([
|
2013-05-04 02:57:12 -04:00
|
|
|
Knex('accounts').whereIn('id', [1, 2, 3]).select()
|
2013-05-09 03:47:03 -04:00
|
|
|
]).then(resolver(ok, true), ok);
|
2013-05-02 00:21:49 -04:00
|
|
|
});
|
|
|
|
|
2013-05-02 00:50:54 -04:00
|
|
|
it('handles "or where in" cases', function(ok) {
|
2013-05-04 02:57:12 -04:00
|
|
|
Knex('accounts')
|
|
|
|
.where('email', 'test@example.com')
|
2013-05-02 00:50:54 -04:00
|
|
|
.orWhereIn('id', [2, 3, 4])
|
|
|
|
.select()
|
2013-05-09 03:47:03 -04:00
|
|
|
.then(resolver(ok), ok);
|
2013-05-02 00:21:49 -04:00
|
|
|
});
|
|
|
|
|
2013-05-02 00:50:54 -04:00
|
|
|
it('handles "where exists"', function(ok) {
|
|
|
|
Knex('accounts')
|
|
|
|
.whereExists(function(qb) {
|
2013-05-04 02:57:12 -04:00
|
|
|
this.select('id').from('test_table_two').where({id: 1});
|
2013-05-02 00:50:54 -04:00
|
|
|
})
|
|
|
|
.select()
|
2013-05-09 03:47:03 -04:00
|
|
|
.then(resolver(ok), ok);
|
2013-05-02 00:21:49 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('handles "where between"', function(ok) {
|
2013-05-04 02:57:12 -04:00
|
|
|
Knex('accounts').whereBetween('id', [1, 100])
|
2013-05-02 00:50:54 -04:00
|
|
|
.select()
|
2013-05-09 03:47:03 -04:00
|
|
|
.then(resolver(ok), ok);
|
2013-05-02 00:21:49 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('handles "or where between"', function(ok) {
|
2013-05-04 02:57:12 -04:00
|
|
|
Knex('accounts')
|
2013-05-02 00:50:54 -04:00
|
|
|
.whereBetween('id', [1, 100])
|
|
|
|
.orWhereBetween('id', [200, 300])
|
|
|
|
.select()
|
2013-05-09 03:47:03 -04:00
|
|
|
.then(resolver(ok), ok);
|
2013-05-02 00:21:49 -04:00
|
|
|
});
|
2013-05-02 00:50:54 -04:00
|
|
|
|
2013-05-13 10:38:28 -04:00
|
|
|
it('does whereRaw', function(ok) {
|
|
|
|
Knex('accounts')
|
|
|
|
.whereExists(function() {
|
|
|
|
this.select(Knex.Raw(1))
|
|
|
|
.from('test_table_two')
|
|
|
|
.whereRaw('test_table_two.account_id = accounts.id');
|
|
|
|
})
|
|
|
|
.select()
|
|
|
|
.then(resolver(ok), ok);
|
|
|
|
});
|
|
|
|
|
2013-05-02 00:50:54 -04:00
|
|
|
});
|
|
|
|
|
2013-05-02 00:21:49 -04:00
|
|
|
};
|