@@ -1088,7 +1088,8 @@ class YamlSerializationTest < ActiveRecord::TestCase
10881088 coerce_tests! :test_types_of_virtual_columns_are_not_changed_on_round_trip
10891089 def test_types_of_virtual_columns_are_not_changed_on_round_trip_coerced
10901090 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 )
10921093 assert_equal 5 , author . posts_count
10931094 assert_equal 5 , dumped . posts_count
10941095 end
@@ -1207,6 +1208,7 @@ def test_statement_cache_values_differ_coerced
12071208
12081209 original_test_statement_cache_values_differ
12091210 ensure
1211+ Book . where ( author_id : nil , name : 'my book' ) . delete_all
12101212 Book . connection . add_index ( :books , [ :author_id , :name ] , unique : true )
12111213 end
12121214 end
@@ -1399,6 +1401,7 @@ class EnumTest < ActiveRecord::TestCase
13991401
14001402 send ( :'original_enums are distinct per class' )
14011403 ensure
1404+ Book . where ( author_id : nil , name : nil ) . delete_all
14021405 Book . connection . add_index ( :books , [ :author_id , :name ] , unique : true )
14031406 end
14041407
@@ -1409,6 +1412,7 @@ class EnumTest < ActiveRecord::TestCase
14091412
14101413 send ( :'original_creating new objects with enum scopes' )
14111414 ensure
1415+ Book . where ( author_id : nil , name : nil ) . delete_all
14121416 Book . connection . add_index ( :books , [ :author_id , :name ] , unique : true )
14131417 end
14141418
@@ -1419,6 +1423,7 @@ class EnumTest < ActiveRecord::TestCase
14191423
14201424 send ( :'original_enums are inheritable' )
14211425 ensure
1426+ Book . where ( author_id : nil , name : nil ) . delete_all
14221427 Book . connection . add_index ( :books , [ :author_id , :name ] , unique : true )
14231428 end
14241429
@@ -1429,6 +1434,7 @@ class EnumTest < ActiveRecord::TestCase
14291434
14301435 send ( :'original_declare multiple enums at a time' )
14311436 ensure
1437+ Book . where ( author_id : nil , name : nil ) . delete_all
14321438 Book . connection . add_index ( :books , [ :author_id , :name ] , unique : true )
14331439 end
14341440end
@@ -1522,3 +1528,76 @@ class ReloadModelsTest < ActiveRecord::TestCase
15221528 # `activesupport/lib/active_support/testing/isolation.rb` exceeds what Windows can handle.
15231529 coerce_tests! :test_has_one_with_reload if RbConfig ::CONFIG [ "host_os" ] =~ /mswin|mingw/
15241530end
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://github.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://github.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