Repository navigation
Expand file tree
/
Copy pathend2end_pg.rb
More file actions
executable file
·1625 lines (1475 loc) · 72.3 KB
/
Copy pathend2end_pg.rb
File metadata and controls
executable file
·1625 lines (1475 loc) · 72.3 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
#!/usr/bin/env ruby
gem 'minitest' # ensures using the gem, and not the built-in MT
require 'minitest/autorun'
require 'logger'
require 'stringio'
require 'async'
require 'async/barrier'
require 'bigdecimal'
require 'date'
require 'ipaddr'
require 'securerandom'
require 'timeout'
EXAMPLES_DIR = File.expand_path(ENV.fetch('EXAMPLES_DIR', File.expand_path('../examples', __dir__))).freeze
require_relative 'consts'
require_relative 'rbs_types'
require_relative 'app_helpers'
require 'delegate'
require 'json'
require 'uri'
require File.join(EXAMPLES_DIR, 'pg/query_sql')
RbsTypes.install(Object.const_get("#{ENV.fetch('EXAMPLES_MODULE', 'PgCodegen')}::QuerySql"),
File.join(EXAMPLES_DIR, 'pg/query_sql.rbs'))
# End-to-end tests of the code generated for examples/authors/postgresql, against a real PostgreSQL server.
class TestPg < Minitest::Test
include Object.const_get(ENV.fetch('EXAMPLES_MODULE', 'PgCodegen'))
include AppHelpers
TEXT_PARTS = ["é", '🙂', "\t", "\n", "\r", '\\', "'", '"', ',', '\N', 'NULL'].freeze
def row_for(klass, values)
klass.new(**klass.members.zip(values).to_h)
end
def assert_row_layout(row)
if defined?(::Data) && row.is_a?(::Data)
assert_predicate row, :frozen?
assert_equal row.to_h, row.with.to_h
else
assert_kind_of ::Struct, row
assert row.class.keyword_init?
refute_predicate row, :frozen?
end
case row
in { id:, name: }
assert_kind_of ::Integer, id
assert_equal row.name, name
else
flunk 'row did not support keyword pattern matching'
end
end
def row_value(row, member)
actual_member = row.class.members.find { |name| name == member.to_sym || name == :"#{member}_" }
row.public_send(actual_member)
end
def seeded_random
seed = Integer(ENV.fetch('SEED', '1793729686'))
warn "SEED=#{seed} #{self.class}"
Random.new(seed)
end
def random_text(random)
Array.new(random.rand(1..8)) { TEXT_PARTS.fetch(random.rand(TEXT_PARTS.length)) }.join
end
def old_timeout_version?
defined?(::Timeout::VERSION) && (::Timeout::VERSION.split('.').map(&:to_i) <=> [0, 4]).negative?
end
def return_from_transaction(name)
@query_sql.transaction do |tx|
tx.create_author_return_id(name: name, bio: nil)
return :returned
end
end
def pg_params
{
'dbname' => ENV['DB_NAME'],
'host' => ENV['POSTGRES_HOST'],
'port' => ENV['POSTGRES_PORT'], # optional
'user' => ENV['DB_USER'],
'password' => ENV['DB_PASS']
}.compact
end
def setup
@query_sql = QuerySql.connect(pg_params)
@query_sql.truncate_authors # cascades to books
@query_sql.truncate_test_tables
@query_sql.truncate_exceptions
end
def create_author(name, bio = nil)
@query_sql.create_author_return_id(name: name, bio: bio)
end
# ---- :one / :many / :exec -------------------------------------------------------------------------------------
def test_flow
first = @query_sql.create_author(name: Consts::BOJACK_AUTHOR, bio: Consts::BOJACK_THEME)
assert_equal row_for(CreateAuthorRow, [first.id, Consts::BOJACK_AUTHOR, Consts::BOJACK_THEME]), first
second = @query_sql.create_author(name: Consts::DR_SEUSS_AUTHOR, bio: Consts::DR_SEUSS_QUOTE)
assert_equal row_for(GetAuthorRow, [first.id, Consts::BOJACK_AUTHOR, Consts::BOJACK_THEME]),
@query_sql.get_author(id: first.id)
assert_nil @query_sql.get_author(id: -1)
assert_equal [second.name, first.name].sort, @query_sql.list_authors.map(&:name).sort
@query_sql.delete_author(id: first.id)
assert_equal [row_for(ListAuthorsRow, [second.id, Consts::DR_SEUSS_AUTHOR, Consts::DR_SEUSS_QUOTE])],
@query_sql.list_authors
end
def test_rows_and_embedded_models_have_supported_layout_and_pattern_matching
first = create_author('pattern')
create_author('pattern')
assert_row_layout(@query_sql.get_author(id: first))
embedded = @query_sql.get_duplicate_authors.first.author
assert_row_layout(embedded)
end
def test_nullable_columns_round_trip_nil
id = create_author('no bio', nil)
assert_nil @query_sql.get_author(id: id).bio
end
def test_one_on_empty_table_returns_nil
assert_nil @query_sql.get_postgres_numeric_types
end
# ---- :execlastid / :execrows -----------------------------------------------------------------------------------
def test_execlastid_returns_the_returning_column
first = create_author('a')
second = create_author('b')
assert_kind_of ::Integer, first
assert_equal first + 1, second
end
def test_execlastid_with_uuid_key
author_id = create_author('a')
book_id = @query_sql.create_book(name: 'book', author_id: author_id)
assert_match(/\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/, book_id)
end
def test_execresult_returns_rows_affected
result = @query_sql.create_author_result(name: 'a', bio: nil)
assert_equal 1, result.rows_affected
assert_nil result.last_insert_id
end
def test_batch_commands_run_once_per_args
@query_sql.create_authors_batch([CreateAuthorsBatchArgs.new(name: 'a', bio: nil), CreateAuthorsBatchArgs.new(name: 'b', bio: 'x'),
CreateAuthorsBatchArgs.new(name: 'b', bio: 'y')])
ids = @query_sql.list_authors.to_h { |a| [a.bio, a.id] }
found = @query_sql.get_author_batch([GetAuthorBatchArgs.new(id: ids['x']), GetAuthorBatchArgs.new(id: -1)])
assert_equal ['b', nil], found.map { |r| r&.name }
many = @query_sql.list_authors_by_name_batch(%w[b a c].map { |n| ListAuthorsByNameBatchArgs.new(name: n) })
assert_equal [%w[x y], [nil], []], many.map { |rows| rows.map(&:bio) }
assert_nil @query_sql.create_authors_batch([])
end
def test_batches_are_not_atomic_without_a_transaction
rows = [CreateAuthorsBatchArgs.new(name: 'committed', bio: nil), CreateAuthorsBatchArgs.new(name: nil, bio: nil)]
assert_raises(PG::NotNullViolation) { @query_sql.create_authors_batch(rows) }
assert_equal ['committed'], @query_sql.list_authors.map(&:name)
end
def test_execrows_returns_affected_row_count
create_author('with bio', 'bio')
create_author('with bio 2', 'bio')
create_author('without bio', nil)
assert_equal 2, @query_sql.update_author_bios(bio: 'new bio')
assert_equal 0, (@query_sql.truncate_authors; @query_sql.update_author_bios(bio: 'x'))
end
# ---- sqlc.arg / sqlc.narg --------------------------------------------------------------------------------------
def test_named_args_limit_and_offset
%w[a b c d].each { |name| create_author(name) }
page = @query_sql.list_authors_paged(limit: 2, offset: 1)
assert_equal %w[b c], page.map(&:name)
end
def test_nullable_named_arg
%w[abc abd xyz].each { |name| create_author(name) }
all = @query_sql.get_author_by_name_pattern(name_pattern: nil)
assert_equal 3, all.size
ab = @query_sql.get_author_by_name_pattern(name_pattern: 'ab%')
assert_equal %w[abc abd], ab.map(&:name).sort
end
def test_required_argument_after_optional_named_argument
%w[alpha beta gamma].each { |name| create_author(name) }
assert_equal %w[alpha beta gamma], @query_sql.get_authors_by_optional_name(lim: 10).map(&:name).sort
assert_equal ['beta'], @query_sql.get_authors_by_optional_name(lim: 10, name_filter: 'beta').map(&:name)
end
# ---- array parameters (PostgreSQL has no sqlc.slice) -----------------------------------------------------------
def test_array_parameters
ids = %w[a b c].map { |name| create_author(name) }
assert_equal ids.first(2).sort,
@query_sql.get_authors_by_ids(param1: ids.first(2)).map(&:id).sort
assert_empty @query_sql.get_authors_by_ids(param1: [])
both = @query_sql.get_authors_by_ids_and_names(param1: ids, param2: %w[b c x])
assert_equal %w[b c], both.map(&:name).sort
end
# ---- :copyfrom -------------------------------------------------------------------------------------------------
def test_copyfrom
rows = [
CopyAuthorsArgs.new(name: 'plain', bio: 'bio'),
CopyAuthorsArgs.new(name: 'no bio', bio: nil),
CopyAuthorsArgs.new(name: "tab\there, \"quotes\", back\\slash\nnewline", bio: 'special')
]
@query_sql.copy_authors(rows)
stored = @query_sql.list_authors.to_h { |a| [a.name, a.bio] }
assert_equal rows.to_h { |r| [r.name, r.bio] }, stored
end
def test_copyfrom_many_rows_and_none
@query_sql.copy_authors([])
assert_empty @query_sql.list_authors
@query_sql.copy_authors((1..2500).map { |i| CopyAuthorsArgs.new(name: "author #{i}", bio: nil) })
assert_equal 2500, @query_sql.list_authors.size
end
def test_copyfrom_keeps_the_case_of_table_and_column_names
@query_sql.copy_mixed_case_copies([CopyMixedCaseCopiesArgs.new(id: 1, some_value: 'a'), CopyMixedCaseCopiesArgs.new(id: 2, some_value: nil)])
assert_equal [[1, 'a'], [2, nil]], @query_sql.list_mixed_case_copies.map { |r| r.to_h.values }
end
def test_copyfrom_takes_any_enumerable
@query_sql.copy_authors([].lazy)
assert_empty @query_sql.list_authors
@query_sql.copy_authors((1..3).lazy.map { |i| CopyAuthorsArgs.new(name: "lazy #{i}", bio: nil) })
@query_sql.copy_authors(::Enumerator.new { |rows| rows << CopyAuthorsArgs.new(name: 'streamed', bio: nil) })
assert_equal ['lazy 1', 'lazy 2', 'lazy 3', 'streamed'], @query_sql.list_authors.map(&:name).sort
end
# HasAuthors -> has_authors: the query's own name, RuboCop's Naming/PredicatePrefix is disabled for it
def test_predicate_like_query_names_are_kept
assert_equal false, @query_sql.has_authors!.has_any
create_author('someone')
assert_equal true, @query_sql.has_authors.has_any
end
# ---- sqlc.embed ------------------------------------------------------------------------------------------------
def test_embed
first = create_author('a', 'bio a')
second = create_author('b')
@query_sql.create_book(name: 'book 1', author_id: first)
@query_sql.create_book(name: 'book 2', author_id: second)
rows = @query_sql.list_all_authors_books
assert_equal %w[a b], rows.map { |r| r.author.name }
assert_equal ['book 1', 'book 2'], rows.map { |r| r.book.name }
assert_instance_of Author, rows.first.author
assert_instance_of Book, rows.first.book
assert_equal row_for(Author, [first, 'a', 'bio a']), rows.first.author
assert_equal first, rows.first.book.author_id
end
def test_embed_with_plain_columns
id = create_author('a', 'bio')
@query_sql.create_book(name: 'the book', author_id: id)
row = @query_sql.get_authors_by_book_name(name: 'the book').first
assert_equal [id, 'a', 'bio'], [row.id, row.name, row.bio]
assert_equal 'the book', row.book.name
end
def test_same_table_embedded_twice
low = create_author('same')
high = create_author('same')
create_author('different')
rows = @query_sql.get_duplicate_authors
assert_equal 1, rows.size
assert_equal [low, high], [rows.first.author.id, rows.first.author2.id]
end
def test_embed_on_the_outer_side_of_a_join_is_nil_without_a_match
with_book = create_author('a')
create_author('b')
@query_sql.create_book(name: 'book', author_id: with_book)
rows = @query_sql.list_authors_with_optional_book
assert_equal 'book', rows.first.book.name
assert_equal 'b', rows.last.author.name
assert_nil rows.last.book
end
def test_repeated_column_parameters_get_distinct_names
ids = %w[a b c].map { |name| create_author(name) }
assert_equal [ids[1]], @query_sql.list_authors_between_ids(id: ids[0], id_2: ids[2]).map(&:id)
end
def test_keyword_and_method_names_keep_distinct_row_and_argument_names
args = { id: 1, class_: 'class', end_: 'end', hash: 'hash', method: 'method', count: 7,
camel_case: 'camel', http_server: 'server', initialize: 'init' }
@query_sql.insert_name_case(**args)
row = @query_sql.get_name_case(**args)
count_member = row.is_a?(::Struct) ? :count_ : :count
assert_equal %i[id class_ end hash_ method_].concat([count_member, :camelCase, :HTTPServer, :initialize_]), row.class.members
assert_equal %w[class end hash method], %i[class_ end hash_ method_].map { |member| row.public_send(member) }
assert_equal 'init', row.initialize_
assert_equal row, row.with(id: 1) unless row.is_a?(::Struct)
assert_equal 7, row.public_send(count_member)
assert_equal 'camel', row.public_send(:camelCase)
assert_equal 'server', row.public_send(:HTTPServer)
assert_equal 1, @query_sql.get_http_server(id: 1).id
end
# ---- enums -----------------------------------------------------------------------------------------------------
def test_enum_column
@query_sql.create_extended_bio(author_name: 'a', name: 'n1', bio_type: ExtendedBioType::MEMOIR)
@query_sql.create_extended_bio(author_name: 'a', name: 'n2', bio_type: ExtendedBioType::BIOGRAPHY)
row = @query_sql.get_first_extended_bio_by_type(bio_type: 'Biography')
assert_equal row_for(GetFirstExtendedBioByTypeRow, %w[a n2 Biography]), row
assert_equal %w[Autobiography Biography Memoir], ExtendedBioType::ALL
labels = @query_sql.instance_variable_get(:@db).with do |c|
c.exec("SELECT unnest(enum_range(NULL::extended.bio_type))::text").column_values(0)
end
assert_equal labels, ExtendedBioType::ALL
assert_equal 'Memoir', ExtendedBioType::MEMOIR
end
def test_enum_rejects_unknown_values
assert_raises(PG::InvalidTextRepresentation) do
@query_sql.create_extended_bio(author_name: 'a', name: 'n', bio_type: 'Fiction')
end
end
# ---- transactions ----------------------------------------------------------------------------------------------
def test_transaction_commits_and_returns_the_block_value
result = @query_sql.transaction do |tx|
id = tx.create_author_return_id(name: 'in tx', bio: nil)
tx.create_book(name: 'book', author_id: id)
:done
end
assert_equal :done, result
assert_equal ['in tx'], @query_sql.list_authors.map(&:name)
end
def test_transaction_rolls_back_on_error
assert_raises(RuntimeError) do
@query_sql.transaction do |tx|
tx.create_author_return_id(name: 'rolled back', bio: nil)
raise 'boom'
end
end
assert_empty @query_sql.list_authors
end
def test_transaction_is_isolated_until_commit
other = QuerySql.connect(pg_params)
@query_sql.transaction do |tx|
tx.create_author_return_id(name: 'uncommitted', bio: nil)
assert_empty other.list_authors
end
assert_equal ['uncommitted'], other.list_authors.map(&:name)
end
# ---- connection pool -------------------------------------------------------------------------------------------
def test_every_pooled_connection_prepares_its_own_statements
if ENV['PREPARED_STATEMENTS'] == 'false'
assert_empty @query_sql.list_authors
return
end
pooled = QuerySql.connect(pg_params, size: 3)
pool = pooled.instance_variable_get(:@db)
held = Queue.new
gates = Array.new(3) { Queue.new }
# three threads, each holding a different connection until it is released
threads = gates.map do |gate|
::Thread.new do
pool.with do |client|
held << client.object_id
gate.pop
pooled.list_authors # reentrant: runs on the connection this thread holds
end
end
end
assert_equal 3, Array.new(3) { held.pop }.uniq.size, 'expected three distinct connections'
# one after the other, so that the first run has finished (and "remembered" the statement) before the next one
threads.zip(gates).each do |thread, gate|
gate << true
assert_equal [], thread.value
end
end
# ---- more data types -------------------------------------------------------------------------------------------
def assert_matches_rbs(row, row_type)
assert_empty RbsTypes.mismatches(row, File.join(EXAMPLES_DIR, 'pg/query_sql.rbs'), row_type)
end
def test_numeric_types_round_trip
numeric = BigDecimal('12345678901234567890.1234567890123456789012345678901234567890')
@query_sql.insert_postgres_numeric_types(
c_boolean: true, c_smallint: -32_768, c_integer: 2_147_483_647, c_bigint: -9_223_372_036_854_775_808,
c_decimal: BigDecimal('1.5'), c_numeric: numeric, c_real: 1.25, c_double_precision: 0.1 + 0.2, c_money: '12.50',
c_oid: 42, c_positive: 5
)
row = @query_sql.get_postgres_numeric_types
assert_equal [true, -32_768, 2_147_483_647, -9_223_372_036_854_775_808, BigDecimal('1.5'), numeric, 1.25, 0.1 + 0.2, 42, 5],
row.to_h.values_at(:c_boolean, :c_smallint, :c_integer, :c_bigint, :c_decimal, :c_numeric, :c_real,
:c_double_precision, :c_oid, :c_positive)
assert_kind_of ::Integer, row.c_serial
assert_includes row.c_money, '12.50'
assert_matches_rbs(row, 'GetPostgresNumericTypesRow')
assert_raises(PG::CheckViolation) do
@query_sql.insert_postgres_numeric_types(**row.to_h.except(:c_serial).merge(c_money: nil, c_positive: -1))
end
end
def test_string_types_round_trip
@query_sql.insert_postgres_string_types(
c_char: 'a', c_varchar: 'quote \' 🙂', c_text: "line\nbreak \\ back", c_not_null: 'nn',
c_xml: '<greeting>Grüße🙂</greeting>', c_tsvector: 'a:1 b:2', c_ltree: 'Top.Science.Astronomy',
c_hstore: '"key"=>"value", "quoted"=>"two"', c_regclass: 'authors'
)
row = @query_sql.get_postgres_string_types
assert_equal ['a', 'quote \' 🙂', "line\nbreak \\ back", 'nn', '<greeting>Grüße🙂</greeting>', "'a':1 'b':2",
'Top.Science.Astronomy', '"key"=>"value", "quoted"=>"two"', 'authors'], row.to_h.values
assert_matches_rbs(row, 'GetPostgresStringTypesRow')
end
def test_datetime_types_round_trip
timestamp = ::Time.new(2024, 2, 3, 4, 5, Rational(6_123_456, 1_000_000), '+05:30')
timestamptz = ::Time.new(2024, 3, 4, 5, 6, Rational(7_654_321, 1_000_000), '-07:00')
@query_sql.insert_postgres_datetime_types(
c_date: Date.new(2024, 1, 2), c_time: '03:04:05.678', c_timetz: '12:00:00+02', c_timestamp: timestamp,
c_timestamptz: timestamptz, c_interval: '1 year 2 mons 3 days 04:05:06.789'
)
row = @query_sql.get_postgres_datetime_types
assert_equal [Date.new(2024, 1, 2), '03:04:05.678', '12:00:00+02', timestamp, timestamptz, '1 year 2 mons 3 days 04:05:06.789'],
row.to_h.values
assert_equal timestamptz.utc, row.c_timestamptz.utc
assert_matches_rbs(row, 'GetPostgresDatetimeTypesRow')
end
def array_type_values(id, **values)
{ id: id, c_boolean: nil, c_double_precision: nil, c_text_array: nil, c_integer_array: nil, c_uuid_array: nil,
c_bio_type_array: nil, c_bytea_array: nil, c_jsonb_array: nil, c_numeric_array: nil, c_timestamptz_array: nil,
c_integer_matrix: nil, c_text_matrix: nil }.merge(values)
end
# includes arrays of types without a decoder (uuid[], enum[]) and multi-dimensional arrays
def test_array_types_round_trip
uuids = [SecureRandom.uuid, SecureRandom.uuid]
matrix = [['', "a,'b", nil], ["back\\slash", "line\nbreak", '🙂']]
values = array_type_values(
1, c_text_array: ['x', nil, 'y z'], c_integer_array: [1, 2, 3], c_uuid_array: uuids,
c_bio_type_array: %w[Memoir Biography], c_bytea_array: ["\x00\xFF".b], c_jsonb_array: [{ 'a' => [1, nil] }],
c_numeric_array: [BigDecimal('1.25')], c_integer_matrix: [[1, 2], [3, 4]], c_text_matrix: matrix
)
@query_sql.insert_postgres_array_types(**values)
row = @query_sql.list_postgres_array_types.first
assert_equal values, row.to_h
assert_matches_rbs(row, 'ListPostgresArrayTypesRow')
end
def test_special_types_round_trip
uuid = '0e52082f-bf69-480e-aff0-1a85139a1074'
@query_sql.insert_postgres_special_types(
c_bytea: "\x00\xFF\x01".b, c_bit: '1', c_varbit: '1010010110', c_uuid: uuid, c_inet: '10.1.2.3',
c_cidr: '192.168.1.0/24', c_macaddr: '08:00:2b:01:02:03', c_point: '(1,2)', c_int4range: '[1,3]', c_dimensions: '(3,4)'
)
row = @query_sql.get_postgres_special_types
assert_equal ["\x00\xFF\x01".b, '1', '1010010110', uuid, IPAddr.new('10.1.2.3'), '08:00:2b:01:02:03', '(1,2)', '[1,4)', '(3,4)'],
row.to_h.except(:c_cidr).values.tap { |v| v[0] = v[0].b }
assert_equal ['192.168.1.0', 24], [row.c_cidr.to_s, row.c_cidr.prefix]
assert_matches_rbs(row, 'GetPostgresSpecialTypesRow')
end
def test_type_families_round_trip_nil
@query_sql.insert_postgres_numeric_types(c_boolean: nil, c_smallint: nil, c_integer: nil, c_bigint: nil, c_decimal: nil,
c_numeric: nil, c_real: nil, c_double_precision: nil, c_money: nil, c_oid: nil,
c_positive: nil)
@query_sql.insert_postgres_string_types(c_char: nil, c_varchar: nil, c_text: nil, c_not_null: 'x', c_xml: nil,
c_tsvector: nil, c_ltree: nil, c_hstore: nil, c_regclass: nil)
@query_sql.insert_postgres_datetime_types(c_date: nil, c_time: nil, c_timetz: nil, c_timestamp: nil, c_timestamptz: nil,
c_interval: nil)
@query_sql.insert_postgres_array_types(**array_type_values(1))
@query_sql.insert_postgres_special_types(c_bytea: nil, c_bit: nil, c_varbit: nil, c_uuid: nil, c_inet: nil, c_cidr: nil,
c_macaddr: nil, c_point: nil, c_int4range: nil, c_dimensions: nil)
{
'GetPostgresNumericTypesRow' => @query_sql.get_postgres_numeric_types.to_h.except(:c_serial),
'GetPostgresStringTypesRow' => @query_sql.get_postgres_string_types.to_h.except(:c_not_null),
'GetPostgresDatetimeTypesRow' => @query_sql.get_postgres_datetime_types.to_h,
'ListPostgresArrayTypesRow' => @query_sql.list_postgres_array_types.first.to_h.except(:id),
'GetPostgresSpecialTypesRow' => @query_sql.get_postgres_special_types.to_h
}.each { |type, values| assert_equal [nil], values.values.uniq, type }
assert_equal 'x', @query_sql.get_postgres_string_types.c_not_null
end
def test_json_and_jsonb_parameters_round_trip_all_json_value_types
cases = [
[true, true], [false, false], [7, 7], [1.25, 1.25],
[{ 'nested' => [1, { 'nil' => nil }] }, { 'nested' => [1, { 'nil' => nil }] }],
[[1, false, { 'a' => 'b' }], [1, false, { 'a' => 'b' }]],
['{"from":"json text"}', { 'from' => 'json text' }],
[{ 'a' => [1, 2, { 'b' => nil }] }.to_json, { 'a' => [1, 2, { 'b' => nil }] }], [nil, nil]
]
cases.each_with_index do |(value, expected), id|
@query_sql.insert_postgres_json_types(id: id, c_json: value, c_jsonb: value)
row = @query_sql.get_postgres_json_types(id: id)
if expected.nil?
assert_nil row.c_json
assert_nil row.c_jsonb
else
assert_equal expected, row.c_json
assert_equal expected, row.c_jsonb
end
end
@query_sql.insert_postgres_json_types(id: 10, c_json: true, c_jsonb: true)
@query_sql.toggle_postgres_jsonb_type(id: 10)
assert_equal false, @query_sql.get_postgres_json_types(id: 10).c_jsonb
end
def raw_client
PG.connect(**pg_params.transform_keys(&:to_sym))
end
# values written by SQL literals, not by generated parameters, are read with the declared types
def test_declared_types_match_runtime_values
client = raw_client
client.exec(<<~SQL)
INSERT INTO postgres_numeric_types VALUES (true, 2, 4, 5, 6, 1.5, 2.5, 1.25, 2.25, 3.5, 7, 8);
INSERT INTO postgres_string_types VALUES ('a', 'b', 'c', 'd', '<x/>', 'e', 'f.g', 'h=>i', 'authors');
INSERT INTO postgres_datetime_types VALUES ('2024-01-02', '03:04:05', '03:04:05+02', '2024-01-02 03:04:05',
'2024-01-02 03:04:05+00', '1 day');
INSERT INTO postgres_json_types VALUES (1, '{"a": 1}', '[1]');
INSERT INTO postgres_array_types VALUES (1, true, 1.5, '{a}', '{1}', '{0e52082f-bf69-480e-aff0-1a85139a1074}',
'{Memoir}', '{"\\\\x0102"}', '{"{}"}', '{1.5}', '{"2024-01-02 03:04:05+00"}', '{{1}}', '{{a}}');
INSERT INTO postgres_special_types VALUES ('\\x0102', B'1', B'101', '0e52082f-bf69-480e-aff0-1a85139a1074',
'::1', '10.0.0.0/8', '08:00:2b:01:02:03', '(1,2)', '[1,3)', '(3,4)');
SQL
rows = { 'GetPostgresNumericTypesRow' => @query_sql.get_postgres_numeric_types,
'GetPostgresStringTypesRow' => @query_sql.get_postgres_string_types,
'GetPostgresDatetimeTypesRow' => @query_sql.get_postgres_datetime_types,
'GetPostgresJSONTypesRow' => @query_sql.get_postgres_json_types(id: 1),
'ListPostgresArrayTypesRow' => @query_sql.list_postgres_array_types.first,
'GetPostgresSpecialTypesRow' => @query_sql.get_postgres_special_types }
rows.each { |type, row| assert_matches_rbs(row, type) }
assert_equal BigDecimal('1.5'), rows['GetPostgresNumericTypesRow'].c_decimal
assert_equal Date.new(2024, 1, 2), rows['GetPostgresDatetimeTypesRow'].c_date
assert_equal [{ 'a' => 1 }, [1]], rows['GetPostgresJSONTypesRow'].to_h.values
assert_equal ["\x01\x02".b], rows['ListPostgresArrayTypesRow'].c_bytea_array
assert_equal "\x01\x02".b, rows['GetPostgresSpecialTypesRow'].c_bytea.b
ensure
client&.close
end
def test_declared_types_match_runtime_values_for_nulls
client = raw_client
client.exec(<<~SQL)
INSERT INTO postgres_numeric_types (c_integer) VALUES (NULL);
INSERT INTO postgres_string_types (c_text) VALUES (NULL);
INSERT INTO postgres_datetime_types (c_date) VALUES (NULL);
INSERT INTO postgres_json_types (id) VALUES (1);
INSERT INTO postgres_array_types (id) VALUES (1);
INSERT INTO postgres_special_types (c_uuid) VALUES (NULL);
SQL
numeric = @query_sql.get_postgres_numeric_types
assert_matches_rbs(numeric, 'GetPostgresNumericTypesRow')
assert_equal [nil], numeric.to_h.except(:c_serial).values.uniq # c_serial is filled by its default
string = @query_sql.get_postgres_string_types
assert_matches_rbs(string, 'GetPostgresStringTypesRow')
assert_equal 'x', string.c_not_null # filled by its default
assert_equal [nil], string.to_h.except(:c_not_null).values.uniq
{ 'GetPostgresDatetimeTypesRow' => @query_sql.get_postgres_datetime_types,
'GetPostgresJSONTypesRow' => @query_sql.get_postgres_json_types(id: 1),
'GetPostgresSpecialTypesRow' => @query_sql.get_postgres_special_types }.each do |type, row|
assert_matches_rbs(row, type)
assert_equal [nil], row.to_h.values.uniq, type
end
arrays = @query_sql.list_postgres_array_types.first
assert_matches_rbs(arrays, 'ListPostgresArrayTypesRow')
assert_equal [nil], arrays.to_h.except(:id).values.uniq
ensure
client&.close
end
def test_nested_transaction_rolls_back_to_a_savepoint
@query_sql.transaction do |tx|
tx.create_author_return_id(name: 'outer', bio: nil)
assert_raises(RuntimeError) do
tx.transaction do |inner|
inner.create_author_return_id(name: 'inner, rolled back', bio: nil)
raise 'boom'
end
end
tx.create_author_return_id(name: 'outer 2', bio: nil)
end
assert_equal ['outer', 'outer 2'], @query_sql.list_authors.map(&:name).sort
end
def test_nested_transaction_commits_with_the_outer_one
@query_sql.transaction do |tx|
assert_equal 7, tx.transaction { |inner| inner.create_author_return_id(name: 'inner', bio: nil) && 7 }
end
assert_equal ['inner'], @query_sql.list_authors.map(&:name)
assert_raises(RuntimeError) do
@query_sql.transaction do |tx|
tx.transaction { |inner| inner.create_author_return_id(name: 'inner 2', bio: nil) }
raise 'boom'
end
end
assert_equal ['inner'], @query_sql.list_authors.map(&:name)
end
def test_missing_keyword_arguments_are_errors
assert_raises(ArgumentError) { @query_sql.get_author }
assert_raises(ArgumentError) { @query_sql.get_author(id: 1, unknown: 2) }
end
def test_named_nullable_arguments_are_optional
create_author('abc')
assert_equal ['abc'], @query_sql.get_author_by_name_pattern.map(&:name)
end
def test_transaction_isolation_level
@query_sql.transaction(isolation: :serializable) do
level = @query_sql.instance_variable_get(:@db).with { |c| c.exec('SHOW transaction_isolation').getvalue(0, 0) }
assert_equal 'serializable', level
end
assert_raises(ArgumentError) { @query_sql.transaction(isolation: :bogus) { flunk } }
@query_sql.instance_variable_get(:@db).with { |c| assert_equal PG::PQTRANS_IDLE, c.transaction_status }
end
def test_queries_can_share_an_injected_pool
pool = ::ConnectionPool.new(size: 2) { PG.connect(**pg_params.transform_keys(&:to_sym)) }
first = QuerySql.new(pool)
second = QuerySql.new(pool) # same connections and statement names: must not clash
id = first.create_author_return_id(name: 'shared', bio: nil)
assert_equal 'shared', second.get_author(id: id).name
assert_equal 'shared', first.get_author(id: id).name
assert_kind_of ::Integer, id # connection was configured on first use (typed results)
end
# The generated classes are plain synchronous Ruby; under the Fiber scheduler (the `async` gem) the pooled
# queries overlap because pg and connection_pool are fiber aware.
def test_queries_run_concurrently_under_the_fiber_scheduler
queries = QuerySql.connect(pg_params, size: 5)
started = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
Sync do
barrier = Async::Barrier.new
5.times { barrier.async { queries.pause_query(seconds: 0.4) } }
barrier.wait
end
elapsed = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - started
assert_operator elapsed, :<, 1.5, "5 x 0.4s queries took #{elapsed}s: they ran one after the other"
names = Sync do
barrier = Async::Barrier.new
tasks = Array.new(10) { |i| barrier.async { queries.create_author_return_id(name: "fiber #{i}", bio: nil) } }
barrier.wait
tasks.map(&:wait)
end
assert_equal 10, names.uniq.size
assert_equal 10, @query_sql.list_authors.size
end
# ---- Ruby conventions: bang finders, logger ----------------------------------------------------------------
def test_bang_finder_raises_not_found
id = create_author('a', nil)
assert_equal 'a', @query_sql.get_author!(id: id).name
error = assert_raises(QuerySql::NotFound) { @query_sql.get_author!(id: -1) }
assert_equal 'GetAuthor returned no row', error.message
assert_nil @query_sql.get_author(id: -1) # the plain finder still returns nil
end
def test_every_one_query_has_a_bang_method
methods = File.read(File.join(EXAMPLES_DIR, 'pg/query_sql.rbs')).scan(/^\s+def (\w+):.* -> \w+Row\?$/).flatten
refute_empty methods
methods.each { |name| assert_respond_to @query_sql, "#{name}!" }
end
def test_logger_receives_query_names_and_durations
io = StringIO.new
logged = QuerySql.connect(pg_params, logger: Logger.new(io, level: Logger::DEBUG))
logged.get_author(id: -987654321)
logged.transaction { |tx| tx.list_authors }
assert_match(/DEBUG -- .*QuerySql#get_author \(\d+\.\dms\)/, io.string)
assert_match(/DEBUG -- .*QuerySql#transaction \(\d+\.\dms\)/, io.string)
refute_includes io.string, '-987654321'
@query_sql.list_authors # no logger: nothing is logged, nothing breaks
end
# ---- models ----------------------------------------------------------------------------------------------------
def test_models_are_generated_for_every_table
assert_equal %i[id name bio], Author.members
assert_equal %i[id name author_id description], Book.members
assert_equal %i[author_name name bio_type], ExtendedBio.members
end
# ---- regressions -----------------------------------------------------------------------------------------------
def test_bytea_json_arrays_and_string_arrays_round_trip
binary = "\x00\xFFa\x00".b
uid = SecureRandom.uuid
row = @query_sql.create_regression!(id: 1, order: 'o', payload: binary, doc: [1, { 'a' => 'é' }],
bio_types: ['Memoir', nil], uids: [uid])
assert_equal [binary, [1, { 'a' => 'é' }], ['Memoir', nil], [uid]], [row.payload, row.doc, row.bio_types, row.uids]
assert_equal row.to_h, @query_sql.list_regressions.first.to_h
end
def test_execlastid_is_nil_when_no_row_is_returned
assert_equal 7, @query_sql.create_regression_if_new(id: 7, order: 'first')
assert_nil @query_sql.create_regression_if_new(id: 7, order: 'again')
end
def test_copyfrom_quotes_identifiers_and_encodes_bytea_and_json
binary = "\x00\x01\xFE".b
@query_sql.copy_regressions([CopyRegressionsArgs.new(id: 1, order: 'a', payload: binary, doc: [1, 2]),
CopyRegressionsArgs.new(id: 2, order: 'b', payload: nil, doc: nil),
CopyRegressionsArgs.new(id: 3, order: 'true', payload: nil, doc: true),
CopyRegressionsArgs.new(id: 4, order: 'false', payload: nil, doc: false)])
assert_equal [[1, 'a', binary, [1, 2]], [2, 'b', nil, nil], [3, 'true', nil, true], [4, 'false', nil, false]],
@query_sql.list_regressions.map { |r| [r.id, r.order, r.payload, r.doc] }
end
def test_casted_narg_is_optional
@query_sql.create_regression_if_new(id: 1, order: 'a')
@query_sql.create_regression_if_new(id: 2, order: 'b')
assert_equal [1, 2], @query_sql.list_regressions.map(&:id)
assert_equal [2], @query_sql.list_regressions(order_name: 'b').map(&:id)
end
def test_prepared_statements_survive_a_connection_reset
client = PG.connect(**pg_params)
query_sql = QuerySql.new(::ConnectionPool.new(size: 1) { client })
query_sql.list_authors
client.reset
assert_empty query_sql.list_authors
ensure
client&.close
end
def test_batch_parameters_encode_bytea_and_json
binary = "\x00\xFFb".b
@query_sql.create_regressions_batch([CreateRegressionsBatchArgs.new(id: 1, order: 'a', payload: binary, doc: [1, { 'k' => 'é' }]),
CreateRegressionsBatchArgs.new(id: 2, order: 'b', payload: nil, doc: nil),
CreateRegressionsBatchArgs.new(id: 3, order: 'c', payload: nil, doc: true),
CreateRegressionsBatchArgs.new(id: 4, order: 'd', payload: nil, doc: false)])
assert_equal [[1, binary, [1, { 'k' => 'é' }]], [2, nil, nil], [3, nil, true], [4, nil, false]],
@query_sql.list_regressions.map { |r| [r.id, r.payload, r.doc] }
end
def test_transaction_whose_failed_statement_was_rescued_raises_instead_of_committing
@query_sql.create_regression(id: 1, order: 'taken', payload: nil, doc: nil, bio_types: nil, uids: nil)
assert_raises(PG::InFailedSqlTransaction) do
@query_sql.transaction do |tx|
tx.create_author_return_id(name: 'lost', bio: nil)
begin
tx.create_regression(id: 1, order: 'duplicate', payload: nil, doc: nil, bio_types: nil, uids: nil)
rescue PG::UniqueViolation
nil
end
end
end
assert_empty @query_sql.list_authors
assert_equal ['taken'], @query_sql.list_regressions.map(&:order)
end
def test_transaction_left_with_break_or_throw_commits_and_frees_the_connection
pooled = QuerySql.connect(pg_params, size: 1)
2.times do |i|
pooled.transaction do |tx|
tx.create_author_return_id(name: "break #{i}", bio: nil)
break
end
end
catch(:out) do
pooled.transaction do |tx|
tx.create_author_return_id(name: 'throw', bio: nil)
throw :out
end
end
pooled.instance_variable_get(:@db).with { |client| assert_equal PG::PQTRANS_IDLE, client.transaction_status }
expected = old_timeout_version? ? [] : ['break 0', 'break 1', 'throw']
assert_equal expected, @query_sql.list_authors.map(&:name).sort
end
# Also with Ruby 3.1's bundled timeout 0.2.0 (CI variant bundled-timeout), whose Timeout leaves the block with throw:
# the cancelled query must not replace Timeout::Error with PG::InFailedSqlTransaction.
def test_transaction_timeout_rolls_back_and_keeps_connection_usable
%i[database ruby_sleep nested_database].each do |kind|
assert_raises(Timeout::Error) do
Timeout.timeout(0.05) do
@query_sql.transaction do |tx|
tx.create_author_return_id(name: kind.to_s, bio: nil)
case kind
when :database then tx.pause_query(seconds: 0.2)
when :ruby_sleep then sleep(0.2)
else tx.transaction { |inner| inner.pause_query(seconds: 0.2) }
end
end
end
end
assert_empty @query_sql.list_authors
@query_sql.create_author_return_id(name: 'usable', bio: nil)
assert_equal ['usable'], @query_sql.list_authors.map(&:name)
@query_sql.truncate_authors
end
end
def test_transaction_nonlocal_exit_semantics_follow_timeout_version
break_value = @query_sql.transaction do |tx|
tx.create_author_return_id(name: 'break', bio: nil)
break :break_value
end
assert_equal :break_value, break_value
assert_equal :returned, return_from_transaction('return')
thrown = catch(:transaction_exit) do
@query_sql.transaction do |tx|
tx.create_author_return_id(name: 'throw', bio: nil)
throw :transaction_exit, :throw_value
end
end
assert_equal :throw_value, thrown
expected = old_timeout_version? ? [] : %w[break return throw]
assert_equal expected, @query_sql.list_authors.map(&:name).sort
end
def test_thread_kill_rolls_back_and_leaves_connection_idle
started = Queue.new
thread = ::Thread.new do
@query_sql.transaction do |tx|
tx.create_author_return_id(name: 'killed', bio: nil)
started << true
sleep(5)
end
end
started.pop
thread.kill
thread.join
assert_empty @query_sql.list_authors
@query_sql.instance_variable_get(:@db).with do |client|
assert_equal PG::PQTRANS_IDLE, client.transaction_status
end
end
def wait_until_query_runs(admin, pattern)
deadline = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) + 5
sql = "SELECT 1 FROM pg_stat_activity WHERE state = 'active' AND query LIKE $1 AND pid <> pg_backend_pid()"
until admin.exec_params(sql, [pattern]).ntuples.positive?
flunk "no running query like #{pattern}" if ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) > deadline
sleep 0.01
end
end
# A kill skips `rescue`: the running query used to stay on the pooled connection (PQTRANS_ACTIVE), so the next
# transaction sent SAVEPOINT instead of BEGIN, and a kill inside a transaction waited for the query to finish.
def test_thread_kill_during_a_query_cancels_it_and_leaves_the_connection_idle
queries = QuerySql.connect(pg_params, size: 1)
admin = PG.connect(**pg_params)
[false, true].each do |in_transaction|
worker = ::Thread.new do
if in_transaction
queries.transaction do |tx|
tx.create_author_return_id(name: 'killed', bio: nil)
tx.pause_query(seconds: 5)
end
else
queries.pause_query(seconds: 5)
end
end
wait_until_query_runs(admin, '%pg_sleep%')
started = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
worker.kill.join
assert_operator ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - started, :<, 2, "in_transaction: #{in_transaction}"
queries.instance_variable_get(:@db).with do |client|
assert_equal PG::PQTRANS_IDLE, client.transaction_status
end
assert_equal %w[after], queries.transaction { |tx| tx.create_author_return_id(name: 'after', bio: nil) && tx.list_authors.map(&:name) }
queries.truncate_authors
end
ensure
admin&.close
end
def test_nested_savepoint_rolls_back_while_outer_transaction_commits
@query_sql.transaction do |tx|
tx.create_author_return_id(name: 'outer before', bio: nil)
assert_raises(RuntimeError) do
tx.transaction do |inner|
inner.create_author_return_id(name: 'nested', bio: nil)
raise 'rollback savepoint'
end
end
tx.create_author_return_id(name: 'outer after', bio: nil)
end
assert_equal ['outer after', 'outer before'], @query_sql.list_authors.map(&:name).sort
end
def test_parent_query_object_calls_are_part_of_same_thread_transaction
assert_raises(RuntimeError) do
@query_sql.transaction do |tx|
tx.create_author_return_id(name: 'tx', bio: nil)
@query_sql.create_author_return_id(name: 'parent', bio: nil)
raise 'rollback outer transaction'
end
end
assert_empty @query_sql.list_authors
end
# ---- final sweep fixes: :batch* / :copyfrom parameter encoding, nested rescued errors, dead connections ---------
def batch_type_rows
stamp = ::Time.at(1_700_000_000, 123_456, :usec)
[{ id: 1, c_boolean: true, c_double_precision: 115_610.38729014261, c_bytea_array: ["\x00\xFF".b, nil],
c_jsonb_array: [{ 'a' => 1 }, [1, 2], nil], c_integer_array: [], c_numeric_array: [nil, BigDecimal('1.25')],
c_timestamptz_array: [nil, stamp] },
{ id: 2, c_boolean: false, c_double_precision: 1.0e-305, c_bytea_array: [], c_jsonb_array: [], c_integer_array: [nil],
c_numeric_array: [], c_timestamptz_array: [] },
{ id: 3, c_boolean: nil, c_double_precision: nil, c_bytea_array: nil, c_jsonb_array: nil, c_integer_array: nil,
c_numeric_array: nil, c_timestamptz_array: nil }]
end
def assert_batch_types_round_trip(rows)
got = @query_sql.list_postgres_array_types.map(&:to_h)
assert_equal rows.size, got.size
rows.zip(got).each do |want, row|
want.each do |key, value|
if key == :c_timestamptz_array && value
assert_equal value.map { |t| t&.to_r }, row[key].map { |t| t&.to_r }, "#{want[:id]} #{key}"
elsif value.nil?
assert_nil row[key], "#{want[:id]} #{key}"
else
assert_equal value, row[key], "#{want[:id]} #{key}"
end
end
end
end
def test_copyfrom_encodes_booleans_floats_and_arrays
@query_sql.copy_postgres_array_types(batch_type_rows.map { |r| CopyPostgresArrayTypesArgs.new(**r) })
assert_batch_types_round_trip(batch_type_rows)
end
def test_batchexec_encodes_booleans_floats_and_arrays
@query_sql.insert_postgres_array_types_batch(batch_type_rows.map { |r| InsertPostgresArrayTypesBatchArgs.new(**r) })
assert_batch_types_round_trip(batch_type_rows)
end
def test_array_parameters_need_no_server_side_parameter_types
# what preparedStatements: false sends: exec_params without prepared parameter types
params = [9, nil, nil, @query_sql.send(:encode_array, ["\x00".b], :bytea), @query_sql.send(:encode_array, [{ 'a' => 1 }], :json),
@query_sql.send(:encode_array, []), @query_sql.send(:encode_array, [nil]), @query_sql.send(:encode_array, [nil, ::Time.now])]
@query_sql.instance_variable_get(:@db).with { |client| client.exec_params(INSERT_POSTGRES_ARRAY_TYPES_BATCH_SQL, params) }
row = @query_sql.list_postgres_array_types.first
assert_equal [["\x00".b], [{ 'a' => 1 }], [], [nil]], [row.c_bytea_array, row.c_jsonb_array, row.c_integer_array, row.c_numeric_array]
end
def test_all_caps_parameter_names_are_snake_cased
assert_equal 0, row_value(@query_sql.count_postgres_array_types_above!(min_id: 0), :count)
end
def test_nested_transaction_rescuing_a_failed_statement_rolls_back_only_itself
@query_sql.transaction do |tx|
tx.create_author(name: 'outer', bio: nil)
assert_raises(PG::InFailedSqlTransaction) do
tx.transaction do |inner|
inner.create_author(name: 'inner', bio: nil)
assert_raises(PG::Error) { inner.create_book(name: 'b', author_id: -1) } # foreign key violation
end
end
tx.create_author(name: 'outer 2', bio: nil)
end
assert_equal ['outer', 'outer 2'], @query_sql.list_authors.map(&:name).sort
end
def test_nested_transaction_rejects_an_isolation_level
@query_sql.transaction do |tx|
assert_raises(ArgumentError) { tx.transaction(isolation: :serializable) { nil } }
end
end
def test_a_connection_closed_by_the_server_is_reconnected
pooled = QuerySql.connect(pg_params, size: 1)
assert_equal [], pooled.list_authors
pid = pooled.instance_variable_get(:@db).with(&:backend_pid)
PG.connect(**pg_params.transform_keys(&:to_sym)) { |admin| admin.exec_params('SELECT pg_terminate_backend($1)', [pid]) }
assert_raises(PG::Error) { pooled.list_authors }
assert_equal [], pooled.list_authors
end