aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/finder_test.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/test/cases/finder_test.rb')
-rw-r--r--activerecord/test/cases/finder_test.rb41
1 files changed, 38 insertions, 3 deletions
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index b413212e26..2f256dd36a 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -355,6 +355,12 @@ class FinderTest < ActiveRecord::TestCase
end
def test_find_on_relation_with_large_number
+ assert_raises(ActiveRecord::RecordNotFound) do
+ Topic.where("1=1").find(9999999999999999999999999999999)
+ end
+ end
+
+ def test_find_by_on_relation_with_large_number
assert_nil Topic.where("1=1").find_by(id: 9999999999999999999999999999999)
end
@@ -414,7 +420,7 @@ class FinderTest < ActiveRecord::TestCase
end
def test_take
- assert_equal topics(:first), Topic.take
+ assert_equal topics(:first), Topic.where("title = 'The First Topic'").take
end
def test_take_failing
@@ -727,8 +733,8 @@ class FinderTest < ActiveRecord::TestCase
assert_raise(ActiveModel::MissingAttributeError) { topic.title? }
assert_nil topic.read_attribute("title")
assert_equal "David", topic.author_name
- assert !topic.attribute_present?("title")
- assert !topic.attribute_present?(:title)
+ assert_not topic.attribute_present?("title")
+ assert_not topic.attribute_present?(:title)
assert topic.attribute_present?("author_name")
assert_respond_to topic, "author_name"
end
@@ -812,6 +818,15 @@ class FinderTest < ActiveRecord::TestCase
assert_equal [1, 2, 6, 7, 8], Comment.where(id: [1..2, 6..8]).to_a.map(&:id).sort
end
+ def test_find_on_hash_conditions_with_open_ended_range
+ assert_equal [1, 2, 3], Comment.where(id: Float::INFINITY..3).to_a.map(&:id).sort
+ end
+
+ def test_find_on_hash_conditions_with_numeric_range_for_string
+ topic = Topic.create!(title: "12 Factor App")
+ assert_equal [topic], Topic.where(title: 10..2).to_a
+ end
+
def test_find_on_multiple_hash_conditions
assert Topic.where(author_name: "David", title: "The First Topic", replies_count: 1, approved: false).find(1)
assert_raise(ActiveRecord::RecordNotFound) { Topic.where(author_name: "David", title: "The First Topic", replies_count: 1, approved: true).find(1) }
@@ -1180,6 +1195,11 @@ class FinderTest < ActiveRecord::TestCase
order("author_addresses_authors.id DESC").limit(3).to_a.size
end
+ def test_find_with_eager_loading_collection_and_ordering_by_collection_primary_key
+ assert_equal Post.first, Post.eager_load(comments: :ratings).
+ order("posts.id, ratings.id, comments.id").first
+ end
+
def test_find_with_nil_inside_set_passed_for_one_attribute
client_of = Company.
where(client_of: [2, 1, nil],
@@ -1245,6 +1265,21 @@ class FinderTest < ActiveRecord::TestCase
end
end
+ def test_first_and_last_with_limit_for_order_without_primary_key
+ # While Topic.first should impose an ordering by primary key,
+ # Topic.limit(n).first should not
+
+ Topic.first.touch # PostgreSQL changes the default order if no order clause is used
+
+ assert_equal Topic.limit(1).to_a.first, Topic.limit(1).first
+ assert_equal Topic.limit(2).to_a.first, Topic.limit(2).first
+ assert_equal Topic.limit(2).to_a.first(2), Topic.limit(2).first(2)
+
+ assert_equal Topic.limit(1).to_a.last, Topic.limit(1).last
+ assert_equal Topic.limit(2).to_a.last, Topic.limit(2).last
+ assert_equal Topic.limit(2).to_a.last(2), Topic.limit(2).last(2)
+ end
+
def test_finder_with_offset_string
assert_nothing_raised { Topic.offset("3").to_a }
end