sequelize.transaction.test.js
5.4 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
169
170
171
172
173
174
175
'use strict';
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/support')
, Promise = require(__dirname + '/../../lib/promise')
, Transaction = require(__dirname + '/../../lib/transaction')
, current = Support.sequelize;
if (current.dialect.supports.transactions) {
describe(Support.getTestDialectTeaser('Sequelize#transaction'), function() {
this.timeout(4000);
describe('success', function() {
it('gets triggered once a transaction has been successfully committed', function(done) {
this
.sequelize
.transaction().then(function(t) { t.commit(); })
.success(function() { done(); });
});
it('gets triggered once a transaction has been successfully rolled back', function(done) {
this
.sequelize
.transaction().then(function(t) { t.rollback(); })
.success(function() { done(); });
});
if (Support.getTestDialect() !== 'sqlite') {
it('works for long running transactions', function() {
this.timeout(10000);
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) {
var query = 'select sleep(2);';
switch (Support.getTestDialect()) {
case 'postgres':
query = 'select pg_sleep(2);';
break;
case 'sqlite':
query = 'select sqlite3_sleep(2000);';
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(function() {
return t.commit();
});
}).then(function() {
return this.User.all();
}).then(function(users) {
expect(users.length).to.equal(1);
expect(users[0].name).to.equal('foo');
});
});
}
});
describe('error', function() {
if (Support.getTestDialect() === 'sqlite') {
// not sure if we can test this in sqlite ...
// how could we enforce an authentication error in sqlite?
} else {
it('gets triggered once an error occurs', function(done) {
// Supply bad config to force an error
var sequelize = Support.getSequelizeInstance('this', 'is', 'fake config');
sequelize
.transaction().then(function() {})
.catch (function(err) {
expect(err).to.not.be.undefined;
done();
});
});
}
});
describe('complex long running example', function() {
it('works with promise syntax', function(done) {
Support.prepareTransactionTest(this.sequelize, function(sequelize) {
var Test = sequelize.define('Test', {
id: { type: Support.Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
name: { type: Support.Sequelize.STRING }
});
sequelize
.sync({ force: true })
.then(function() {
sequelize.transaction().then(function(transaction) {
expect(transaction).to.be.instanceOf(Transaction);
Test
.create({ name: 'Peter' }, { transaction: transaction })
.then(function() {
setTimeout(function() {
transaction
.commit()
.then(function() { return Test.count(); })
.then(function(count) {
expect(count).to.equal(1);
done();
});
}, 1000);
});
});
});
});
});
});
describe('concurrency', function() {
describe('having tables with uniqueness constraints', function() {
beforeEach(function(done) {
var self = this;
Support.prepareTransactionTest(this.sequelize, function(sequelize) {
self.sequelize = sequelize;
self.Model = sequelize.define('Model', {
name: { type: Support.Sequelize.STRING, unique: true }
}, {
timestamps: false
});
self.Model
.sync({ force: true })
.success(function() { done(); });
});
});
it('triggers the error event for the second transactions', function(done) {
var self = this;
this.sequelize.transaction().then(function(t1) {
self.sequelize.transaction().then(function(t2) {
self
.Model
.create({ name: 'omnom' }, { transaction: t1 })
.success(function(m1) {
self
.Model
.create({ name: 'omnom' }, { transaction: t2 })
.error(function(err) {
t2.rollback().success(function() {
expect(err).to.be.defined;
done();
});
});
setTimeout(function() { t1.commit(); }, 100);
});
});
});
});
});
});
});
}