forked from brianc/node-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-tests.js
219 lines (207 loc) · 5.64 KB
/
api-tests.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
'use strict'
var helper = require(__dirname + '/../test-helper')
var pg = helper.pg
var suite = new helper.Suite()
suite.test('pool callback behavior', done => {
// test weird callback behavior with node-pool
const pool = new pg.Pool()
pool.connect(function (err) {
assert(!err)
arguments[1].emit('drain')
arguments[2]()
pool.end(done)
})
})
suite.test('query timeout', (cb) => {
const pool = new pg.Pool({query_timeout: 1000})
pool.connect().then((client) => {
client.query('SELECT pg_sleep(2)', assert.calls(function (err, result) {
assert(err)
client.release()
pool.end(cb)
}))
})
})
suite.test('callback API', done => {
const client = new helper.Client()
client.query('CREATE TEMP TABLE peep(name text)')
client.query('INSERT INTO peep(name) VALUES ($1)', ['brianc'])
const config = {
text: 'INSERT INTO peep(name) VALUES ($1)',
values: ['brian']
}
client.query(config)
client.query('INSERT INTO peep(name) VALUES ($1)', ['aaron'])
client.query('SELECT * FROM peep ORDER BY name COLLATE "C"', (err, res) => {
assert(!err)
assert.equal(res.rowCount, 3)
assert.deepEqual(res.rows, [
{
name: 'aaron'
},
{
name: 'brian'
},
{
name: 'brianc'
}
])
done()
})
client.connect(err => {
assert(!err)
client.once('drain', () => client.end())
})
})
suite.test('executing nested queries', function (done) {
const pool = new pg.Pool()
pool.connect(
assert.calls(function (err, client, release) {
assert(!err)
client.query(
'select now as now from NOW()',
assert.calls(function (err, result) {
assert.equal(new Date().getYear(), result.rows[0].now.getYear())
client.query(
'select now as now_again FROM NOW()',
assert.calls(function () {
client.query(
'select * FROM NOW()',
assert.calls(function () {
assert.ok('all queries hit')
release()
pool.end(done)
})
)
})
)
})
)
})
)
})
suite.test('raises error if cannot connect', function () {
var connectionString = 'pg://sfalsdkf:asdf@localhost/ieieie'
const pool = new pg.Pool({ connectionString: connectionString })
pool.connect(
assert.calls(function (err, client, done) {
assert.ok(err, 'should have raised an error')
done()
})
)
})
suite.test('query errors are handled and do not bubble if callback is provided', function (done) {
const pool = new pg.Pool()
pool.connect(
assert.calls(function (err, client, release) {
assert(!err)
client.query(
'SELECT OISDJF FROM LEIWLISEJLSE',
assert.calls(function (err, result) {
assert.ok(err)
release()
pool.end(done)
})
)
})
)
}
)
suite.test('callback is fired once and only once', function (done) {
const pool = new pg.Pool()
pool.connect(
assert.calls(function (err, client, release) {
assert(!err)
client.query('CREATE TEMP TABLE boom(name varchar(10))')
var callCount = 0
client.query(
[
"INSERT INTO boom(name) VALUES('hai')",
"INSERT INTO boom(name) VALUES('boom')",
"INSERT INTO boom(name) VALUES('zoom')"
].join(';'),
function (err, callback) {
assert.equal(
callCount++,
0,
'Call count should be 0. More means this callback fired more than once.'
)
release()
pool.end(done)
}
)
})
)
})
suite.test('can provide callback and config object', function (done) {
const pool = new pg.Pool()
pool.connect(
assert.calls(function (err, client, release) {
assert(!err)
client.query(
{
name: 'boom',
text: 'select NOW()'
},
assert.calls(function (err, result) {
assert(!err)
assert.equal(result.rows[0].now.getYear(), new Date().getYear())
release()
pool.end(done)
})
)
})
)
})
suite.test('can provide callback and config and parameters', function (done) {
const pool = new pg.Pool()
pool.connect(
assert.calls(function (err, client, release) {
assert(!err)
var config = {
text: 'select $1::text as val'
}
client.query(
config,
['hi'],
assert.calls(function (err, result) {
assert(!err)
assert.equal(result.rows.length, 1)
assert.equal(result.rows[0].val, 'hi')
release()
pool.end(done)
})
)
})
)
})
suite.test('null and undefined are both inserted as NULL', function (done) {
const pool = new pg.Pool()
pool.connect(
assert.calls(function (err, client, release) {
assert(!err)
client.query(
'CREATE TEMP TABLE my_nulls(a varchar(1), b varchar(1), c integer, d integer, e date, f date)'
)
client.query(
'INSERT INTO my_nulls(a,b,c,d,e,f) VALUES ($1,$2,$3,$4,$5,$6)',
[null, undefined, null, undefined, null, undefined]
)
client.query(
'SELECT * FROM my_nulls',
assert.calls(function (err, result) {
assert(!err)
assert.equal(result.rows.length, 1)
assert.isNull(result.rows[0].a)
assert.isNull(result.rows[0].b)
assert.isNull(result.rows[0].c)
assert.isNull(result.rows[0].d)
assert.isNull(result.rows[0].e)
assert.isNull(result.rows[0].f)
pool.end(done)
release()
})
)
})
)
})