-
Notifications
You must be signed in to change notification settings - Fork 0
/
mockPoolTest.js
162 lines (137 loc) · 5.15 KB
/
mockPoolTest.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
'use strict';
const db = require('../lib/db');
exports.testSimpleSelect = function (test) {
test.expect(1);
const SUT = db.newMockPool()
.expect('connect')
.expect('query', 'SELECT 1 AS a').ok([{a: 1}])
.expect('close');
db.using(SUT.connect(), client => client.query('SELECT 1 AS a'))
.then(res => {
test.deepEqual(res, [{a: 1}]);
SUT.assertStoryDone();
test.done();
});
};
exports.testTransactions = function (test) {
test.expect(2);
const SUT = db.newMockPool()
.expect('connect')
.expect('connect')
.expect('query', /DROP TABLE/)
.expect('query', /CREATE TABLE/)
.expect('query', 'BEGIN')
.expect('query', 'INSERT INTO test SELECT 1')
.expect('query', 'SELECT * FROM test').ok([])
.expect('query', 'COMMIT')
.expect('query', 'SELECT * FROM test').ok([{a: 1}])
.expect('close')
.expect('close');
// Testing transactional behavior by setting up 2 connections that try to read each others data
db.using(SUT.connect(), SUT.connect(), (client1, client2) => {
return prepareTestTable(client1)
.then(client1.begin)
.then(() => client1.query('INSERT INTO test SELECT 1'))
.then(() => client2.query('SELECT * FROM test'))
.then(data => {
test.deepEqual([], data);
})
.then(client1.commit)
.then(() => client2.query('SELECT * FROM test'))
.then(data => {
test.deepEqual([{a: 1}], data);
})
}).then(() => {
SUT.assertStoryDone();
test.done();
})
};
exports.testTransactional = function (test) {
test.expect(3);
const SUT = db.newMockPool()
.expect('connect').ok()
.expect('query', /DROP TABLE/)
.expect('query', /CREATE TABLE/)
.expect('query', `
BEGIN
`)
.expect('query', `INSERT INTO test SELECT '1'`)
.expect('query', `INSERT INTO test SELECT '2', '2', '3'`)
.expect('query', 'COMMIT')
.expect('query', 'SELECT * FROM test').ok([{a: 1}, {a: 2}])
.expect('query', /DROP TABLE/)
.expect('query', /CREATE TABLE/)
.expect('query', 'BEGIN')
.expect('query', 'INSERT INTO test SELECT 1')
.expect('query', 'INSERT INTO this_table_does_not_exist SELECT 2')
.fail('relation "this_table_does_not_exist" does not exist')
.expect('query', 'ROLLBACK')
.expect('query', 'SELECT * FROM test').ok([])
.expect('close');
// First we will test that two statements in a transactional block work, then we will test that when the second
// operation fails, the first one doesn't have effect. Together, that proves atomicity.
db.using(SUT.connect(), client => {
return prepareTestTable(client)
.then(() => client.transactional(() => {
return client.query('INSERT INTO test SELECT %L', 1)
.then(() => client.query("INSERT INTO test SELECT %1$L, %1$L, %2$L", 2, 3));
}))
.then(() => client.query("SELECT * FROM test"))
.then(data => test.deepEqual([{a: 1}, {a: 2}], data))
.then(() => prepareTestTable(client))
.then(() => client.transactional(() => {
return client.query('INSERT INTO test SELECT 1')
.then(() => client.query("INSERT INTO this_table_does_not_exist SELECT 2"));
}))
.catch(err => test.equal('relation "this_table_does_not_exist" does not exist', err.message))
.then(() => client.query("SELECT * FROM test"))
.then(data => test.deepEqual([], data))
}).then(() => {
SUT.assertStoryDone();
test.done()
});
};
exports.testPoolWith = function (test) {
test.expect(1);
const SUT = db.newMockPool()
.expect('connect')
.expect('query', 'SELECT 1 AS a').ok([{a: 1}])
.expect('close');
SUT.with(conn => conn.query('SELECT 1 AS a'))
.then(res => {
test.deepEqual(res, [{a: 1}]);
SUT.assertStoryDone();
test.done();
});
};
exports.testPoolTransactional = function (test) {
const SUT = db.newMockPool()
.expect('connect')
.expect('query', 'BEGIN')
.expect('query', 'SELECT 1 AS a').ok([{a: 1}])
.expect('query', 'COMMIT')
.expect('close');
SUT.transactional(conn => conn.query('SELECT 1 AS a'))
.then(result => {
test.deepEqual(result, [{a: 1}]);
SUT.assertStoryDone();
test.done();
});
};
exports.testPoolQuery = function (test) {
test.expect(1);
const SUT = db.newMockPool()
.expect('connect')
.expect('query', 'SELECT 1 AS a').ok([{a: 1}])
.expect('close');
SUT.query('SELECT 1 AS a')
.then(res => {
test.deepEqual(res, [{a: 1}]);
SUT.assertStoryDone();
test.done();
});
};
function prepareTestTable(client) {
return client.query('DROP TABLE IF EXISTS test')
.then(() => client.query('CREATE TABLE test (a integer)'));
}