Skip to content

Commit fe4ebf5

Browse files
aidanharanAidan Haran
andauthored
Rails 6.1: Coerce tests that check for LIMIT in SQL (#886)
* Coerce tests that check for LIMIT in SQL * Added missing call to coerce test Co-authored-by: Aidan Haran <aharan@fusioneer.com>
1 parent 3cfc0b7 commit fe4ebf5

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

test/cases/coerced_tests.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,11 @@ def test_count_with_include_coerced
656656
end
657657

658658
require "models/topic"
659+
require "models/customer"
660+
require "models/non_primary_key"
659661
class FinderTest < ActiveRecord::TestCase
662+
fixtures :customers, :topics, :authors
663+
660664
# We have implicit ordering, via FETCH.
661665
coerce_tests! %r{doesn't have implicit ordering},
662666
:test_find_doesnt_have_implicit_ordering
@@ -701,6 +705,84 @@ def test_condition_local_time_interpolation_with_default_timezone_utc_coerced
701705
end
702706
end
703707
end
708+
709+
# Check for `FETCH NEXT x ROWS` rather then `LIMIT`.
710+
coerce_tests! :test_include_on_unloaded_relation_with_match
711+
def test_include_on_unloaded_relation_with_match_coerced
712+
assert_sql(/1 AS one.*FETCH NEXT @2 ROWS ONLY.*@2 = 1/) do
713+
assert_equal true, Customer.where(name: "David").include?(customers(:david))
714+
end
715+
end
716+
717+
# Check for `FETCH NEXT x ROWS` rather then `LIMIT`.
718+
coerce_tests! :test_include_on_unloaded_relation_without_match
719+
def test_include_on_unloaded_relation_without_match_coerced
720+
assert_sql(/1 AS one.*FETCH NEXT @2 ROWS ONLY.*@2 = 1/) do
721+
assert_equal false, Customer.where(name: "David").include?(customers(:mary))
722+
end
723+
end
724+
725+
# Check for `FETCH NEXT x ROWS` rather then `LIMIT`.
726+
coerce_tests! :test_member_on_unloaded_relation_with_match
727+
def test_member_on_unloaded_relation_with_match_coerced
728+
assert_sql(/1 AS one.*FETCH NEXT @2 ROWS ONLY.*@2 = 1/) do
729+
assert_equal true, Customer.where(name: "David").member?(customers(:david))
730+
end
731+
end
732+
733+
# Check for `FETCH NEXT x ROWS` rather then `LIMIT`.
734+
coerce_tests! :test_member_on_unloaded_relation_without_match
735+
def test_member_on_unloaded_relation_without_match_coerced
736+
assert_sql(/1 AS one.*FETCH NEXT @2 ROWS ONLY.*@2 = 1/) do
737+
assert_equal false, Customer.where(name: "David").member?(customers(:mary))
738+
end
739+
end
740+
741+
# Check for `FETCH NEXT x ROWS` rather then `LIMIT`.
742+
coerce_tests! :test_implicit_order_column_is_configurable
743+
def test_implicit_order_column_is_configurable_coerced
744+
old_implicit_order_column = Topic.implicit_order_column
745+
Topic.implicit_order_column = "title"
746+
747+
assert_equal topics(:fifth), Topic.first
748+
assert_equal topics(:third), Topic.last
749+
750+
c = Topic.connection
751+
assert_sql(/ORDER BY #{Regexp.escape(c.quote_table_name("topics.title"))} DESC, #{Regexp.escape(c.quote_table_name("topics.id"))} DESC OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY.*@0 = 1/i) {
752+
Topic.last
753+
}
754+
ensure
755+
Topic.implicit_order_column = old_implicit_order_column
756+
end
757+
758+
# Check for `FETCH NEXT x ROWS` rather then `LIMIT`.
759+
coerce_tests! :test_implicit_order_set_to_primary_key
760+
def test_implicit_order_set_to_primary_key_coerced
761+
old_implicit_order_column = Topic.implicit_order_column
762+
Topic.implicit_order_column = "id"
763+
764+
c = Topic.connection
765+
assert_sql(/ORDER BY #{Regexp.escape(c.quote_table_name("topics.id"))} DESC OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY.*@0 = 1/i) {
766+
Topic.last
767+
}
768+
ensure
769+
Topic.implicit_order_column = old_implicit_order_column
770+
end
771+
772+
# Check for `FETCH NEXT x ROWS` rather then `LIMIT`.
773+
coerce_tests! :test_implicit_order_for_model_without_primary_key
774+
def test_implicit_order_for_model_without_primary_key_coerced
775+
old_implicit_order_column = NonPrimaryKey.implicit_order_column
776+
NonPrimaryKey.implicit_order_column = "created_at"
777+
778+
c = NonPrimaryKey.connection
779+
780+
assert_sql(/ORDER BY #{Regexp.escape(c.quote_table_name("non_primary_keys.created_at"))} DESC OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY.*@0 = 1/i) {
781+
NonPrimaryKey.last
782+
}
783+
ensure
784+
NonPrimaryKey.implicit_order_column = old_implicit_order_column
785+
end
704786
end
705787

706788
module ActiveRecord

0 commit comments

Comments
 (0)