forked from brianc/node-postgres
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils-tests.js
More file actions
332 lines (262 loc) · 9.89 KB
/
utils-tests.js
File metadata and controls
332 lines (262 loc) · 9.89 KB
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
'use strict'
const helper = require('./test-helper')
const utils = require('./../../lib/utils')
const defaults = require('./../../lib').defaults
const assert = require('assert')
const suite = new helper.Suite()
const test = suite.test.bind(suite)
test('ensure types is exported on root object', function () {
const pg = require('../../lib')
assert(pg.types)
assert(pg.types.getTypeParser)
assert(pg.types.setTypeParser)
})
test('normalizing query configs', function () {
let config
const callback = function () {}
config = utils.normalizeQueryConfig({ text: 'TEXT' })
assert.same(config, { text: 'TEXT' })
config = utils.normalizeQueryConfig({ text: 'TEXT' }, [10])
assert.deepEqual(config, { text: 'TEXT', values: [10] })
config = utils.normalizeQueryConfig({ text: 'TEXT', values: [10] })
assert.deepEqual(config, { text: 'TEXT', values: [10] })
config = utils.normalizeQueryConfig('TEXT', [10], callback)
assert.deepEqual(config, { text: 'TEXT', values: [10], callback: callback })
config = utils.normalizeQueryConfig({ text: 'TEXT', values: [10] }, callback)
assert.deepEqual(config, { text: 'TEXT', values: [10], callback: callback })
})
test('prepareValues: buffer prepared properly', function () {
const buf = Buffer.from('quack')
const out = utils.prepareValue(buf)
assert.strictEqual(buf, out)
})
test('prepareValues: Uint8Array prepared properly', function () {
const buf = new Uint8Array([1, 2, 3]).subarray(1, 2)
const out = utils.prepareValue(buf)
assert.ok(Buffer.isBuffer(out))
assert.equal(out.length, 1)
assert.deepEqual(out[0], 2)
})
test('prepareValues: date prepared properly', function () {
helper.setTimezoneOffset(-330)
const date = new Date(2014, 1, 1, 11, 11, 1, 7)
const out = utils.prepareValue(date)
assert.strictEqual(out, '2014-02-01T11:11:01.007+05:30')
helper.resetTimezoneOffset()
})
test('prepareValues: date prepared properly as UTC', function () {
defaults.parseInputDatesAsUTC = true
// make a date in the local timezone that represents a specific UTC point in time
const date = new Date(Date.UTC(2014, 1, 1, 11, 11, 1, 7))
const out = utils.prepareValue(date)
assert.strictEqual(out, '2014-02-01T11:11:01.007+00:00')
defaults.parseInputDatesAsUTC = false
})
test('prepareValues: BC date prepared properly', function () {
helper.setTimezoneOffset(-330)
const date = new Date(-3245, 1, 1, 11, 11, 1, 7)
const out = utils.prepareValue(date)
assert.strictEqual(out, '3246-02-01T11:11:01.007+05:30 BC')
helper.resetTimezoneOffset()
})
test('prepareValues: 1 BC date prepared properly', function () {
helper.setTimezoneOffset(-330)
// can't use the multi-argument constructor as year 0 would be interpreted as 1900
const date = new Date('0000-02-01T11:11:01.007')
const out = utils.prepareValue(date)
assert.strictEqual(out, '0001-02-01T11:11:01.007+05:30 BC')
helper.resetTimezoneOffset()
})
test('prepareValues: undefined prepared properly', function () {
const out = utils.prepareValue(void 0)
assert.strictEqual(out, null)
})
test('prepareValues: invalid date throws an error', function () {
const invalidDate = new Date(undefined)
assert.throws(
() => utils.prepareValue(invalidDate),
/date is invalid/
)
})
test('prepareValues: invalid date (NaN) does not produce NaN string', function () {
const invalidDate = new Date('not a date')
assert.throws(
() => utils.prepareValue(invalidDate),
/date is invalid/
)
})
test('prepareValue: null prepared properly', function () {
const out = utils.prepareValue(null)
assert.strictEqual(out, null)
})
test('prepareValue: true prepared properly', function () {
const out = utils.prepareValue(true)
assert.strictEqual(out, 'true')
})
test('prepareValue: false prepared properly', function () {
const out = utils.prepareValue(false)
assert.strictEqual(out, 'false')
})
test('prepareValue: number prepared properly', function () {
const out = utils.prepareValue(3.042)
assert.strictEqual(out, '3.042')
})
test('prepareValue: string prepared properly', function () {
const out = utils.prepareValue('big bad wolf')
assert.strictEqual(out, 'big bad wolf')
})
test('prepareValue: simple array prepared properly', function () {
const out = utils.prepareValue([1, null, 3, undefined, [5, 6, 'squ,awk']])
assert.strictEqual(out, '{"1",NULL,"3",NULL,{"5","6","squ,awk"}}')
})
test('prepareValue: complex array prepared properly', function () {
const out = utils.prepareValue([{ x: 42 }, { y: 84 }])
assert.strictEqual(out, '{"{\\"x\\":42}","{\\"y\\":84}"}')
})
test('prepareValue: date array prepared properly', function () {
helper.setTimezoneOffset(-330)
const date = new Date(2014, 1, 1, 11, 11, 1, 7)
const out = utils.prepareValue([date])
assert.strictEqual(out, '{"2014-02-01T11:11:01.007+05:30"}')
helper.resetTimezoneOffset()
})
test('prepareValue: arbitrary objects prepared properly', function () {
const out = utils.prepareValue({ x: 42 })
assert.strictEqual(out, '{"x":42}')
})
test('prepareValue: objects with simple toPostgres prepared properly', function () {
const customType = {
toPostgres: function () {
return 'zomgcustom!'
},
}
const out = utils.prepareValue(customType)
assert.strictEqual(out, 'zomgcustom!')
})
test('prepareValue: buffer array prepared properly', function () {
const buffer1 = Buffer.from('dead', 'hex')
const buffer2 = Buffer.from('beef', 'hex')
const out = utils.prepareValue([buffer1, buffer2])
assert.strictEqual(out, '{\\\\xdead,\\\\xbeef}')
})
test('prepareValue: Uint8Array array prepared properly', function () {
const buffer1 = Uint8Array.from(Buffer.from('dead', 'hex'))
const buffer2 = Uint8Array.from(Buffer.from('beef', 'hex'))
const out = utils.prepareValue([buffer1, buffer2])
assert.strictEqual(out, '{\\\\xdead,\\\\xbeef}')
})
test('prepareValue: objects with complex toPostgres prepared properly', function () {
const customType = {
toPostgres: function () {
return [1, 2]
},
}
const out = utils.prepareValue(customType)
assert.strictEqual(out, '{"1","2"}')
})
test('prepareValue: objects with toPostgres receive prepareValue', function () {
const customRange = {
lower: {
toPostgres: function () {
return 5
},
},
upper: {
toPostgres: function () {
return 10
},
},
toPostgres: function (prepare) {
return '[' + prepare(this.lower) + ',' + prepare(this.upper) + ']'
},
}
const out = utils.prepareValue(customRange)
assert.strictEqual(out, '[5,10]')
})
test('prepareValue: objects with circular toPostgres rejected', function () {
const customType = {
toPostgres: function () {
return {
toPostgres: function () {
return customType
},
}
},
}
// can't use `assert.throws` since we need to distinguish circular reference
// errors from call stack exceeded errors
try {
utils.prepareValue(customType)
} catch (e) {
assert.ok(e.message.match(/circular/), 'Expected circular reference error but got ' + e)
return
}
throw new Error('Expected prepareValue to throw exception')
})
test('prepareValue: can safely be used to map an array of values including those with toPostgres functions', function () {
const customType = {
toPostgres: function () {
return 'zomgcustom!'
},
}
const values = [1, 'test', customType]
const out = values.map(utils.prepareValue)
assert.deepEqual(out, [1, 'test', 'zomgcustom!'])
})
const testEscapeLiteral = function (testName, input, expected) {
test(`escapeLiteral: ${testName}`, function () {
const actual = utils.escapeLiteral(input)
assert.equal(expected, actual)
})
}
testEscapeLiteral('no special characters', 'hello world', "'hello world'")
testEscapeLiteral('contains double quotes only', 'hello " world', "'hello \" world'")
testEscapeLiteral('contains single quotes only', "hello ' world", "'hello '' world'")
testEscapeLiteral('contains backslashes only', 'hello \\ world', " E'hello \\\\ world'")
testEscapeLiteral('contains single quotes and double quotes', 'hello \' " world', "'hello '' \" world'")
testEscapeLiteral('date', new Date(), "''")
testEscapeLiteral('null', null, "''")
testEscapeLiteral('undefined', undefined, "''")
testEscapeLiteral('boolean', false, "''")
testEscapeLiteral('number', 1, "''")
testEscapeLiteral('number', 1, "''")
testEscapeLiteral('boolean', true, "''")
testEscapeLiteral('array', [1, 2, 3], "''")
testEscapeLiteral('object', { x: 42 }, "''")
testEscapeLiteral('contains double quotes and backslashes', 'hello \\ " world', " E'hello \\\\ \" world'")
testEscapeLiteral('contains single quotes and backslashes', "hello \\ ' world", " E'hello \\\\ '' world'")
testEscapeLiteral(
'contains single quotes, double quotes, and backslashes',
'hello \\ \' " world',
" E'hello \\\\ '' \" world'"
)
const testEscapeIdentifier = function (testName, input, expected) {
test(testName, function () {
const actual = utils.escapeIdentifier(input)
assert.equal(expected, actual)
})
}
testEscapeIdentifier('escapeIdentifier: no special characters', 'hello world', '"hello world"')
testEscapeIdentifier('escapeIdentifier: contains double quotes only', 'hello " world', '"hello "" world"')
testEscapeIdentifier('escapeIdentifier: contains single quotes only', "hello ' world", '"hello \' world"')
testEscapeIdentifier('escapeIdentifier: contains backslashes only', 'hello \\ world', '"hello \\ world"')
testEscapeIdentifier(
'escapeIdentifier: contains single quotes and double quotes',
'hello \' " world',
'"hello \' "" world"'
)
testEscapeIdentifier(
'escapeIdentifier: contains double quotes and backslashes',
'hello \\ " world',
'"hello \\ "" world"'
)
testEscapeIdentifier(
'escapeIdentifier: contains single quotes and backslashes',
"hello \\ ' world",
'"hello \\ \' world"'
)
testEscapeIdentifier(
'escapeIdentifier: contains single quotes, double quotes, and backslashes',
'hello \\ \' " world',
'"hello \\ \' "" world"'
)