@@ -176,6 +176,14 @@ def test_limit_with_offset_is_kept_coerced
176176 queries . first . must_match %r{ORDER BY \[ accounts\] \. \[ id\] ASC OFFSET @0 ROWS FETCH NEXT @1 ROWS ONLY.*@0 = 1, @1 = 1}
177177 end
178178
179+ # SQL Server needs an alias for the calculated column
180+ coerce_tests! :test_distinct_count_all_with_custom_select_and_order
181+ def test_distinct_count_all_with_custom_select_and_order_coerced
182+ accounts = Account . distinct . select ( "credit_limit % 10 AS the_limit" ) . order ( Arel . sql ( "credit_limit % 10" ) )
183+ assert_queries ( 1 ) { assert_equal 3 , accounts . count ( :all ) }
184+ assert_queries ( 1 ) { assert_equal 3 , accounts . load . size }
185+ end
186+
179187 # Leave it up to users to format selects/functions so HAVING works correctly.
180188 coerce_tests! :test_having_with_strong_parameters
181189end
@@ -189,6 +197,7 @@ class ChangeSchemaTest < ActiveRecord::TestCase
189197 coerce_tests! :test_create_table_with_bigint ,
190198 :test_create_table_with_defaults
191199 end
200+
192201 class ChangeSchemaWithDependentObjectsTest < ActiveRecord ::TestCase
193202 # In SQL Server you have to delete the tables yourself in the right order.
194203 coerce_tests! :test_create_table_with_force_cascade_drops_dependent_objects
@@ -701,6 +710,22 @@ def test_relations_dont_load_all_records_in_inspect_coerced
701710 # so we are skipping all together.
702711 coerce_tests! :test_empty_complex_chained_relations
703712
713+ # Can't apply offset withour ORDER
714+ coerce_tests! %r{using a custom table affects the wheres}
715+ test 'using a custom table affects the wheres coerced' do
716+ post = posts ( :welcome )
717+
718+ assert_equal post , custom_post_relation . where! ( title : post . title ) . order ( :id ) . take
719+ end
720+
721+ # Can't apply offset withour ORDER
722+ coerce_tests! %r{using a custom table with joins affects the joins}
723+ test 'using a custom table with joins affects the joins coerced' do
724+ post = posts ( :welcome )
725+
726+ assert_equal post , custom_post_relation . joins ( :author ) . where! ( title : post . title ) . order ( :id ) . take
727+ end
728+
704729 # Use LEN() vs length() function.
705730 coerce_tests! :test_reverse_arel_assoc_order_with_function
706731 def test_reverse_arel_assoc_order_with_function_coerced
0 commit comments