forked from rails-sqlserver/activerecord-sqlserver-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoerced_tests.rb
1562 lines (1329 loc) · 57.6 KB
/
coerced_tests.rb
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
# frozen_string_literal: true
require "cases/helper_sqlserver"
require "models/event"
class UniquenessValidationTest < ActiveRecord::TestCase
# So sp_executesql swallows this exception. Run without prepared to see it.
coerce_tests! :test_validate_uniqueness_with_limit
def test_validate_uniqueness_with_limit_coerced
connection.unprepared_statement do
assert_raise(ActiveRecord::ValueTooLong) do
Event.create(title: "abcdefgh")
end
end
end
# So sp_executesql swallows this exception. Run without prepared to see it.
coerce_tests! :test_validate_uniqueness_with_limit_and_utf8
def test_validate_uniqueness_with_limit_and_utf8_coerced
connection.unprepared_statement do
assert_raise(ActiveRecord::ValueTooLong) do
Event.create(title: "一二三四五六七八")
end
end
end
# Skip the test if database is case-insensitive.
coerce_tests! :test_validate_case_sensitive_uniqueness_by_default
def test_validate_case_sensitive_uniqueness_by_default_coerced
database_collation = connection.select_one("SELECT collation_name FROM sys.databases WHERE name = 'activerecord_unittest'").values.first
skip if database_collation.include?("_CI_")
original_test_validate_case_sensitive_uniqueness_by_default_coerced
end
end
require "models/event"
module ActiveRecord
class AdapterTest < ActiveRecord::TestCase
# I really don`t think we can support legacy binds.
coerce_tests! :test_select_all_with_legacy_binds
coerce_tests! :test_insert_update_delete_with_legacy_binds
# As far as I can tell, SQL Server does not support null bytes in strings.
coerce_tests! :test_update_prepared_statement
# So sp_executesql swallows this exception. Run without prepared to see it.
coerce_tests! :test_value_limit_violations_are_translated_to_specific_exception
def test_value_limit_violations_are_translated_to_specific_exception_coerced
connection.unprepared_statement do
error = assert_raises(ActiveRecord::ValueTooLong) do
Event.create(title: "abcdefgh")
end
assert_not_nil error.cause
end
end
# Fix randomly failing test. The loading of the model's schema was affecting the test.
coerce_tests! :test_errors_when_an_insert_query_is_called_while_preventing_writes
def test_errors_when_an_insert_query_is_called_while_preventing_writes_coerced
Subscriber.send(:load_schema!)
original_test_errors_when_an_insert_query_is_called_while_preventing_writes
end
end
end
module ActiveRecord
class AdapterTestWithoutTransaction < ActiveRecord::TestCase
# SQL Server does not allow truncation of tables that are referenced by foreign key
# constraints. So manually remove/add foreign keys in test.
coerce_tests! :test_truncate_tables
def test_truncate_tables_coerced
# Remove foreign key constraint to allow truncation.
@connection.remove_foreign_key :authors, :author_addresses
assert_operator Post.count, :>, 0
assert_operator Author.count, :>, 0
assert_operator AuthorAddress.count, :>, 0
@connection.truncate_tables("author_addresses", "authors", "posts")
assert_equal 0, Post.count
assert_equal 0, Author.count
assert_equal 0, AuthorAddress.count
ensure
reset_fixtures("posts", "authors", "author_addresses")
# Restore foreign key constraint.
@connection.add_foreign_key :authors, :author_addresses
end
# SQL Server does not allow truncation of tables that are referenced by foreign key
# constraints. So manually remove/add foreign keys in test.
coerce_tests! :test_truncate_tables_with_query_cache
def test_truncate_tables_with_query_cache
# Remove foreign key constraint to allow truncation.
@connection.remove_foreign_key :authors, :author_addresses
@connection.enable_query_cache!
assert_operator Post.count, :>, 0
assert_operator Author.count, :>, 0
assert_operator AuthorAddress.count, :>, 0
@connection.truncate_tables("author_addresses", "authors", "posts")
assert_equal 0, Post.count
assert_equal 0, Author.count
assert_equal 0, AuthorAddress.count
ensure
reset_fixtures("posts", "authors", "author_addresses")
@connection.disable_query_cache!
# Restore foreign key constraint.
@connection.add_foreign_key :authors, :author_addresses
end
end
end
require "models/topic"
class AttributeMethodsTest < ActiveRecord::TestCase
# Use IFF for boolean statement in SELECT
coerce_tests! %r{typecast attribute from select to false}
def test_typecast_attribute_from_select_to_false_coerced
Topic.create(:title => "Budget")
topic = Topic.all.merge!(:select => "topics.*, IIF (1 = 2, 1, 0) as is_test").first
assert_not_predicate topic, :is_test?
end
# Use IFF for boolean statement in SELECT
coerce_tests! %r{typecast attribute from select to true}
def test_typecast_attribute_from_select_to_true_coerced
Topic.create(:title => "Budget")
topic = Topic.all.merge!(:select => "topics.*, IIF (1 = 1, 1, 0) as is_test").first
assert_predicate topic, :is_test?
end
end
class BasicsTest < ActiveRecord::TestCase
# Use square brackets as SQL Server escaped character
coerce_tests! :test_column_names_are_escaped
def test_column_names_are_escaped_coerced
conn = ActiveRecord::Base.connection
assert_equal "[t]]]", conn.quote_column_name("t]")
end
# Just like PostgreSQLAdapter does.
coerce_tests! :test_respect_internal_encoding
# Caused in Rails v4.2.5 by adding `firm_id` column in this http://git.io/vBfMs
# commit. Trust Rails has this covered.
coerce_tests! :test_find_keeps_multiple_group_values
def test_update_date_time_attributes
Time.use_zone("Eastern Time (US & Canada)") do
topic = Topic.find(1)
time = Time.zone.parse("2017-07-17 10:56")
topic.update!(written_on: time)
assert_equal(time, topic.written_on)
end
end
def test_update_date_time_attributes_with_default_timezone_local
with_env_tz "America/New_York" do
with_timezone_config default: :local do
Time.use_zone("Eastern Time (US & Canada)") do
topic = Topic.find(1)
time = Time.zone.parse("2017-07-17 10:56")
topic.update!(written_on: time)
assert_equal(time, topic.written_on)
end
end
end
end
# SQL Server does not have query for release_savepoint
coerce_tests! %r{an empty transaction does not raise if preventing writes}
test "an empty transaction does not raise if preventing writes coerced" do
ActiveRecord::Base.connection_handler.while_preventing_writes do
assert_queries(1, ignore_none: true) do
Bird.transaction do
ActiveRecord::Base.connection.materialize_transactions
end
end
end
end
end
class BelongsToAssociationsTest < ActiveRecord::TestCase
# Since @client.firm is a single first/top, and we use FETCH the order clause is used.
coerce_tests! :test_belongs_to_does_not_use_order_by
# Square brackets around column name
coerce_tests! :test_belongs_to_with_primary_key_joins_on_correct_column
def test_belongs_to_with_primary_key_joins_on_correct_column_coerced
sql = Client.joins(:firm_with_primary_key).to_sql
assert_no_match(/\[firm_with_primary_keys_companies\]\.\[id\]/, sql)
assert_match(/\[firm_with_primary_keys_companies\]\.\[name\]/, sql)
end
# Asserted SQL to get one row different from original test.
coerce_tests! :test_belongs_to
def test_belongs_to_coerced
client = Client.find(3)
first_firm = companies(:first_firm)
assert_sql(/FETCH NEXT @(\d) ROWS ONLY(.)*@\1 = 1/) do
assert_equal first_firm, client.firm
assert_equal first_firm.name, client.firm.name
end
end
end
module ActiveRecord
class BindParameterTest < ActiveRecord::TestCase
# Same as original coerced test except log is found using `EXEC sp_executesql` wrapper.
coerce_tests! :test_binds_are_logged
def test_binds_are_logged_coerced
sub = Arel::Nodes::BindParam.new(1)
binds = [Relation::QueryAttribute.new("id", 1, Type::Value.new)]
sql = "select * from topics where id = #{sub.to_sql}"
@connection.exec_query(sql, "SQL", binds)
logged_sql = "EXEC sp_executesql N'#{sql}', N'#{sub.to_sql} int', #{sub.to_sql} = 1"
message = @subscriber.calls.find { |args| args[4][:sql] == logged_sql }
assert_equal binds, message[4][:binds]
end
# SQL Server adapter does not use a statement cache as query plans are already reused using `EXEC sp_executesql`.
coerce_tests! :test_statement_cache
coerce_tests! :test_statement_cache_with_query_cache
coerce_tests! :test_statement_cache_with_find
coerce_tests! :test_statement_cache_with_find_by
coerce_tests! :test_statement_cache_with_in_clause
coerce_tests! :test_statement_cache_with_sql_string_literal
end
end
module ActiveRecord
class InstrumentationTest < ActiveRecord::TestCase
# Fix randomly failing test. The loading of the model's schema was affecting the test.
coerce_tests! :test_payload_name_on_load
def test_payload_name_on_load_coerced
Book.send(:load_schema!)
original_test_payload_name_on_load
end
end
end
class CalculationsTest < ActiveRecord::TestCase
# Fix randomly failing test. The loading of the model's schema was affecting the test.
coerce_tests! :test_offset_is_kept
def test_offset_is_kept_coerced
Account.send(:load_schema!)
original_test_offset_is_kept
end
# Are decimal, not integer.
coerce_tests! :test_should_return_decimal_average_of_integer_field
def test_should_return_decimal_average_of_integer_field_coerced
value = Account.average(:id)
assert_equal BigDecimal("3.0").to_s, BigDecimal(value).to_s
end
# Match SQL Server limit implementation
coerce_tests! :test_limit_is_kept
def test_limit_is_kept_coerced
queries = capture_sql_ss { Account.limit(1).count }
assert_equal 1, queries.length
assert_match(/ORDER BY \[accounts\]\.\[id\] ASC OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY.*@0 = 1/, queries.first)
end
# Match SQL Server limit implementation
coerce_tests! :test_limit_with_offset_is_kept
def test_limit_with_offset_is_kept_coerced
queries = capture_sql_ss { Account.limit(1).offset(1).count }
assert_equal 1, queries.length
assert_match(/ORDER BY \[accounts\]\.\[id\] ASC OFFSET @0 ROWS FETCH NEXT @1 ROWS ONLY.*@0 = 1, @1 = 1/, queries.first)
end
# SQL Server needs an alias for the calculated column
coerce_tests! :test_distinct_count_all_with_custom_select_and_order
def test_distinct_count_all_with_custom_select_and_order_coerced
accounts = Account.distinct.select("credit_limit % 10 AS the_limit").order(Arel.sql("credit_limit % 10"))
assert_queries(1) { assert_equal 3, accounts.count(:all) }
assert_queries(1) { assert_equal 3, accounts.load.size }
end
# Leave it up to users to format selects/functions so HAVING works correctly.
coerce_tests! :test_having_with_strong_parameters
end
module ActiveRecord
class Migration
class ChangeSchemaTest < ActiveRecord::TestCase
# Integer.default is a number and not a string
coerce_tests! :test_create_table_with_defaults
def test_create_table_with_defaults_coerce
connection.create_table :testings do |t|
t.column :one, :string, default: "hello"
t.column :two, :boolean, default: true
t.column :three, :boolean, default: false
t.column :four, :integer, default: 1
t.column :five, :text, default: "hello"
end
columns = connection.columns(:testings)
one = columns.detect { |c| c.name == "one" }
two = columns.detect { |c| c.name == "two" }
three = columns.detect { |c| c.name == "three" }
four = columns.detect { |c| c.name == "four" }
five = columns.detect { |c| c.name == "five" }
assert_equal "hello", one.default
assert_equal true, connection.lookup_cast_type_from_column(two).deserialize(two.default)
assert_equal false, connection.lookup_cast_type_from_column(three).deserialize(three.default)
assert_equal 1, four.default
assert_equal "hello", five.default
end
end
end
end
module ActiveRecord
module ConnectionAdapters
class QuoteARBaseTest < ActiveRecord::TestCase
# Use our date format.
coerce_tests! :test_quote_ar_object
def test_quote_ar_object_coerced
value = DatetimePrimaryKey.new(id: @time)
assert_equal "'02-14-2017 12:34:56.79'", @connection.quote(value)
end
# Use our date format.
coerce_tests! :test_type_cast_ar_object
def test_type_cast_ar_object_coerced
value = DatetimePrimaryKey.new(id: @time)
assert_equal "02-14-2017 12:34:56.79", @connection.type_cast(value)
end
end
end
end
module ActiveRecord
class Migration
class ColumnAttributesTest < ActiveRecord::TestCase
# We have a default 4000 varying character limit.
coerce_tests! :test_add_column_without_limit
def test_add_column_without_limit_coerced
add_column :test_models, :description, :string, limit: nil
TestModel.reset_column_information
_(TestModel.columns_hash["description"].limit).must_equal 4000
end
end
end
end
module ActiveRecord
class Migration
class ColumnsTest < ActiveRecord::TestCase
# Our defaults are real 70000 integers vs '70000' strings.
coerce_tests! :test_rename_column_preserves_default_value_not_null
def test_rename_column_preserves_default_value_not_null_coerced
add_column "test_models", "salary", :integer, :default => 70000
default_before = connection.columns("test_models").find { |c| c.name == "salary" }.default
assert_equal 70000, default_before
rename_column "test_models", "salary", "annual_salary"
TestModel.reset_column_information
assert TestModel.column_names.include?("annual_salary")
default_after = connection.columns("test_models").find { |c| c.name == "annual_salary" }.default
assert_equal 70000, default_after
end
# Dropping the column removes the single index.
coerce_tests! :test_remove_column_with_multi_column_index
def test_remove_column_with_multi_column_index_coerced
add_column "test_models", :hat_size, :integer
add_column "test_models", :hat_style, :string, :limit => 100
add_index "test_models", ["hat_style", "hat_size"], :unique => true
assert_equal 1, connection.indexes("test_models").size
remove_column("test_models", "hat_size")
assert_equal [], connection.indexes("test_models").map(&:name)
end
# Choose `StatementInvalid` vs `ActiveRecordError`.
coerce_tests! :test_rename_nonexistent_column
def test_rename_nonexistent_column_coerced
exception = ActiveRecord::StatementInvalid
assert_raise(exception) do
rename_column "test_models", "nonexistent", "should_fail"
end
end
end
end
end
class MigrationTest < ActiveRecord::TestCase
# We do not have do the DecimalWithoutScale type.
coerce_tests! :test_add_table_with_decimals
def test_add_table_with_decimals_coerced
Person.connection.drop_table :big_numbers rescue nil
assert !BigNumber.table_exists?
GiveMeBigNumbers.up
BigNumber.reset_column_information
assert BigNumber.create(
:bank_balance => 1586.43,
:big_bank_balance => BigDecimal("1000234000567.95"),
:world_population => 6000000000,
:my_house_population => 3,
:value_of_e => BigDecimal("2.7182818284590452353602875")
)
b = BigNumber.first
assert_not_nil b
assert_not_nil b.bank_balance
assert_not_nil b.big_bank_balance
assert_not_nil b.world_population
assert_not_nil b.my_house_population
assert_not_nil b.value_of_e
assert_kind_of BigDecimal, b.world_population
assert_equal "6000000000.0", b.world_population.to_s
assert_kind_of Integer, b.my_house_population
assert_equal 3, b.my_house_population
assert_kind_of BigDecimal, b.bank_balance
assert_equal BigDecimal("1586.43"), b.bank_balance
assert_kind_of BigDecimal, b.big_bank_balance
assert_equal BigDecimal("1000234000567.95"), b.big_bank_balance
GiveMeBigNumbers.down
assert_raise(ActiveRecord::StatementInvalid) { BigNumber.first }
end
# For some reason our tests set Rails.@_env which breaks test env switching.
coerce_tests! :test_internal_metadata_stores_environment_when_other_data_exists
coerce_tests! :test_internal_metadata_stores_environment
end
class CoreTest < ActiveRecord::TestCase
# I think fixtures are using the wrong time zone and the `:first`
# `topics`.`bonus_time` attribute of 2005-01-30t15:28:00.00+01:00 is
# getting local EST time for me and set to "09:28:00.0000000".
coerce_tests! :test_pretty_print_persisted
end
module ActiveRecord
module ConnectionAdapters
# Just like PostgreSQLAdapter does.
TypeLookupTest.coerce_all_tests! if defined?(TypeLookupTest)
# All sorts of errors due to how we test. Even setting ENV['RAILS_ENV'] to
# a value of 'default_env' will still show tests failing. Just ignoring all
# of them since we have no monkey in this circus.
MergeAndResolveDefaultUrlConfigTest.coerce_all_tests! if defined?(MergeAndResolveDefaultUrlConfigTest)
ConnectionHandlerTest.coerce_all_tests! if defined?(ConnectionHandlerTest)
end
end
module ActiveRecord
# The original module is hardcoded for PostgreSQL/SQLite/MySQL tests.
module DatabaseTasksSetupper
def setup
@sqlserver_tasks =
Class.new do
def create; end
def drop; end
def purge; end
def charset; end
def collation; end
def structure_dump(*); end
def structure_load(*); end
end.new
$stdout, @original_stdout = StringIO.new, $stdout
$stderr, @original_stderr = StringIO.new, $stderr
end
def with_stubbed_new
ActiveRecord::Tasks::SQLServerDatabaseTasks.stub(:new, @sqlserver_tasks) do
yield
end
end
end
class DatabaseTasksCreateTest < ActiveRecord::TestCase
# Coerce PostgreSQL/SQLite/MySQL tests.
coerce_all_tests!
def test_sqlserver_create
with_stubbed_new do
assert_called(eval("@sqlserver_tasks"), :create) do
ActiveRecord::Tasks::DatabaseTasks.create "adapter" => :sqlserver
end
end
end
end
class DatabaseTasksDropTest < ActiveRecord::TestCase
# Coerce PostgreSQL/SQLite/MySQL tests.
coerce_all_tests!
def test_sqlserver_drop
with_stubbed_new do
assert_called(eval("@sqlserver_tasks"), :drop) do
ActiveRecord::Tasks::DatabaseTasks.drop "adapter" => :sqlserver
end
end
end
end
class DatabaseTasksPurgeTest < ActiveRecord::TestCase
# Coerce PostgreSQL/SQLite/MySQL tests.
coerce_all_tests!
def test_sqlserver_purge
with_stubbed_new do
assert_called(eval("@sqlserver_tasks"), :purge) do
ActiveRecord::Tasks::DatabaseTasks.purge "adapter" => :sqlserver
end
end
end
end
class DatabaseTasksCharsetTest < ActiveRecord::TestCase
# Coerce PostgreSQL/SQLite/MySQL tests.
coerce_all_tests!
def test_sqlserver_charset
with_stubbed_new do
assert_called(eval("@sqlserver_tasks"), :charset) do
ActiveRecord::Tasks::DatabaseTasks.charset "adapter" => :sqlserver
end
end
end
end
class DatabaseTasksCollationTest < ActiveRecord::TestCase
# Coerce PostgreSQL/SQLite/MySQL tests.
coerce_all_tests!
def test_sqlserver_collation
with_stubbed_new do
assert_called(eval("@sqlserver_tasks"), :collation) do
ActiveRecord::Tasks::DatabaseTasks.collation "adapter" => :sqlserver
end
end
end
end
class DatabaseTasksStructureDumpTest < ActiveRecord::TestCase
# Coerce PostgreSQL/SQLite/MySQL tests.
coerce_all_tests!
def test_sqlserver_structure_dump
with_stubbed_new do
assert_called_with(
eval("@sqlserver_tasks"), :structure_dump,
["awesome-file.sql", nil]
) do
ActiveRecord::Tasks::DatabaseTasks.structure_dump({ "adapter" => :sqlserver }, "awesome-file.sql")
end
end
end
end
class DatabaseTasksStructureLoadTest < ActiveRecord::TestCase
# Coerce PostgreSQL/SQLite/MySQL tests.
coerce_all_tests!
def test_sqlserver_structure_load
with_stubbed_new do
assert_called_with(
eval("@sqlserver_tasks"),
:structure_load,
["awesome-file.sql", nil]
) do
ActiveRecord::Tasks::DatabaseTasks.structure_load({ "adapter" => :sqlserver }, "awesome-file.sql")
end
end
end
end
class DatabaseTasksDumpSchemaCacheTest < ActiveRecord::TestCase
# Skip this test with /tmp/my_schema_cache.yml path on Windows.
coerce_tests! :test_dump_schema_cache if RbConfig::CONFIG["host_os"] =~ /mswin|mingw/
end
class DatabaseTasksCreateAllTest < ActiveRecord::TestCase
# We extend `local_database?` so that common VM IPs can be used.
coerce_tests! :test_ignores_remote_databases, :test_warning_for_remote_databases
end
class DatabaseTasksDropAllTest < ActiveRecord::TestCase
# We extend `local_database?` so that common VM IPs can be used.
coerce_tests! :test_ignores_remote_databases, :test_warning_for_remote_databases
end
end
class DefaultScopingTest < ActiveRecord::TestCase
# We are not doing order duplicate removal anymore.
coerce_tests! :test_order_in_default_scope_should_not_prevail
# Use our escaped format in assertion.
coerce_tests! :test_with_abstract_class_scope_should_be_executed_in_correct_context
def test_with_abstract_class_scope_should_be_executed_in_correct_context_coerced
vegetarian_pattern, gender_pattern = [/[lions].[is_vegetarian]/, /[lions].[gender]/]
assert_match vegetarian_pattern, Lion.all.to_sql
assert_match gender_pattern, Lion.female.to_sql
end
end
require "models/post"
require "models/subscriber"
class EachTest < ActiveRecord::TestCase
# Quoting in tests does not cope with bracket quoting.
coerce_tests! :test_find_in_batches_should_quote_batch_order
def test_find_in_batches_should_quote_batch_order_coerced
Post.connection
assert_sql(/ORDER BY \[posts\]\.\[id\]/) do
Post.find_in_batches(:batch_size => 1) do |batch|
assert_kind_of Array, batch
assert_kind_of Post, batch.first
end
end
end
# Quoting in tests does not cope with bracket quoting.
coerce_tests! :test_in_batches_should_quote_batch_order
def test_in_batches_should_quote_batch_order_coerced
Post.connection
assert_sql(/ORDER BY \[posts\]\.\[id\]/) do
Post.in_batches(of: 1) do |relation|
assert_kind_of ActiveRecord::Relation, relation
assert_kind_of Post, relation.first
end
end
end
end
class EagerAssociationTest < ActiveRecord::TestCase
# Use LEN() vs length() function.
coerce_tests! :test_count_with_include
def test_count_with_include_coerced
assert_equal 3, authors(:david).posts_with_comments.where("LEN(comments.body) > 15").references(:comments).count
end
# Use TOP (1) in scope vs limit 1.
coerce_tests! %r{including association based on sql condition and no database column}
end
require "models/topic"
class FinderTest < ActiveRecord::TestCase
# We have implicit ordering, via FETCH.
coerce_tests! %r{doesn't have implicit ordering},
:test_find_doesnt_have_implicit_ordering
# Square brackets around column name
coerce_tests! :test_exists_does_not_select_columns_without_alias
def test_exists_does_not_select_columns_without_alias_coerced
assert_sql(/SELECT\s+1 AS one FROM \[topics\].*OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY.*@0 = 1/i) do
Topic.exists?
end
end
# Assert SQL Server limit implementation
coerce_tests! :test_take_and_first_and_last_with_integer_should_use_sql_limit
def test_take_and_first_and_last_with_integer_should_use_sql_limit_coerced
assert_sql(/OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY.* @0 = 3/) { Topic.take(3).entries }
assert_sql(/OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY.* @0 = 2/) { Topic.first(2).entries }
assert_sql(/OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY.* @0 = 5/) { Topic.last(5).entries }
end
# This fails only when run in the full test suite task. Just taking it out of the mix.
coerce_tests! :test_find_with_order_on_included_associations_with_construct_finder_sql_for_association_limiting_and_is_distinct
# Can not use array condition due to not finding right type and hence fractional second quoting.
coerce_tests! :test_condition_utc_time_interpolation_with_default_timezone_local
def test_condition_utc_time_interpolation_with_default_timezone_local_coerced
with_env_tz "America/New_York" do
with_timezone_config default: :local do
topic = Topic.first
assert_equal topic, Topic.where(written_on: topic.written_on.getutc).first
end
end
end
# Can not use array condition due to not finding right type and hence fractional second quoting.
coerce_tests! :test_condition_local_time_interpolation_with_default_timezone_utc
def test_condition_local_time_interpolation_with_default_timezone_utc_coerced
with_env_tz "America/New_York" do
with_timezone_config default: :utc do
topic = Topic.first
assert_equal topic, Topic.where(written_on: topic.written_on.getlocal).first
end
end
end
end
module ActiveRecord
class Migration
class ForeignKeyTest < ActiveRecord::TestCase
# We do not support :restrict.
coerce_tests! :test_add_on_delete_restrict_foreign_key
def test_add_on_delete_restrict_foreign_key_coerced
assert_raises ArgumentError do
@connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", on_delete: :restrict
end
assert_raises ArgumentError do
@connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", on_update: :restrict
end
end
end
end
end
class HasOneAssociationsTest < ActiveRecord::TestCase
# We use OFFSET/FETCH vs TOP. So we always have an order.
coerce_tests! :test_has_one_does_not_use_order_by
# Asserted SQL to get one row different from original test.
coerce_tests! :test_has_one
def test_has_one_coerced
firm = companies(:first_firm)
first_account = Account.find(1)
assert_sql(/FETCH NEXT @(\d) ROWS ONLY(.)*@\1 = 1/) do
assert_equal first_account, firm.account
assert_equal first_account.credit_limit, firm.account.credit_limit
end
end
end
class HasOneThroughAssociationsTest < ActiveRecord::TestCase
# Asserted SQL to get one row different from original test.
coerce_tests! :test_has_one_through_executes_limited_query
def test_has_one_through_executes_limited_query_coerced
boring_club = clubs(:boring_club)
assert_sql(/FETCH NEXT @(\d) ROWS ONLY(.)*@\1 = 1/) do
assert_equal boring_club, @member.general_club
end
end
end
require "models/company"
class InheritanceTest < ActiveRecord::TestCase
# Rails test required inserting to a identity column.
coerce_tests! :test_a_bad_type_column
def test_a_bad_type_column_coerced
Company.connection.with_identity_insert_enabled("companies") do
Company.connection.insert "INSERT INTO companies (id, #{QUOTED_TYPE}, name) VALUES(100, 'bad_class!', 'Not happening')"
end
assert_raise(ActiveRecord::SubclassNotFound) { Company.find(100) }
end
# Use Square brackets around column name
coerce_tests! :test_eager_load_belongs_to_primary_key_quoting
def test_eager_load_belongs_to_primary_key_quoting_coerced
Account.connection
assert_sql(/\[companies\]\.\[id\] = @0.* @0 = 1/) do
Account.all.merge!(:includes => :firm).find(1)
end
end
end
class LeftOuterJoinAssociationTest < ActiveRecord::TestCase
# Uses || operator in SQL. Just trust core gets value out of this test.
coerce_tests! :test_does_not_override_select
end
require "models/developer"
require "models/computer"
class NestedRelationScopingTest < ActiveRecord::TestCase
# Assert SQL Server limit implementation
coerce_tests! :test_merge_options
def test_merge_options_coerced
Developer.where("salary = 80000").scoping do
Developer.limit(10).scoping do
devs = Developer.all
sql = devs.to_sql
assert_match "(salary = 80000)", sql
assert_match "FETCH NEXT 10 ROWS ONLY", sql
end
end
end
end
require "models/topic"
class PersistenceTest < ActiveRecord::TestCase
# Rails test required updating a identity column.
coerce_tests! :test_update_columns_changing_id
# Rails test required updating a identity column.
coerce_tests! :test_update
def test_update_coerced
topic = Topic.find(1)
assert_not_predicate topic, :approved?
assert_equal "The First Topic", topic.title
topic.update("approved" => true, "title" => "The First Topic Updated")
topic.reload
assert_predicate topic, :approved?
assert_equal "The First Topic Updated", topic.title
topic.update(approved: false, title: "The First Topic")
topic.reload
assert_not_predicate topic, :approved?
assert_equal "The First Topic", topic.title
end
end
require "models/author"
class UpdateAllTest < ActiveRecord::TestCase
# Rails test required updating a identity column.
coerce_tests! :test_update_all_doesnt_ignore_order
def test_update_all_doesnt_ignore_order_coerced
david, mary = authors(:david), authors(:mary)
_(david.id).must_equal 1
_(mary.id).must_equal 2
_(david.name).wont_equal mary.name
assert_sql(/UPDATE.*\(SELECT \[authors\].\[id\] FROM \[authors\].*ORDER BY \[authors\].\[id\]/i) do
Author.where("[id] > 1").order(:id).update_all(name: "Test")
end
_(david.reload.name).must_equal "David"
_(mary.reload.name).must_equal "Test"
end
end
require "models/topic"
module ActiveRecord
class PredicateBuilderTest < ActiveRecord::TestCase
# Same as original test except string has `N` prefix to indicate unicode string.
coerce_tests! :test_registering_new_handlers
def test_registering_new_handlers_coerced
assert_match %r{#{Regexp.escape(topic_title)} ~ N'rails'}i, Topic.where(title: /rails/).to_sql
end
# Same as original test except string has `N` prefix to indicate unicode string.
coerce_tests! :test_registering_new_handlers_for_association
def test_registering_new_handlers_for_association_coerced
assert_match %r{#{Regexp.escape(topic_title)} ~ N'rails'}i, Reply.joins(:topic).where(topics: { title: /rails/ }).to_sql
end
end
end
class PrimaryKeysTest < ActiveRecord::TestCase
# SQL Server does not have query for release_savepoint
coerce_tests! :test_create_without_primary_key_no_extra_query
def test_create_without_primary_key_no_extra_query_coerced
klass = Class.new(ActiveRecord::Base) do
self.table_name = "dashboards"
end
klass.create! # warmup schema cache
assert_queries(2, ignore_none: true) { klass.create! }
end
end
require "models/task"
class QueryCacheTest < ActiveRecord::TestCase
# SQL Server adapter not in list of supported adapters in original test.
coerce_tests! :test_cache_does_not_wrap_results_in_arrays
def test_cache_does_not_wrap_results_in_arrays_coerced
Task.cache do
assert_equal 2, Task.connection.select_value("SELECT count(*) AS count_all FROM tasks")
end
end
# Same as original test except that we expect one query to be performed to retrieve the table's primary key.
# When we generate the SQL for the `find` it includes ordering on the primary key. If we reset the column
# information then the primary key needs to be retrieved from the database again to generate the SQL causing the
# original test's `assert_no_queries` assertion to fail. Assert that the query was to get the primary key.
coerce_tests! :test_query_cached_even_when_types_are_reset
def test_query_cached_even_when_types_are_reset_coerced
Task.cache do
# Warm the cache
Task.find(1)
# Preload the type cache again (so we don't have those queries issued during our assertions)
Task.connection.send(:reload_type_map)
# Clear places where type information is cached
Task.reset_column_information
Task.initialize_find_by_cache
Task.define_attribute_methods
assert_queries(1, ignore_none: true) do
Task.find(1)
end
assert_includes ActiveRecord::SQLCounter.log_all.first, "TC.CONSTRAINT_TYPE = N''PRIMARY KEY''"
end
end
end
require "models/post"
class RelationTest < ActiveRecord::TestCase
# Use LEN vs LENGTH function.
coerce_tests! :test_reverse_order_with_function
def test_reverse_order_with_function_coerced
topics = Topic.order(Arel.sql("LEN(title)")).reverse_order
assert_equal topics(:second).title, topics.first.title
end
# Use LEN vs LENGTH function.
coerce_tests! :test_reverse_order_with_function_other_predicates
def test_reverse_order_with_function_other_predicates_coerced
topics = Topic.order(Arel.sql("author_name, LEN(title), id")).reverse_order
assert_equal topics(:second).title, topics.first.title
topics = Topic.order(Arel.sql("LEN(author_name), id, LEN(title)")).reverse_order
assert_equal topics(:fifth).title, topics.first.title
end
# We have implicit ordering, via FETCH.
coerce_tests! %r{doesn't have implicit ordering}
# We have implicit ordering, via FETCH.
coerce_tests! :test_reorder_with_take
def test_reorder_with_take_coerced
sql_log = capture_sql do
assert Post.order(:title).reorder(nil).take
end
assert sql_log.none? { |sql| /order by [posts].[title]/i.match?(sql) }, "ORDER BY title was used in the query: #{sql_log}"
assert sql_log.all? { |sql| /order by \[posts\]\.\[id\]/i.match?(sql) }, "default ORDER BY ID was not used in the query: #{sql_log}"
end
# We have implicit ordering, via FETCH.
coerce_tests! :test_reorder_with_first
def test_reorder_with_first_coerced
sql_log = capture_sql do
assert Post.order(:title).reorder(nil).first
end
assert sql_log.none? { |sql| /order by [posts].[title]/i.match?(sql) }, "ORDER BY title was used in the query: #{sql_log}"
assert sql_log.all? { |sql| /order by \[posts\]\.\[id\]/i.match?(sql) }, "default ORDER BY ID was not used in the query: #{sql_log}"
end
# We are not doing order duplicate removal anymore.
coerce_tests! :test_order_using_scoping
# We are not doing order duplicate removal anymore.
coerce_tests! :test_default_scope_order_with_scope_order
# Leave it up to users to format selects/functions so HAVING works correctly.
coerce_tests! :test_multiple_where_and_having_clauses
coerce_tests! :test_having_with_binds_for_both_where_and_having
# Find any limit via our expression.
coerce_tests! %r{relations don't load all records in #inspect}
def test_relations_dont_load_all_records_in_inspect_coerced
assert_sql(/NEXT @0 ROWS.*@0 = \d+/) do
Post.all.inspect
end
end
# I wanted to add `.order("author_id")` scope to avoid error: Column "posts.id" is invalid in the ORDER BY
# However, this pull request on Rails core drops order on exists relation. https://github.com/rails/rails/pull/28699
# so we are skipping all together.
coerce_tests! :test_empty_complex_chained_relations
# Can't apply offset without ORDER
coerce_tests! %r{using a custom table affects the wheres}
test "using a custom table affects the wheres coerced" do
post = posts(:welcome)
assert_equal post, custom_post_relation.where!(title: post.title).order(:id).take
end
# Can't apply offset without ORDER
coerce_tests! %r{using a custom table with joins affects the joins}
test "using a custom table with joins affects the joins coerced" do
post = posts(:welcome)
assert_equal post, custom_post_relation.joins(:author).where!(title: post.title).order(:id).take
end
# Use LEN() vs length() function.
coerce_tests! :test_reverse_arel_assoc_order_with_function
def test_reverse_arel_assoc_order_with_function_coerced
topics = Topic.order(Arel.sql("LEN(title)") => :asc).reverse_order
assert_equal topics(:second).title, topics.first.title
end
end
module ActiveRecord
class RelationTest < ActiveRecord::TestCase
# Skipping this test. SQL Server doesn't support optimizer hint as comments
coerce_tests! :test_relation_with_optimizer_hints_filters_sql_comment_delimiters
coerce_tests! :test_does_not_duplicate_optimizer_hints_on_merge
def test_does_not_duplicate_optimizer_hints_on_merge_coerced
escaped_table = Post.connection.quote_table_name("posts")
expected = "SELECT #{escaped_table}.* FROM #{escaped_table} OPTION (OMGHINT)"
query = Post.optimizer_hints("OMGHINT").merge(Post.optimizer_hints("OMGHINT")).to_sql
assert_equal expected, query
end
end
end
require "models/post"