@@ -982,6 +982,83 @@ def test_member_on_unloaded_relation_with_composite_primary_key_coerced
982982 end
983983 end
984984
985+ # Check for `FETCH NEXT x ROWS` rather then `LIMIT`.
986+ coerce_tests! :test_implicit_order_column_prepends_query_constraints
987+ def test_implicit_order_column_prepends_query_constraints_coerced
988+ c = ClothingItem . connection
989+ ClothingItem . implicit_order_column = "description"
990+ quoted_type = Regexp . escape ( c . quote_table_name ( "clothing_items.clothing_type" ) )
991+ quoted_color = Regexp . escape ( c . quote_table_name ( "clothing_items.color" ) )
992+ quoted_descrption = Regexp . escape ( c . quote_table_name ( "clothing_items.description" ) )
993+
994+ assert_sql ( /ORDER BY #{ quoted_descrption } ASC, #{ quoted_type } ASC, #{ quoted_color } ASC OFFSET 0 ROWS FETCH NEXT @(\d ) ROWS ONLY.*@\1 = 1/i ) do
995+ assert_kind_of ClothingItem , ClothingItem . first
996+ end
997+ ensure
998+ ClothingItem . implicit_order_column = nil
999+ end
1000+
1001+ # Check for `FETCH NEXT x ROWS` rather then `LIMIT`.
1002+ coerce_tests! %r{#last for a model with composite query constraints}
1003+ test "#last for a model with composite query constraints coerced" do
1004+ c = ClothingItem . connection
1005+ quoted_type = Regexp . escape ( c . quote_table_name ( "clothing_items.clothing_type" ) )
1006+ quoted_color = Regexp . escape ( c . quote_table_name ( "clothing_items.color" ) )
1007+
1008+ assert_sql ( /ORDER BY #{ quoted_type } DESC, #{ quoted_color } DESC OFFSET 0 ROWS FETCH NEXT @(\d ) ROWS ONLY.*@\1 = 1/i ) do
1009+ assert_kind_of ClothingItem , ClothingItem . last
1010+ end
1011+ end
1012+
1013+ # Check for `FETCH NEXT x ROWS` rather then `LIMIT`.
1014+ coerce_tests! %r{#first for a model with composite query constraints}
1015+ test "#first for a model with composite query constraints coerced" do
1016+ c = ClothingItem . connection
1017+ quoted_type = Regexp . escape ( c . quote_table_name ( "clothing_items.clothing_type" ) )
1018+ quoted_color = Regexp . escape ( c . quote_table_name ( "clothing_items.color" ) )
1019+
1020+ assert_sql ( /ORDER BY #{ quoted_type } ASC, #{ quoted_color } ASC OFFSET 0 ROWS FETCH NEXT @(\d ) ROWS ONLY.*@\1 = 1/i ) do
1021+ assert_kind_of ClothingItem , ClothingItem . first
1022+ end
1023+ end
1024+
1025+ # Check for `FETCH NEXT x ROWS` rather then `LIMIT`.
1026+ coerce_tests! :test_implicit_order_column_reorders_query_constraints
1027+ def test_implicit_order_column_reorders_query_constraints_coerced
1028+ c = ClothingItem . connection
1029+ ClothingItem . implicit_order_column = "color"
1030+ quoted_type = Regexp . escape ( c . quote_table_name ( "clothing_items.clothing_type" ) )
1031+ quoted_color = Regexp . escape ( c . quote_table_name ( "clothing_items.color" ) )
1032+
1033+ assert_sql ( /ORDER BY #{ quoted_color } ASC, #{ quoted_type } ASC OFFSET 0 ROWS FETCH NEXT @(\d ) ROWS ONLY.*@\1 = 1/i ) do
1034+ assert_kind_of ClothingItem , ClothingItem . first
1035+ end
1036+ ensure
1037+ ClothingItem . implicit_order_column = nil
1038+ end
1039+
1040+ # Check for `FETCH NEXT x ROWS` rather then `LIMIT`.
1041+ coerce_tests! :test_include_on_unloaded_relation_with_composite_primary_key
1042+ def test_include_on_unloaded_relation_with_composite_primary_key_coerced
1043+ assert_sql ( /1 AS one.*OFFSET 0 ROWS FETCH NEXT @(\d ) ROWS ONLY.*@\1 = 1/ ) do
1044+ book = cpk_books ( :cpk_great_author_first_book )
1045+ assert Cpk ::Book . where ( title : "The first book" ) . include? ( book )
1046+ end
1047+ end
1048+
1049+ # Check for `FETCH NEXT x ROWS` rather then `LIMIT`.
1050+ coerce_tests! :test_nth_to_last_with_order_uses_limit
1051+ def test_nth_to_last_with_order_uses_limit_coerced
1052+ c = Topic . connection
1053+ assert_sql ( /ORDER BY #{ Regexp . escape ( c . quote_table_name ( "topics.id" ) ) } DESC OFFSET @(\d ) ROWS FETCH NEXT @(\d ) ROWS ONLY.*@\1 = 1.*@\2 = 1/i ) do
1054+ Topic . second_to_last
1055+ end
1056+
1057+ assert_sql ( /ORDER BY #{ Regexp . escape ( c . quote_table_name ( "topics.updated_at" ) ) } DESC OFFSET @(\d ) ROWS FETCH NEXT @(\d ) ROWS ONLY.*@\1 = 1.*@\2 = 1/i ) do
1058+ Topic . order ( :updated_at ) . second_to_last
1059+ end
1060+ end
1061+
9851062 # SQL Server is unable to use aliased SELECT in the HAVING clause.
9861063 coerce_tests! :test_include_on_unloaded_relation_with_having_referencing_aliased_select
9871064end
0 commit comments