forked from loopbackio/loopback-connector-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.test.js
180 lines (155 loc) · 5.26 KB
/
transaction.test.js
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
176
177
178
179
var Transaction = require('loopback-datasource-juggler').Transaction;
require('./init.js');
require('should');
var db, Post, Review;
describe('transactions', function() {
before(function(done) {
db = getDataSource({collation: 'utf8_general_ci', createDatabase: true});
db.once('connected', function() {
Post = db.define('PostTX', {
title: {type: String, length: 255, index: true},
content: {type: String}
}, {mysql: {engine: 'INNODB'}});
Review = db.define('ReviewTX', {
author: String,
content: {type: String}
}, {mysql: {engine: 'INNODB'}});
Post.hasMany(Review, {as: 'reviews', foreignKey: 'postId'});
db.automigrate(['PostTX', 'ReviewTX'], done);
});
});
var currentTx;
var hooks = [];
// Return an async function to start a transaction and create a post
function createPostInTx(post, timeout) {
return function(done) {
// Transaction.begin(db.connector, Transaction.READ_COMMITTED,
Post.beginTransaction({
isolationLevel: Transaction.READ_COMMITTED,
timeout: timeout
},
function(err, tx) {
if (err) return done(err);
(typeof tx.id).should.be.eql('string');
hooks = [];
tx.observe('before commit', function(context, next) {
hooks.push('before commit');
next();
});
tx.observe('after commit', function(context, next) {
hooks.push('after commit');
next();
});
tx.observe('before rollback', function(context, next) {
hooks.push('before rollback');
next();
});
tx.observe('after rollback', function(context, next) {
hooks.push('after rollback');
next();
});
currentTx = tx;
Post.create(post, {transaction: tx},
function(err, p) {
if (err) {
done(err);
} else {
p.reviews.create({
author: 'John',
content: 'Review for ' + p.title
}, {transaction: tx},
function(err, c) {
done(err);
});
}
});
});
};
}
// Return an async function to find matching posts and assert number of
// records to equal to the count
function expectToFindPosts(where, count, inTx) {
return function(done) {
var options = {};
if (inTx) {
options.transaction = currentTx;
}
Post.find({where: where}, options,
function(err, posts) {
if (err) return done(err);
posts.length.should.be.eql(count);
if (count) {
// Find related reviews
// Please note the empty {} is required, otherwise, the options
// will be treated as a filter
posts[0].reviews({}, options, function(err, reviews) {
if (err) return done(err);
reviews.length.should.be.eql(count);
done();
});
} else {
done();
}
});
};
}
describe('commit', function() {
var post = {title: 't1', content: 'c1'};
before(createPostInTx(post));
it('should not see the uncommitted insert', expectToFindPosts(post, 0));
it('should see the uncommitted insert from the same transaction',
expectToFindPosts(post, 1, true));
it('should commit a transaction', function(done) {
currentTx.commit(function(err) {
hooks.should.be.eql(['before commit', 'after commit']);
done(err);
});
});
it('should see the committed insert', expectToFindPosts(post, 1));
it('should report error if the transaction is not active', function(done) {
currentTx.commit(function(err) {
(err).should.be.instanceof(Error);
done();
});
});
});
describe('rollback', function() {
var post = {title: 't2', content: 'c2'};
before(createPostInTx(post));
it('should not see the uncommitted insert', expectToFindPosts(post, 0));
it('should see the uncommitted insert from the same transaction',
expectToFindPosts(post, 1, true));
it('should rollback a transaction', function(done) {
currentTx.rollback(function(err) {
hooks.should.be.eql(['before rollback', 'after rollback']);
done(err);
});
});
it('should not see the rolledback insert', expectToFindPosts(post, 0));
it('should report error if the transaction is not active', function(done) {
currentTx.rollback(function(err) {
(err).should.be.instanceof(Error);
done();
});
});
});
describe('timeout', function() {
var post = {title: 't3', content: 'c3'};
before(createPostInTx(post, 500));
it('should invoke the timeout hook', function(done) {
currentTx.observe('timeout', function(context, next) {
next();
// It will only proceed upon timeout
done();
});
});
it('should rollback the transaction if timeout', function(done) {
Post.find({where: {title: 't3'}}, {transaction: currentTx},
function(err, posts) {
if (err) return done(err);
posts.length.should.be.eql(0);
done();
});
});
});
});