2013-09-12 01:00:44 -04:00
|
|
|
|
|
|
|
module.exports = function(knex) {
|
|
|
|
|
2013-09-12 13:30:47 -04:00
|
|
|
describe('Joins', function() {
|
2013-09-12 01:00:44 -04:00
|
|
|
|
|
|
|
it('uses inner join by default', function() {
|
|
|
|
return knex('accounts')
|
2013-09-12 13:30:47 -04:00
|
|
|
.logMe()
|
2013-09-12 01:00:44 -04:00
|
|
|
.join('test_table_two', 'accounts.id', '=', 'test_table_two.account_id')
|
|
|
|
.select('accounts.*', 'test_table_two.details');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('takes a fifth parameter to specify the join type', function() {
|
|
|
|
return knex('accounts')
|
2013-09-12 13:30:47 -04:00
|
|
|
.logMe()
|
2013-09-12 01:00:44 -04:00
|
|
|
.join('test_table_two', 'accounts.id', '=', 'test_table_two.account_id', 'left')
|
|
|
|
.select('accounts.*', 'test_table_two.details');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('accepts a callback as the second argument for advanced joins', function() {
|
2013-09-12 13:30:47 -04:00
|
|
|
return knex('accounts').logMe().join('test_table_two', function(join) {
|
2013-09-12 01:00:44 -04:00
|
|
|
join.on('accounts.id', '=', 'test_table_two.account_id');
|
|
|
|
join.orOn('accounts.email', '=', 'test_table_two.details');
|
|
|
|
}, 'left')
|
|
|
|
.select();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports join aliases', function() {
|
|
|
|
//Expected output: all pairs of account emails, excluding pairs where the emails are the same.
|
|
|
|
return knex('accounts')
|
2013-09-12 13:30:47 -04:00
|
|
|
.logMe()
|
2013-09-12 01:00:44 -04:00
|
|
|
.join('accounts as a2', 'a2.email', '<>', 'accounts.email')
|
2013-09-12 13:30:47 -04:00
|
|
|
.select(['accounts.email as e1', 'a2.email as e2'])
|
|
|
|
.limit(5);
|
2013-09-12 01:00:44 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('supports join aliases with advanced joins', function() {
|
|
|
|
//Expected output: all pairs of account emails, excluding pairs where the emails are the same.
|
|
|
|
//But also include the case where the emails are the same, for account 2.
|
|
|
|
return knex('accounts')
|
2013-09-12 13:30:47 -04:00
|
|
|
.logMe()
|
2013-09-12 01:00:44 -04:00
|
|
|
.join('accounts as a2', function() {
|
|
|
|
this.on('accounts.email', '<>', 'a2.email').orOn('accounts.id','=',2);
|
|
|
|
})
|
2013-09-12 13:30:47 -04:00
|
|
|
.select(['accounts.email as e1', 'a2.email as e2'])
|
|
|
|
.limit(5);
|
2013-09-12 01:00:44 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|