forked from JuliaDatabases/LibPQ.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntests.jl
1701 lines (1407 loc) · 69.1 KB
/
runtests.jl
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
using LibPQ2
using Test
using Dates
using DataFrames
using DataFrames: eachrow
using Decimals
using Infinity
using Intervals
using IterTools: imap
using OffsetArrays
using TimeZones
using Tables
macro test_broken_on_windows(ex)
if Sys.iswindows()
:(@test_broken $(esc(ex)))
else
:(@test $(esc(ex)))
end
end
macro test_nolog_on_windows(ex...)
if Sys.iswindows()
:(@test_nolog($(map(esc, ex)...)))
else
:(@test_log($(map(esc, ex)...)))
end
end
@testset "LibPQ2" begin
@testset "ConninfoDisplay" begin
@test parse(LibPQ2.ConninfoDisplay, "") == LibPQ2.Normal
@test parse(LibPQ2.ConninfoDisplay, "*") == LibPQ2.Password
@test parse(LibPQ2.ConninfoDisplay, "D") == LibPQ2.Debug
@test_throws LibPQ2.Errors.JLConnectionError parse(LibPQ2.ConninfoDisplay, "N")
end
@testset "Version Numbers" begin
valid_versions = [
(LibPQ2.pqv"11", v"11"),
(LibPQ2.pqv"11.80", v"11.0.80"),
(LibPQ2.pqv"10.1", v"10.0.1"),
(LibPQ2.pqv"9.1.5", v"9.1.5"),
(LibPQ2.pqv"9.2", v"9.2.0"),
(LibPQ2.pqv"8", v"8.0.0"),
]
@testset "Valid Versions" for (pg_version, jl_version) in valid_versions
@test pg_version == jl_version
end
invalid_versions = [
"10.1.1",
"10.0.1",
"10.0.0.1",
"9.0.0.1",
"",
]
# can't do cross-version macro testing apparently
@testset "Invalid Versions" for pg_version_str in invalid_versions
@test_throws ArgumentError LibPQ2._pqv_str(pg_version_str)
end
end
@testset "Online" begin
DATABASE_USER = get(ENV, "LibPQ2JL_DATABASE_USER", "postgres")
@testset "Example SELECT" begin
conn = LibPQ2.Connection("dbname=postgres user=$DATABASE_USER"; throw_error=false)
@test conn isa LibPQ2.Connection
@test isopen(conn)
@test status(conn) == LibPQ2.LibPQ2_c.CONNECTION_OK
@test conn.closed[] == false
text_display = sprint(show, conn)
@test occursin("dbname = postgres", text_display)
@test occursin("user = $DATABASE_USER", text_display)
result = execute(
conn,
"SELECT typname FROM pg_type WHERE oid = 16";
throw_error=false,
)
@test result isa LibPQ2.Result
@test status(result) == LibPQ2.LibPQ2_c.PGRES_TUPLES_OK
@test isopen(result)
@test LibPQ2.num_columns(result) == 1
@test LibPQ2.num_rows(result) == 1
@test LibPQ2.column_name(result, 1) == "typname"
@test LibPQ2.column_number(result, "typname") == 1
data = columntable(result)
@test data[:typname][1] == "bool"
close(result)
@test !isopen(result)
# the same but with parameters
result = execute(
conn,
"SELECT typname FROM pg_type WHERE oid = \$1",
[16];
throw_error=false,
)
@test result isa LibPQ2.Result
@test status(result) == LibPQ2.LibPQ2_c.PGRES_TUPLES_OK
@test isopen(result)
@test LibPQ2.num_columns(result) == 1
@test LibPQ2.num_rows(result) == 1
@test LibPQ2.column_name(result, 1) == "typname"
data = columntable(result)
@test data[:typname][1] == "bool"
close(result)
@test !isopen(result)
# the same but with tuple parameters
qstr = "SELECT \$1::double precision as foo, typname FROM pg_type WHERE oid = \$2"
stmt = prepare(conn, qstr)
result = execute(
conn,
qstr,
(1.0, 16);
throw_error=false,
)
@test result isa LibPQ2.Result
@test status(result) == LibPQ2.LibPQ2_c.PGRES_TUPLES_OK
@test isopen(result)
@test LibPQ2.num_columns(result) == 2
@test LibPQ2.num_rows(result) == 1
@test LibPQ2.column_name(result, 1) == "foo"
@test LibPQ2.column_name(result, 2) == "typname"
data = columntable(result)
@test data[:foo][1] == 1.0
@test data[:typname][1] == "bool"
close(result)
@test !isopen(result)
result = execute(
stmt,
(1.0, 16);
throw_error=false,
)
@test result isa LibPQ2.Result
@test status(result) == LibPQ2.LibPQ2_c.PGRES_TUPLES_OK
@test isopen(result)
@test LibPQ2.num_columns(result) == 2
@test LibPQ2.num_rows(result) == 1
@test LibPQ2.column_name(result, 1) == "foo"
@test LibPQ2.column_name(result, 2) == "typname"
data = columntable(result)
@test data[:foo][1] == 1.0
@test data[:typname][1] == "bool"
close(result)
@test !isopen(result)
close(conn)
@test !isopen(conn)
@test conn.closed[] == true
text_display_closed = sprint(show, conn)
@test occursin("closed", text_display_closed)
end
@testset "Example INSERT and DELETE" begin
conn = LibPQ2.Connection("dbname=postgres user=$DATABASE_USER")
result = execute(conn, """
CREATE TEMPORARY TABLE LibPQ2jl_test (
no_nulls varchar(10) PRIMARY KEY,
yes_nulls varchar(10)
);
""")
@test status(result) == LibPQ2.LibPQ2_c.PGRES_COMMAND_OK
close(result)
# get the data from PostgreSQL and let columntable construct my NamedTuple
result = execute(conn, """
SELECT no_nulls, yes_nulls FROM (
VALUES ('foo', 'bar'), ('baz', NULL)
) AS temp (no_nulls, yes_nulls)
ORDER BY no_nulls DESC;
""";
throw_error=true,
)
@test status(result) == LibPQ2.LibPQ2_c.PGRES_TUPLES_OK
@test LibPQ2.num_rows(result) == 2
@test LibPQ2.num_columns(result) == 2
data = columntable(result)
@test data[:no_nulls] == ["foo", "baz"]
@test data[:yes_nulls][1] == "bar"
@test data[:yes_nulls][2] === missing
stmt = LibPQ2.load!(
data,
conn,
"INSERT INTO LibPQ2jl_test (no_nulls, yes_nulls) VALUES (\$1, \$2);",
)
@test_throws ArgumentError num_affected_rows(stmt.description)
@test num_params(stmt) == 2
@test num_columns(stmt) == 0 # an insert has no results
@test LibPQ2.column_number(stmt, "no_nulls") == 0
@test LibPQ2.column_names(stmt) == []
result = execute(
conn,
"SELECT no_nulls, yes_nulls FROM LibPQ2jl_test ORDER BY no_nulls DESC;";
throw_error=true,
)
@test status(result) == LibPQ2.LibPQ2_c.PGRES_TUPLES_OK
@test LibPQ2.num_rows(result) == 2
@test LibPQ2.num_columns(result) == 2
table_data = columntable(result)
@test table_data[:no_nulls] == data[:no_nulls]
@test table_data[:yes_nulls][1] == data[:yes_nulls][1]
@test table_data[:yes_nulls][2] === missing
close(result)
result = execute(
conn,
"DELETE FROM LibPQ2jl_test WHERE no_nulls = 'foo';";
throw_error=true,
)
@test status(result) == LibPQ2.LibPQ2_c.PGRES_COMMAND_OK
@test num_rows(result) == 0
@test num_affected_rows(result) == 1
close(result)
result = execute(
conn,
"SELECT no_nulls, yes_nulls FROM LibPQ2jl_test ORDER BY no_nulls DESC;";
throw_error=true,
)
table_data_after_delete = columntable(result)
@test table_data_after_delete[:no_nulls] == ["baz"]
@test table_data_after_delete[:yes_nulls][1] === missing
close(result)
result = execute(
conn,
"INSERT INTO LibPQ2jl_test (no_nulls, yes_nulls) VALUES (\$1, \$2), (\$3, \$4);",
Union{String, Missing}["foo", "bar", "quz", missing],
)
@test status(result) == LibPQ2.LibPQ2_c.PGRES_COMMAND_OK
@test num_rows(result) == 0
@test num_affected_rows(result) == 2
close(result)
close(conn)
end
@testset "load!" begin
@testset "issue #204" begin
conn = LibPQ2.Connection("dbname=postgres user=$DATABASE_USER")
close(execute(conn, """
CREATE TEMPORARY TABLE LibPQ2jl_test (
id serial PRIMARY KEY,
data text[]
);
"""))
table = (; data = [["foo", "bar"], ["baz"]])
LibPQ2.load!(table, conn, "INSERT INTO LibPQ2jl_test (data) VALUES (\$1);")
# TODO: compare entire tables once string array parsing is supported
result = execute(conn, "SELECT data[1] as data1 FROM LibPQ2jl_test ORDER BY id;", not_null=true)
retrieved = columntable(result)
@test first.(table.data) == retrieved.data1
close(result)
close(conn)
end
end
@testset "COPY FROM" begin
@testset "Example COPY FROM" begin
conn = LibPQ2.Connection("dbname=postgres user=$DATABASE_USER")
result = execute(conn, """
CREATE TEMPORARY TABLE LibPQ2jl_test (
no_nulls varchar(10) PRIMARY KEY,
yes_nulls varchar(10)
);
""")
@test status(result) == LibPQ2.LibPQ2_c.PGRES_COMMAND_OK
close(result)
no_nulls = map(string, 'a':'z')
yes_nulls = Union{String, Missing}[isodd(Int(c)) ? string(c) : missing for c in 'a':'z']
data = DataFrame(no_nulls=no_nulls, yes_nulls=yes_nulls)
row_strings = imap(eachrow(data)) do row
if ismissing(row[:yes_nulls])
"$(row[:no_nulls]),\n"
else
"$(row[:no_nulls]),$(row[:yes_nulls])\n"
end
end
copyin = LibPQ2.CopyIn("COPY LibPQ2jl_test FROM STDIN (FORMAT CSV);", row_strings)
result = execute(conn, copyin)
@test isopen(result)
@test status(result) == LibPQ2.LibPQ2_c.PGRES_COMMAND_OK
@test isempty(LibPQ2.error_message(result))
close(result)
result = execute(
conn,
"SELECT no_nulls, yes_nulls FROM LibPQ2jl_test ORDER BY no_nulls ASC;";
throw_error=true
)
table_data = DataFrame(result)
@test isequal(table_data, data)
close(result)
close(conn)
end
@testset "Wrong column order" begin
conn = LibPQ2.Connection("dbname=postgres user=$DATABASE_USER")
result = execute(conn, """
CREATE TEMPORARY TABLE LibPQ2jl_test (
pri bigint PRIMARY KEY,
sec varchar(10)
);
""")
@test status(result) == LibPQ2.LibPQ2_c.PGRES_COMMAND_OK
close(result)
data = (pri = 1:26, sec = map(string, 'a':'z'))
row_strings = imap(Tables.rows(data)) do row
"$(row.sec),$(row.pri)\n"
end
copyin = LibPQ2.CopyIn("COPY LibPQ2jl_test FROM STDIN (FORMAT CSV);", row_strings)
result = execute(conn, copyin; throw_error=false)
@test isopen(result)
@test status(result) == LibPQ2.LibPQ2_c.PGRES_FATAL_ERROR
err_msg = LibPQ2.error_message(result)
@test occursin("ERROR", err_msg)
if LibPQ2.server_version(conn) >= v"12"
@test occursin("invalid input syntax for type bigint", err_msg)
else
@test occursin("invalid input syntax for integer", err_msg)
end
close(result)
result = execute(
conn,
"SELECT pri, sec FROM LibPQ2jl_test ORDER BY pri ASC;";
throw_error=true
)
table_data = columntable(result)
@test isequal(table_data, (pri = Int[], sec = String[]))
close(result)
row_strings = imap(Tables.rows(data)) do row
"$(row.sec),$(row.pri)\n"
end
copyin = LibPQ2.CopyIn("COPY LibPQ2jl_test FROM STDIN (FORMAT CSV);", row_strings)
@test_throws LibPQ2.Errors.DataExceptionErrorClass execute(conn, copyin; throw_error=true)
close(conn)
end
end
@testset "LibPQ2.Connection" begin
@testset "do" begin
local saved_conn
was_open = LibPQ2.Connection("dbname=postgres user=$DATABASE_USER"; throw_error=true) do jl_conn
saved_conn = jl_conn
return isopen(jl_conn)
end
@test was_open
@test !isopen(saved_conn)
@test_throws LibPQ2.Errors.PQConnectionError LibPQ2.Connection("dbname=123fake user=$DATABASE_USER"; throw_error=true) do jl_conn
saved_conn = jl_conn
@test false
end
@test !isopen(saved_conn)
end
@testset "Version Numbers" begin
conn = LibPQ2.Connection("dbname=postgres user=$DATABASE_USER"; throw_error=true)
# update this test before PostgreSQL 20.0 ;)
@test LibPQ2.pqv"7" <= LibPQ2.server_version(conn) <= LibPQ2.pqv"20"
end
@testset "Encoding" begin
conn = LibPQ2.Connection("dbname=postgres user=$DATABASE_USER"; throw_error=true)
@test LibPQ2.encoding(conn) == "UTF8"
LibPQ2.set_encoding!(conn, "SQL_ASCII")
@test LibPQ2.encoding(conn) == "SQL_ASCII"
LibPQ2.reset_encoding!(conn)
@test LibPQ2.encoding(conn) == "SQL_ASCII"
reset!(conn)
@test LibPQ2.encoding(conn) == "SQL_ASCII"
LibPQ2.set_encoding!(conn, "UTF8")
@test LibPQ2.encoding(conn) == "UTF8"
LibPQ2.reset_encoding!(conn)
@test LibPQ2.encoding(conn) == "UTF8"
conn.encoding = "SQL_ASCII"
LibPQ2.reset_encoding!(conn)
@test LibPQ2.encoding(conn) == "SQL_ASCII"
@test_throws LibPQ2.Errors.JLConnectionError LibPQ2.set_encoding!(conn, "NOT A REAL ENCODING")
close(conn)
end
@testset "Options" begin
conn = LibPQ2.Connection(
"dbname=postgres user=$DATABASE_USER";
options=Dict("IntervalStyle" => "postgres_verbose"),
throw_error=true,
type_map=Dict(:interval => String),
)
conn_info = LibPQ2.conninfo(conn)
options = first(filter(conn_info) do conn_opt
conn_opt.keyword == "options"
end)
@test occursin("IntervalStyle=postgres_verbose", options.val)
results = columntable(execute(conn, "SELECT '1 12:59:10'::interval;"))
@test results[1][1] == "@ 1 day 12 hours 59 mins 10 secs"
close(conn)
# ERROR: missing "=" after "barf" in connection info string
@test_throws LibPQ2.Errors.ConninfoParseError LibPQ2.conninfo("wrong")
end
@testset "Time Zone" begin
function connection_tz(conn::LibPQ2.Connection)
result = execute(conn, "SELECT current_setting('TIMEZONE');")
tz = columntable(result)[:current_setting][1]
close(result)
return tz
end
# test with rare time zones to avoid collision with actual server time zones
# NOTE: do not run tests in Greenland
default_tz = LibPQ2.DEFAULT_CLIENT_TIME_ZONE[]
try
LibPQ2.DEFAULT_CLIENT_TIME_ZONE[] = "America/Scoresbysund"
LibPQ2.CONNECTION_OPTION_DEFAULTS["TimeZone"] = LibPQ2.DEFAULT_CLIENT_TIME_ZONE[]
merge!(LibPQ2.CONNECTION_PARAMETER_DEFAULTS, LibPQ2._connection_parameter_dict(connection_options=LibPQ2.CONNECTION_OPTION_DEFAULTS))
withenv("PGTZ" => nothing) do # unset
LibPQ2.Connection(
"dbname=postgres user=$DATABASE_USER"; throw_error=true
) do conn
@test connection_tz(conn) == "America/Scoresbysund"
end
LibPQ2.Connection(
"dbname=postgres user=$DATABASE_USER";
options=Dict("TimeZone" => "America/Danmarkshavn"),
throw_error=true,
) do conn
@test connection_tz(conn) == "America/Danmarkshavn"
end
LibPQ2.Connection(
"dbname=postgres user=$DATABASE_USER";
options=Dict("TimeZone" => ""),
throw_error=true,
) do conn
@test connection_tz(conn) != "America/Scoresbysund"
@test connection_tz(conn) != "America/Danmarkshavn"
end
end
# For some reason, LibPQ2 won't pick up environment variables which are set
# after it has been loaded. This seems to happen with Julia only; psycopg2
# does not have this problem. Perhaps we need to set some dlopen option?
withenv("PGTZ" => "America/Thule") do
LibPQ2.Connection(
"dbname=postgres user=$DATABASE_USER"; throw_error=true
) do conn
@test_broken_on_windows connection_tz(conn) == "America/Thule"
end
LibPQ2.Connection(
"dbname=postgres user=$DATABASE_USER";
options=Dict("TimeZone" => "America/Danmarkshavn"),
throw_error=true,
) do conn
@test_broken_on_windows connection_tz(conn) == "America/Thule"
end
LibPQ2.Connection(
"dbname=postgres user=$DATABASE_USER";
options=Dict("TimeZone" => ""),
throw_error=true,
) do conn
@test_broken_on_windows connection_tz(conn) == "America/Thule"
end
end
withenv("PGTZ" => "") do
@test_nolog_on_windows LibPQ2.LOGGER "error" "invalid value for parameter" try
LibPQ2.Connection(
"dbname=postgres user=$DATABASE_USER"; throw_error=true
)
catch
end
@test_nolog_on_windows LibPQ2.LOGGER "error" "invalid value for parameter" try
LibPQ2.Connection(
"dbname=postgres user=$DATABASE_USER";
options=Dict("TimeZone" => "America/Danmarkshavn"),
throw_error=true,
)
catch
end
@test_nolog_on_windows LibPQ2.LOGGER "error" "invalid value for parameter" try
LibPQ2.Connection(
"dbname=postgres user=$DATABASE_USER";
options=Dict("TimeZone" => ""),
throw_error=true,
)
catch
end
end
finally
LibPQ2.DEFAULT_CLIENT_TIME_ZONE[] = default_tz
LibPQ2.CONNECTION_OPTION_DEFAULTS["TimeZone"] = LibPQ2.DEFAULT_CLIENT_TIME_ZONE[]
merge!(LibPQ2.CONNECTION_PARAMETER_DEFAULTS, LibPQ2._connection_parameter_dict(connection_options=LibPQ2.CONNECTION_OPTION_DEFAULTS))
end
end
@testset "Finalizer" begin
closed_flags = map(1:50) do _
conn = LibPQ2.Connection("dbname=postgres user=$DATABASE_USER")
closed = conn.closed
finalize(conn)
return closed
end
sleep(1)
@test all(closed -> closed[], closed_flags)
# with Results, which don't hold a reference to Connection
results = LibPQ2.Result[]
closed_flags = map(1:50) do _
conn = LibPQ2.Connection("dbname=postgres user=$DATABASE_USER")
push!(results, execute(conn, "SELECT 1;"))
return conn.closed
end
GC.gc()
sleep(1)
GC.gc()
sleep(1)
@test all(closed -> closed[], closed_flags)
@test all(result -> LibPQ2.num_rows(result) == 1, results)
# with AsyncResults, which hold a reference to Connection
closed_flags = asyncmap(1:50) do _
conn = LibPQ2.Connection("dbname=postgres user=$DATABASE_USER")
wait(async_execute(conn, "SELECT pg_sleep(1);"))
return conn.closed
end
GC.gc()
sleep(1)
GC.gc()
sleep(1)
@test all(closed -> closed[], closed_flags)
end
@testset "Bad Connection" begin
@testset "timeout" begin
try
# could be any SSL connection, just need to get through one stage of
# processing successfully so it has an opportunity to time out
LibPQ2.Connection("host=enterprisedb.com port=443"; connect_timeout=0.01)
@test false
catch err
@test err isa LibPQ2.Errors.JLConnectionError
@test occursin("timed out", sprint(showerror, err))
end
end
# I don't think it's actually possible to hit this error condition on Windows
# I also don't know if it's possible to reproduce this, given how sockets seem
# to work on Windows
@static if !Sys.iswindows()
@testset "closed socket (EBADF)" begin
conn = LibPQ2.LibPQ2_c.PQconnectStart("dbname=123fake user=$DATABASE_USER")
close(Base.Libc.FILE(LibPQ2.socket(conn), "r")) # close socket
LibPQ2._connect_poll(conn, Timer(0), 0)
try
LibPQ2.handle_new_connection(LibPQ2.Connection(conn))
@test false
catch err
@test err isa LibPQ2.Errors.PQConnectionError
@test occursin("Bad file descriptor", sprint(show, err))
end
end
end
@testset "throw_error=false" begin
conn = LibPQ2.Connection("dbname=123fake user=$DATABASE_USER"; throw_error=false)
@test conn isa LibPQ2.Connection
@test status(conn) == LibPQ2.LibPQ2_c.CONNECTION_BAD
@test isopen(conn)
reset!(conn; throw_error=false)
@test status(conn) == LibPQ2.LibPQ2_c.CONNECTION_BAD
@test isopen(conn)
close(conn)
@test !isopen(conn)
@test conn.closed[] == true
@test_throws LibPQ2.Errors.JLConnectionError reset!(conn; throw_error=false)
end
@testset "throw_error=true" begin
@test_throws LibPQ2.Errors.PQConnectionError LibPQ2.Connection("dbname=123fake user=$DATABASE_USER"; throw_error=true)
conn = LibPQ2.Connection("dbname=123fake user=$DATABASE_USER"; throw_error=false)
@test conn isa LibPQ2.Connection
@test status(conn) == LibPQ2.LibPQ2_c.CONNECTION_BAD
@test isopen(conn)
@test_throws LibPQ2.Errors.PQConnectionError reset!(conn; throw_error=true)
@test !isopen(conn)
@test conn.closed[] == true
@test_throws LibPQ2.Errors.JLConnectionError reset!(conn; throw_error=true)
end
end
end
@testset "Results" begin
@testset "Nulls" begin
conn = LibPQ2.Connection("dbname=postgres user=$DATABASE_USER"; throw_error=true)
result = execute(conn, "SELECT NULL"; throw_error=true)
@test status(result) == LibPQ2.LibPQ2_c.PGRES_TUPLES_OK
@test LibPQ2.num_columns(result) == 1
@test LibPQ2.num_rows(result) == 1
data = columntable(result)
@test data[1][1] === missing
close(result)
result = execute(conn, """
SELECT no_nulls, yes_nulls FROM (
VALUES ('foo', 'bar'), ('baz', NULL)
) AS temp (no_nulls, yes_nulls)
ORDER BY no_nulls DESC;
""";
throw_error=true,
)
@test status(result) == LibPQ2.LibPQ2_c.PGRES_TUPLES_OK
@test LibPQ2.num_rows(result) == 2
@test LibPQ2.num_columns(result) == 2
data = columntable(result)
@test data[:no_nulls] == ["foo", "baz"]
@test data[:yes_nulls][1] == "bar"
@test data[:yes_nulls][2] === missing
close(result)
# NULL first this time, to check for errors that might come up with lazy
# initialization of the output data vectors
result = execute(conn, """
SELECT no_nulls, yes_nulls FROM (
VALUES ('foo', 'bar'), ('baz', NULL)
) AS temp (no_nulls, yes_nulls)
ORDER BY no_nulls ASC;
""";
throw_error=true,
)
@test result isa LibPQ2.Result
@test status(result) == LibPQ2.LibPQ2_c.PGRES_TUPLES_OK
@test LibPQ2.num_rows(result) == 2
@test LibPQ2.num_columns(result) == 2
data = columntable(result)
@test data[:no_nulls] == ["baz", "foo"]
@test data[:yes_nulls][1] === missing
@test data[:yes_nulls][2] == "bar"
close(result)
# Verify that Connection is treated as a scalar during broadcast
commands = [
"""
SELECT no_nulls, yes_nulls FROM (
VALUES ('foo', 'bar'), ('baz', NULL)
) AS temp (no_nulls, yes_nulls)
ORDER BY no_nulls ASC;
""",
"""
SELECT no_nulls, yes_nulls FROM (
VALUES ('foo', 'bar'), ('baz', NULL)
) AS temp (no_nulls, yes_nulls)
ORDER BY no_nulls DESC;
""",
]
results = execute.(conn, commands; throw_error=true)
for result in results
@test result isa LibPQ2.Result
@test status(result) == LibPQ2.LibPQ2_c.PGRES_TUPLES_OK
@test LibPQ2.num_rows(result) == 2
@test LibPQ2.num_columns(result) == 2
close(result)
end
close(conn)
end
@testset "Not Nulls" begin
conn = LibPQ2.Connection("dbname=postgres user=$DATABASE_USER"; throw_error=true)
result = execute(conn, "SELECT NULL"; not_null=[false], throw_error=true)
@test status(result) == LibPQ2.LibPQ2_c.PGRES_TUPLES_OK
@test LibPQ2.num_columns(result) == 1
@test LibPQ2.num_rows(result) == 1
data = columntable(result)
@test data[1][1] === missing
close(result)
result = execute(conn, "SELECT NULL"; not_null=true, throw_error=true)
@test status(result) == LibPQ2.LibPQ2_c.PGRES_TUPLES_OK
@test LibPQ2.num_columns(result) == 1
@test LibPQ2.num_rows(result) == 1
@test_throws MethodError columntable(result)[1][1]
close(result)
result = execute(conn, "SELECT NULL"; not_null=[true], throw_error=true)
@test status(result) == LibPQ2.LibPQ2_c.PGRES_TUPLES_OK
@test LibPQ2.num_columns(result) == 1
@test LibPQ2.num_rows(result) == 1
@test_throws MethodError columntable(result)[1][1]
close(result)
result = execute(conn, """
SELECT no_nulls, yes_nulls FROM (
VALUES ('foo', 'bar'), ('baz', NULL)
) AS temp (no_nulls, yes_nulls)
ORDER BY no_nulls DESC;
""";
not_null=[true, false],
throw_error=true,
)
@test status(result) == LibPQ2.LibPQ2_c.PGRES_TUPLES_OK
@test LibPQ2.num_rows(result) == 2
@test LibPQ2.num_columns(result) == 2
data = columntable(result)
@test data[:no_nulls] == ["foo", "baz"]
@test data[:no_nulls] isa AbstractVector{String}
@test data[:yes_nulls][1] == "bar"
@test data[:yes_nulls][2] === missing
@test data[:yes_nulls] isa AbstractVector{Union{String, Missing}}
close(result)
result = execute(conn, """
SELECT no_nulls, yes_nulls FROM (
VALUES ('foo', 'bar'), ('baz', NULL)
) AS temp (no_nulls, yes_nulls)
ORDER BY no_nulls DESC;
""";
not_null=["no_nulls"],
throw_error=true,
)
@test status(result) == LibPQ2.LibPQ2_c.PGRES_TUPLES_OK
@test LibPQ2.num_rows(result) == 2
@test LibPQ2.num_columns(result) == 2
data = columntable(result)
@test data[:no_nulls] == ["foo", "baz"]
@test data[:no_nulls] isa AbstractVector{String}
@test data[:yes_nulls][1] == "bar"
@test data[:yes_nulls][2] === missing
@test data[:yes_nulls] isa AbstractVector{Union{String, Missing}}
close(result)
result = execute(conn, """
SELECT no_nulls, yes_nulls FROM (
VALUES ('foo', 'bar'), ('baz', NULL)
) AS temp (no_nulls, yes_nulls)
ORDER BY no_nulls DESC;
""";
not_null=[:fake_column, :no_nulls],
throw_error=true,
)
@test status(result) == LibPQ2.LibPQ2_c.PGRES_TUPLES_OK
@test LibPQ2.num_rows(result) == 2
@test LibPQ2.num_columns(result) == 2
data = columntable(result)
@test data[:no_nulls] == ["foo", "baz"]
@test data[:no_nulls] isa AbstractVector{String}
@test data[:yes_nulls][1] == "bar"
@test data[:yes_nulls][2] === missing
@test data[:yes_nulls] isa AbstractVector{Union{String, Missing}}
close(result)
result = execute(conn, """
SELECT no_nulls, yes_nulls FROM (
VALUES ('foo', 'bar'), ('baz', NULL)
) AS temp (no_nulls, yes_nulls)
ORDER BY no_nulls DESC;
""";
not_null=false,
throw_error=true,
)
@test status(result) == LibPQ2.LibPQ2_c.PGRES_TUPLES_OK
@test LibPQ2.num_rows(result) == 2
@test LibPQ2.num_columns(result) == 2
data = columntable(result)
@test data[:no_nulls] == ["foo", "baz"]
@test data[:no_nulls] isa AbstractVector{Union{String, Missing}}
@test data[:yes_nulls][1] == "bar"
@test data[:yes_nulls][2] === missing
@test data[:yes_nulls] isa AbstractVector{Union{String, Missing}}
close(result)
end
@testset "Tables.jl" begin
conn = LibPQ2.Connection("dbname=postgres user=$DATABASE_USER"; throw_error=true)
result = execute(conn, """
SELECT no_nulls, yes_nulls FROM (
VALUES ('foo', 'bar'), ('baz', NULL)
) AS temp (no_nulls, yes_nulls)
ORDER BY no_nulls DESC;
""";
not_null=false,
throw_error=true,
)
# rows
rt = Tables.rows(result)
data = collect(rt)
@test data[1].no_nulls == "foo"
@test data[1][1] == "foo"
@test data[2].no_nulls == "baz"
@test data[2][1] == "baz"
@test data[1].yes_nulls == "bar"
@test data[1][2] == "bar"
@test data[2].yes_nulls === missing
@test data[2][2] === missing
# columns
ct = Tables.columns(result)
no_nulls = collect(ct.no_nulls)
@test no_nulls == ["foo", "baz"]
yes_nulls = collect(ct.yes_nulls)
@test isequal(yes_nulls, ["bar", missing])
collected = map(collect, Tables.columns(result))
@test collected isa AbstractVector{<:AbstractVector{Union{String, Missing}}}
@test collected isa Vector{Vector{Union{String, Missing}}}
@test collected[1] == ["foo", "baz"]
@test isequal(collected[2], ["bar", missing])
close(result)
close(conn)
end
@testset "Duplicate names" begin
conn = LibPQ2.Connection("dbname=postgres user=$DATABASE_USER"; throw_error=true)
result = execute(conn, "SELECT 1 AS col, 2 AS col;", not_null=true, throw_error=true)
columns = Tables.columns(result)
@test columns[1] == [1]
@test columns[2] == [2]
row = first(Tables.rows(result))
@test row[1] == 1
@test row[2] == 2
close(result)
close(conn)
end
@testset "Uppercase Columns" begin
conn = LibPQ2.Connection("dbname=postgres user=$DATABASE_USER"; throw_error=true)
result = execute(conn, "SELECT 1 AS \"Column\";")
@test num_columns(result) == 1
@test LibPQ2.column_name(result, 1) == "Column"
@test LibPQ2.column_number(result, :Column) == 1
table = Tables.columntable(result)
@test :Column in propertynames(table)
table = Tables.rowtable(result)
@test :Column in propertynames(table[1])
close(result)
close(conn)
end
@testset "PQResultError" begin
conn = LibPQ2.Connection("dbname=postgres user=$DATABASE_USER"; throw_error=true)
try
execute(conn, "SELECT log(-1);")
@test false
catch err
@test err isa LibPQ2.Errors.InvalidArgumentForLogarithm
@test LibPQ2.Errors.error_class(err) == LibPQ2.Errors.C22
@test LibPQ2.Errors.error_code(err) == LibPQ2.Errors.E2201E
end
@test sprint(show, LibPQ2.Errors.C22) == "LibPQ2.Errors.C22"
@test string(LibPQ2.Errors.C22) == "C22"
@test sprint(LibPQ2.Errors.C22) do io, class
show(io, MIME"text/plain"(), class)
end == "C22::LibPQ2.Errors.Class"
@test sprint(show, LibPQ2.Errors.E2201E) == "LibPQ2.Errors.E2201E"
@test string(LibPQ2.Errors.E2201E) == "E2201E"
@test sprint(LibPQ2.Errors.E2201E) do io, code
show(io, MIME"text/plain"(), code)
end == "E2201E::LibPQ2.Errors.ErrorCode"
result = execute(conn, "SELECT log(-1);"; throw_error=false)
err = LibPQ2.Errors.PQResultError(result; verbose=false)
verbose_err = LibPQ2.Errors.PQResultError(result; verbose=true)
@test err isa LibPQ2.Errors.InvalidArgumentForLogarithm
@test verbose_err isa LibPQ2.Errors.InvalidArgumentForLogarithm
@test err.msg == verbose_err.msg
@test err.verbose_msg === nothing
@test verbose_err.verbose_msg !== nothing
@test length(verbose_err.verbose_msg) > length(err.msg)