Skip to content

Commit ef281a3

Browse files
add tests for transactional interfaces
1 parent df2635f commit ef281a3

File tree

3 files changed

+355
-0
lines changed

3 files changed

+355
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
var Pack = require('../../');
2+
3+
describe('Transactional ::', function() {
4+
describe.skip('Begin Transaction', function() {
5+
var manager;
6+
var connection;
7+
8+
// Create a manager and connection
9+
before(function(done) {
10+
// Needed to dynamically get the host using the docker container
11+
var host = process.env.MYSQL_PORT_3306_TCP_ADDR || 'localhost';
12+
13+
Pack.createManager({
14+
connectionString: 'mysql://mp:mp@' + host + ':3306/mppg'
15+
})
16+
.exec(function(err, report) {
17+
if (err) {
18+
return done(err);
19+
}
20+
21+
// Store the manager
22+
manager = report.manager;
23+
24+
Pack.getConnection({
25+
manager: manager
26+
})
27+
.exec(function(err, report) {
28+
if (err) {
29+
return done(err);
30+
}
31+
32+
// Store the connection
33+
connection = report.connection;
34+
35+
return done();
36+
});
37+
});
38+
});
39+
40+
// Afterwards close the transaction and release the connection
41+
after(function(done) {
42+
Pack.sendNativeQuery({
43+
connection: connection,
44+
nativeQuery: 'ROLLBACK;'
45+
})
46+
.exec(function(err) {
47+
if (err) {
48+
return done(err);
49+
}
50+
51+
Pack.releaseConnection({
52+
connection: connection
53+
}).exec(done);
54+
});
55+
});
56+
57+
// TODO: Find a way to get a transaction id in mysql??
58+
});
59+
});
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
var assert = require('assert');
2+
var Pack = require('../../');
3+
4+
describe('Transactional ::', function() {
5+
describe('Commit Transaction', function() {
6+
var manager;
7+
var connectionA;
8+
var connectionB;
9+
10+
// Create a manager, two connections, and a table
11+
before(function(done) {
12+
// Needed to dynamically get the host using the docker container
13+
var host = process.env.MYSQL_PORT_3306_TCP_ADDR || 'localhost';
14+
15+
Pack.createManager({
16+
connectionString: 'mysql://mp:mp@' + host + ':3306/mppg'
17+
})
18+
.exec(function(err, report) {
19+
if (err) {
20+
return done(err);
21+
}
22+
23+
// Store the manager
24+
manager = report.manager;
25+
26+
Pack.getConnection({
27+
manager: manager
28+
})
29+
.exec(function(err, report) {
30+
if (err) {
31+
return done(err);
32+
}
33+
34+
// Store the connection
35+
connectionA = report.connection;
36+
37+
Pack.getConnection({
38+
manager: manager
39+
})
40+
.exec(function(err, report) {
41+
if (err) {
42+
return done(err);
43+
}
44+
45+
// Store the connection
46+
connectionB = report.connection;
47+
48+
// Create a table to use for testing
49+
Pack.sendNativeQuery({
50+
connection: connectionA,
51+
nativeQuery: 'CREATE TABLE IF NOT EXISTS people(id serial primary key, name varchar(255));'
52+
})
53+
.exec(function(err) {
54+
if (err) {
55+
return done(err);
56+
}
57+
58+
return done();
59+
});
60+
});
61+
});
62+
});
63+
});
64+
65+
// Afterwards destroy the table and release the connections
66+
after(function(done) {
67+
Pack.sendNativeQuery({
68+
connection: connectionA,
69+
nativeQuery: 'DROP TABLE people;'
70+
})
71+
.exec(function(err) {
72+
if (err) {
73+
return done(err);
74+
}
75+
76+
Pack.releaseConnection({
77+
connection: connectionA
78+
})
79+
.exec(function(err) {
80+
if (err) {
81+
return done(err);
82+
}
83+
84+
Pack.releaseConnection({
85+
connection: connectionB
86+
}).exec(done);
87+
});
88+
});
89+
});
90+
91+
// To Test:
92+
// * Open a transaction on connectionA and insert a record into the DB
93+
// * Run a query on connectionB and make sure the record doesn't exist
94+
// * Commit the transaction
95+
// * Run the select query again and the record should exist
96+
it('should perform a transaction and make sure the results are commited correctly', function(done) {
97+
// Start a transaction on connection A
98+
Pack.beginTransaction({
99+
connection: connectionA
100+
})
101+
.exec(function(err) {
102+
if (err) {
103+
return done(err);
104+
}
105+
106+
// Insert a record using the transaction
107+
Pack.sendNativeQuery({
108+
connection: connectionA,
109+
nativeQuery: 'INSERT INTO people (name) VALUES (\'hugo\');'
110+
})
111+
.exec(function(err) {
112+
if (err) {
113+
return done(err);
114+
}
115+
116+
// Query the table using connection B and ensure the record doesn't exist
117+
Pack.sendNativeQuery({
118+
connection: connectionB,
119+
nativeQuery: 'SELECT * FROM people;'
120+
})
121+
.exec(function(err, report) {
122+
if (err) {
123+
return done(err);
124+
}
125+
126+
// Ensure no results were returned
127+
assert.equal(report.result.rows.length, 0);
128+
129+
// Commit the transaction
130+
Pack.commitTransaction({
131+
connection: connectionA
132+
})
133+
.exec(function(err) {
134+
if (err) {
135+
return done(err);
136+
}
137+
138+
// Query the table using connection B and ensure the record does exist
139+
Pack.sendNativeQuery({
140+
connection: connectionB,
141+
nativeQuery: 'SELECT * FROM people;'
142+
})
143+
.exec(function(err, report) {
144+
if (err) {
145+
return done(err);
146+
}
147+
148+
// Ensure 1 result was returned
149+
assert.equal(report.result.rows.length, 1);
150+
151+
return done();
152+
});
153+
});
154+
});
155+
});
156+
});
157+
});
158+
});
159+
});
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
var assert = require('assert');
2+
var Pack = require('../../');
3+
4+
describe('Transactional ::', function() {
5+
describe('Rollback Transaction', function() {
6+
var manager;
7+
var connection;
8+
9+
// Create a manager, a connection, and a table
10+
before(function(done) {
11+
// Needed to dynamically get the host using the docker container
12+
var host = process.env.MYSQL_PORT_3306_TCP_ADDR || 'localhost';
13+
14+
Pack.createManager({
15+
connectionString: 'mysql://mp:mp@' + host + ':3306/mppg'
16+
})
17+
.exec(function(err, report) {
18+
if (err) {
19+
return done(err);
20+
}
21+
22+
// Store the manager
23+
manager = report.manager;
24+
25+
Pack.getConnection({
26+
manager: manager
27+
})
28+
.exec(function(err, report) {
29+
if (err) {
30+
return done(err);
31+
}
32+
33+
// Store the connection
34+
connection = report.connection;
35+
36+
// Create a table to use for testing
37+
Pack.sendNativeQuery({
38+
connection: connection,
39+
nativeQuery: 'CREATE TABLE IF NOT EXISTS people(id serial primary key, name varchar(255));'
40+
})
41+
.exec(function(err) {
42+
if (err) {
43+
return done(err);
44+
}
45+
46+
return done();
47+
});
48+
});
49+
});
50+
});
51+
52+
// Afterwards destroy the table and release the connection
53+
after(function(done) {
54+
Pack.sendNativeQuery({
55+
connection: connection,
56+
nativeQuery: 'DROP TABLE people;'
57+
})
58+
.exec(function(err) {
59+
if (err) {
60+
return done(err);
61+
}
62+
63+
Pack.releaseConnection({
64+
connection: connection
65+
}).exec(done);
66+
});
67+
});
68+
69+
// To Test:
70+
// * Open a transaction on connection and insert a record into the DB
71+
// * Run a query on connection and make sure the record exist
72+
// * Rollback the transaction
73+
// * Run the select query again and the record should not exist
74+
it('should perform a transaction and make sure the results are rolled back correctly', function(done) {
75+
// Start a transaction
76+
Pack.beginTransaction({
77+
connection: connection
78+
})
79+
.exec(function(err) {
80+
if (err) {
81+
return done(err);
82+
}
83+
84+
// Insert a record using the transaction
85+
Pack.sendNativeQuery({
86+
connection: connection,
87+
nativeQuery: 'INSERT INTO people (name) VALUES (\'hugo\');'
88+
})
89+
.exec(function(err) {
90+
if (err) {
91+
return done(err);
92+
}
93+
94+
// Query the table and ensure the record does exist
95+
Pack.sendNativeQuery({
96+
connection: connection,
97+
nativeQuery: 'SELECT * FROM people;'
98+
})
99+
.exec(function(err, report) {
100+
if (err) {
101+
return done(err);
102+
}
103+
104+
// Ensure 1 result were returned
105+
assert.equal(report.result.rows.length, 1);
106+
107+
// Rollback the transaction
108+
Pack.rollbackTransaction({
109+
connection: connection
110+
})
111+
.exec(function(err) {
112+
if (err) {
113+
return done(err);
114+
}
115+
116+
// Query the table using and ensure the record doesn't exist
117+
Pack.sendNativeQuery({
118+
connection: connection,
119+
nativeQuery: 'SELECT * FROM people;'
120+
})
121+
.exec(function(err, report) {
122+
if (err) {
123+
return done(err);
124+
}
125+
126+
// Ensure no results were returned
127+
assert.equal(report.result.rows.length, 0);
128+
129+
return done();
130+
});
131+
});
132+
});
133+
});
134+
});
135+
});
136+
});
137+
});

0 commit comments

Comments
 (0)