-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.js
583 lines (456 loc) · 13.5 KB
/
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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
const test = require(`tape`)
const q = require(`./index`)
test(`first query`, t => {
q.select(`WRONG`)
const result = q.select(`butt`)
.from(`pants`)
.build()
t.equal(result.sql, [
`SELECT butt`,
`FROM pants`,
].join(`\n`))
t.end()
})
test(`select/from/where with params`, t => {
const result = q.select(`butt`)
.from(`pants`)
.where(`touching`, true)
.where(`hugging`, true)
.where(`feeling`, false)
.build()
t.equal(result.sql, [
`SELECT butt`,
`FROM pants`,
`WHERE touching = ? AND hugging = ? AND feeling = ?`,
].join(`\n`))
t.deepEqual(result.values, [ true, true, false ])
t.equal(result.sql, [
`SELECT butt`,
`FROM pants`,
`WHERE touching = ? AND hugging = ? AND feeling = ?`,
].join(`\n`))
t.deepEqual(result.values, [ true, true, false ])
t.end()
})
test(`some joins`, t => {
const result = q.select(`wat`)
.from(`meh`)
.join(`no_in_fact_u`, `no_in_fact_u.meh_id = meh.id`)
.leftJoin(`who`, `who.no_in_fact_u_id = no_in_fact_u.id`)
.build()
t.equal(result.sql, [
`SELECT wat`,
`FROM meh`,
`JOIN no_in_fact_u ON no_in_fact_u.meh_id = meh.id`,
`LEFT JOIN who ON who.no_in_fact_u_id = no_in_fact_u.id`,
].join(`\n`))
t.end()
})
test(`WHERE a OR b`, t => {
const result = q.select(`wat`)
.from(`whatever`)
.where(`a`, 1)
.orWhere(`b`, 2)
.orWhere(`c`, [ 3, 4 ])
.build()
t.equal(result.sql, [
`SELECT wat`,
`FROM whatever`,
`WHERE a = ? OR b = ? OR c IN(?)`,
].join(`\n`))
t.deepEqual(result.values, [ 1, 2, [ 3, 4 ] ])
t.end()
})
test(`multiple select values`, t => {
t.equal(q.select(`a`, `b`, `c`).from(`blah`).build().sql, `SELECT a, b, c\nFROM blah`)
t.end()
})
test(`from clause with alias`, t => {
t.equal(
q.select(`whatever`).from(`blah`, `a_longer_name_for_no_reason`).build().sql,
`SELECT whatever\nFROM blah AS a_longer_name_for_no_reason`,
)
t.end()
})
test(`subqueries`, t => {
const subquery = q.select(`thing`).from(`place`).where(`column`, `value`)
let result = q.select(`a`)
.from(subquery, `subquery_alias`)
.having(`subquery_alias.thing`, 2)
.where(`subquery_alias.thing`, 1)
.build()
t.equal(result.sql, [
`SELECT a`,
`FROM (`,
`\tSELECT thing`,
`\tFROM place`,
`\tWHERE column = ?`,
`) AS subquery_alias`,
`WHERE subquery_alias.thing = ?`,
`HAVING subquery_alias.thing = ?`,
].join(`\n`))
t.deepEqual(result.values, [ `value`, 1, 2 ])
result = q.select(`dumb_column`)
.from(`dumb_table`, `dumb_alias`)
.leftJoin(subquery, `dumb_subquery`, `dumb_subquery.column = dumb_alias.column`)
.build()
t.equal(result.sql, [
`SELECT dumb_column`,
`FROM dumb_table AS dumb_alias`,
`LEFT JOIN (`,
`\tSELECT thing`,
`\tFROM place`,
`\tWHERE column = ?`,
`) AS dumb_subquery ON dumb_subquery.column = dumb_alias.column`,
].join(`\n`))
t.deepEqual(result.values, [ `value` ])
t.end()
})
test(`group by`, t => {
t.equal(
q.groupBy(`a`, `b`).from(`wat`).select(`lol`).where(`butts`, 13).build().sql,
[ `SELECT lol`, `FROM wat`, `WHERE butts = ?`, `GROUP BY a, b` ].join(`\n`),
)
t.end()
})
test(`where like`, t => {
const result = q.select(`lol`).from(`butt`).whereLike(`column`, `starts with%`).build()
t.equal(result.sql, [ `SELECT lol`, `FROM butt`, `WHERE column LIKE ?` ].join(`\n`))
t.deepEqual(result.values, [ `starts with%` ])
const result2 = q.select(`lol`).from(`butt`).whereLike(`column`, `starts with%`).orWhereLike(`other_column`, `%ends with`).build()
t.equal(result2.sql, [ `SELECT lol`, `FROM butt`, `WHERE column LIKE ? OR other_column LIKE ?` ].join(`\n`))
t.deepEqual(result2.values, [ `starts with%`, `%ends with` ])
t.end()
})
test(`order by`, t => {
const result = q.select(`lol`).from(`butt`).orderBy(`column1`, `column2`).build()
t.equal(result.sql, [ `SELECT lol`, `FROM butt`, `ORDER BY column1, column2` ].join(`\n`))
t.deepEqual(result.values, [ ])
t.end()
})
test(`limit`, t => {
const result = q.select(`lol`).from(`butt`).limit(10).build()
t.equal(result.sql, [ `SELECT lol`, `FROM butt`, `LIMIT 10` ].join(`\n`))
t.deepEqual(result.values, [ ])
t.end()
})
test(`null in a where clause`, t => {
const result = q.select(`whatever`).from(`meh`).where(`something`, null).where(`thingy`, `whatevs`).build()
t.equal(result.sql, [ `SELECT whatever`, `FROM meh`, `WHERE something IS ? AND thingy = ?` ].join(`\n`))
t.deepEqual(result.values, [ null, `whatevs` ])
t.end()
})
test(`null in a where clause with comparator`, t => {
const result = q.select(`whatever`).from(`meh`).where(`something`, `IS NOT`, null).where(`thingy`, `whatevs`).build()
t.equal(result.sql, [ `SELECT whatever`, `FROM meh`, `WHERE something IS NOT ? AND thingy = ?` ].join(`\n`))
t.deepEqual(result.values, [ null, `whatevs` ])
t.end()
})
test(`where in(array)`, t => {
const result = q.select(`whatever`).from(`meh`).where(`something`, [ 1, 2, 3 ]).build()
t.equal(result.sql, [ `SELECT whatever`, `FROM meh`, `WHERE something IN(?)` ].join(`\n`))
t.deepEqual(result.values, [ [ 1, 2, 3 ] ])
t.end()
})
test(`lock in shared mode`, t => {
const result = q.select(`whatever`).from(`meh`).where(`something`, 22).lockInShareMode().build()
t.equal(result.sql, [ `SELECT whatever`, `FROM meh`, `WHERE something = ?`, `LOCK IN SHARE MODE` ].join(`\n`))
t.deepEqual(result.values, [ 22 ])
t.end()
})
test(`select for update`, t => {
const result = q.select(`whatever`).from(`meh`).where(`something`, 22).forUpdate().build()
t.equal(result.sql, [ `SELECT whatever`, `FROM meh`, `WHERE something = ?`, `FOR UPDATE` ].join(`\n`))
t.deepEqual(result.values, [ 22 ])
t.end()
})
test(`WHERE gt/lt/gte/lte AND`, t => {
const result = q.select(`wat`)
.from(`whatever`)
.where(`a`, `>`, 1)
.where(`b`, `>=`, 2)
.where(`c`, `<`, 3)
.where(`d`, `<=`, 4)
.build()
t.equal(result.sql, [
`SELECT wat`,
`FROM whatever`,
`WHERE a > ? AND b >= ? AND c < ? AND d <= ?`,
].join(`\n`))
t.deepEqual(result.values, [ 1, 2, 3, 4 ])
t.end()
})
test(`WHERE gt/lt/gte/lte OR`, t => {
const result = q.select(`wat`)
.from(`whatever`)
.orWhere(`a`, `>`, 1)
.orWhere(`b`, `>=`, 2)
.orWhere(`c`, `<`, 3)
.orWhere(`d`, `<=`, 4)
.build()
t.equal(result.sql, [
`SELECT wat`,
`FROM whatever`,
`WHERE a > ? OR b >= ? OR c < ? OR d <= ?`,
].join(`\n`))
t.deepEqual(result.values, [ 1, 2, 3, 4 ])
t.end()
})
test(`HAVING gt/lt/gte/lte AND`, t => {
const result = q.select(`wat`)
.from(`whatever`)
.having(`a`, `>`, 1)
.having(`b`, `>=`, 2)
.having(`c`, `<`, 3)
.having(`d`, `<=`, 4)
.build()
t.equal(result.sql, [
`SELECT wat`,
`FROM whatever`,
`HAVING a > ? AND b >= ? AND c < ? AND d <= ?`,
].join(`\n`))
t.deepEqual(result.values, [ 1, 2, 3, 4 ])
t.end()
})
test(`HAVING gt/lt/gte/lte OR`, t => {
const result = q.select(`wat`)
.from(`whatever`)
.orHaving(`a`, `>`, 1)
.orHaving(`b`, `>=`, 2)
.orHaving(`c`, `<`, 3)
.orHaving(`d`, `<=`, 4)
.build()
t.equal(result.sql, [
`SELECT wat`,
`FROM whatever`,
`HAVING a > ? OR b >= ? OR c < ? OR d <= ?`,
].join(`\n`))
t.deepEqual(result.values, [ 1, 2, 3, 4 ])
t.end()
})
test(`Tagged template string`, t => {
const result = q`SELECT wat FROM a WHERE foo = ${ 4 } AND bar IN(${ [ 1, 2 ] })`
t.equal(result.sql, `SELECT wat FROM a WHERE foo = ? AND bar IN(?)`)
t.deepEqual(result.values, [ 4, [ 1, 2 ] ])
t.end()
})
test(`Query object in a tagged template string`, t => {
const subquery = q.select(`sub`).from(`other`).where(`three`, 3).build()
const result = q`SELECT wat FROM a WHERE foo = ${ subquery } AND bar IN(${ [ 1, 2 ] })`
t.equal(result.sql, `SELECT wat FROM a WHERE foo = (SELECT sub\nFROM other\nWHERE three = ?) AND bar IN(?)`)
t.deepEqual(result.values, [ 3, [ 1, 2 ] ])
t.end()
})
test(`Passing a str/params object as a value`, t => {
const { sql, values } = q.select(`howdy`)
.from(`meh`)
.where(`a`, 1)
.where(`tag`, {
sql: `FANCY(?, ?)`,
values: [ `pants`, `butts` ],
})
.where(`b`, 2)
.build()
t.equal(sql, `SELECT howdy\nFROM meh\nWHERE a = ? AND tag = FANCY(?, ?) AND b = ?`)
t.deepEqual(values, [ 1, `pants`, `butts`, 2 ])
t.end()
})
test(`Passing a str/params object in an "on" clause`, t => {
const { sql, values } = q.select(`howdy`)
.from(`meh`)
.where(`a`, 1)
.join(`tag`, {
sql: `something_cool = FANCY(?, ?)`,
values: [ `pants`, `butts` ],
})
.where(`b`, 2)
.build()
t.equal(sql, `SELECT howdy\nFROM meh\nJOIN tag ON something_cool = FANCY(?, ?)\nWHERE a = ? AND b = ?`)
t.deepEqual(values, [ `pants`, `butts`, 1, 2 ])
t.end()
})
test(`Integration: passing a tagged template string result as an argument`, t => {
const { sql, values } = q.where(`tag`, q`FANCY(${ `pants` }, ${ `butts` })`).build()
t.equal(sql, `WHERE tag = FANCY(?, ?)`)
t.deepEqual(values, [ `pants`, `butts` ])
t.end()
})
test(`Passing str/params into every clause`, t => {
const assertLegit = (query, expectedStr) => {
const { sql, values } = query.build()
t.equal(sql, expectedStr, expectedStr)
t.deepEqual(values, [ 1, 2 ])
}
assertLegit(
q.select(q`FOO(${ 1 })`, q`BAR(${ 2 })`),
`SELECT FOO(?), BAR(?)`,
)
assertLegit(
q.join(`table`, q`FOO(${ 1 }) = BAR(${ 2 })`),
`JOIN table ON FOO(?) = BAR(?)`,
)
assertLegit(
q.leftJoin(`table`, q`FOO(${ 1 }) = BAR(${ 2 })`),
`LEFT JOIN table ON FOO(?) = BAR(?)`,
)
assertLegit(
q.where(q`FOO(${ 1 })`, q`BAR(${ 2 })`),
`WHERE FOO(?) = BAR(?)`,
)
assertLegit(
q.whereLike(q`FOO(${ 1 })`, q`BAR(${ 2 })`),
`WHERE FOO(?) LIKE BAR(?)`,
)
assertLegit(
q.having(q`FOO(${ 1 })`, q`BAR(${ 2 })`),
`HAVING FOO(?) = BAR(?)`,
)
assertLegit(
q.groupBy(q`FOO(${ 1 })`, q`BAR(${ 2 })`),
`GROUP BY FOO(?), BAR(?)`,
)
assertLegit(
q.orderBy(q`FOO(${ 1 })`, q`BAR(${ 2 })`),
`ORDER BY FOO(?), BAR(?)`,
)
t.end()
})
test(`sql/values are legit with mulitple clauses`, t => {
const result = q.select(`table1.some_boring_id, table2.something_interesting, mystery_table.surprise`, q`LEAST(table1.whatever, ?) AS whatever`)
.from(`table1`)
.join(`table2`, `table1.some_boring_id = table2.id`)
.leftJoin(`mystery_table`, `mystery_table.twister_reality = table2.probably_null_column`)
.where(`table1.pants`, `fancy`)
.where(`table1.britches`, `>`, 99)
.build()
t.equal(result.sql, [
`SELECT table1.some_boring_id, table2.something_interesting, mystery_table.surprise, LEAST(table1.whatever, ?) AS whatever`,
`FROM table1`,
`JOIN table2 ON table1.some_boring_id = table2.id`,
`LEFT JOIN mystery_table ON mystery_table.twister_reality = table2.probably_null_column`,
`WHERE table1.pants = ? AND table1.britches > ?`,
].join(`\n`))
t.equal(result.sql, [
`SELECT table1.some_boring_id, table2.something_interesting, mystery_table.surprise, LEAST(table1.whatever, ?) AS whatever`,
`FROM table1`,
`JOIN table2 ON table1.some_boring_id = table2.id`,
`LEFT JOIN mystery_table ON mystery_table.twister_reality = table2.probably_null_column`,
`WHERE table1.pants = ? AND table1.britches > ?`,
].join(`\n`))
t.deepEqual(result.values, [ `fancy`, 99 ])
t.deepEqual(result.values, [ `fancy`, 99 ])
t.end()
})
test(`custom comparator`, t => {
const input = `whatever`
const result = q.select(`myColumn`)
.from(`table1`)
.where(`MATCH(myColumn)`, ` `, q`AGAINST(${ input })`)
.build()
t.equal(result.sql, [
`SELECT myColumn`,
`FROM table1`,
`WHERE MATCH(myColumn) AGAINST(?)`,
].join(`\n`))
t.deepEqual(result.values, [ `whatever` ])
t.end()
})
test(`no where value`, t => {
const input = `whatever`
const result = q.select(`myColumn`)
.from(`table1`)
.where(q`MATCH(myColumn) AGAINST(${ input })`)
.where(`someOtherColumn = somethingUnrelated`)
.build()
t.equal(result.sql, [
`SELECT myColumn`,
`FROM table1`,
`WHERE MATCH(myColumn) AGAINST(?) AND someOtherColumn = somethingUnrelated`,
].join(`\n`))
t.deepEqual(result.values, [ `whatever` ])
t.end()
})
test(`Throws an error if you call whereLike without a value`, t => {
t.throws(() => {
q.whereLike(`nuthin`)
}, /like/i)
t.end()
})
test(`toString`, t => {
const result = q.select(`myColumn`)
.from(`table1`)
.where(`foo`, `!=`, `baz`)
.toString()
t.equal(result, `SELECT myColumn\nFROM table1\nWHERE foo != 'baz'`)
t.end()
})
test(`toString custom separator`, t => {
const result = q.select(`myColumn`)
.from(`table1`)
.where(`foo`, `!=`, `baz`)
.toString(` `)
t.equal(result, `SELECT myColumn FROM table1 WHERE foo != 'baz'`)
t.end()
})
test(`union + unionAll`, t => {
const query = q.select(`myColumn`)
.from(`table1`)
.where(`foo`, `!=`, `baz`)
.union(q.select(`wat`).from(`bar`).where(`biz`, true))
.unionAll(q.select(`huh`).from(`baz`).having(`wat`, `whatever`))
const buildResult = query.build()
t.equal(buildResult.sql, `SELECT myColumn
FROM table1
WHERE foo != ?
UNION
SELECT wat
FROM bar
WHERE biz = ?
UNION ALL
SELECT huh
FROM baz
HAVING wat = ?`)
t.deepEqual(buildResult.values, [ `baz`, true, `whatever` ])
t.equal(query.toString(), `SELECT myColumn
FROM table1
WHERE foo != 'baz'
UNION
SELECT wat
FROM bar
WHERE biz = true
UNION ALL
SELECT huh
FROM baz
HAVING wat = 'whatever'`)
t.equal(query.toString(`\n\n`), `SELECT myColumn
FROM table1
WHERE foo != 'baz'
UNION
SELECT wat
FROM bar
WHERE biz = true
UNION ALL
SELECT huh
FROM baz
HAVING wat = 'whatever'`)
t.end()
})
test(`union query as subquery`, t => {
const subquery = q.select(`1 AS wat`)
.from(`table1`)
.where(`foo`, false)
.unionAll(q.select(`2`).from(`bar`).where(`biz`, true))
const buildResult = q.select(`wat`).from(subquery, `meh`).build()
t.equal(buildResult.sql, `SELECT wat
FROM (
SELECT 1 AS wat
FROM table1
WHERE foo = ?
UNION ALL
SELECT 2
FROM bar
WHERE biz = ?
) AS meh`)
t.deepEqual(buildResult.values, [ false, true ])
t.end()
})