-
Notifications
You must be signed in to change notification settings - Fork 5
/
connection-test.js
244 lines (209 loc) · 8.11 KB
/
connection-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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
"use strict"; // eslint-disable-line semi
const pgrm = require('../index.js')
const pgConfig = {dbUrl: "postgres://localhost/pgrm-tests"}
const BPromise = require('bluebird')
const using = BPromise.using
const pgrmWithDefaults = pgrm(pgConfig)
const chai = require('chai')
const chaiAsPromised = require('chai-as-promised')
chai.use(chaiAsPromised)
const assert = chai.assert
const QUERY_CANCELED = '57014' // http://www.postgresql.org/docs/9.4/static/errcodes-appendix.html
describe('connection-test.js', function () {
beforeEach(() =>
using(pgrmWithDefaults.getConnection(), conn =>
conn.queryAsync("drop table if exists foo").then(() =>
conn.queryAsync("create table foo(bar integer unique, id serial)")
)))
describe('configuration', function () {
it('disables parsing of SQL dates to javascript dates', () =>
assert.eventually.deepEqual(
pgrmWithDefaults.queryRowsAsync("select date('2015-03-30') as the_date"),
[{the_date: '2015-03-30'}])
)
})
describe('single queries', function () {
it('queryRowsAsync returns the rows of the result', () =>
pgrmWithDefaults.queryRowsAsync("insert into foo(bar) values ($1)", [1])
.then(() => pgrmWithDefaults.queryRowsAsync("select bar from foo"))
.then(rows => assert.deepEqual(rows, [{ bar: 1 }]))
)
it('queryAsync returns the result object', () =>
pgrmWithDefaults.queryAsync("insert into foo(bar) values ($1)", [1])
.then(() => pgrmWithDefaults.queryAsync("select bar from foo"))
.tap(resultObj => assert.equal(resultObj.rowCount, 1))
.then(resultObj => assert.deepEqual(resultObj.rows, [{ bar: 1 }]))
)
})
describe('connections', function () {
it('return the result object', () =>
using(pgrmWithDefaults.getConnection(), assertResponseObj)
)
it('commit automatically', () =>
using(pgrmWithDefaults.getConnection(), insert1IntoFoo)
.then(assertOneEventuallyInFoo)
)
it('do not rollback errors', () =>
using(pgrmWithDefaults.getConnection(), conn =>
insert1IntoFoo(conn)
.then(throwAnError))
.catch(assertOneEventuallyInFoo)
)
})
describe('timeouts', function () {
describe('can be configured on pgrm level', function () {
const pgrmWithShortTimeout = pgrm(Object.assign({}, pgConfig, {statementTimeout: '1ms'}))
it('for transactions', () =>
using(pgrmWithShortTimeout.getTransaction(), causeAndAssertATimeout)
)
it('for connections', () =>
using(pgrmWithShortTimeout.getConnection(), causeAndAssertATimeout)
)
})
describe('can be configured per session', function () {
it('for transactions', () =>
using(pgrmWithDefaults.getTransaction(), tx =>
tx.queryAsync("SET statement_timeout TO '1ms'")
.then(() => causeAndAssertATimeout(tx))
)
)
it('cause rollback in transaction', () =>
using(pgrmWithDefaults.getTransaction(), tx =>
tx.queryAsync("SET statement_timeout TO '99ms'")
.then(() => insert1IntoFoo(tx))
.then(() => causeAndAssertATimeout(tx))
).then(assertFooIsEventuallyEmpty)
)
it('for connections', () =>
using(pgrmWithDefaults.getConnection(), conn =>
conn.queryAsync("SET statement_timeout TO '1ms'")
.then(() => causeAndAssertATimeout(conn))
)
)
})
})
describe('transactions', function () {
it('return the result object', () =>
using(pgrmWithDefaults.getTransaction(), assertResponseObj)
)
it('are committed if there are no exceptions', () =>
using(pgrmWithDefaults.getTransaction(), insert1IntoFoo)
.then(assertOneEventuallyInFoo)
)
it('are rollbacked in case of exceptions within the using-block', () =>
using(pgrmWithDefaults.getTransaction(), tx =>
insert1IntoFoo(tx).then(throwAnError)
).catch(assertFooIsEventuallyEmpty)
)
it('are rollbacked in case of SQL exceptions', () => {
assert.isRejected(
using(pgrmWithDefaults.getTransaction(), tx =>
insert1IntoFoo(tx)
.then(assertOneEventuallyInFoo)
.then(() =>
tx.queryAsync("this is not sql")
)
),
Error)
return assertFooIsEventuallyEmpty()
})
describe('support locking of tables', function () {
it('and do not lock anything by default and be in read committed isolation level', () =>
using(pgrmWithDefaults.getTransaction(), outerTx =>
using(pgrmWithDefaults.getTransaction(), innerTx =>
insert1IntoFoo(outerTx)
.then(() => innerTx.queryAsync('insert into foo(bar) values(2)'))
.then(() =>
assert.eventually.deepEqual(
BPromise.all([
outerTx.queryRowsAsync("select * from foo"),
innerTx.queryRowsAsync("select * from foo"),
pgrmWithDefaults.queryRowsAsync("select * from foo")]),
[
[{ bar: 1, id: 1 }],
[{ bar: 2, id: 2 }],
[]
])
)
)
).then(() =>
assert.eventually.deepEqual(
pgrmWithDefaults.queryRowsAsync("select bar from foo order by bar"),
[{ bar: 1 }, { bar: 2 }])
)
)
it('and lock the given table', function () {
let selectingTxFn
const selectingTxP = new BPromise(resolve => { selectingTxFn = resolve })
const earlierTxP = using(pgrmWithDefaults.getTransaction(['foo']), earlierTx => {
using(pgrmWithDefaults.getTransaction(['foo']), laterTx => {
selectingTxFn(laterTx.queryAsync("select bar from foo").then(res => res.rows))
})
return BPromise.delay(100)
.then(() => insert1IntoFoo(earlierTx))
.then(() => 'inserted')
})
return assert.eventually.deepEqual(
BPromise.all([earlierTxP, selectingTxP]),
['inserted', [{bar: 1}]])
})
})
})
describe('withConnection', function () {
it('wraps using() and getConnection()', () =>
pgrmWithDefaults.withConnection(assertResponseObj)
)
it('does not rollback errors', () =>
pgrmWithDefaults.withConnection(conn =>
insert1IntoFoo(conn).then(throwAnError)
).catch(assertOneEventuallyInFoo)
)
})
describe('withTransaction', function () {
it('wraps using() and getTransaction()', () =>
pgrmWithDefaults.withTransaction(assertResponseObj)
)
it('rolls back the transaction if the function throws', () =>
pgrmWithDefaults.withTransaction(tx =>
insert1IntoFoo(tx).then(throwAnError)
).catch(assertFooIsEventuallyEmpty)
)
})
function insert1IntoFoo(connOrTx) {
return connOrTx.queryAsync("insert into foo (bar) values (1)")
}
function assertOneEventuallyInFoo() {
return assert.eventually.deepEqual(pgrmWithDefaults.queryRowsAsync("select bar from foo"), [{bar: 1}])
}
function assertFooIsEventuallyEmpty() {
return assert.eventually.deepEqual(pgrmWithDefaults.queryRowsAsync("select bar from foo"), [])
}
function assertErrCodeIsQueryCanceled(err) {
assert.equal(err.code, QUERY_CANCELED)
throw err
}
function causeAndAssertATimeout(txOrConn) {
return assert.isRejected(txOrConn.queryAsync('SELECT pg_sleep(100)')
.catch(assertErrCodeIsQueryCanceled), /canceling statement due to statement timeout/)
}
function assertResponseObj(connOrTx) {
return () => {
insert1IntoFoo(connOrTx).then(function assertResponseObject(conn) {
return () =>
conn.queryAsync('SELECT bar from foo').then(res => {
assert.equal(res.rowCount, 1)
assert.equal(res.command, 'SELECT')
assert.deepEqual(res.rows, [{bar: 1}])
})
}).then(function assertRowsObject(conn) {
return () =>
conn.queryRowsAsync('SELECT bar from foo').then(rows => {
assert.deepEqual(rows, [{bar: 1}])
})
})
}
}
function throwAnError() {
throw new Error('an error after the insertion has happened')
}
})