-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathuserbind.test.js
More file actions
executable file
·536 lines (487 loc) · 14.6 KB
/
Copy pathuserbind.test.js
File metadata and controls
executable file
·536 lines (487 loc) · 14.6 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
'use strict'
import { createRequire } from 'module'
import chaiAsPromised from 'chai-as-promised'
const require = createRequire(import.meta.url)
const { TestEnv } = require('./env/test-env')
const env = new TestEnv()
const chai = require('chai')
chai.use(chaiAsPromised)
const expect = chai.expect
const sql = require('../lib/sql')
const { configureTestLogging } = require('./common/logging-helper')
// Configure logging based on environment variables
// By default, tests run silently. To enable logging:
// - MSNODESQLV8_TEST_VERBOSE=true npm test (for full trace logging)
// - MSNODESQLV8_TEST_LOG_LEVEL=DEBUG MSNODESQLV8_TEST_LOG_CONSOLE=true npm test
// - MSNODESQLV8_TEST_LOG_LEVEL=INFO MSNODESQLV8_TEST_LOG_FILE=/tmp/test.log npm test
configureTestLogging(sql)
describe('userbind', function () {
this.timeout(30000)
beforeEach(async function () {
await env.open()
})
afterEach(async function () {
await env.close()
})
async function testUserBindAsync (params) {
const promises = env.theConnection.promises
const r1 = await promises.query(params.query, [params.setter(params.min)])
const r2 = await promises.query(params.query, [params.setter(params.max)])
const allres = [r1.first[0], r2.first[0]]
if (params.test_null) {
const r3 = promises.query(params.query, [params.setter(null)])
allres.push(r3)
}
return allres
}
function compare (params, res) {
const min = params.expected ? params.expected[0] : params.min
const max = params.expected ? params.expected[1] : params.max
const expected = [
{ v: min },
{ v: max }
]
if (res.length === 3) {
expected.push({
v: null
})
}
expect(res).to.deep.equal(expected)
}
it('user bind DateTime2 to sql type datetime2(7) - with scale set to illegal value, should error', async function handler () {
const now = new Date()
const params = {
query: 'declare @v DATETIME2(7) = ?; select @v as v',
min: now,
max: now,
setter: v => {
return env.sql.DateTime2(v, 8) // set scale illegal
}
}
await expect(testUserBindAsync(params))
.to.be.rejectedWith('Invalid precision value')
})
it('user bind DateTimeOffset to sql type DateTimeOffset - provide offset of 60 minutes', async function handler () {
const offset = 60
const scale = 7
const smalldt = env.timeHelper.getUTCTodayHHMSS(14, 0, 0)
const expected = new Date(smalldt.getTime() - offset * 60000)
expected.nanosecondsDelta = 0
const params = {
query: 'declare @v DateTimeOffset = ?; select @v as v',
min: smalldt,
max: smalldt,
expected: [
expected,
expected
],
setter: v => {
return env.sql.DateTimeOffset(v, scale, offset)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind UniqueIdentifier', async function handler () {
const params = {
query: 'declare @v uniqueidentifier = ?; select @v as v',
min: 'F01251E5-96A3-448D-981E-0F99D789110D',
max: '45E8F437-670D-4409-93CB-F9424A40D6EE',
setter: v => {
return env.sql.UniqueIdentifier(v)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind time', async function handler () {
const timeOnly = env.timeHelper.getUTCTime1970HHMMSSMS()
timeOnly.nanosecondsDelta = 0
const params = {
query: 'declare @v time = ?; select @v as v',
min: timeOnly,
max: timeOnly,
expected: [
timeOnly,
timeOnly
],
setter: v => {
return env.sql.Time(v)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
async function timeTest (n) {
const timeOnly = n > 0
? env.timeHelper.getUTCTime1970HHMMSSMS()
: env.timeHelper.getUTCTime1970HHMMSS()
timeOnly.nanosecondsDelta = 0
const params = {
query: `declare @v time(${n}) = ?; select @v as v`,
min: timeOnly,
max: timeOnly,
expected: [
timeOnly,
timeOnly
],
setter: v => {
return env.sql.Time(v, n)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
}
it('user bind time(0)', async function handler () {
await timeTest(0)
})
it('user bind time(7)', async function handler () {
await timeTest(7)
})
it('user bind time(6)', async function handler () {
await timeTest(6)
})
it('user bind time(5)', async function handler () {
await timeTest(5)
})
it('user bind time(4)', async function handler () {
await timeTest(4)
})
it('user bind time(3)', async function handler () {
await timeTest(3)
})
it('user bind DateTime2 to sql type datetime2(7) - with scale set too low, should error', async function handler () {
const jsonDate = '2011-05-26T07:56:00.123Z'
const then = new Date(jsonDate)
const params = {
query: 'declare @v DATETIME2(7) = ?; select @v as v',
min: then,
max: then,
setter: v => {
return env.sql.DateTime2(v, 1) // set scale too low
}
}
await expect(testUserBindAsync(params))
.to.be.rejectedWith('Fractional second precision exceeds the scale specified')
})
function repeat (a, num) {
return new Array(num + 1).join(a)
}
it('user bind WLongVarChar to NVARCHAR(MAX)', async function handler () {
const smallLen = 2200
const largeLen = 8200
const params = {
query: 'declare @v NVARCHAR(MAX) = ?; select @v as v',
min: repeat('N', smallLen),
max: repeat('X', largeLen),
test_null: false,
setter: v => {
return env.sql.WLongVarChar(v)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind DateTimeOffset to sql type DateTimeOffset - no offset ', async function handler () {
const now = new Date()
const smalldt = new Date(Date.UTC(now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate(),
14,
0,
0,
0))
smalldt.nanosecondsDelta = 0
const params = {
query: 'declare @v DateTimeOffset = ?; select @v as v',
min: smalldt,
max: smalldt,
setter: v => {
return env.sql.DateTimeOffset(v)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind SmallDateTime to sql type smalldatetime', async function handler () {
const now = new Date()
const smalldt = env.timeHelper.getUTCDateHHMM(now)
smalldt.nanosecondsDelta = 0
const params = {
query: 'declare @v smalldatetime = ?; select @v as v',
min: smalldt,
max: smalldt,
setter: v => {
return env.sql.SmallDateTime(v)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind DateTime2 to sql type datetime2(7) default scale', async function handler () {
const now = new Date()
now.nanosecondsDelta = 0
const params = {
query: 'declare @v DATETIME2(7) = ?; select @v as v',
min: now,
max: now,
setter: v => {
return env.sql.DateTime2(v)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind DateTime2 to sql type datetime2(7) - with scale set correctly, should pass', async function handler () {
const now = new Date()
now.nanosecondsDelta = 0
const params = {
query: 'declare @v DATETIME2(7) = ?; select @v as v',
min: now,
max: now,
setter: v => {
return env.sql.DateTime2(v, 3) // set scale just right for ms
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind DateTime to sql type datetime2(7)', async function handler () {
const now = new Date()
now.nanosecondsDelta = 0
const params = {
query: 'declare @v DATETIME2(7) = ?; select @v as v',
min: now,
max: now,
setter: v => {
return env.sql.DateTime(v)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind DateTime to sql type datetime - driver currently only supports 10ms accuracy with datetime', async function handler () {
const now = env.sql.DateRound()
now.nanosecondsDelta = 0
const params = {
query: 'declare @v DATETIME2(7) = ?; select @v as v',
min: now,
max: now,
setter: v => {
return env.sql.DateTime(v)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind Date', async function handler () {
const today = new Date()
const dateOnly = env.timeHelper.getUTCDate()
dateOnly.nanosecondsDelta = 0
const params = {
query: 'declare @v date = ?; select @v as v',
min: today,
max: today,
expected: [
dateOnly,
dateOnly
],
setter: v => {
return env.sql.Date(v)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind Xml - well formatted.', async function handler () {
const params = {
query: 'declare @v xml = ?; select @v as v',
min: '<Cars><Car id="1234"><Make>Volkswagen</Make><Model>Eurovan</Model><Year>2003</Year><Color>White</Color></Car></Cars>',
max: '<Cars><Car id="1234"><Make>Volkswagen</Make><Model>Eurovan</Model><Year>2003</Year><Color>White</Color></Car><Car id="5678"><Make>Honda</Make><Model>CRV</Model><Year>2009</Year><Color>Black</Color><Mileage>35,600</Mileage></Car></Cars>',
setter: v => {
return env.sql.Xml(v)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind Xml - bad xml should give error', async function handler () {
const params = {
query: 'declare @v xml = ?; select @v as v',
min: '',
max: '<Cars><Car id="1234"><Make>Volkswagen</Make><Model>Eurovan</Model><Year>2003</Year><Color>White</Color></Cars>',
setter: v => {
return env.sql.Xml(v)
}
}
await expect(testUserBindAsync(params)).to.be.rejectedWith('end tag does not match start tag')
})
it('user bind nchar - check truncated user strings (1)', async function handler () {
const params = {
query: 'declare @v nchar(5) = ?; select @v as v',
min: 'five',
max: 'hello world',
expected: [
'five ',
'hello'
],
setter: v => {
return env.sql.NChar(v)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind Char - check truncated user strings (1)', async function handler () {
const params = {
query: 'declare @v char(5) = ?; select @v as v',
min: 'five',
max: 'hello world',
expected: [
'five ',
'hello'
],
setter: v => {
return env.sql.Char(v)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind Char - returned string will be padded (2)', async function handler () {
const params = {
query: 'declare @v char(5) = ?; select @v as v',
min: 'h',
max: 'world',
expected: [
'h ',
'world'
],
setter: v => {
return env.sql.Char(v)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind Char - use precision to clip user string (3)', async function handler () {
const params = {
query: 'declare @v char(11) = ?; select @v as v',
min: 'h',
max: 'world',
expected: [
'h' + new Array(11).join(' '),
'wo' + new Array(10).join(' ')
],
setter: v => {
return env.sql.Char(v, 2)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind NVarChar /16 bit encoded', async function handler () {
const params = {
query: 'declare @v varchar(100) = ?; select @v as v',
min: 'hello',
max: 'world',
expected: [
'hello',
'world'
],
setter: v => {
return env.sql.NVarChar(v)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind Float, maps to numeric data structure.', async function handler () {
const params = {
query: 'declare @v float = ?; select @v as v',
// eslint-disable-next-line no-loss-of-precision
min: -1.7976931348623158E+308,
// eslint-disable-next-line no-loss-of-precision
max: 1.7976931348623158E+308,
setter: v => {
return env.sql.Float(v)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind Double, maps to numeric data structure.', async function handler () {
const params = {
query: 'declare @v float = ?; select @v as v',
// eslint-disable-next-line no-loss-of-precision
min: -1.7976931348623158E+308,
// eslint-disable-next-line no-loss-of-precision
max: 1.7976931348623158E+308,
setter: v => {
return env.sql.Float(v)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind Bit', async function handler () {
const params = {
query: 'declare @v bit = ?; select @v as v',
min: false,
max: true,
setter: v => {
return env.sql.Bit(v)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind BigInt', async function handler () {
const params = {
query: 'declare @v bigint = ?; select @v as v',
min: -9007199254740991,
max: 9007199254740991,
setter: v => {
return env.sql.BigInt(v)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind Int', async function handler () {
const params = {
query: 'declare @v int = ?; select @v as v',
min: -2147483648,
max: 2147483647,
setter: v => {
return env.sql.Int(v)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind TinyInt', async function handler () {
const params = {
query: 'declare @v tinyint = ?; select @v as v',
min: 0,
max: 255,
setter: v => {
return env.sql.TinyInt(v)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
it('user bind SmallInt', async function handler () {
const params = {
query: 'declare @v smallint = ?; select @v as v',
min: -32768,
max: 32767,
setter: v => {
return env.sql.SmallInt(v)
}
}
const res = await testUserBindAsync(params)
compare(params, res)
})
})