module.exports = function(knex) { describe('Transactions', function() { it('can run with exec', function(ok) { knex.transaction(function(t) { t.commit(); }).exec(ok); }); it('should be able to commit transactions', function() { var id = null; return knex.transaction(function(t) { knex('accounts') .transacting(t) .returning('id') .insert({ first_name: 'Transacting', last_name: 'User', email:'transaction-test1@example.com', logins: 1, about: 'Lorem ipsum Dolore labore incididunt enim.', created_at: new Date(), updated_at: new Date() }).then(function(resp) { return knex('test_table_two').transacting(t).insert({ account_id: (id = resp[0]), details: '', status: 1 }); }).then(function() { t.commit('Hello world'); }); }).then(function(commitMessage) { expect(commitMessage).to.equal('Hello world'); return knex('accounts').where('id', id).select('first_name'); }).then(function(resp) { expect(resp).to.have.length(1); }); }); it('should be able to rollback transactions', function() { var id = null; var err = new Error('error message'); return knex.transaction(function(t) { knex('accounts') .transacting(t) .returning('id') .insert({ first_name: 'Transacting', last_name: 'User2', email:'transaction-test2@example.com', logins: 1, about: 'Lorem ipsum Dolore labore incididunt enim.', created_at: new Date(), updated_at: new Date() }).then(function(resp) { return knex('test_table_two').transacting(t).insert({ account_id: (id = resp[0]), details: '', status: 1 }); }).then(function() { t.rollback(err); }); }).catch(function(msg) { expect(msg).to.equal(err); return knex('accounts').where('id', id).select('first_name'); }).then(function(resp) { expect(resp).to.be.empty; }); }); it('should be able to commit transactions with a resolved trx query', function() { var id = null; return knex.transaction(function(trx) { return trx('accounts') .returning('id') .insert({ first_name: 'Transacting', last_name: 'User', email:'transaction-test3@example.com', logins: 1, about: 'Lorem ipsum Dolore labore incididunt enim.', created_at: new Date(), updated_at: new Date() }).then(function(resp) { return trx('test_table_two').insert({ account_id: (id = resp[0]), details: '', status: 1 }); }).then(function() { return 'Hello World'; }); }).then(function(commitMessage) { expect(commitMessage).to.equal('Hello World'); return knex('accounts').where('id', id).select('first_name'); }).then(function(resp) { expect(resp).to.have.length(1); }); }); it('should be able to rollback transactions with rejected trx query', function() { var id = null; var err = new Error('error message'); var __cid, count = 0; return knex.transaction(function(trx) { return trx('accounts') .returning('id') .insert({ first_name: 'Transacting', last_name: 'User2', email:'transaction-test4@example.com', logins: 1, about: 'Lorem ipsum Dolore labore incididunt enim.', created_at: new Date(), updated_at: new Date() }).then(function(resp) { return trx.insert({ account_id: (id = resp[0]), details: '', status: 1 }).into('test_table_two'); }).then(function() { throw err; }); }) .on('query', function(obj) { count++; if (!__cid) __cid = obj.__cid; expect(__cid).to.equal(obj.__cid); }) .catch(function(msg) { expect(count).to.equal(4); expect(msg).to.equal(err); return knex('accounts').where('id', id).select('first_name'); }).then(function(resp) { expect(resp).to.be.empty; }); }); }); };