-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathquery_test.exs
More file actions
2145 lines (1778 loc) · 75.7 KB
/
query_test.exs
File metadata and controls
2145 lines (1778 loc) · 75.7 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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
defmodule QueryTest do
use ExUnit.Case, async: true
import Postgrex.TestHelper
import ExUnit.CaptureLog
alias Postgrex, as: P
Postgrex.Types.define(Postgrex.ElixirDurationTypes, [],
interval_decode_type: Duration,
allow_infinite_intervals: true
)
setup context do
opts = [
database: "postgrex_test",
backoff_type: :stop,
prepare: context[:prepare] || :named,
max_restarts: 0
]
{:ok, pid} = P.start_link(opts)
{:ok, [pid: pid, options: opts]}
end
test "iodata", context do
assert [[123]] = query(["S", ?E, ["LEC" | "T"], " ", ~c"123"], [])
end
test "decode basic types", context do
assert [[nil]] = query("SELECT NULL", [])
assert [[true, false]] = query("SELECT true, false", [])
assert [["e"]] = query("SELECT 'e'::char", [])
assert [["ẽ"]] = query("SELECT 'ẽ'::char", [])
assert [[42]] = query("SELECT 42", [])
assert [[42.0]] = query("SELECT 42::float", [])
assert [[:NaN]] = query("SELECT 'NaN'::float", [])
assert [[:inf]] = query("SELECT 'inf'::float", [])
assert [[:"-inf"]] = query("SELECT '-inf'::float", [])
assert [["ẽric"]] = query("SELECT 'ẽric'", [])
assert [["ẽric"]] = query("SELECT 'ẽric'::varchar", [])
assert [[<<1, 2, 3>>]] = query("SELECT '\\001\\002\\003'::bytea", [])
end
test "decode numeric", context do
assert [[Decimal.new("42")]] == query("SELECT 42::numeric", [])
assert [[Decimal.new("42.0000000000")]] == query("SELECT 42.0::numeric(100, 10)", [])
assert [[Decimal.new("1.001")]] == query("SELECT 1.001", [])
assert [[Decimal.new("0.4242")]] == query("SELECT 0.4242", [])
assert [[Decimal.new("42.4242")]] == query("SELECT 42.4242", [])
assert [[Decimal.new("12345.12345")]] == query("SELECT 12345.12345", [])
assert [[Decimal.new("0.00012345")]] == query("SELECT 0.00012345", [])
assert [[Decimal.new("1000000000.0")]] == query("SELECT 1000000000.0", [])
assert [[Decimal.new("1000000000.1")]] == query("SELECT 1000000000.1", [])
assert [[Decimal.new("123456789123456789123456789")]] ==
query("SELECT 123456789123456789123456789::numeric", [])
assert [[Decimal.new("123456789123456789123456789.123456789")]] ==
query("SELECT 123456789123456789123456789.123456789", [])
assert [[Decimal.new("1.1234500000")]] == query("SELECT 1.1234500000", [])
assert [[Decimal.new("NaN")]] == query("SELECT 'NaN'::numeric", [])
end
@tag min_pg_version: "10.0"
test "decode lsn", context do
assert [[int, text]] = query("SELECT pg_current_wal_lsn(), pg_current_wal_lsn()::text", [])
assert Postgrex.ReplicationConnection.decode_lsn(text) == {:ok, int}
end
@tag min_pg_version: "14.0"
test "decode numeric infinity", context do
assert [[Decimal.new("Inf")]] == query("SELECT NUMERIC 'Infinity'", [])
assert [[Decimal.new("-Inf")]] == query("SELECT NUMERIC '-Infinity'", [])
end
@tag min_pg_version: "9.5"
test "decode json/jsonb", context do
assert [[%{"foo" => 42}]] == query("SELECT '{\"foo\": 42}'::json", [])
assert [[%{"foo" => 42}]] == query("SELECT '{\"foo\": 42}'::jsonb", [])
end
test "decode uuid", context do
uuid = <<160, 238, 188, 153, 156, 11, 78, 248, 187, 109, 107, 185, 189, 56, 10, 17>>
assert [[^uuid]] = query("SELECT 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11'::uuid", [])
end
test "decode arrays", context do
assert [[[]]] = query("SELECT ARRAY[]::integer[]", [])
assert [[[1]]] = query("SELECT ARRAY[1]", [])
assert [[[1, 2]]] = query("SELECT ARRAY[1,2]", [])
assert [[[[0], [1]]]] = query("SELECT ARRAY[[0],[1]]", [])
assert [[[[0]]]] = query("SELECT ARRAY[ARRAY[0]]", [])
end
test "decode array domain", context do
assert [[[1.0, 2.0, 3.0]]] = query("SELECT ARRAY[1, 2, 3]::floats_domain", [])
assert [
[
[
%Postgrex.Point{x: 1.0, y: 1.0},
%Postgrex.Point{x: 2.0, y: 2.0},
%Postgrex.Point{x: 3.0, y: 3.0}
]
]
] = query("SELECT ARRAY[point '1,1', point '2,2', point '3,3']::points_domain", [])
end
test "encode array domain", context do
floats = [1.0, 2.0, 3.0]
floats_string = "{1,2,3}"
assert [[^floats_string]] = query("SELECT $1::floats_domain::text", [floats])
points = [
%Postgrex.Point{x: 1.0, y: 1.0},
%Postgrex.Point{x: 2.0, y: 2.0},
%Postgrex.Point{x: 3.0, y: 3.0}
]
points_string = "{\"(1,1)\",\"(2,2)\",\"(3,3)\"}"
assert [[^points_string]] = query("SELECT $1::points_domain::text", [points])
end
test "decode interval", context do
assert [[%Postgrex.Interval{months: 0, days: 0, secs: 0, microsecs: 0}]] =
query("SELECT interval '0'", [])
assert [[%Postgrex.Interval{months: 100, days: 0, secs: 0, microsecs: 0}]] =
query("SELECT interval '100 months'", [])
assert [[%Postgrex.Interval{months: 0, days: 100, secs: 0, microsecs: 0}]] =
query("SELECT interval '100 days'", [])
assert [[%Postgrex.Interval{months: 0, days: 0, secs: 100, microsecs: 0}]] =
query("SELECT interval '100 secs'", [])
assert [[%Postgrex.Interval{months: 14, days: 40, secs: 10920, microsecs: 0}]] =
query("SELECT interval '1 year 2 months 40 days 3 hours 2 minutes'", [])
assert [[%Postgrex.Interval{months: 0, days: 0, secs: 53, microsecs: 204_800}]] =
query("SELECT interval '53 secs 204800 microseconds'", [])
assert [[%Postgrex.Interval{months: 0, days: 0, secs: 10, microsecs: 240_000}]] =
query("SELECT interval '10240000 microseconds'", [])
end
@tag min_pg_version: "17.0"
test "decode infinite interval" do
opts = [database: "postgrex_test", backoff_type: :stop, types: Postgrex.ElixirDurationTypes]
{:ok, pid} = P.start_link(opts)
assert P.query!(pid, "SELECT 'infinity'::interval", []).rows == [[:inf]]
assert P.query!(pid, "SELECT '-infinity'::interval", []).rows == [[:"-inf"]]
end
@tag min_pg_version: "17.0"
test "decode infinite interval raise when option not specified" do
opts = [database: "postgrex_test", backoff_type: :stop]
{:ok, pid} = P.start_link(opts)
assert_raise ArgumentError, ~r/got "infinity" from PostgreSQL/, fn ->
P.query(pid, "SELECT 'infinity'::interval", [])
end
assert_raise ArgumentError, ~r/got "-infinity" from PostgreSQL/, fn ->
P.query(pid, "SELECT '-infinity'::interval", [])
end
end
if Version.match?(System.version(), ">= 1.17.0") do
test "decode interval with Elixir Duration" do
opts = [database: "postgrex_test", backoff_type: :stop, types: Postgrex.ElixirDurationTypes]
{:ok, pid} = P.start_link(opts)
assert [[%Duration{microsecond: {0, 6}}]] =
P.query!(pid, "SELECT interval '0'", []).rows
assert [[%Duration{month: 1200, microsecond: {0, 6}}]] =
P.query!(pid, "SELECT interval '100 years'", []).rows
assert [[%Duration{month: 10, microsecond: {0, 6}}]] =
P.query!(pid, "SELECT interval '10 months'", []).rows
assert [[%Duration{day: 700, microsecond: {0, 6}}]] =
P.query!(pid, "SELECT interval '100 weeks'", []).rows
assert [[%Duration{day: 5, microsecond: {0, 6}}]] =
P.query!(pid, "SELECT interval '5 days'", []).rows
assert [[%Duration{second: 360_000, microsecond: {0, 6}}]] =
P.query!(pid, "SELECT interval '100 hours'", []).rows
assert [[%Duration{second: 600, microsecond: {0, 6}}]] =
P.query!(pid, "SELECT interval '10 minutes'", []).rows
assert [[%Duration{second: 10, microsecond: {0, 6}}]] =
P.query!(pid, "SELECT interval '10 secs'", []).rows
assert [
[
%Duration{
month: 14,
day: 40,
second: 10921,
microsecond: {0, 6}
}
]
] =
P.query!(
pid,
"SELECT interval '1 year 2 months 40 days 3 hours 2 minutes 1 seconds'",
[]
).rows
assert [[%Duration{second: 53, microsecond: {204_800, 6}}]] =
P.query!(pid, "SELECT interval '53 secs 204800 microseconds'", []).rows
assert [[%Duration{second: 10, microsecond: {240_000, 6}}]] =
P.query!(pid, "SELECT interval '10240000 microseconds'", []).rows
assert [[[%Duration{second: 10, microsecond: {240_000, 6}}]]] =
P.query!(pid, "SELECT ARRAY[interval '10240000 microseconds']", []).rows
end
test "decode interval with Elixir Duration: precision is given" do
opts = [database: "postgrex_test", backoff_type: :stop, types: Postgrex.ElixirDurationTypes]
{:ok, pid} = P.start_link(opts)
assert [[%Duration{second: 10, microsecond: {240_000, 2}}]] =
P.query!(pid, "SELECT interval(2) '10240000 microseconds'", []).rows
assert [[[%Duration{second: 10, microsecond: {0, 0}}]]] =
P.query!(pid, "SELECT ARRAY[interval(0) '10240000 microseconds']", []).rows
end
test "decode interval with Elixir Duration: field is given but not precision" do
opts = [database: "postgrex_test", backoff_type: :stop, types: Postgrex.ElixirDurationTypes]
{:ok, pid} = P.start_link(opts)
assert [[%Duration{day: 10, microsecond: {0, 6}}]] =
P.query!(pid, "SELECT interval '10' DAY", []).rows
assert [[[%Duration{day: 10, microsecond: {0, 6}}]]] =
P.query!(pid, "SELECT ARRAY[interval '10' DAY]", []).rows
end
end
test "decode point", context do
assert [[%Postgrex.Point{x: -97.5, y: 100.1}]] ==
query("SELECT point(-97.5, 100.1)::point", [])
end
test "encode point", context do
assert [[%Postgrex.Point{x: -97.0, y: 100.0}]] ==
query("SELECT $1::point", [%Postgrex.Point{x: -97, y: 100}])
end
test "decode polygon", context do
p1 = %Postgrex.Point{x: 100.0, y: 101.5}
p2 = %Postgrex.Point{x: 100.0, y: -99.1}
p3 = %Postgrex.Point{x: -91.1, y: -101.1}
p4 = %Postgrex.Point{x: -100.0, y: 99.9}
polygon = %Postgrex.Polygon{vertices: [p1, p2, p3, p4]}
polystring = "((100.0,101.5),(100.0,-99.1),(-91.1,-101.1),(-100.0,99.9))"
assert [[polygon]] == query("SELECT '#{polystring}'" <> "::polygon", [])
end
test "encode polygon", context do
p1 = %Postgrex.Point{x: 100.0, y: 101.5}
p2 = %Postgrex.Point{x: 100.0, y: -99.1}
p3 = %Postgrex.Point{x: -91.1, y: -101.1}
p4 = %Postgrex.Point{x: -100.0, y: 99.9}
polygon = %Postgrex.Polygon{vertices: [p1, p2, p3, p4]}
assert [[polygon]] == query("SELECT $1::polygon", [polygon])
assert %DBConnection.EncodeError{} = catch_error(query("SELECT $1::polygon", [1]))
bad_polygon = %Postgrex.Polygon{vertices: ["x"]}
assert %DBConnection.EncodeError{} = catch_error(query("SELECT $1::polygon", [bad_polygon]))
end
@tag min_pg_version: "9.4"
test "decode line", context do
# 98.6x - y = 0 <=> y = 98.6x
line = %Postgrex.Line{a: 98.6, b: -1.0, c: 0.0}
assert [[line]] == query("SELECT '{98.6,-1.0,0.0}'::line", [])
assert [[line]] == query("SELECT '(0.0,0.0),(1.0,98.6)'::line", [])
end
@tag min_pg_version: "9.4"
test "encode line", context do
# 98.6x - y = 0 <=> y = 98.6x
line = %Postgrex.Line{a: 98.6, b: -1.0, c: 0.0}
assert [[line]] == query("SELECT $1::line", [line])
assert %DBConnection.EncodeError{} = catch_error(query("SELECT $1::line", ["foo"]))
bad_line = %Postgrex.Line{a: nil, b: "foo"}
assert %DBConnection.EncodeError{} = catch_error(query("SELECT $1::line", [bad_line]))
end
test "decode line segment", context do
segment = %Postgrex.LineSegment{
point1: %Postgrex.Point{x: 0.0, y: 0.0},
point2: %Postgrex.Point{x: 1.0, y: 1.0}
}
assert [[segment]] == query("SELECT '(0.0,0.0)(1.0,1.0)'::lseg", [])
end
test "encode line segment", context do
segment = %Postgrex.LineSegment{
point1: %Postgrex.Point{x: 0.0, y: 0.0},
point2: %Postgrex.Point{x: 1.0, y: 1.0}
}
assert [[segment]] == query("SELECT $1::lseg", [segment])
assert %DBConnection.EncodeError{} = catch_error(query("SELECT $1::lseg", [1.0]))
assert %DBConnection.EncodeError{} =
catch_error(query("SELECT $1::lseg", [%Postgrex.LineSegment{}]))
end
test "decode box", context do
box = %Postgrex.Box{
upper_right: %Postgrex.Point{x: 1.0, y: 1.0},
bottom_left: %Postgrex.Point{x: 0.0, y: 0.0}
}
# postgres automatically sorts the points so that we get UR/BL
assert [[box]] == query("SELECT '(0.0,0.0)(1.0,1.0)'::box", [])
assert [[box]] == query("SELECT '(1.0,1.0)(0.0,0.0)'::box", [])
assert [[box]] == query("SELECT '(1.0,0.0)(0.0,1.0)'::box", [])
assert [[box]] == query("SELECT '(0.0,1.0)(1.0,0.0)'::box", [])
end
test "encode box", context do
box = %Postgrex.Box{
upper_right: %Postgrex.Point{x: 1.0, y: 1.0},
bottom_left: %Postgrex.Point{x: 0.0, y: 0.0}
}
assert [[box]] == query("SELECT $1::box", [box])
assert %DBConnection.EncodeError{} = catch_error(query("SELECT $1::box", [1.0]))
assert %DBConnection.EncodeError{} = catch_error(query("SELECT $1::box", [%Postgrex.Box{}]))
end
test "decode path", context do
p1 = %Postgrex.Point{x: 0.0, y: 0.0}
p2 = %Postgrex.Point{x: 1.0, y: 3.0}
p3 = %Postgrex.Point{x: -4.0, y: 3.14}
path = %Postgrex.Path{points: [p1, p2, p3], open: true}
assert [[path]] == query("SELECT '[(0.0,0.0),(1.0,3.0),(-4.0,3.14)]'::path", [])
assert [[%{path | open: false}]] ==
query("SELECT '((0.0,0.0),(1.0,3.0),(-4.0,3.14))'::path", [])
assert %DBConnection.EncodeError{} = catch_error(query("SELECT $1::path", [1.0]))
bad_path = %Postgrex.Path{points: "foo", open: false}
assert %DBConnection.EncodeError{} = catch_error(query("SELECT $1::path", [bad_path]))
# open must be true/false
bad_path = %Postgrex.Path{points: []}
assert %DBConnection.EncodeError{} = catch_error(query("SELECT $1::path", [bad_path]))
end
test "encode path", context do
p1 = %Postgrex.Point{x: 0.0, y: 0.0}
p2 = %Postgrex.Point{x: 1.0, y: 3.0}
p3 = %Postgrex.Point{x: -4.0, y: 3.14}
path = %Postgrex.Path{points: [p1, p2, p3], open: false}
assert [[path]] == query("SELECT $1::path", [path])
end
test "decode circle", context do
center = %Postgrex.Point{x: 1.0, y: -3.5}
circle = %Postgrex.Circle{center: center, radius: 100.0}
assert [[circle]] == query("SELECT '<(1.0,-3.5),100.0>'::circle", [])
end
test "encode circle", context do
center = %Postgrex.Point{x: 1.0, y: -3.5}
circle = %Postgrex.Circle{center: center, radius: 100.0}
assert [[circle]] == query("SELECT $1::circle", [circle])
assert %DBConnection.EncodeError{} = catch_error(query("SELECT $1::path", ["snu"]))
bad_circle = %Postgrex.Circle{center: 1.5, radius: 1.0}
assert %DBConnection.EncodeError{} = catch_error(query("SELECT $1::path", [bad_circle]))
bad_circle = %Postgrex.Circle{center: %Postgrex.Point{x: 1.0, y: 0.0}, radius: "five"}
assert %DBConnection.EncodeError{} = catch_error(query("SELECT $1::path", [bad_circle]))
end
test "decode name", context do
assert [["test"]] == query("SELECT 'test'::name", [])
end
test "encode name", context do
assert [["test"]] == query("SELECT $1::name", ["test"])
end
test "decode \"char\"", context do
assert [["X"]] == query("SELECT 'X'::\"char\"", [])
end
test "encode \"char\"", context do
assert [["x"]] == query("SELECT $1::\"char\"", ["x"])
end
@tag :capture_log
test "decode record", context do
assert [[{1, "2"}]] = query("SELECT (1, '2')::composite1", [])
assert [[[{1, "2"}]]] = query("SELECT ARRAY[(1, '2')::composite1]", [])
end
test "decode enum", context do
assert [["elixir"]] = query("SELECT 'elixir'::enum1", [])
end
@tag min_pg_version: "9.2"
test "decode range", context do
# These do not appear to match what is selected, but that's because
# PostgreSQL itself returns range values this way.
# `SELECT '(1,5)'::int4range` returns `[2,5)`.
assert [[%Postgrex.Range{lower: 2, upper: 5, lower_inclusive: true, upper_inclusive: false}]] =
query("SELECT '(1,5)'::int4range", [])
assert [[%Postgrex.Range{lower: 1, upper: 7, lower_inclusive: true, upper_inclusive: false}]] =
query("SELECT '[1,6]'::int4range", [])
assert [
[
%Postgrex.Range{
lower: :unbound,
upper: 5,
lower_inclusive: false,
upper_inclusive: false
}
]
] = query("SELECT '(,5)'::int4range", [])
assert [
[
%Postgrex.Range{
lower: 1,
upper: :unbound,
lower_inclusive: true,
upper_inclusive: false
}
]
] = query("SELECT '[1,)'::int4range", [])
assert [
[
%Postgrex.Range{
lower: :empty,
upper: :empty,
lower_inclusive: false,
upper_inclusive: false
}
]
] = query("SELECT '(1,1)'::int4range", [])
assert [[%Postgrex.Range{lower: 1, upper: 2, lower_inclusive: true, upper_inclusive: false}]] =
query("SELECT '[1,1]'::int4range", [])
assert [
[
%Postgrex.Range{
lower: :unbound,
upper: :unbound,
lower_inclusive: false,
upper_inclusive: false
}
]
] = query("SELECT '(,)'::int4range", [])
assert [
[
%Postgrex.Range{
lower: :unbound,
upper: :unbound,
lower_inclusive: false,
upper_inclusive: false
}
]
] = query("SELECT '[,]'::int4range", [])
assert [[%Postgrex.Range{lower: 3, upper: 8, lower_inclusive: true, upper_inclusive: false}]] =
query("SELECT '(2,8)'::int8range", [])
assert [
[
%Postgrex.Range{
lower: Decimal.new("1.2"),
upper: Decimal.new("3.4"),
lower_inclusive: false,
upper_inclusive: false
}
]
] ==
query("SELECT '(1.2,3.4)'::numrange", [])
assert [
[
%Postgrex.Range{
lower: %Date{year: 2014, month: 1, day: 1},
upper: %Date{year: 2014, month: 12, day: 31}
}
]
] = query("SELECT '[2014-1-1,2014-12-31)'::daterange", [])
assert [[%Postgrex.Range{lower: :unbound, upper: %Date{year: 2014, month: 12, day: 31}}]] =
query("SELECT '(,2014-12-31)'::daterange", [])
assert [[%Postgrex.Range{lower: %Date{year: 2014, month: 1, day: 2}, upper: :unbound}]] =
query("SELECT '(2014-1-1,]'::daterange", [])
assert [
[
%Postgrex.Range{
lower: :unbound,
upper: :unbound,
lower_inclusive: false,
upper_inclusive: false
}
]
] = query("SELECT '(,)'::daterange", [])
assert [
[
%Postgrex.Range{
lower: :unbound,
upper: :unbound,
lower_inclusive: false,
upper_inclusive: false
}
]
] = query("SELECT '[,]'::daterange", [])
assert [
[
%Postgrex.Range{
lower: %NaiveDateTime{
year: 2014,
month: 1,
day: 1,
hour: 0,
minute: 0,
second: 0
},
upper: %NaiveDateTime{
year: 2014,
month: 12,
day: 31,
hour: 0,
minute: 0,
second: 0
}
}
]
] = query("SELECT '[2014-1-1,2014-12-31)'::tsrange", [])
assert [
[
[
%Postgrex.Range{
lower: ~N[2014-01-01 00:00:00.000000],
upper: ~N[2014-12-31 00:00:00.000000],
lower_inclusive: true,
upper_inclusive: false
},
%Postgrex.Range{
lower: ~N[2014-01-01 00:00:00.000000],
upper: ~N[2014-12-31 00:00:00.000000],
lower_inclusive: true,
upper_inclusive: false
}
]
]
]
query("SELECT ARRAY['[2014-1-1,2014-12-31)'::tsrange, '[2014-1-1,2014-12-31)'::tsrange]", [])
end
@tag min_pg_version: "14.0"
test "decode multirange", context do
# empty ranges
empty_multirange = %Postgrex.Multirange{ranges: []}
assert [[empty_multirange]] == query("SELECT '{}'::int4multirange", [])
# Postgres will normalize discrete ranges so they are lower inclusive and upper exclusive
expected_int_multirange = %Postgrex.Multirange{
ranges: [
%Postgrex.Range{lower: 3, upper: 5, lower_inclusive: true, upper_inclusive: false},
%Postgrex.Range{lower: 14, upper: 28, lower_inclusive: true, upper_inclusive: false}
]
}
assert [[expected_int_multirange]] ==
query("SELECT '{(2,5), [14, 27]}'::int4multirange", [])
expected_date_multirange = %Postgrex.Multirange{
ranges: [
%Postgrex.Range{
lower: %Date{year: 2014, month: 1, day: 2},
upper: %Date{year: 2014, month: 1, day: 12},
lower_inclusive: true,
upper_inclusive: false
},
%Postgrex.Range{
lower: %Date{year: 2015, month: 1, day: 1},
upper: %Date{year: 2015, month: 1, day: 10},
lower_inclusive: true,
upper_inclusive: false
}
]
}
assert [[expected_date_multirange]] ==
query(
"SELECT '{(2014-01-01, 2014-01-11], [2015-01-01, 2015-01-10)}'::datemultirange",
[]
)
# Contiuous ranges can't be normalized
expected_num_multirange = %Postgrex.Multirange{
ranges: [
%Postgrex.Range{
lower: Decimal.new("1.1"),
upper: Decimal.new("3.3"),
lower_inclusive: false,
upper_inclusive: false
},
%Postgrex.Range{
lower: Decimal.new("4.4"),
upper: Decimal.new("6.6"),
lower_inclusive: true,
upper_inclusive: true
}
]
}
assert [[expected_num_multirange]] ==
query("SELECT '{(1.1, 3.3), [4.4, 6.6]}'::nummultirange", [])
end
@tag min_pg_version: "9.0"
test "decode network types", context do
assert [[%Postgrex.INET{address: {127, 0, 0, 1}, netmask: nil}]] =
query("SELECT '127.0.0.1'::inet", [])
assert [[%Postgrex.INET{address: {127, 0, 0, 1}, netmask: nil}]] =
query("SELECT '127.0.0.1/32'::inet", [])
assert [[%Postgrex.INET{address: {127, 0, 0, 1}, netmask: 32}]] =
query("SELECT '127.0.0.1/32'::inet::cidr", [])
assert [[%Postgrex.INET{address: {127, 0, 0, 1}, netmask: 32}]] =
query("SELECT '127.0.0.1/32'::cidr", [])
assert [[%Postgrex.INET{address: {127, 0, 0, 1}, netmask: 4}]] =
query("SELECT '127.0.0.1/4'::inet", [])
assert [[%Postgrex.INET{address: {112, 0, 0, 0}, netmask: 4}]] =
query("SELECT '127.0.0.1/4'::inet::cidr", [])
assert %Postgrex.Error{
postgres: %{
code: :invalid_text_representation,
detail: "Value has bits set to right of mask.",
message: "invalid cidr value: \"127.0.0.1/4\""
},
query: "SELECT '127.0.0.1/4'::cidr"
} = query("SELECT '127.0.0.1/4'::cidr", [])
assert [[%Postgrex.INET{address: {112, 0, 0, 0}, netmask: 4}]] =
query("SELECT '112.0.0.0/4'::cidr", [])
assert [[%Postgrex.INET{address: {0, 0, 0, 0, 0, 0, 0, 1}, netmask: nil}]] =
query("SELECT '::1'::inet", [])
assert [[%Postgrex.INET{address: {0, 0, 0, 0, 0, 0, 0, 1}, netmask: nil}]] =
query("SELECT '::1/128'::inet", [])
assert [[%Postgrex.INET{address: {0, 0, 0, 0, 0, 0, 0, 1}, netmask: 128}]] =
query("SELECT '::1/128'::inet::cidr", [])
assert [[%Postgrex.INET{address: {8193, 43981, 0, 0, 0, 0, 0, 0}, netmask: 8}]] =
query("SELECT '2001:abcd::/8'::inet", [])
assert [[%Postgrex.INET{address: {8192, 0, 0, 0, 0, 0, 0, 0}, netmask: 8}]] =
query("SELECT '2001:abcd::/8'::inet::cidr", [])
assert %Postgrex.Error{
postgres: %{
code: :invalid_text_representation,
detail: "Value has bits set to right of mask.",
message: "invalid cidr value: \"2001:abcd::/8\""
},
query: "SELECT '2001:abcd::/8'::cidr"
} = query("SELECT '2001:abcd::/8'::cidr", [])
assert [[%Postgrex.INET{address: {8192, 0, 0, 0, 0, 0, 0, 0}, netmask: 8}]] =
query("SELECT '2000::/8'::cidr", [])
assert [[%Postgrex.MACADDR{address: {8, 1, 43, 5, 7, 9}}]] =
query("SELECT '08:01:2b:05:07:09'::macaddr", [])
end
test "decode oid and its aliases", context do
assert [[4_294_967_295]] = query("select 4294967295::oid;", [])
assert [["-"]] = query("select '-'::regproc::text;", [])
assert [["sum(integer)"]] = query("select 'sum(int4)'::regprocedure::text;", [])
assert [["||/"]] = query("select 'pg_catalog.||/'::regoper::text;", [])
assert [["+(integer,integer)"]] = query("select '+(integer,integer)'::regoperator::text;", [])
assert [["pg_type"]] = query("select 'pg_type'::regclass::text;", [])
assert [["english"]] = query("select 'english'::regconfig::text;", [])
assert [["integer"]] = query("select 'int4'::regtype::text;", [])
assert [[0]] = query("select '-'::regproc;", [])
assert [[44]] = query("select 'regprocin'::regproc;", [])
assert [[2108]] = query("select 'sum(int4)'::regprocedure;", [])
assert [[597]] = query("select 'pg_catalog.||/'::regoper;", [])
assert [[551]] = query("select '+(integer,integer)'::regoperator;", [])
assert [[1247]] = query("select 'pg_type'::regclass;", [])
[[regconfig_oid]] = query("select 'english'::regconfig;", [])
assert is_integer(regconfig_oid)
assert [[23]] = query("select 'int4'::regtype;", [])
# xid type
assert [[xmin, xmax]] = query("select xmin, xmax from pg_type limit 1;", [])
assert is_number(xmin) and is_number(xmax)
# cid type
assert [[cmin, cmax]] = query("select cmin, cmax from pg_type limit 1;", [])
assert is_number(cmin) and is_number(cmax)
end
@tag min_pg_version: "9.0"
test "hstore copies binaries by default", context do
# For OTP 20+ refc binaries up to 64 bytes might be copied during a GC
text = String.duplicate("hello world", 6)
assert [[bin]] = query("SELECT $1::text", [text])
assert :binary.referenced_byte_size(bin) == byte_size(text)
assert [[%{"hello" => value}]] = query("SELECT $1::hstore", [%{"hello" => text}])
assert :binary.referenced_byte_size(value) == byte_size(text)
end
test "decode bit string", context do
assert [[<<1::1, 0::1, 1::1>>]] == query("SELECT bit '101'", [])
assert [[<<1::1, 1::1, 0::1>>]] == query("SELECT bit '110'", [])
assert [[<<1::1, 1::1, 0::1>>]] == query("SELECT bit '110' :: varbit", [])
assert [[<<1::1, 0::1, 1::1, 1::1, 0::1>>]] == query("SELECT bit '10110'", [])
assert [[<<1::1, 0::1, 1::1, 0::1, 0::1>>]] ==
query("SELECT bit '101' :: bit(5)", [])
assert [[<<1::1, 0::1, 0::1, 0::1, 0::1, 0::1, 0::1, 0::1, 1::1, 0::1, 1::1>>]] ==
query("SELECT bit '10000000101'", [])
assert [
[
<<0::1, 0::1, 0::1, 0::1, 0::1, 0::1, 0::1, 0::1, 0::1, 0::1, 0::1, 0::1, 0::1,
0::1, 0::1, 0::1, 1::1, 0::1, 1::1>>
]
] ==
query("SELECT bit '0000000000000000101'", [])
assert [
[
<<1::1, 0::1, 0::1, 0::1, 0::1, 0::1, 0::1, 0::1, 0::1, 0::1, 0::1, 0::1, 0::1,
0::1, 0::1, 0::1, 1::1, 0::1, 1::1>>
]
] ==
query("SELECT bit '1000000000000000101'", [])
assert [
[
<<1::1, 0::1, 0::1, 0::1, 0::1, 0::1, 0::1, 1::1, 1::1, 0::1, 0::1, 0::1, 0::1,
0::1, 0::1, 0::1, 1::1, 0::1, 1::1>>
]
] ==
query("SELECT bit '1000000110000000101'", [])
end
@tag min_pg_version: "13.0"
test "decode lquery", context do
lquery = "*.path1.*"
assert [[lquery]] == query("SELECT '#{lquery}'::lquery", [])
end
@tag min_pg_version: "13.0"
test "decode ltree", context do
ltree = "this.is.a.path"
assert [[ltree]] == query("SELECT '#{ltree}'::ltree", [])
end
@tag min_pg_version: "13.0"
test "decode ltxtquery", context do
ltxtquery = "Europe% & Russia@* & !( Transportation & Test )"
assert [[ltxtquery]] == query("SELECT '#{ltxtquery}'::ltxtquery", [])
end
test "encode oid and its aliases", context do
# oid's range is 0 to 4294967295
assert [[0]] = query("select $1::oid;", [0])
assert [[4_294_967_295]] = query("select $1::oid;", [4_294_967_295])
assert %DBConnection.EncodeError{} = catch_error(query("SELECT $1::oid", [0 - 1]))
assert %DBConnection.EncodeError{} = catch_error(query("SELECT $1::oid", [4_294_967_295 + 1]))
assert [["-"]] = query("select $1::regproc::text;", [0])
assert [["regprocin"]] = query("select $1::regproc::text;", [44])
assert [["sum(integer)"]] = query("select $1::regprocedure::text;", [2108])
assert [["||/"]] = query("select $1::regoper::text;", [597])
assert [["+(integer,integer)"]] = query("select $1::regoperator::text;", [551])
assert [["pg_type"]] = query("select $1::regclass::text;", [1247])
assert [["integer"]] = query("select $1::regtype::text;", [23])
assert [[0]] = query("select $1::text::regproc;", ["-"])
assert [[44]] = query("select $1::text::regproc;", ["regprocin"])
assert [[2108]] = query("select $1::text::regprocedure;", ["sum(int4)"])
assert [[597]] = query("select $1::text::regoper;", ["pg_catalog.||/"])
assert [[551]] = query("select $1::text::regoperator;", ["+(integer,integer)"])
assert [[1247]] = query("select $1::text::regclass;", ["pg_type"])
assert [[23]] = query("select $1::text::regtype;", ["int4"])
end
test "tuple ids", context do
assert [[_tid]] = query("select ctid from pg_type limit 1;", [])
assert [[{5, 10}]] = query("select $1::tid;", [{5, 10}])
end
test "encoding oids as binary fails with a helpful error message", context do
assert_raise ArgumentError,
~r"See https://github.com/elixir-ecto/postgrex#oid-type-encoding",
fn -> query("select $1::regclass;", ["pg_type"]) end
end
test "fail on encoding wrong value", context do
assert %DBConnection.EncodeError{message: message} =
catch_error(query("SELECT $1::integer", ["123"]))
assert message =~ "Postgrex expected an integer in -2147483648..2147483647"
end
@tag min_pg_version: "9.0"
test "decode hstore", context do
assert [[%{}]] = query(~s|SELECT ''::hstore|, [])
assert [[%{"Bubbles" => "7", "Name" => "Frank"}]] =
query(~s|SELECT '"Name" => "Frank", "Bubbles" => "7"'::hstore|, [])
assert [[%{"non_existant" => nil, "present" => "&accounted_for"}]] =
query(~s|SELECT '"non_existant" => NULL, "present" => "&accounted_for"'::hstore|, [])
assert [[%{"spaces in the key" => "are easy!", "floats too" => "66.6"}]] =
query(
~s|SELECT '"spaces in the key" => "are easy!", "floats too" => "66.6"'::hstore|,
[]
)
assert [[%{"this is true" => "true", "though not this" => "false"}]] =
query(
~s|SELECT '"this is true" => "true", "though not this" => "false"'::hstore|,
[]
)
end
test "encode basic types", context do
assert [[nil, nil]] = query("SELECT $1::text, $2::int", [nil, nil])
assert [[true, false]] = query("SELECT $1::bool, $2::bool", [true, false])
assert [["ẽ"]] = query("SELECT $1::char", ["ẽ"])
assert [[42]] = query("SELECT $1::int", [42])
assert [[42.0, 43.0]] = query("SELECT $1::float, $2::float", [42, 43.0])
assert [[:NaN]] = query("SELECT $1::float", [:NaN])
assert [[:inf]] = query("SELECT $1::float", [:inf])
assert [[:"-inf"]] = query("SELECT $1::float", [:"-inf"])
assert [["ẽric"]] = query("SELECT $1::varchar", ["ẽric"])
assert [[<<1, 2, 3>>]] = query("SELECT $1::bytea", [<<1, 2, 3>>])
end
test "encode numeric", context do
nums = [
"42",
"0.4242",
"42.4242",
"1.001",
"1.00123",
"0.01",
"0.00012345",
"1000000000",
"1000000000.0",
"123456789123456789123456789",
"123456789123456789123456789.123456789",
"1.1234500000",
"1.0000000000",
"1.111101",
"1.1111111101",
"1.11110001",
"NaN",
"-42"
]
Enum.each(nums, fn num ->
dec = Decimal.new(num)
assert [[dec]] == query("SELECT $1::numeric", [dec])
end)
end
test "encode integers and floats as numeric", context do
dec = Decimal.new(1)
assert [[dec]] == query("SELECT $1::numeric", [1])
dec = Decimal.from_float(1.0)
assert [[dec]] == query("SELECT $1::numeric", [1.0])
end
@tag min_pg_version: "14.0"
test "encode infinite values as numeric", context do
pos_inf = Decimal.new("Infinity")
neg_inf = Decimal.new("Infinity")
assert [[pos_inf]] == query("SELECT $1::numeric", [pos_inf])
assert [[neg_inf]] == query("SELECT $1::numeric", [neg_inf])
end
@tag min_pg_version: "9.5"
test "encode json/jsonb", context do
json = %{"foo" => 42}
assert [[json]] == query("SELECT $1::json", [json])
assert [[json]] == query("SELECT $1::jsonb", [json])
end
test "encode custom numerics", context do
assert [[%Decimal{sign: 1, coef: 1500, exp: 0}]] ==
query("SELECT $1::numeric", [Decimal.from_float(1500.0)])
assert [[%Decimal{sign: 1, coef: 1, exp: 0}]] ==
query("SELECT $1::numeric", [Decimal.new(1, 1, 0)])
assert [[%Decimal{sign: 1, coef: 10, exp: 0}]] ==
query("SELECT $1::numeric", [Decimal.new(1, 1, 1)])
assert [[%Decimal{sign: 1, coef: 100, exp: 0}]] ==
query("SELECT $1::numeric", [Decimal.new(1, 1, 2)])
assert [[%Decimal{sign: 1, coef: 1000, exp: 0}]] ==
query("SELECT $1::numeric", [Decimal.new(1, 1, 3)])
assert [[%Decimal{sign: 1, coef: 10000, exp: 0}]] ==
query("SELECT $1::numeric", [Decimal.new(1, 1, 4)])
assert [[%Decimal{sign: 1, coef: 100_000, exp: 0}]] ==
query("SELECT $1::numeric", [Decimal.new(1, 1, 5)])
assert [[%Decimal{sign: 1, coef: 1, exp: -5}]] ==
query("SELECT $1::numeric", [Decimal.new(1, 1, -5)])
end
test "encode enforces bounds on integers", context do
# int2's range is -32768 to +32767
assert [[-32768]] = query("SELECT $1::int2", [-32768])
assert [[32767]] = query("SELECT $1::int2", [32767])
assert %DBConnection.EncodeError{} = catch_error(query("SELECT $1::int2", [32767 + 1]))
assert %DBConnection.EncodeError{} = catch_error(query("SELECT $1::int2", [-32768 - 1]))
# int4's range is -2147483648 to +2147483647
assert [[-2_147_483_648]] = query("SELECT $1::int4", [-2_147_483_648])
assert [[2_147_483_647]] = query("SELECT $1::int4", [2_147_483_647])
assert %DBConnection.EncodeError{} =
catch_error(query("SELECT $1::int4", [2_147_483_647 + 1]))
assert %DBConnection.EncodeError{} =
catch_error(query("SELECT $1::int4", [-2_147_483_648 - 1]))
# int8's range is -9223372036854775808 to 9223372036854775807
assert [[-9_223_372_036_854_775_808]] = query("SELECT $1::int8", [-9_223_372_036_854_775_808])
assert [[9_223_372_036_854_775_807]] = query("SELECT $1::int8", [9_223_372_036_854_775_807])
assert %DBConnection.EncodeError{} =
catch_error(query("SELECT $1::int8", [9_223_372_036_854_775_807 + 1]))
assert %DBConnection.EncodeError{} =
catch_error(query("SELECT $1::int8", [-9_223_372_036_854_775_808 - 1]))
end
@tag min_pg_version: "13.0"
test "decode xid8 from pg_current_xact_id", context do
assert [[int]] = query("SELECT pg_current_xact_id()", [])
assert is_integer(int)
end
@tag min_pg_version: "13.0"
test "encode enforces bounds on xid8", context do
# xid8's range is 0 to +18_446_744_073_709_551_615
assert [[0]] = query("SELECT $1::xid", [0])
assert [[18_446_744_073_709_551_615]] = query("SELECT $1::xid8", [18_446_744_073_709_551_615])
assert %DBConnection.EncodeError{} =
catch_error(query("SELECT $1::xid8", [18_446_744_073_709_551_615 + 1]))
assert %DBConnection.EncodeError{} = catch_error(query("SELECT $1::xid", [0 - 1]))
end