forked from elixir-sqlite/ecto_sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect_test.exs
More file actions
667 lines (538 loc) · 15.4 KB
/
select_test.exs
File metadata and controls
667 lines (538 loc) · 15.4 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
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
defmodule Ecto.Adapters.SQLite3.Connection.SelectTest do
use ExUnit.Case, async: true
import Ecto.Query
import Ecto.Adapters.SQLite3.TestHelpers
alias EctoSQLite3.Schemas.Schema
test "select" do
query =
Schema
|> select([r], {r.x, r.y})
|> plan()
assert ~s{SELECT s0."x", s0."y" FROM "schema" AS s0} == all(query)
query =
Schema
|> select([r], [r.x, r.y])
|> plan()
assert ~s{SELECT s0."x", s0."y" FROM "schema" AS s0} == all(query)
query =
Schema
|> select([r], struct(r, [:x, :y]))
|> plan()
assert ~s{SELECT s0."x", s0."y" FROM "schema" AS s0} == all(query)
end
describe "distinct" do
test "distinct true with binding" do
query =
Schema
|> distinct([r], true)
|> select([r], {r.x, r.y})
|> plan()
assert ~s{SELECT DISTINCT s0."x", s0."y" FROM "schema" AS s0} == all(query)
end
test "distinct false with binding" do
query =
Schema
|> distinct([r], false)
|> select([r], {r.x, r.y})
|> plan()
assert ~s{SELECT s0."x", s0."y" FROM "schema" AS s0} == all(query)
end
test "distinct true without binding" do
query =
Schema
|> distinct(true)
|> select([r], {r.x, r.y})
|> plan()
assert ~s{SELECT DISTINCT s0."x", s0."y" FROM "schema" AS s0} == all(query)
end
test "distinct false without binding" do
query =
Schema
|> distinct(false)
|> select([r], {r.x, r.y})
|> plan()
assert ~s{SELECT s0."x", s0."y" FROM "schema" AS s0} == all(query)
end
test "distinct with multiple columns is not supported" do
assert_raise Ecto.QueryError,
~r"DISTINCT with multiple columns is not supported by SQLite3",
fn ->
query =
Schema
|> distinct([r], [r.x, r.y])
|> select([r], {r.x, r.y})
|> plan()
all(query)
end
end
end
test "where" do
query =
Schema
|> where([r], r.x == 42)
|> where([r], r.y != 43)
|> select([r], r.x)
|> plan()
assert ~s{SELECT s0."x" FROM "schema" AS s0 } <>
~s{WHERE (s0."x" = 42) AND (s0."y" != 43)} == all(query)
query =
Schema
|> where([r], {r.x, r.y} > {1, 2})
|> select([r], r.x)
|> plan()
assert ~s{SELECT s0."x" FROM "schema" AS s0 } <>
~s{WHERE ((s0."x",s0."y") > (1,2))} == all(query)
end
test "or_where" do
query =
Schema
|> or_where([r], r.x == 42)
|> or_where([r], r.y != 43)
|> select([r], r.x)
|> plan()
assert ~s{SELECT s0."x" FROM "schema" AS s0 } <>
~s{WHERE (s0."x" = 42) OR (s0."y" != 43)} == all(query)
query =
Schema
|> or_where([r], r.x == 42)
|> or_where([r], r.y != 43)
|> where([r], r.z == 44)
|> select([r], r.x)
|> plan()
assert ~s{SELECT s0."x" FROM "schema" AS s0 } <>
~s{WHERE ((s0."x" = 42) OR (s0."y" != 43)) AND (s0."z" = 44)} == all(query)
end
describe "fragments" do
test "passing now for the fragment" do
query =
Schema
|> select([r], fragment("now"))
|> plan()
assert ~s{SELECT now FROM "schema" AS s0} == all(query)
end
test "passing function with table" do
query =
Schema
|> select([r], fragment("fun(?)", r))
|> plan()
assert ~s{SELECT fun(s0) FROM "schema" AS s0} == all(query)
end
test "passing function with column" do
query =
Schema
|> select([r], fragment("downcase(?)", r.x))
|> plan()
assert ~s{SELECT downcase(s0."x") FROM "schema" AS s0} == all(query)
end
test "collating with literal" do
query =
Schema
|> select([r], fragment("? COLLATE ?", r.x, literal(^"es_ES")))
|> plan()
assert ~s{SELECT s0."x" COLLATE "es_ES" FROM "schema" AS s0} == all(query)
end
test "collating with identifier" do
query =
Schema
|> select([r], fragment("? COLLATE ?", r.x, identifier(^"es_ES")))
|> plan()
assert ~s{SELECT s0."x" COLLATE "es_ES" FROM "schema" AS s0} == all(query)
end
test "fragment with a column and value specified" do
value = 13
query =
Schema
|> select([r], fragment("downcase(?, ?)", r.x, ^value))
|> plan()
assert ~s{SELECT downcase(s0."x", ?) FROM "schema" AS s0} == all(query)
end
test "does not support keywords in fragments" do
assert_raise Ecto.QueryError, fn ->
Schema
|> select([], fragment(title: 2))
|> plan()
|> all()
end
end
test "splicing" do
query =
Schema
|> select([r], r.x)
|> where([r], fragment("? in (?,?,?)", r.x, ^1, splice(^[2, 3, 4]), ^5))
|> plan()
assert all(query) ==
~s{SELECT s0."x" FROM "schema" AS s0 WHERE (s0."x" in (?,?,?,?,?))}
end
end
describe "literals" do
test "translates true to 1" do
query =
"schema"
|> where(foo: true)
|> select([], true)
|> plan()
assert ~s{SELECT 1 FROM "schema" AS s0 WHERE (s0."foo" = 1)} == all(query)
end
test "translates false to 0" do
query =
"schema"
|> where(foo: false)
|> select([], true)
|> plan()
assert ~s{SELECT 1 FROM "schema" AS s0 WHERE (s0."foo" = 0)} == all(query)
end
test "binds string" do
query =
"schema"
|> where(foo: "abc")
|> select([], true)
|> plan()
assert ~s{SELECT 1 FROM "schema" AS s0 WHERE (s0."foo" = 'abc')} == all(query)
end
test "puts binary as hex literal" do
query =
"schema"
|> where(foo: <<0, ?a, ?b, ?c>>)
|> select([], true)
|> plan()
assert ~s{SELECT 1 FROM "schema" AS s0 WHERE (s0."foo" = x'00616263')} ==
all(query)
end
test "puts integer" do
query =
"schema"
|> where(foo: 123)
|> select([], true)
|> plan()
assert ~s{SELECT 1 FROM "schema" AS s0 WHERE (s0."foo" = 123)} == all(query)
end
test "puts float as a REAL" do
query =
"schema"
|> where(foo: 123.0)
|> select([], true)
|> plan()
assert ~s{SELECT 1 FROM "schema" AS s0 WHERE (s0."foo" = CAST(123.0 AS REAL))} ==
all(query)
end
end
test "aliasing a selected value with selected_as/2" do
query =
"schema"
|> select([s], selected_as(s.x, :integer))
|> plan()
assert ~s{SELECT s0."x" AS "integer" FROM "schema" AS s0} == all(query)
query =
"schema"
|> select([s], s.x |> coalesce(0) |> sum() |> selected_as(:integer))
|> plan()
assert ~s{SELECT sum(coalesce(s0."x", 0)) AS "integer" FROM "schema" AS s0} ==
all(query)
end
test "is_nil" do
query =
Schema
|> select([r], is_nil(r.x))
|> plan()
assert ~s{SELECT s0."x" IS NULL FROM "schema" AS s0} == all(query)
end
test "not is_nil" do
query =
Schema
|> select([r], not is_nil(r.x))
|> plan()
assert ~s{SELECT NOT (s0."x" IS NULL) FROM "schema" AS s0} == all(query)
end
test "is_nil with comparison" do
query =
"schema"
|> select([r], r.x == is_nil(r.y))
|> plan()
assert ~s{SELECT s0."x" = (s0."y" IS NULL) FROM "schema" AS s0} == all(query)
end
describe "casting" do
test "as integer" do
query =
Schema
|> select([t], type(t.x + t.y, :integer))
|> plan()
assert ~s{SELECT CAST(s0."x" + s0."y" AS INTEGER) FROM "schema" AS s0} ==
all(query)
end
test "casting uuid" do
query =
Schema
|> select([], type(^"601d74e4-a8d3-4b6e-8365-eddb4c893327", Ecto.UUID))
|> plan()
assert ~s{SELECT CAST(? AS TEXT) FROM "schema" AS s0} == all(query)
end
test "casting array of uuids" do
query =
Schema
|> select(
[],
type(^["601d74e4-a8d3-4b6e-8365-eddb4c893327"], {:array, Ecto.UUID})
)
|> plan()
assert ~s{SELECT CAST(? AS TEXT) FROM "schema" AS s0} == all(query)
end
end
test "nested expressions" do
z = 123
query =
from(r in Schema, [])
|> select([r], (r.x > 0 and r.y > ^(-z)) or true)
|> plan()
assert ~s{SELECT ((s0."x" > 0) AND (s0."y" > ?)) OR 1 FROM "schema" AS s0} ==
all(query)
end
describe "in expression" do
test "empty array" do
query =
Schema
|> select([e], 1 in [])
|> plan()
assert ~s{SELECT 0 FROM "schema" AS s0} == all(query)
end
test "empty array binded" do
query =
Schema
|> select([e], 1 in ^[])
|> plan()
assert ~s{SELECT 0 FROM "schema" AS s0} == all(query)
end
test "array of integers" do
query =
Schema
|> select([e], 1 in ^[1, 2, 3])
|> plan()
assert ~s{SELECT 1 IN (?,?,?) FROM "schema" AS s0} == all(query)
end
test "array of integers with one binded" do
query =
Schema
|> select([e], 1 in [1, ^2, 3])
|> plan()
assert ~s{SELECT 1 IN (1,?,3) FROM "schema" AS s0} == all(query)
end
test "mixed select with `in`" do
query =
Schema
|> select([e], e.x == ^0 or e.x in ^[1, 2, 3] or e.x == ^4)
|> plan()
assert ~s{SELECT (} <>
~s{(s0."x" = ?) OR s0."x" IN (?,?,?)} <>
~s{) OR (s0."x" = ?) } <>
~s{FROM "schema" AS s0} == all(query)
end
test "json each" do
query =
Schema
|> select([e], e in [1, 2, 3])
|> plan()
assert all(query) ==
~s{SELECT s0 IN (SELECT value FROM JSON_EACH('[1,2,3]')) FROM "schema" AS s0}
end
test "in with column binding" do
query =
Schema
|> select([e], 1 in [1, e.x, 3])
|> plan()
assert all(query) == ~s{SELECT 1 IN (1,s0."x",3) FROM "schema" AS s0}
end
test "with value in binded in and out" do
query =
Schema
|> select([e], ^1 in [1, ^2, 3])
|> plan()
assert all(query) == ~s{SELECT ? IN (1,?,3) FROM "schema" AS s0}
end
end
test "in subquery" do
posts = subquery("posts" |> where(title: ^"hello") |> select([p], p.id))
query =
"comments"
|> where([c], c.post_id in subquery(posts))
|> select([c], c.x)
|> plan()
assert all(query) ==
~s{SELECT c0."x" FROM "comments" AS c0 } <>
~s{WHERE (c0."post_id" IN (SELECT sp0."id" FROM "posts" AS sp0 WHERE (sp0."title" = ?)))}
posts =
subquery(
"posts"
|> where(title: parent_as(:comment).subtitle)
|> select([p], p.id)
)
query =
"comments"
|> from(as: :comment)
|> where([c], c.post_id in subquery(posts))
|> select([c], c.x)
|> plan()
assert ~s{SELECT c0."x" FROM "comments" AS c0 } <>
~s{WHERE (c0."post_id" IN (} <>
~s{SELECT sp0."id" FROM "posts" AS sp0 WHERE (sp0."title" = c0."subtitle")} <>
~s{))} == all(query)
end
describe "arrays" do
test "array of integers fragment is not supported" do
assert_raise Ecto.QueryError, fn ->
Schema
|> select([], fragment("?", [1, 2, 3]))
|> plan()
|> all()
end
end
test "array of strings fragment is not supported" do
assert_raise Ecto.QueryError, fn ->
Schema
|> select([], fragment("?", ~w(abc def)))
|> plan()
|> all()
end
end
test "empty array is supported" do
query =
Schema
|> where([s], s.w == [])
|> select([s], s.w)
|> plan()
assert ~s{SELECT s0."w" FROM "schema" AS s0 WHERE (s0."w" = '[]')} == all(query)
end
end
test "fragments and types" do
query =
plan(
from(e in "schema",
where:
fragment(
"extract(? from ?) = ?",
^"month",
e.start_time,
type(^"4", :integer)
),
where:
fragment(
"extract(? from ?) = ?",
^"year",
e.start_time,
type(^"2015", :integer)
),
select: true
)
)
assert ~s{SELECT 1 FROM "schema" AS s0 } <>
~s{WHERE (extract(? from s0."start_time") = CAST(? AS INTEGER)) } <>
~s{AND (extract(? from s0."start_time") = CAST(? AS INTEGER))} ==
all(query)
end
test "fragments allow ? to be escaped with backslash" do
query =
plan(
from(e in "schema",
where: fragment("? = \"query\\?\"", e.start_time),
select: true
)
)
assert ~s{SELECT 1 FROM "schema" AS s0 } <>
~s{WHERE (s0."start_time" = "query?")} == all(query)
end
describe "binary operations" do
test "equals" do
query =
Schema
|> select([r], r.x == 2)
|> plan()
assert ~s{SELECT s0."x" = 2 FROM "schema" AS s0} == all(query)
end
test "does not equal" do
query =
Schema
|> select([r], r.x != 2)
|> plan()
assert ~s{SELECT s0."x" != 2 FROM "schema" AS s0} == all(query)
end
test "lte" do
query =
Schema
|> select([r], r.x <= 2)
|> plan()
assert ~s{SELECT s0."x" <= 2 FROM "schema" AS s0} == all(query)
end
test "gte" do
query =
Schema
|> select([r], r.x >= 2)
|> plan()
assert ~s{SELECT s0."x" >= 2 FROM "schema" AS s0} == all(query)
end
test "lt" do
query =
Schema
|> select([r], r.x < 2)
|> plan()
assert ~s{SELECT s0."x" < 2 FROM "schema" AS s0} == all(query)
end
test "gt" do
query =
Schema
|> select([r], r.x > 2)
|> plan()
assert ~s{SELECT s0."x" > 2 FROM "schema" AS s0} == all(query)
end
test "add" do
query =
Schema
|> select([r], r.x + 2)
|> plan()
assert ~s{SELECT s0."x" + 2 FROM "schema" AS s0} == all(query)
end
test "subtract" do
query =
Schema
|> select([r], r.x - 2)
|> plan()
assert ~s{SELECT s0."x" - 2 FROM "schema" AS s0} == all(query)
end
test "multiply" do
query =
Schema
|> select([r], r.x * 2)
|> plan()
assert ~s{SELECT s0."x" * 2 FROM "schema" AS s0} == all(query)
end
test "divide" do
query =
Schema
|> select([r], r.x / 2)
|> plan()
assert ~s{SELECT s0."x" / 2 FROM "schema" AS s0} == all(query)
end
end
test "limit specified" do
query =
Schema
|> limit([r], 3)
|> select([], true)
|> plan()
assert ~s{SELECT 1 FROM "schema" AS s0 LIMIT 3} == all(query)
end
test "offset specified" do
query =
Schema
|> offset([r], 5)
|> select([], true)
|> plan()
assert ~s{SELECT 1 FROM "schema" AS s0 OFFSET 5} == all(query)
end
test "limit and offset specified" do
query =
Schema
|> offset([r], 5)
|> limit([r], 3)
|> select([], true)
|> plan()
assert ~s{SELECT 1 FROM "schema" AS s0 LIMIT 3 OFFSET 5} == all(query)
end
end