sequelize.transaction.test.js
4.18 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
'use strict';
const chai = require('chai'),
expect = chai.expect,
Support = require('./support'),
Transaction = require('../../lib/transaction'),
current = Support.sequelize,
delay = require('delay');
if (current.dialect.supports.transactions) {
describe(Support.getTestDialectTeaser('Sequelize#transaction'), () => {
describe('then', () => {
it('gets triggered once a transaction has been successfully committed', async function() {
let called = false;
const t = await this
.sequelize
.transaction();
await t.commit();
called = 1;
expect(called).to.be.ok;
});
it('gets triggered once a transaction has been successfully rolled back', async function() {
let called = false;
const t = await this
.sequelize
.transaction();
await t.rollback();
called = 1;
expect(called).to.be.ok;
});
if (Support.getTestDialect() !== 'sqlite') {
it('works for long running transactions', async function() {
const sequelize = await Support.prepareTransactionTest(this.sequelize);
this.sequelize = sequelize;
this.User = sequelize.define('User', {
name: Support.Sequelize.STRING
}, { timestamps: false });
await sequelize.sync({ force: true });
const t = await this.sequelize.transaction();
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;
}
await this.sequelize.query(query, { transaction: t });
await this.User.create({ name: 'foo' });
await this.sequelize.query(query, { transaction: t });
await t.commit();
const users = await this.User.findAll();
expect(users.length).to.equal(1);
expect(users[0].name).to.equal('foo');
});
}
});
describe('complex long running example', () => {
it('works with promise syntax', async function() {
const sequelize = await Support.prepareTransactionTest(this.sequelize);
const Test = sequelize.define('Test', {
id: { type: Support.Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
name: { type: Support.Sequelize.STRING }
});
await sequelize.sync({ force: true });
const transaction = await sequelize.transaction();
expect(transaction).to.be.instanceOf(Transaction);
await Test
.create({ name: 'Peter' }, { transaction });
await delay(1000);
await transaction
.commit();
const count = await Test.count();
expect(count).to.equal(1);
});
});
describe('concurrency', () => {
describe('having tables with uniqueness constraints', () => {
beforeEach(async function() {
const sequelize = await Support.prepareTransactionTest(this.sequelize);
this.sequelize = sequelize;
this.Model = sequelize.define('Model', {
name: { type: Support.Sequelize.STRING, unique: true }
}, {
timestamps: false
});
await this.Model.sync({ force: true });
});
it('triggers the error event for the second transactions', async function() {
const t1 = await this.sequelize.transaction();
const t2 = await this.sequelize.transaction();
await this.Model.create({ name: 'omnom' }, { transaction: t1 });
await Promise.all([
(async () => {
try {
return await this.Model.create({ name: 'omnom' }, { transaction: t2 });
} catch (err) {
expect(err).to.be.ok;
return t2.rollback();
}
})(),
delay(100).then(() => {
return t1.commit();
})
]);
});
});
});
});
}