aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorEileen M. Uchitelle <eileencodes@gmail.com>2016-02-13 09:45:04 -0500
committerEileen M. Uchitelle <eileencodes@gmail.com>2016-02-13 09:45:04 -0500
commit7ee2002008eb7bd4546f556670f5332f06901a0d (patch)
tree9aabcaa32c2eec9ec7ed0fcb6d7cd4456ba957ec /activerecord/test
parent4e80c12de3058eafd6c4f209bc78d9e3f6e26a0f (diff)
parentb67991614349d68e0d8ad489c8c84c3254ac847b (diff)
downloadrails-7ee2002008eb7bd4546f556670f5332f06901a0d.tar.gz
rails-7ee2002008eb7bd4546f556670f5332f06901a0d.tar.bz2
rails-7ee2002008eb7bd4546f556670f5332f06901a0d.zip
Merge pull request #23377 from bogdan/last-with-sql
Fix AR::Relation#last bugs instroduced in 7705fc
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/finder_test.rb44
1 files changed, 36 insertions, 8 deletions
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index ba0400f799..5b41630cd8 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -516,16 +516,44 @@ class FinderTest < ActiveRecord::TestCase
assert_equal Topic.order("title").to_a.last(2), Topic.order("title").last(2)
end
- def test_last_with_integer_and_order_should_not_use_sql_limit
- query = assert_sql { Topic.order("title").last(5).entries }
- assert_equal 1, query.length
- assert_no_match(/LIMIT/, query.first)
+ def test_last_with_integer_and_order_should_use_sql_limit
+ relation = Topic.order("title")
+ assert_queries(1) { relation.last(5) }
+ assert !relation.loaded?
end
- def test_last_with_integer_and_reorder_should_not_use_sql_limit
- query = assert_sql { Topic.reorder("title").last(5).entries }
- assert_equal 1, query.length
- assert_no_match(/LIMIT/, query.first)
+ def test_last_with_integer_and_reorder_should_use_sql_limit
+ relation = Topic.reorder("title")
+ assert_queries(1) { relation.last(5) }
+ assert !relation.loaded?
+ end
+
+ def test_last_on_loaded_relation_should_not_use_sql
+ relation = Topic.limit(10).load
+ assert_no_queries do
+ relation.last
+ relation.last(2)
+ end
+ end
+
+ def test_last_with_irreversible_order
+ assert_deprecated do
+ Topic.order("coalesce(author_name, title)").last
+ end
+ end
+
+ def test_last_on_relation_with_limit_and_offset
+ post = posts('sti_comments')
+
+ comments = post.comments.order(id: :asc)
+ assert_equal comments.limit(2).to_a.last, comments.limit(2).last
+ assert_equal comments.limit(2).to_a.last(2), comments.limit(2).last(2)
+ assert_equal comments.limit(2).to_a.last(3), comments.limit(2).last(3)
+
+ comments = comments.offset(1)
+ assert_equal comments.limit(2).to_a.last, comments.limit(2).last
+ assert_equal comments.limit(2).to_a.last(2), comments.limit(2).last(2)
+ assert_equal comments.limit(2).to_a.last(3), comments.limit(2).last(3)
end
def test_take_and_first_and_last_with_integer_should_return_an_array