112 lines
3.1 KiB
JavaScript
Raw Normal View History

module.exports = function(knex) {
2013-09-12 13:30:47 -04:00
describe('Updates', function () {
2013-09-13 10:24:39 -04:00
it('should handle updates', function() {
return knex('accounts')
.where('id', 1)
.update({
first_name: 'User',
last_name: 'Test',
email:'test100@example.com'
2013-12-27 14:44:21 -05:00
}).testSql(function(tester) {
tester(
'mysql',
'update `accounts` set `email` = ?, `first_name` = ?, `last_name` = ? where `id` = ?',
['test100@example.com','User','Test',1],
1
);
tester(
'postgresql',
'update "accounts" set "email" = ?, "first_name" = ?, "last_name" = ? where "id" = ?',
['test100@example.com','User','Test',1],
1
);
tester(
'sqlite3',
'update "accounts" set "email" = ?, "first_name" = ?, "last_name" = ? where "id" = ?',
['test100@example.com','User','Test',1],
1
);
});
});
2013-11-20 09:17:08 -05:00
it('should increment a value', function() {
return knex('accounts').select('logins').where('id', 1).tap(function() {
return knex('accounts').where('id', 1).increment('logins');
}).then(function(attrs1) {
return knex('accounts').select('logins').where('id', 1).then(function(attrs2) {
expect(attrs1[0].logins).to.equal(attrs2[0].logins - 1);
});
});
});
it('should decrement a value', function() {
return knex('accounts').select('logins').where('id', 1).tap(function() {
return knex('accounts').where('id', 1).decrement('logins');
}).then(function(attrs1) {
return knex('accounts').select('logins').where('id', 1).then(function(attrs2) {
expect(attrs1[0].logins).to.equal(attrs2[0].logins + 1);
});
});
});
it('should allow returning for updates in postgresql', function() {
2013-12-27 14:44:21 -05:00
return knex('accounts').where('id', 1).update({
first_name: 'UpdatedUser',
last_name: 'UpdatedTest',
email:'test100@example.com'
2013-12-27 14:44:21 -05:00
}, '*').testSql(function(tester) {
tester(
'mysql',
'update `accounts` set `email` = ?, `first_name` = ?, `last_name` = ? where `id` = ?',
['test100@example.com','UpdatedUser','UpdatedTest',1],
1
);
tester(
'postgresql',
'update "accounts" set "email" = ?, "first_name" = ?, "last_name" = ? where "id" = ? returning *',
['test100@example.com','UpdatedUser','UpdatedTest',1],
[{
id: '1',
first_name: 'UpdatedUser',
last_name: 'UpdatedTest',
email: 'test100@example.com',
logins: 1,
about: 'Lorem ipsum Dolore labore incididunt enim.',
created_at: d,
updated_at: d,
phone: null
}]
);
tester(
'sqlite3',
'update "accounts" set "email" = ?, "first_name" = ?, "last_name" = ? where "id" = ?',
['test100@example.com','UpdatedUser','UpdatedTest',1],
1
);
});
});
});
};