-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest-postgresql-client.coffee
341 lines (319 loc) · 15.6 KB
/
test-postgresql-client.coffee
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
path = require 'path'
should_continue = true
try
require('pg')
catch err
console.error ""
console.error "WARNING: require('pg') failed with the following error:"
console.error err
console.error "The tests in #{path.basename(__filename)} will be skipped."
console.error ""
console.error "You must add the pg library to your devDependencies to enable"
console.error "these tests. See `./test/README.md` for details."
console.error ""
should_continue = false
if should_continue
pg = require( '../lib/postgresql-client')
should = require('should')
Util = require( '../lib/util' ).Util
describe 'PostgreSQL',->
CONNECT_STRING = "postgres://sqlclient_test_user:password@localhost/sqlclient_test_db"
PAUSE = 2
it 'check for database',(done)->
database_available = (callback)->
client = new pg.PostgreSQLClient(CONNECT_STRING)
client.connect (err)->
if err?
report_no_database(err)
x = 0
console.error "(Pausing for #{PAUSE} seconds to make sure you notice the message.)"
console.error "============================================================"
console.error ""
setTimeout (callback), PAUSE*1000
else
client.disconnect (err)->
callback(true)
report_no_database = (err)->
console.error ""
console.error "============================================================"
console.error "!WARNING! COULD NOT CONNECT TO POSTGRESQL DATABASE !WARNING!"
console.error "------------------------------------------------------------"
console.error ""
console.error " The automated functional tests in:"
console.error " #{path.basename(__filename)}"
console.error " require access to a test account in a PostgeSQL database. "
console.error ""
console.error " The following error was encountered while trying to"
console.error " connect to the database using the connect string:"
console.error " #{CONNECT_STRING}"
console.error ""
console.error " The specific error encountered was:"
console.error " #{err}"
console.error " Some unit tests will be skipped because of this error."
console.error ""
console.error " See the README file at:"
console.error " #{path.join(__dirname,'README.md')}"
console.error " for instructions on how to set up and configure the test"
console.error " database in order to enable these tests."
console.error ""
console.error "------------------------------------------------------------"
database_available (available)->
if available
describe 'PostgreSQLClient',->
it 'can connect to the database', (done)->
client = new pg.PostgreSQLClient(CONNECT_STRING)
client.connect (err)->
if err?
report_no_database(err)
should.not.exist err
client.disconnect (err)->
should.not.exist err
done()
it 'can execute a query (straight sql)', (done)->
client = new pg.PostgreSQLClient(CONNECT_STRING)
client.connect (err)->
should.not.exist err
client.execute "SELECT 17 AS n, NOW() as dt", (err,results)->
should.not.exist err
should.exist results
should.exist results.rows
results.rows.length.should.equal 1
results.rows[0].n.should.equal 17
results.rows[0].dt.should.be.ok
client.disconnect (err)->
should.not.exist err
done()
it 'can execute a query (postgresql-style ($1) bind-variables)', (done)->
client = new pg.PostgreSQLClient(CONNECT_STRING)
client.connect (err)->
should.not.exist err
client.execute "SELECT $1::INTEGER AS x", [19], (err,results)->
should.not.exist err
should.exist results
should.exist results.rows
results.rows.length.should.equal 1
results.rows[0].x.should.equal 19
client.disconnect (err)->
should.not.exist err
done()
it 'can execute a query (?-style bind-variables)', (done)->
client = new pg.PostgreSQLClient(CONNECT_STRING)
client.connect (err)->
should.not.exist err
client.execute "SELECT ?::INTEGER AS x", [19], (err,results)->
should.not.exist err
should.exist results
should.exist results.rows
results.rows.length.should.equal 1
results.rows[0].x.should.equal 19
client.disconnect (err)->
should.not.exist err
done()
describe 'PostgreSQLClientPool',->
it 'supports borrow, execute, return pattern (default config)', (done)->
pool = new pg.PostgreSQLClientPool(CONNECT_STRING)
pool.open (err)->
should.not.exist err
pool.borrow (err,client)->
should.not.exist err
should.exist client
client.execute "SELECT ?::INTEGER AS x, ?::INTEGER AS y", [32,18], (err,results)->
should.not.exist err
should.exist results
should.exist results.rows
results.rows.length.should.equal 1
results.rows[0].x.should.equal 32
results.rows[0].y.should.equal 18
pool.return client, (err)->
should.not.exist err
pool.close (err)->
should.not.exist err
done()
it 'supports execute method to wrapping borrow/return logic (default config)', (done)->
pool = new pg.PostgreSQLClientPool(CONNECT_STRING)
pool.open (err)->
should.not.exist err
pool.execute "SELECT ?::INTEGER AS x, ?::INTEGER AS y", [32,18], (err,results)->
should.not.exist err
should.exist results
should.exist results.rows
results.rows.length.should.equal 1
results.rows[0].x.should.equal 32
results.rows[0].y.should.equal 18
pool.close (err)->
should.not.exist err
done()
it 'supports borrow, execute, return X 5 pattern (default config)', (done)->
pool = new pg.PostgreSQLClientPool(CONNECT_STRING)
pool.open (err)->
should.not.exist err
action = (e,i,l,next)=>
pool.borrow (err,client)->
should.not.exist err
should.exist client
client.execute "SELECT ?::INTEGER AS x, ?::INTEGER AS y", [32,18], (err,results)->
should.not.exist err
should.exist results
should.exist results.rows
results.rows.length.should.equal 1
results.rows[0].x.should.equal 32
results.rows[0].y.should.equal 18
pool.return client, (err)->
should.not.exist err
next()
Util.for_each_async [0...5], action, ()=>
pool.close (err)->
should.not.exist err
done()
it 'supports borrow, execute, borrow, return, execute, return X 5 pattern (default config)', (done)->
pool = new pg.PostgreSQLClientPool(CONNECT_STRING)
pool.open (err)->
should.not.exist err
action = (e,i,l,next)=>
pool.borrow (err,client)->
should.not.exist err
should.exist client
client.execute "SELECT ?::INTEGER AS x, ?::INTEGER AS y", [32,18], (err,results)->
should.not.exist err
should.exist results
should.exist results.rows
results.rows.length.should.equal 1
results.rows[0].x.should.equal 32
results.rows[0].y.should.equal 18
pool.borrow (err,client2)->
should.not.exist err
should.exist client2
pool.return client, (err)->
should.not.exist err
client2.execute "SELECT ?::INTEGER AS x, ?::INTEGER AS y", [3,18], (err,results)->
should.not.exist err
should.exist results
should.exist results.rows
results.rows.length.should.equal 1
results.rows[0].x.should.equal 3
results.rows[0].y.should.equal 18
pool.return client2, (err)->
should.not.exist err
next()
Util.for_each_async [0...5], action, ()=>
pool.close (err)->
should.not.exist err
done()
it 'supports borrow, execute, borrow, execute, return, return X 5 pattern (default config)', (done)->
pool = new pg.PostgreSQLClientPool(CONNECT_STRING)
pool.open (err)->
should.not.exist err
action = (e,i,l,next)=>
pool.borrow (err,client)->
should.not.exist err
should.exist client
client.execute "SELECT ?::INTEGER AS x, ?::INTEGER AS y", [32,18], (err,results)->
should.not.exist err
should.exist results
should.exist results.rows
results.rows.length.should.equal 1
results.rows[0].x.should.equal 32
results.rows[0].y.should.equal 18
pool.borrow (err,client2)->
should.not.exist err
should.exist client2
client2.execute "SELECT ?::INTEGER AS x, ?::INTEGER AS y", [3,18], (err,results)->
should.not.exist err
should.exist results
should.exist results.rows
results.rows.length.should.equal 1
results.rows[0].x.should.equal 3
results.rows[0].y.should.equal 18
pool.return client2, (err)->
should.not.exist err
pool.return client, (err)->
should.not.exist err
next()
Util.for_each_async [0...5], action, ()=>
pool.close (err)->
should.not.exist err
done()
it 'supports borrow, execute, return pattern (max_idle=5,min_idle=3)', (done)->
options = { min_idle:3, max_idle:5 }
pool = new pg.PostgreSQLClientPool(CONNECT_STRING)
pool.open options, (err)->
should.not.exist err
pool.borrow (err,client)->
should.not.exist err
should.exist client
client.execute "SELECT ?::INTEGER AS x, ?::INTEGER AS y", [32,18], (err,results)->
should.not.exist err
should.exist results
should.exist results.rows
results.rows.length.should.equal 1
results.rows[0].x.should.equal 32
results.rows[0].y.should.equal 18
pool.return client, (err)->
should.not.exist err
pool.close (err)->
should.not.exist err
done()
it 'supports borrow, execute, borrow, execute, return, return pattern (default config)', (done)->
pool = new pg.PostgreSQLClientPool(CONNECT_STRING)
pool.open (err)->
should.not.exist err
pool.borrow (err,client1)->
should.not.exist err
should.exist client1
client1.execute "SELECT ?::INTEGER AS x, ?::INTEGER AS y", [32,18], (err,results)->
should.not.exist err
should.exist results
should.exist results.rows
results.rows.length.should.equal 1
results.rows[0].x.should.equal 32
results.rows[0].y.should.equal 18
pool.borrow (err,client2)->
should.not.exist err
should.exist client2
client2.execute "SELECT ?::INTEGER AS x, ?::INTEGER AS y", [1,5], (err,results)->
should.not.exist err
should.exist results
should.exist results.rows
results.rows.length.should.equal 1
results.rows[0].x.should.equal 1
results.rows[0].y.should.equal 5
pool.return client2, (err)->
should.not.exist err
pool.return client1, (err)->
should.not.exist err
pool.close (err)->
should.not.exist err
done()
it 'supports borrow, execute, return, borrow, execute, return pattern (max_idle=1,min_idle=0)', (done)->
options = { max_idle:1,min_idle:0 }
pool = new pg.PostgreSQLClientPool(CONNECT_STRING)
pool.open options,(err)->
should.not.exist err
pool.borrow (err,client1)->
should.not.exist err
should.exist client1
client1.execute "SELECT ?::INTEGER AS x, ?::INTEGER AS y", [32,18], (err,results)->
should.not.exist err
should.exist results
should.exist results.rows
results.rows.length.should.equal 1
results.rows[0].x.should.equal 32
results.rows[0].y.should.equal 18
pool.return client1, (err)->
should.not.exist err
pool.borrow (err,client2)->
should.not.exist err
should.exist client2
client2.execute "SELECT ?::INTEGER AS x, ?::INTEGER AS y", [1,5], (err,results)->
should.not.exist err
should.exist results
should.exist results.rows
results.rows.length.should.equal 1
results.rows[0].x.should.equal 1
results.rows[0].y.should.equal 5
pool.return client2, (err)->
should.not.exist err
pool.close (err)->
should.not.exist err
done()
done()