sequelize.transaction.test.js
5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require('./support'),
Promise = require('../../lib/promise'),
Transaction = require('../../lib/transaction'),
sinon = require('sinon'),
current = Support.sequelize;
if (current.dialect.supports.transactions) {
describe(Support.getTestDialectTeaser('Sequelize#transaction'), () => {
beforeEach(function() {
this.sinon = sinon.createSandbox();
});
afterEach(function() {
this.sinon.restore();
});
describe('then', () => {
it('gets triggered once a transaction has been successfully committed', function() {
let called = false;
return this
.sequelize
.transaction().then(t => {
return t.commit().then(() => {
called = 1;
});
})
.then(() => {
expect(called).to.be.ok;
});
});
it('gets triggered once a transaction has been successfully rolled back', function() {
let called = false;
return this
.sequelize
.transaction().then(t => {
return t.rollback().then(() => {
called = 1;
});
})
.then(() => {
expect(called).to.be.ok;
});
});
if (Support.getTestDialect() !== 'sqlite') {
it('works for long running transactions', function() {
return Support.prepareTransactionTest(this.sequelize).bind(this).then(function(sequelize) {
this.sequelize = sequelize;
this.User = sequelize.define('User', {
name: Support.Sequelize.STRING
}, { timestamps: false });
return sequelize.sync({ force: true });
}).then(function() {
return this.sequelize.transaction();
}).then(function(t) {
let query = 'select sleep(2);';
switch (Support.getTestDialect()) {
case 'postgres':
query = 'select pg_sleep(2);';
break;
case 'sqlite':
query = 'select sqlite3_sleep(2000);';
break;
case 'mssql':
query = 'WAITFOR DELAY \'00:00:02\';';
break;
default:
break;
}
return this.sequelize.query(query, { transaction: t }).bind(this).then(function() {
return this.User.create({ name: 'foo' });
}).then(function() {
return this.sequelize.query(query, { transaction: t });
}).then(() => {
return t.commit();
});
}).then(function() {
return this.User.all();
}).then(users => {
expect(users.length).to.equal(1);
expect(users[0].name).to.equal('foo');
});
});
}
});
describe('complex long running example', () => {
it('works with promise syntax', function() {
return Support.prepareTransactionTest(this.sequelize).then(sequelize => {
const Test = sequelize.define('Test', {
id: { type: Support.Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
name: { type: Support.Sequelize.STRING }
});
return sequelize.sync({ force: true }).then(() => {
return sequelize.transaction().then(transaction => {
expect(transaction).to.be.instanceOf(Transaction);
return Test
.create({ name: 'Peter' }, { transaction })
.then(() => {
return Promise.delay(1000).then(() => {
return transaction
.commit()
.then(() => { return Test.count(); })
.then(count => {
expect(count).to.equal(1);
});
});
});
});
});
});
});
});
describe('concurrency', () => {
describe('having tables with uniqueness constraints', () => {
beforeEach(function() {
const self = this;
return Support.prepareTransactionTest(this.sequelize).then(sequelize => {
self.sequelize = sequelize;
self.Model = sequelize.define('Model', {
name: { type: Support.Sequelize.STRING, unique: true }
}, {
timestamps: false
});
return self.Model.sync({ force: true });
});
});
it('triggers the error event for the second transactions', function() {
const self = this;
return this.sequelize.transaction().then(t1 => {
return self.sequelize.transaction().then(t2 => {
return self.Model.create({ name: 'omnom' }, { transaction: t1 }).then(() => {
return Promise.all([
self.Model.create({ name: 'omnom' }, { transaction: t2 }).catch(err => {
expect(err).to.be.ok;
return t2.rollback();
}),
Promise.delay(100).then(() => {
return t1.commit();
})
]);
});
});
});
});
});
});
});
}