transaction.test.js
2.06 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
'use strict';
const chai = require('chai');
const expect = chai.expect;
const sinon = require('sinon');
const Support = require('./support');
const Sequelize = Support.Sequelize;
const dialect = Support.getTestDialect();
const current = Support.sequelize;
describe('Transaction', () => {
before(function() {
this.stub = sinon.stub(current, 'query').resolves({});
this.stubConnection = sinon.stub(current.connectionManager, 'getConnection')
.resolves({
uuid: 'ssfdjd-434fd-43dfg23-2d',
close() {}
});
this.stubRelease = sinon.stub(current.connectionManager, 'releaseConnection')
.resolves();
});
beforeEach(function() {
this.stub.resetHistory();
this.stubConnection.resetHistory();
this.stubRelease.resetHistory();
});
after(function() {
this.stub.restore();
this.stubConnection.restore();
});
it('should run auto commit query only when needed', function() {
const expectations = {
all: [
'START TRANSACTION;'
],
sqlite: [
'BEGIN DEFERRED TRANSACTION;'
],
mssql: [
'BEGIN TRANSACTION;'
]
};
return current.transaction(() => {
expect(this.stub.args.map(arg => arg[0])).to.deep.equal(expectations[dialect] || expectations.all);
return Sequelize.Promise.resolve();
});
});
it('should set isolation level correctly', function() {
const expectations = {
all: [
'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;',
'START TRANSACTION;'
],
postgres: [
'START TRANSACTION;',
'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;'
],
sqlite: [
'BEGIN DEFERRED TRANSACTION;',
'PRAGMA read_uncommitted = ON;'
],
mssql: [
'BEGIN TRANSACTION;'
]
};
return current.transaction({ isolationLevel: Sequelize.Transaction.ISOLATION_LEVELS.READ_UNCOMMITTED }, () => {
expect(this.stub.args.map(arg => arg[0])).to.deep.equal(expectations[dialect] || expectations.all);
return Sequelize.Promise.resolve();
});
});
});