forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresNIOTests.swift
1099 lines (990 loc) · 44.4 KB
/
PostgresNIOTests.swift
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
import Logging
@testable import PostgresNIO
import XCTest
import NIOTestUtils
final class PostgresNIOTests: XCTestCase {
private var group: EventLoopGroup!
private var eventLoop: EventLoop { self.group.next() }
override func setUpWithError() throws {
try super.setUpWithError()
XCTAssertTrue(isLoggingConfigured)
self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
}
override func tearDownWithError() throws {
try self.group?.syncShutdownGracefully()
self.group = nil
try super.tearDownWithError()
}
// MARK: Tests
func testConnectAndClose() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
try conn.close().wait()
}
func testSimpleQueryVersion() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let rows = try conn.simpleQuery("SELECT version()").wait()
XCTAssertEqual(rows.count, 1)
let version = rows[0].column("version")?.string
XCTAssertEqual(version?.contains("PostgreSQL"), true)
}
func testQueryVersion() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let rows = try conn.query("SELECT version()", .init()).wait()
XCTAssertEqual(rows.count, 1)
let version = rows[0].column("version")?.string
XCTAssertEqual(version?.contains("PostgreSQL"), true)
}
func testQuerySelectParameter() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let rows = try conn.query("SELECT $1::TEXT as foo", ["hello"]).wait()
XCTAssertEqual(rows.count, 1)
let version = rows[0].column("foo")?.string
XCTAssertEqual(version, "hello")
}
func testSQLError() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
XCTAssertThrowsError(_ = try conn.simpleQuery("SELECT &").wait()) { error in
guard let postgresError = try? XCTUnwrap(error as? PostgresError) else { return }
XCTAssertEqual(postgresError.code, .syntaxError)
}
}
func testNotificationsEmptyPayload() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
var receivedNotifications: [PostgresMessage.NotificationResponse] = []
conn.addListener(channel: "example") { context, notification in
receivedNotifications.append(notification)
}
_ = try conn.simpleQuery("LISTEN example").wait()
_ = try conn.simpleQuery("NOTIFY example").wait()
// Notifications are asynchronous, so we should run at least one more query to make sure we'll have received the notification response by then
_ = try conn.simpleQuery("SELECT 1").wait()
XCTAssertEqual(receivedNotifications.count, 1)
XCTAssertEqual(receivedNotifications[0].channel, "example")
XCTAssertEqual(receivedNotifications[0].payload, "")
}
func testNotificationsNonEmptyPayload() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
var receivedNotifications: [PostgresMessage.NotificationResponse] = []
conn.addListener(channel: "example") { context, notification in
receivedNotifications.append(notification)
}
_ = try conn.simpleQuery("LISTEN example").wait()
_ = try conn.simpleQuery("NOTIFY example, 'Notification payload example'").wait()
// Notifications are asynchronous, so we should run at least one more query to make sure we'll have received the notification response by then
_ = try conn.simpleQuery("SELECT 1").wait()
XCTAssertEqual(receivedNotifications.count, 1)
XCTAssertEqual(receivedNotifications[0].channel, "example")
XCTAssertEqual(receivedNotifications[0].payload, "Notification payload example")
}
func testNotificationsRemoveHandlerWithinHandler() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
var receivedNotifications = 0
conn.addListener(channel: "example") { context, notification in
receivedNotifications += 1
context.stop()
}
_ = try conn.simpleQuery("LISTEN example").wait()
_ = try conn.simpleQuery("NOTIFY example").wait()
_ = try conn.simpleQuery("NOTIFY example").wait()
_ = try conn.simpleQuery("SELECT 1").wait()
XCTAssertEqual(receivedNotifications, 1)
}
func testNotificationsRemoveHandlerOutsideHandler() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
var receivedNotifications = 0
let context = conn.addListener(channel: "example") { context, notification in
receivedNotifications += 1
}
_ = try conn.simpleQuery("LISTEN example").wait()
_ = try conn.simpleQuery("NOTIFY example").wait()
_ = try conn.simpleQuery("SELECT 1").wait()
context.stop()
_ = try conn.simpleQuery("NOTIFY example").wait()
_ = try conn.simpleQuery("SELECT 1").wait()
XCTAssertEqual(receivedNotifications, 1)
}
func testNotificationsMultipleRegisteredHandlers() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
var receivedNotifications1 = 0
conn.addListener(channel: "example") { context, notification in
receivedNotifications1 += 1
}
var receivedNotifications2 = 0
conn.addListener(channel: "example") { context, notification in
receivedNotifications2 += 1
}
_ = try conn.simpleQuery("LISTEN example").wait()
_ = try conn.simpleQuery("NOTIFY example").wait()
_ = try conn.simpleQuery("SELECT 1").wait()
XCTAssertEqual(receivedNotifications1, 1)
XCTAssertEqual(receivedNotifications2, 1)
}
func testNotificationsMultipleRegisteredHandlersRemoval() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
var receivedNotifications1 = 0
conn.addListener(channel: "example") { context, notification in
receivedNotifications1 += 1
context.stop()
}
var receivedNotifications2 = 0
conn.addListener(channel: "example") { context, notification in
receivedNotifications2 += 1
}
_ = try conn.simpleQuery("LISTEN example").wait()
_ = try conn.simpleQuery("NOTIFY example").wait()
_ = try conn.simpleQuery("NOTIFY example").wait()
_ = try conn.simpleQuery("SELECT 1").wait()
XCTAssertEqual(receivedNotifications1, 1)
XCTAssertEqual(receivedNotifications2, 2)
}
func testNotificationHandlerFiltersOnChannel() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
conn.addListener(channel: "desired") { context, notification in
XCTFail("Received notification on channel that handler was not registered for")
}
_ = try conn.simpleQuery("LISTEN undesired").wait()
_ = try conn.simpleQuery("NOTIFY undesired").wait()
_ = try conn.simpleQuery("SELECT 1").wait()
}
func testSelectTypes() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let results = try conn.simpleQuery("SELECT * FROM pg_type").wait()
XCTAssert(results.count >= 350, "Results count not large enough: \(results.count)")
}
func testSelectType() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let results = try conn.simpleQuery("SELECT * FROM pg_type WHERE typname = 'float8'").wait()
// [
// "typreceive": "float8recv",
// "typelem": "0",
// "typarray": "1022",
// "typalign": "d",
// "typanalyze": "-",
// "typtypmod": "-1",
// "typname": "float8",
// "typnamespace": "11",
// "typdefault": "<null>",
// "typdefaultbin": "<null>",
// "typcollation": "0",
// "typispreferred": "t",
// "typrelid": "0",
// "typbyval": "t",
// "typnotnull": "f",
// "typinput": "float8in",
// "typlen": "8",
// "typcategory": "N",
// "typowner": "10",
// "typtype": "b",
// "typdelim": ",",
// "typndims": "0",
// "typbasetype": "0",
// "typacl": "<null>",
// "typisdefined": "t",
// "typmodout": "-",
// "typmodin": "-",
// "typsend": "float8send",
// "typstorage": "p",
// "typoutput": "float8out"
// ]
switch results.count {
case 1:
XCTAssertEqual(results[0].column("typname")?.string, "float8")
XCTAssertEqual(results[0].column("typnamespace")?.int, 11)
XCTAssertEqual(results[0].column("typowner")?.int, 10)
XCTAssertEqual(results[0].column("typlen")?.int, 8)
default: XCTFail("Should be exactly one result, but got \(results.count)")
}
}
func testIntegers() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
struct Integers: Decodable {
let smallint: Int16
let smallint_min: Int16
let smallint_max: Int16
let int: Int32
let int_min: Int32
let int_max: Int32
let bigint: Int64
let bigint_min: Int64
let bigint_max: Int64
}
let results = try conn.query("""
SELECT
1::SMALLINT as smallint,
-32767::SMALLINT as smallint_min,
32767::SMALLINT as smallint_max,
1::INT as int,
-2147483647::INT as int_min,
2147483647::INT as int_max,
1::BIGINT as bigint,
-9223372036854775807::BIGINT as bigint_min,
9223372036854775807::BIGINT as bigint_max
""").wait()
switch results.count {
case 1:
XCTAssertEqual(results[0].column("smallint")?.int16, 1)
XCTAssertEqual(results[0].column("smallint_min")?.int16, -32_767)
XCTAssertEqual(results[0].column("smallint_max")?.int16, 32_767)
XCTAssertEqual(results[0].column("int")?.int32, 1)
XCTAssertEqual(results[0].column("int_min")?.int32, -2_147_483_647)
XCTAssertEqual(results[0].column("int_max")?.int32, 2_147_483_647)
XCTAssertEqual(results[0].column("bigint")?.int64, 1)
XCTAssertEqual(results[0].column("bigint_min")?.int64, -9_223_372_036_854_775_807)
XCTAssertEqual(results[0].column("bigint_max")?.int64, 9_223_372_036_854_775_807)
default: XCTFail("Should be exactly one result, but got \(results.count)")
}
}
func testPi() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
struct Pi: Decodable {
let text: String
let numeric_string: String
let numeric_decimal: Decimal
let double: Double
let float: Float
}
let results = try conn.query("""
SELECT
pi()::TEXT as text,
pi()::NUMERIC as numeric_string,
pi()::NUMERIC as numeric_decimal,
pi()::FLOAT8 as double,
pi()::FLOAT4 as float
""").wait()
switch results.count {
case 1:
//print(results[0])
XCTAssertEqual(results[0].column("text")?.string?.hasPrefix("3.14159265"), true)
XCTAssertEqual(results[0].column("numeric_string")?.string?.hasPrefix("3.14159265"), true)
XCTAssertTrue(results[0].column("numeric_decimal")?.decimal?.isLess(than: 3.14159265358980) ?? false)
XCTAssertFalse(results[0].column("numeric_decimal")?.decimal?.isLess(than: 3.14159265358978) ?? true)
XCTAssertTrue(results[0].column("double")?.double?.description.hasPrefix("3.141592") ?? false)
XCTAssertTrue(results[0].column("float")?.float?.description.hasPrefix("3.141592") ?? false)
default: XCTFail("Should be exactly one result, but got \(results.count)")
}
}
func testUUID() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
struct Model: Decodable {
let id: UUID
let string: String
}
let results = try conn.query("""
SELECT
'123e4567-e89b-12d3-a456-426655440000'::UUID as id,
'123e4567-e89b-12d3-a456-426655440000'::UUID as string
""").wait()
switch results.count {
case 1:
//print(results[0])
XCTAssertEqual(results[0].column("id")?.uuid, UUID(uuidString: "123E4567-E89B-12D3-A456-426655440000"))
XCTAssertEqual(UUID(uuidString: results[0].column("id")?.string ?? ""), UUID(uuidString: "123E4567-E89B-12D3-A456-426655440000"))
default: XCTFail("Should be exactly one result, but got \(results.count)")
}
}
func testDates() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
struct Dates: Decodable {
var date: Date
var timestamp: Date
var timestamptz: Date
}
let results = try conn.query("""
SELECT
'2016-01-18 01:02:03 +0042'::DATE as date,
'2016-01-18 01:02:03 +0042'::TIMESTAMP as timestamp,
'2016-01-18 01:02:03 +0042'::TIMESTAMPTZ as timestamptz
""").wait()
switch results.count {
case 1:
//print(results[0])
XCTAssertEqual(results[0].column("date")?.date?.description, "2016-01-18 00:00:00 +0000")
XCTAssertEqual(results[0].column("timestamp")?.date?.description, "2016-01-18 01:02:03 +0000")
XCTAssertEqual(results[0].column("timestamptz")?.date?.description, "2016-01-18 00:20:03 +0000")
default: XCTFail("Should be exactly one result, but got \(results.count)")
}
}
/// https://github.com/vapor/nio-postgres/issues/20
func testBindInteger() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
_ = try conn.simpleQuery("drop table if exists person;").wait()
_ = try conn.simpleQuery("create table person(id serial primary key, first_name text, last_name text);").wait()
defer { _ = try! conn.simpleQuery("drop table person;").wait() }
let id = PostgresData(int32: 5)
_ = try conn.query("SELECT id, first_name, last_name FROM person WHERE id = $1", [id]).wait()
}
// https://github.com/vapor/nio-postgres/issues/21
func testAverageLengthNumeric() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let rows = try conn.query("select avg(length('foo')) as average_length").wait()
let length = try XCTUnwrap(rows[0].column("average_length")?.double)
XCTAssertEqual(length, 3.0)
}
func testNumericParsing() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let rows = try conn.query("""
select
'1234.5678'::numeric as a,
'-123.456'::numeric as b,
'123456.789123'::numeric as c,
'3.14159265358979'::numeric as d,
'10000'::numeric as e,
'0.00001'::numeric as f,
'100000000'::numeric as g,
'0.000000001'::numeric as h,
'100000000000'::numeric as i,
'0.000000000001'::numeric as j,
'123000000000'::numeric as k,
'0.000000000123'::numeric as l,
'0.5'::numeric as m
""").wait()
XCTAssertEqual(rows[0].column("a")?.string, "1234.5678")
XCTAssertEqual(rows[0].column("b")?.string, "-123.456")
XCTAssertEqual(rows[0].column("c")?.string, "123456.789123")
XCTAssertEqual(rows[0].column("d")?.string, "3.14159265358979")
XCTAssertEqual(rows[0].column("e")?.string, "10000")
XCTAssertEqual(rows[0].column("f")?.string, "0.00001")
XCTAssertEqual(rows[0].column("g")?.string, "100000000")
XCTAssertEqual(rows[0].column("h")?.string, "0.000000001")
XCTAssertEqual(rows[0].column("k")?.string, "123000000000")
XCTAssertEqual(rows[0].column("l")?.string, "0.000000000123")
XCTAssertEqual(rows[0].column("m")?.string, "0.5")
}
func testSingleNumericParsing() throws {
// this seemingly duped test is useful for debugging numeric parsing
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let numeric = "790226039477542363.6032384900176272473"
let rows = try conn.query("""
select
'\(numeric)'::numeric as n
""").wait()
XCTAssertEqual(rows[0].column("n")?.string, numeric)
}
func testRandomlyGeneratedNumericParsing() throws {
// this test takes a long time to run
try XCTSkipUnless(Self.shouldRunLongRunningTests)
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
for _ in 0..<1_000_000 {
let integer = UInt.random(in: UInt.min..<UInt.max)
let fraction = UInt.random(in: UInt.min..<UInt.max)
let number = "\(integer).\(fraction)"
.trimmingCharacters(in: CharacterSet(["0"]))
let rows = try conn.query("""
select
'\(number)'::numeric as n
""").wait()
XCTAssertEqual(rows[0].column("n")?.string, number)
}
}
func testNumericSerialization() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let a = PostgresNumeric(string: "123456.789123")!
let b = PostgresNumeric(string: "-123456.789123")!
let c = PostgresNumeric(string: "3.14159265358979")!
let rows = try conn.query("""
select
$1::numeric::text as a,
$2::numeric::text as b,
$3::numeric::text as c
""", [
.init(numeric: a),
.init(numeric: b),
.init(numeric: c)
]).wait()
XCTAssertEqual(rows[0].column("a")?.string, "123456.789123")
XCTAssertEqual(rows[0].column("b")?.string, "-123456.789123")
XCTAssertEqual(rows[0].column("c")?.string, "3.14159265358979")
}
func testMoney() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let rows = try conn.query("""
select
'0'::money as a,
'0.05'::money as b,
'0.23'::money as c,
'3.14'::money as d,
'12345678.90'::money as e
""").wait()
XCTAssertEqual(rows[0].column("a")?.string, "0.00")
XCTAssertEqual(rows[0].column("b")?.string, "0.05")
XCTAssertEqual(rows[0].column("c")?.string, "0.23")
XCTAssertEqual(rows[0].column("d")?.string, "3.14")
XCTAssertEqual(rows[0].column("e")?.string, "12345678.90")
}
func testIntegerArrayParse() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let rows = try conn.query("""
select
'{1,2,3}'::int[] as array
""").wait()
XCTAssertEqual(rows[0].column("array")?.array(of: Int.self), [1, 2, 3])
}
func testEmptyIntegerArrayParse() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let rows = try conn.query("""
select
'{}'::int[] as array
""").wait()
XCTAssertEqual(rows[0].column("array")?.array(of: Int.self), [])
}
func testNullIntegerArrayParse() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let rows = try conn.query("""
select
null::int[] as array
""").wait()
XCTAssertEqual(rows[0].column("array")?.array(of: Int.self), nil)
}
func testIntegerArraySerialize() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let rows = try conn.query("""
select
$1::int8[] as array
""", [
PostgresData(array: [1, 2, 3])
]).wait()
XCTAssertEqual(rows[0].column("array")?.array(of: Int.self), [1, 2, 3])
}
func testEmptyIntegerArraySerialize() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let rows = try conn.query("""
select
$1::int8[] as array
""", [
PostgresData(array: [] as [Int])
]).wait()
XCTAssertEqual(rows[0].column("array")?.array(of: Int.self), [])
}
func testBoolSerialize() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
do {
let rows = try conn.query("select $1::bool as bool", [true]).wait()
XCTAssertEqual(rows[0].column("bool")?.bool, true)
}
do {
let rows = try conn.query("select $1::bool as bool", [false]).wait()
XCTAssertEqual(rows[0].column("bool")?.bool, false)
}
do {
let rows = try conn.simpleQuery("select true::bool as bool").wait()
XCTAssertEqual(rows[0].column("bool")?.bool, true)
}
do {
let rows = try conn.simpleQuery("select false::bool as bool").wait()
XCTAssertEqual(rows[0].column("bool")?.bool, false)
}
}
func testBytesSerialize() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let rows = try conn.query("select $1::bytea as bytes", [
PostgresData(bytes: [1, 2, 3])
]).wait()
XCTAssertEqual(rows[0].column("bytes")?.bytes, [1, 2, 3])
}
func testJSONBSerialize() throws {
struct Object: Codable {
let foo: Int
let bar: Int
}
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
do {
let postgresData = try PostgresData(jsonb: Object(foo: 1, bar: 2))
let rows = try conn.query("select $1::jsonb as jsonb", [postgresData]).wait()
let object = try rows[0].column("jsonb")?.jsonb(as: Object.self)
XCTAssertEqual(object?.foo, 1)
XCTAssertEqual(object?.bar, 2)
}
do {
let rows = try conn.query("select jsonb_build_object('foo',1,'bar',2) as jsonb").wait()
let object = try rows[0].column("jsonb")?.jsonb(as: Object.self)
XCTAssertEqual(object?.foo, 1)
XCTAssertEqual(object?.bar, 2)
}
}
func testJSONBConvertible() throws {
struct Object: PostgresJSONBCodable {
let foo: Int
let bar: Int
}
XCTAssertEqual(Object.postgresDataType, .jsonb)
let postgresData = Object(foo: 1, bar: 2).postgresData
XCTAssertEqual(postgresData?.type, .jsonb)
let object = Object(postgresData: postgresData!)
XCTAssertEqual(object?.foo, 1)
XCTAssertEqual(object?.bar, 2)
}
func testRemoteTLSServer() throws {
// postgres://uymgphwj:7_tHbREdRwkqAdu4KoIS7hQnNxr8J1LA@elmer.db.elephantsql.com:5432/uymgphwj
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { try! elg.syncShutdownGracefully() }
let conn = try PostgresConnection.connect(
to: SocketAddress.makeAddressResolvingHost("elmer.db.elephantsql.com", port: 5432),
tlsConfiguration: .forClient(certificateVerification: .none),
serverHostname: "elmer.db.elephantsql.com",
on: elg.next()
).wait()
try! conn.authenticate(
username: "uymgphwj",
database: "uymgphwj",
password: "7_tHbREdRwkqAdu4KoIS7hQnNxr8J1LA"
).wait()
defer { try? conn.close().wait() }
let rows = try conn.simpleQuery("SELECT version()").wait()
XCTAssertEqual(rows.count, 1)
let version = rows[0].column("version")?.string
XCTAssertEqual(version?.contains("PostgreSQL"), true)
}
func testFailingTLSConnectionClosesConnection() throws {
// There was a bug (https://github.com/vapor/postgres-nio/issues/133) where we would hit
// an assert because we didn't close the connection. This test should succeed without hitting
// the assert
// postgres://uymgphwj:7_tHbREdRwkqAdu4KoIS7hQnNxr8J1LA@elmer.db.elephantsql.com:5432/uymgphwj
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { try! elg.syncShutdownGracefully() }
// We should get an error because you can't use an IP address for SNI, but we shouldn't bomb out by
// hitting the assert
XCTAssertThrowsError(
try PostgresConnection.connect(
to: SocketAddress.makeAddressResolvingHost("elmer.db.elephantsql.com", port: 5432),
tlsConfiguration: .forClient(certificateVerification: .fullVerification),
serverHostname: "34.228.73.168",
on: elg.next()
).wait()
)
// If we hit this, we're all good
XCTAssertTrue(true)
}
func testInvalidPassword() throws {
let conn = try PostgresConnection.testUnauthenticated(on: eventLoop).wait()
defer { try? conn.close().wait() }
let auth = conn.authenticate(username: "invalid", database: "invalid", password: "bad")
XCTAssertThrowsError(_ = try auth.wait()) { error in
guard let postgresError = try? XCTUnwrap(error as? PostgresError) else { return }
XCTAssert(postgresError.code == .invalidPassword || postgresError.code == .invalidAuthorizationSpecification)
}
}
func testColumnsInJoin() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let dateInTable1 = Date(timeIntervalSince1970: 1234)
let dateInTable2 = Date(timeIntervalSince1970: 5678)
_ = try conn.simpleQuery("DROP TABLE IF EXISTS \"table1\"").wait()
_ = try conn.simpleQuery("""
CREATE TABLE table1 (
"id" int8 NOT NULL,
"table2_id" int8,
"intValue" int8,
"stringValue" text,
"dateValue" timestamptz,
PRIMARY KEY ("id")
);
""").wait()
defer { _ = try! conn.simpleQuery("DROP TABLE \"table1\"").wait() }
_ = try conn.simpleQuery("DROP TABLE IF EXISTS \"table2\"").wait()
_ = try conn.simpleQuery("""
CREATE TABLE table2 (
"id" int8 NOT NULL,
"intValue" int8,
"stringValue" text,
"dateValue" timestamptz,
PRIMARY KEY ("id")
);
""").wait()
defer { _ = try! conn.simpleQuery("DROP TABLE \"table2\"").wait() }
_ = try conn.simpleQuery("INSERT INTO table1 VALUES (12, 34, 56, 'stringInTable1', to_timestamp(1234))").wait()
_ = try conn.simpleQuery("INSERT INTO table2 VALUES (34, 78, 'stringInTable2', to_timestamp(5678))").wait()
let row = try conn.query("""
SELECT
"table1"."id" as "t1_id",
"table1"."intValue" as "t1_intValue",
"table1"."dateValue" as "t1_dateValue",
"table1"."stringValue" as "t1_stringValue",
"table2"."id" as "t2_id",
"table2"."intValue" as "t2_intValue",
"table2"."dateValue" as "t2_dateValue",
"table2"."stringValue" as "t2_stringValue",
*
FROM table1 INNER JOIN table2 ON table1.table2_id = table2.id
""").wait().first!
XCTAssertEqual(12, row.column("t1_id")?.int)
XCTAssertEqual(34, row.column("table2_id")?.int)
XCTAssertEqual(56, row.column("t1_intValue")?.int)
XCTAssertEqual("stringInTable1", row.column("t1_stringValue")?.string)
XCTAssertEqual(dateInTable1, row.column("t1_dateValue")?.date)
XCTAssertEqual(34, row.column("t2_id")?.int)
XCTAssertEqual(78, row.column("t2_intValue")?.int)
XCTAssertEqual("stringInTable2", row.column("t2_stringValue")?.string, "stringInTable2")
XCTAssertEqual(dateInTable2, row.column("t2_dateValue")?.date)
}
func testStringArrays() throws {
let query = """
SELECT
$1::uuid as "id",
$2::bigint as "revision",
$3::timestamp as "updated_at",
$4::timestamp as "created_at",
$5::text as "name",
$6::text[] as "countries",
$7::text[] as "languages",
$8::text[] as "currencies"
"""
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let rows = try conn.query(query, [
PostgresData(uuid: UUID(uuidString: "D2710E16-EB07-4FD6-A87E-B1BE41C9BD3D")!),
PostgresData(int: Int(0)),
PostgresData(date: Date(timeIntervalSince1970: 0)),
PostgresData(date: Date(timeIntervalSince1970: 0)),
PostgresData(string: "Foo"),
PostgresData(array: ["US"]),
PostgresData(array: ["en"]),
PostgresData(array: ["USD", "DKK"]),
]).wait()
XCTAssertEqual(rows[0].column("countries")?.array(of: String.self), ["US"])
XCTAssertEqual(rows[0].column("languages")?.array(of: String.self), ["en"])
XCTAssertEqual(rows[0].column("currencies")?.array(of: String.self), ["USD", "DKK"])
}
func testBindDate() throws {
// https://github.com/vapor/postgres-nio/issues/53
let date = Date(timeIntervalSince1970: 1571425782)
let query = """
SELECT $1::json as "date"
"""
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
XCTAssertThrowsError(_ = try conn.query(query, [.init(date: date)]).wait()) { error in
guard let postgresError = try? XCTUnwrap(error as? PostgresError) else { return }
guard case let .server(serverError) = postgresError else {
XCTFail("Expected a .serverError but got \(postgresError)")
return
}
XCTAssertEqual(serverError.fields[.routine], "transformTypeCast")
}
}
func testBindCharString() throws {
// https://github.com/vapor/postgres-nio/issues/53
let query = """
SELECT $1::char as "char"
"""
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let rows = try conn.query(query, [.init(string: "f")]).wait()
XCTAssertEqual(rows[0].column("char")?.string, "f")
}
func testBindCharUInt8() throws {
// https://github.com/vapor/postgres-nio/issues/53
let query = """
SELECT $1::char as "char"
"""
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let rows = try conn.query(query, [.init(uint8: 42)]).wait()
XCTAssertEqual(rows[0].column("char")?.string, "*")
}
func testDoubleArraySerialization() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let doubles: [Double] = [3.14, 42]
let rows = try conn.query("""
select
$1::double precision[] as doubles
""", [
.init(array: doubles)
]).wait()
XCTAssertEqual(rows[0].column("doubles")?.array(of: Double.self), doubles)
}
// https://github.com/vapor/postgres-nio/issues/42
func testUInt8Serialization() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let rows = try conn.query("""
select
$1::"char" as int
""", [
.init(uint8: 5)
]).wait()
XCTAssertEqual(rows[0].column("int")?.uint8, 5)
}
func testMessageDecoder() throws {
let sample: [UInt8] = [
0x52, // R - authentication
0x00, 0x00, 0x00, 0x0C, // length = 12
0x00, 0x00, 0x00, 0x05, // md5
0x01, 0x02, 0x03, 0x04, // salt
0x4B, // B - backend key data
0x00, 0x00, 0x00, 0x0C, // length = 12
0x05, 0x05, 0x05, 0x05, // process id
0x01, 0x01, 0x01, 0x01, // secret key
]
var input = ByteBufferAllocator().buffer(capacity: 0)
input.writeBytes(sample)
let output: [PostgresMessage] = [
PostgresMessage(identifier: .authentication, bytes: [
0x00, 0x00, 0x00, 0x05,
0x01, 0x02, 0x03, 0x04,
]),
PostgresMessage(identifier: .backendKeyData, bytes: [
0x05, 0x05, 0x05, 0x05,
0x01, 0x01, 0x01, 0x01,
])
]
try XCTUnwrap(ByteToMessageDecoderVerifier.verifyDecoder(
inputOutputPairs: [(input, output)],
decoderFactory: {
PostgresMessageDecoder()
}
))
}
func testPreparedQuery() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let prepared = try conn.prepare(query: "SELECT 1 as one;").wait()
let rows = try prepared.execute().wait()
XCTAssertEqual(rows.count, 1)
let value = rows[0].column("one")
XCTAssertEqual(value?.int, 1)
}
func testPrepareQueryClosure() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let x = conn.prepare(query: "SELECT $1::text as foo;", handler: { query in
let a = query.execute(["a"])
let b = query.execute(["b"])
let c = query.execute(["c"])
return EventLoopFuture.whenAllSucceed([a, b, c], on: conn.eventLoop)
})
let rows = try x.wait()
XCTAssertEqual(rows.count, 3)
XCTAssertEqual(rows[0][0].column("foo")?.string, "a")
XCTAssertEqual(rows[1][0].column("foo")?.string, "b")
XCTAssertEqual(rows[2][0].column("foo")?.string, "c")
}
// https://github.com/vapor/postgres-nio/issues/122
func testPreparedQueryNoResults() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
_ = try conn.simpleQuery("DROP TABLE IF EXISTS \"table_no_results\"").wait()
_ = try conn.simpleQuery("""
CREATE TABLE table_no_results (
"id" int8 NOT NULL,
"stringValue" text,
PRIMARY KEY ("id")
);
""").wait()
defer { _ = try! conn.simpleQuery("DROP TABLE \"table_no_results\"").wait() }
_ = try conn.prepare(query: "DELETE FROM \"table_no_results\" WHERE id = $1").wait()
}
// https://github.com/vapor/postgres-nio/issues/71
func testChar1Serialization() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let rows = try conn.query("""
select
'5'::char(1) as one,
'5'::char(2) as two
""").wait()
XCTAssertEqual(rows[0].column("one")?.uint8, 53)
XCTAssertEqual(rows[0].column("one")?.int16, 53)
XCTAssertEqual(rows[0].column("one")?.string, "5")
XCTAssertEqual(rows[0].column("two")?.uint8, nil)
XCTAssertEqual(rows[0].column("two")?.int16, nil)
XCTAssertEqual(rows[0].column("two")?.string, "5 ")
}
func testUserDefinedType() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
_ = try conn.query("DROP TYPE IF EXISTS foo").wait()
_ = try conn.query("CREATE TYPE foo AS ENUM ('bar', 'qux')").wait()
defer {
_ = try! conn.query("DROP TYPE foo").wait()
}
let res = try conn.query("SELECT 'qux'::foo as foo").wait()
XCTAssertEqual(res[0].column("foo")?.string, "qux")
}
func testNullBind() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let res = try conn.query("SELECT $1::text as foo", [String?.none.postgresData!]).wait()
XCTAssertEqual(res[0].column("foo")?.string, nil)
}
func testUpdateMetadata() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
_ = try conn.simpleQuery("DROP TABLE IF EXISTS test_table").wait()
_ = try conn.simpleQuery("CREATE TABLE test_table(pk int PRIMARY KEY)").wait()
_ = try conn.simpleQuery("INSERT INTO test_table VALUES(1)").wait()
try conn.query("DELETE FROM test_table", onMetadata: { metadata in
XCTAssertEqual(metadata.command, "DELETE")
XCTAssertEqual(metadata.oid, nil)
XCTAssertEqual(metadata.rows, 1)
}, onRow: { _ in }).wait()
let rows = try conn.query("DELETE FROM test_table").wait()
XCTAssertEqual(rows.metadata.command, "DELETE")
XCTAssertEqual(rows.metadata.oid, nil)
XCTAssertEqual(rows.metadata.rows, 0)
}
func testTooManyBinds() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let binds = [PostgresData].init(repeating: .null, count: Int(Int16.max) + 1)
do {
_ = try conn.query("SELECT version()", binds).wait()
XCTFail("Should have failed")
} catch PostgresError.connectionClosed { }
}
func testRemoteClose() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
try conn.channel.close().wait()
}
// https://github.com/vapor/postgres-nio/issues/113
func testVaryingCharArray() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let res = try conn.query(#"SELECT '{"foo", "bar", "baz"}'::VARCHAR[] as foo"#).wait()
XCTAssertEqual(res[0].column("foo")?.array(of: String.self), ["foo", "bar", "baz"])
}
// https://github.com/vapor/postgres-nio/issues/115
func testSetTimeZone() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
_ = try conn.simpleQuery("SET TIME ZONE INTERVAL '+5:45' HOUR TO MINUTE").wait()
_ = try conn.query("SET TIME ZONE INTERVAL '+5:45' HOUR TO MINUTE").wait()
}
func testIntegerConversions() throws {
let conn = try PostgresConnection.test(on: eventLoop).wait()
defer { try! conn.close().wait() }
let rows = try conn.query("""
select
'a'::char as test8,
'-32768'::smallint as min16,
'32767'::smallint as max16,
'-2147483648'::integer as min32,
'2147483647'::integer as max32,
'-9223372036854775808'::bigint as min64,
'9223372036854775807'::bigint as max64
""").wait()
XCTAssertEqual(rows[0].column("test8")?.uint8, 97)
XCTAssertEqual(rows[0].column("test8")?.int16, 97)
XCTAssertEqual(rows[0].column("test8")?.int32, 97)