knex/test/integration/builder/transaction.js

249 lines
7.7 KiB
JavaScript
Raw Normal View History

/*global describe, expect, it, testPromise*/
'use strict';
2014-06-06 17:27:59 -04:00
var Promise = testPromise;
module.exports = function(knex) {
2013-09-12 13:30:47 -04:00
describe('Transactions', function() {
2014-03-19 11:54:18 -04:00
it('can run with exec', function(ok) {
knex.transaction(function(t) {
t.commit();
}).exec(ok);
});
2014-04-16 01:23:50 -04:00
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',
2014-05-08 18:04:55 -04:00
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);
});
});
2014-04-16 01:23:50 -04:00
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);
});
2014-05-08 18:04:55 -04:00
}).catch(function(msg) {
expect(msg).to.equal(err);
return knex('accounts').where('id', id).select('first_name');
}).then(function(resp) {
2014-09-02 22:03:14 +02:00
expect(resp.length).to.equal(0);
2014-05-08 18:04:55 -04:00
});
});
it('should be able to commit transactions with a resolved trx query', function() {
2014-05-08 18:04:55 -04:00
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);
});
2014-05-08 18:04:55 -04:00
});
2014-05-08 18:04:55 -04:00
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;
2014-05-08 18:04:55 -04:00
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()
2014-06-06 17:27:59 -04:00
})
.then(function(resp) {
2014-05-08 18:04:55 -04:00
return trx.insert({
account_id: (id = resp[0]),
details: '',
status: 1
}).into('test_table_two');
2014-06-06 17:27:59 -04:00
})
.then(function() {
2014-05-08 18:04:55 -04:00
throw err;
});
})
.on('query', function(obj) {
count++;
if (!__cid) __cid = obj.__cid;
expect(__cid).to.equal(obj.__cid);
})
.catch(function(msg) {
2014-08-11 12:25:39 +02:00
if (knex.client.dialect === 'oracle') {
// oracle start transaction /rollback are no queries
expect(count).to.equal(2);
} else {
expect(count).to.equal(4);
}
2014-05-08 18:04:55 -04:00
expect(msg).to.equal(err);
return knex('accounts').where('id', id).select('first_name');
2014-06-06 17:27:59 -04:00
})
.then(function(resp) {
expect(resp).to.eql([]);
2014-05-08 18:04:55 -04:00
});
});
2014-06-06 17:27:59 -04:00
it('should be able to run schema methods', function() {
var __cid, count = 0;
var err = new Error('error message');
if (knex.client.dialect === 'postgresql') {
return knex.transaction(function(trx) {
2014-06-06 17:27:59 -04:00
return trx.schema.createTable('test_schema_transactions', function(table) {
table.increments();
table.string('name');
table.timestamps();
}).then(function() {
2014-06-06 17:27:59 -04:00
return trx('test_schema_transactions').insert({name: 'bob'});
}).then(function() {
2014-06-06 17:27:59 -04:00
return trx('test_schema_transactions').count('*');
}).then(function(resp) {
2014-06-06 17:27:59 -04:00
var _count = parseInt(resp[0].count, 10);
expect(_count).to.equal(1);
throw err;
});
})
.on('query', function(obj) {
count++;
if (!__cid) __cid = obj.__cid;
expect(__cid).to.equal(obj.__cid);
})
.catch(function(msg) {
expect(msg).to.equal(err);
expect(count).to.equal(5);
2014-06-06 17:27:59 -04:00
return knex('test_schema_migrations').count('*');
})
.catch(function(e) {
expect(e.message).to.equal('relation "test_schema_migrations" does not exist');
});
2014-06-06 17:27:59 -04:00
} else {
var id = null;
return knex.transaction(function(trx) {
return trx('accounts')
.returning('id')
.insert({
first_name: 'Transacting',
last_name: 'User3',
email:'transaction-test5@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() {
2014-06-06 17:27:59 -04:00
return trx.schema.createTable('test_schema_transactions', function(table) {
table.increments();
table.string('name');
table.timestamps();
});
});
})
.on('query', function(obj) {
count++;
if (!__cid) __cid = obj.__cid;
expect(__cid).to.equal(obj.__cid);
}).then(function() {
expect(count).to.equal(5);
return knex('accounts').where('id', id).select('first_name');
}).then(function(resp) {
expect(resp).to.have.length(1);
}).finally(function() {
return knex.schema.dropTableIfExists('test_schema_transactions');
});
2014-06-06 17:27:59 -04:00
}
});
it('should resolve with the correct value, #298', function() {
return knex.transaction(function(trx) {
trx.debugging = true;
return Promise.resolve(null);
}).then(function(result) {
expect(result).to.equal(null);
});
2014-06-06 17:27:59 -04:00
});
});
};