@@ -1088,7 +1088,8 @@ class YamlSerializationTest < ActiveRecord::TestCase
1088
1088
coerce_tests! :test_types_of_virtual_columns_are_not_changed_on_round_trip
1089
1089
def test_types_of_virtual_columns_are_not_changed_on_round_trip_coerced
1090
1090
author = Author . select ( "authors.*, 5 as posts_count" ) . first
1091
- dumped = YAML . load ( YAML . dump ( author ) )
1091
+ dumped_author = YAML . dump ( author )
1092
+ dumped = YAML . respond_to? ( :unsafe_load ) ? YAML . unsafe_load ( dumped_author ) : YAML . load ( dumped_author )
1092
1093
assert_equal 5 , author . posts_count
1093
1094
assert_equal 5 , dumped . posts_count
1094
1095
end
@@ -1207,6 +1208,7 @@ def test_statement_cache_values_differ_coerced
1207
1208
1208
1209
original_test_statement_cache_values_differ
1209
1210
ensure
1211
+ Book . where ( author_id : nil , name : 'my book' ) . delete_all
1210
1212
Book . connection . add_index ( :books , [ :author_id , :name ] , unique : true )
1211
1213
end
1212
1214
end
@@ -1399,6 +1401,7 @@ class EnumTest < ActiveRecord::TestCase
1399
1401
1400
1402
send ( :'original_enums are distinct per class' )
1401
1403
ensure
1404
+ Book . where ( author_id : nil , name : nil ) . delete_all
1402
1405
Book . connection . add_index ( :books , [ :author_id , :name ] , unique : true )
1403
1406
end
1404
1407
@@ -1409,6 +1412,7 @@ class EnumTest < ActiveRecord::TestCase
1409
1412
1410
1413
send ( :'original_creating new objects with enum scopes' )
1411
1414
ensure
1415
+ Book . where ( author_id : nil , name : nil ) . delete_all
1412
1416
Book . connection . add_index ( :books , [ :author_id , :name ] , unique : true )
1413
1417
end
1414
1418
@@ -1419,6 +1423,7 @@ class EnumTest < ActiveRecord::TestCase
1419
1423
1420
1424
send ( :'original_enums are inheritable' )
1421
1425
ensure
1426
+ Book . where ( author_id : nil , name : nil ) . delete_all
1422
1427
Book . connection . add_index ( :books , [ :author_id , :name ] , unique : true )
1423
1428
end
1424
1429
@@ -1429,6 +1434,7 @@ class EnumTest < ActiveRecord::TestCase
1429
1434
1430
1435
send ( :'original_declare multiple enums at a time' )
1431
1436
ensure
1437
+ Book . where ( author_id : nil , name : nil ) . delete_all
1432
1438
Book . connection . add_index ( :books , [ :author_id , :name ] , unique : true )
1433
1439
end
1434
1440
end
@@ -1522,3 +1528,76 @@ class ReloadModelsTest < ActiveRecord::TestCase
1522
1528
# `activesupport/lib/active_support/testing/isolation.rb` exceeds what Windows can handle.
1523
1529
coerce_tests! :test_has_one_with_reload if RbConfig ::CONFIG [ "host_os" ] =~ /mswin|mingw/
1524
1530
end
1531
+
1532
+ require "models/post"
1533
+ class AnnotateTest < ActiveRecord ::TestCase
1534
+ # Same as original coerced test except our SQL starts with `EXEC sp_executesql`.
1535
+ # TODO: Remove coerce after Rails 7 (see https://.com/rails/rails/pull/42027)
1536
+ coerce_tests! :test_annotate_wraps_content_in_an_inline_comment
1537
+ def test_annotate_wraps_content_in_an_inline_comment_coerced
1538
+ quoted_posts_id , quoted_posts = regexp_escape_table_name ( "posts.id" ) , regexp_escape_table_name ( "posts" )
1539
+
1540
+ assert_sql ( %r{SELECT #{ quoted_posts_id } FROM #{ quoted_posts } /\* foo \* /}i ) do
1541
+ posts = Post . select ( :id ) . annotate ( "foo" )
1542
+ assert posts . first
1543
+ end
1544
+ end
1545
+
1546
+ # Same as original coerced test except our SQL starts with `EXEC sp_executesql`.
1547
+ # TODO: Remove coerce after Rails 7 (see https://.com/rails/rails/pull/42027)
1548
+ coerce_tests! :test_annotate_is_sanitized
1549
+ def test_annotate_is_sanitized_coerced
1550
+ quoted_posts_id , quoted_posts = regexp_escape_table_name ( "posts.id" ) , regexp_escape_table_name ( "posts" )
1551
+
1552
+ assert_sql ( %r{SELECT #{ quoted_posts_id } FROM #{ quoted_posts } /\* \* /foo/ \* \* /}i ) do
1553
+ posts = Post . select ( :id ) . annotate ( "*/foo/*" )
1554
+ assert posts . first
1555
+ end
1556
+
1557
+ assert_sql ( %r{SELECT #{ quoted_posts_id } FROM #{ quoted_posts } /\* \* \* //foo// \* \* \* /}i ) do
1558
+ posts = Post . select ( :id ) . annotate ( "**//foo//**" )
1559
+ assert posts . first
1560
+ end
1561
+
1562
+ assert_sql ( %r{SELECT #{ quoted_posts_id } FROM #{ quoted_posts } /\* \* \* //foo// \* \* \* /}i ) do
1563
+ posts = Post . select ( :id ) . annotate ( "* *//foo//* *" )
1564
+ assert posts . first
1565
+ end
1566
+
1567
+ assert_sql ( %r{SELECT #{ quoted_posts_id } FROM #{ quoted_posts } /\* \* /foo/ \* \* / /\* \* /bar \* /}i ) do
1568
+ posts = Post . select ( :id ) . annotate ( "*/foo/*" ) . annotate ( "*/bar" )
1569
+ assert posts . first
1570
+ end
1571
+
1572
+ assert_sql ( %r{SELECT #{ quoted_posts_id } FROM #{ quoted_posts } /\* \+ MAX_EXECUTION_TIME\( 1\) \* /}i ) do
1573
+ posts = Post . select ( :id ) . annotate ( "+ MAX_EXECUTION_TIME(1)" )
1574
+ assert posts . first
1575
+ end
1576
+ end
1577
+ end
1578
+
1579
+ class NestedThroughAssociationsTest < ActiveRecord ::TestCase
1580
+ # Same as original but replace order with "order(:id)" to ensure that assert_includes_and_joins_equal doesn't raise
1581
+ # "A column has been specified more than once in the order by list"
1582
+ # Example: original test generate queries like "ORDER BY authors.id, [authors].[id]". We don't support duplicate columns in the order list
1583
+ coerce_tests! :test_has_many_through_has_many_with_has_many_through_habtm_source_reflection_preload_via_joins , :test_has_many_through_has_and_belongs_to_many_with_has_many_source_reflection_preload_via_joins
1584
+ def test_has_many_through_has_many_with_has_many_through_habtm_source_reflection_preload_via_joins_coerced
1585
+ # preload table schemas
1586
+ Author . joins ( :category_post_comments ) . first
1587
+
1588
+ assert_includes_and_joins_equal (
1589
+ Author . where ( "comments.id" => comments ( :does_it_hurt ) . id ) . order ( :id ) ,
1590
+ [ authors ( :david ) , authors ( :mary ) ] , :category_post_comments
1591
+ )
1592
+ end
1593
+
1594
+ def test_has_many_through_has_and_belongs_to_many_with_has_many_source_reflection_preload_via_joins_coerced
1595
+ # preload table schemas
1596
+ Category . joins ( :post_comments ) . first
1597
+
1598
+ assert_includes_and_joins_equal (
1599
+ Category . where ( "comments.id" => comments ( :more_greetings ) . id ) . order ( :id ) ,
1600
+ [ categories ( :general ) , categories ( :technology ) ] , :post_comments
1601
+ )
1602
+ end
1603
+ end
0 commit comments