aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorRyan Bates <ryan@railscasts.com>2008-05-20 12:11:25 +0100
committerPratik Naik <pratiknaik@gmail.com>2008-05-20 12:27:14 +0100
commit73c59638549686fccc749ffd3ac53cb533c5fd61 (patch)
tree44aebe6752869fdb13240d0bf8422388cca81767 /activerecord/test
parentebb642fa3a2b1a4e31abf9610ca634e6bb5d57d3 (diff)
downloadrails-73c59638549686fccc749ffd3ac53cb533c5fd61.tar.gz
rails-73c59638549686fccc749ffd3ac53cb533c5fd61.tar.bz2
rails-73c59638549686fccc749ffd3ac53cb533c5fd61.zip
Add first/last methods to associations/named_scope. [#226 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb2
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb66
-rw-r--r--activerecord/test/cases/associations/join_model_test.rb2
-rwxr-xr-xactiverecord/test/cases/associations_test.rb4
-rw-r--r--activerecord/test/cases/named_scope_test.rb28
5 files changed, 100 insertions, 2 deletions
diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
index 64565141f9..294b993c55 100644
--- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
@@ -401,6 +401,8 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
def test_include_uses_array_include_after_loaded
project = projects(:active_record)
+ project.developers.class # force load target
+
developer = project.developers.first
assert_no_queries do
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 9e26e2ad58..53b55022eb 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -818,6 +818,8 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
def test_include_uses_array_include_after_loaded
firm = companies(:first_firm)
+ firm.clients.class # force load target
+
client = firm.clients.first
assert_no_queries do
@@ -857,4 +859,68 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert ! firm.clients.include?(client)
end
+ def test_calling_first_or_last_on_association_should_not_load_association
+ firm = companies(:first_firm)
+ firm.clients.first
+ firm.clients.last
+ assert !firm.clients.loaded?
+ end
+
+ def test_calling_first_or_last_on_loaded_association_should_not_fetch_with_query
+ firm = companies(:first_firm)
+ firm.clients.class # force load target
+ assert firm.clients.loaded?
+
+ assert_no_queries do
+ firm.clients.first
+ assert_equal 2, firm.clients.first(2).size
+ firm.clients.last
+ assert_equal 2, firm.clients.last(2).size
+ end
+ end
+
+ def test_calling_first_or_last_on_existing_record_with_build_should_load_association
+ firm = companies(:first_firm)
+ firm.clients.build(:name => 'Foo')
+ assert !firm.clients.loaded?
+
+ assert_queries 1 do
+ firm.clients.first
+ firm.clients.last
+ end
+
+ assert firm.clients.loaded?
+ end
+
+ def test_calling_first_or_last_on_new_record_should_not_run_queries
+ firm = Firm.new
+
+ assert_no_queries do
+ firm.clients.first
+ firm.clients.last
+ end
+ end
+
+ def test_calling_first_or_last_with_find_options_on_loaded_association_should_fetch_with_query
+ firm = companies(:first_firm)
+ firm.clients.class # force load target
+
+ assert_queries 2 do
+ assert firm.clients.loaded?
+ firm.clients.first(:order => 'name')
+ firm.clients.last(:order => 'name')
+ end
+ end
+
+ def test_calling_first_or_last_with_integer_on_association_should_load_association
+ firm = companies(:first_firm)
+
+ assert_queries 1 do
+ firm.clients.first(2)
+ firm.clients.last(2)
+ end
+
+ assert firm.clients.loaded?
+ end
+
end
diff --git a/activerecord/test/cases/associations/join_model_test.rb b/activerecord/test/cases/associations/join_model_test.rb
index 952ea63706..9e79d9c8a1 100644
--- a/activerecord/test/cases/associations/join_model_test.rb
+++ b/activerecord/test/cases/associations/join_model_test.rb
@@ -664,6 +664,8 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
def test_has_many_through_include_uses_array_include_after_loaded
david = authors(:david)
+ david.categories.class # force load target
+
category = david.categories.first
assert_no_queries do
diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb
index d8fe98bf57..3ad8c608e0 100755
--- a/activerecord/test/cases/associations_test.rb
+++ b/activerecord/test/cases/associations_test.rb
@@ -99,12 +99,12 @@ class AssociationProxyTest < ActiveRecord::TestCase
david = authors(:david)
assert_equal david, david.posts.proxy_owner
assert_equal david.class.reflect_on_association(:posts), david.posts.proxy_reflection
- david.posts.first # force load target
+ david.posts.class # force load target
assert_equal david.posts, david.posts.proxy_target
assert_equal david, david.posts_with_extension.testing_proxy_owner
assert_equal david.class.reflect_on_association(:posts_with_extension), david.posts_with_extension.testing_proxy_reflection
- david.posts_with_extension.first # force load target
+ david.posts_with_extension.class # force load target
assert_equal david.posts_with_extension, david.posts_with_extension.testing_proxy_target
end
diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb
index 9730f93579..8f2fc53d67 100644
--- a/activerecord/test/cases/named_scope_test.rb
+++ b/activerecord/test/cases/named_scope_test.rb
@@ -118,4 +118,32 @@ class NamedScopeTest < ActiveRecord::TestCase
assert_equal expected_proxy_options, Topic.approved.proxy_options
end
+ def test_first_and_last_should_support_find_options
+ assert_equal Topic.base.first(:order => 'title'), Topic.base.find(:first, :order => 'title')
+ assert_equal Topic.base.last(:order => 'title'), Topic.base.find(:last, :order => 'title')
+ end
+
+ def test_first_and_last_should_allow_integers_for_limit
+ assert_equal Topic.base.first(2), Topic.base.to_a.first(2)
+ assert_equal Topic.base.last(2), Topic.base.to_a.last(2)
+ end
+
+ def test_first_and_last_should_not_use_query_when_results_are_loaded
+ topics = Topic.base
+ topics.reload # force load
+ assert_no_queries do
+ topics.first
+ topics.last
+ end
+ end
+
+ def test_first_and_last_find_options_should_use_query_when_results_are_loaded
+ topics = Topic.base
+ topics.reload # force load
+ assert_queries(2) do
+ topics.first(:order => 'title')
+ topics.last(:order => 'title')
+ end
+ end
+
end