From c01c21b31d590f7e8d12e3ae083fcdf0f0c6fd54 Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Tue, 1 Sep 2009 15:36:09 -0300 Subject: Added association preload to relation. --- activerecord/test/cases/relations_test.rb | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 655eb3314d..6fb505ca36 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -1,6 +1,7 @@ require "cases/helper" require 'models/post' require 'models/topic' +require 'models/comment' require 'models/reply' require 'models/author' require 'models/entrant' @@ -8,7 +9,7 @@ require 'models/developer' require 'models/company' class RelationTest < ActiveRecord::TestCase - fixtures :authors, :topics, :entrants, :developers, :companies, :developers_projects, :accounts, :categories, :categorizations, :posts + fixtures :authors, :topics, :entrants, :developers, :companies, :developers_projects, :accounts, :categories, :categorizations, :posts, :comments def test_finding_with_conditions assert_equal Author.find(:all, :conditions => "name = 'David'"), Author.all.conditions("name = 'David'").to_a @@ -85,5 +86,33 @@ class RelationTest < ActiveRecord::TestCase Developer.all.readonly.each { |d| assert d.readonly? } Developer.all(:readonly => true).each { |d| assert d.readonly? } end + + def test_eager_association_loading_of_stis_with_multiple_references + authors = Author.all(:include => { :posts => { :special_comments => { :post => [ :special_comments, :very_special_comment ] } } }, :order => 'comments.body, very_special_comments_posts.body', :conditions => 'posts.id = 4').to_a + assert_equal [authors(:david)], authors + assert_no_queries do + authors.first.posts.first.special_comments.first.post.special_comments + authors.first.posts.first.special_comments.first.post.very_special_comment + end + end + + def test_find_with_included_associations + assert_queries(2) do + posts = Post.find(:all, :include => :comments) + posts.first.comments.first + end + assert_queries(2) do + posts = Post.all(:include => :comments).to_a + posts.first.comments.first + end + assert_queries(2) do + posts = Post.find(:all, :include => :author) + posts.first.author + end + assert_queries(2) do + posts = Post.all(:include => :author).to_a + posts.first.author + end + end end -- cgit v1.2.3 From 3747f896a1b727d67e6022001007e5f58b24a267 Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Mon, 5 Oct 2009 14:39:20 -0300 Subject: Moved relation's test to relation_test. --- activerecord/test/cases/method_scoping_test.rb | 4 ++-- activerecord/test/cases/relations_test.rb | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index 6dec474f7d..eb4ce0e774 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -593,12 +593,12 @@ class DefaultScopingTest < ActiveRecord::TestCase end def test_default_scope_with_conditions_string - assert_equal Developer.find_all_by_name('David').map(&:id).sort, DeveloperCalledDavid.all.to_a.map(&:id).sort + assert_equal Developer.find_all_by_name('David').map(&:id).sort, DeveloperCalledDavid.find(:all).map(&:id).sort assert_equal nil, DeveloperCalledDavid.create!.name end def test_default_scope_with_conditions_hash - assert_equal Developer.find_all_by_name('Jamis').map(&:id).sort, DeveloperCalledJamis.all.to_a.map(&:id).sort + assert_equal Developer.find_all_by_name('Jamis').map(&:id).sort, DeveloperCalledJamis.find(:all).map(&:id).sort assert_equal 'Jamis', DeveloperCalledJamis.create!.name end diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 6fb505ca36..17c228616b 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -114,5 +114,16 @@ class RelationTest < ActiveRecord::TestCase posts.first.author end end + + def test_default_scope_with_conditions_string + assert_equal Developer.find_all_by_name('David').map(&:id).sort, DeveloperCalledDavid.all.to_a.map(&:id).sort + assert_equal nil, DeveloperCalledDavid.create!.name + end + + def test_default_scope_with_conditions_hash + assert_equal Developer.find_all_by_name('Jamis').map(&:id).sort, DeveloperCalledJamis.all.to_a.map(&:id).sort + assert_equal 'Jamis', DeveloperCalledJamis.create!.name + end + end -- cgit v1.2.3 From 65f055a3ed790d41aeca8d4ca7f3771b05cf544f Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Mon, 5 Oct 2009 15:24:08 -0300 Subject: Added eager loading support to Relation and ActiveRecord#all. --- activerecord/test/cases/associations/cascaded_eager_loading_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb index 45e74ea024..ed2e2e9f8f 100644 --- a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb +++ b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb @@ -104,7 +104,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase authors.first.posts.first.special_comments.first.post.very_special_comment end end - + def test_eager_association_loading_where_first_level_returns_nil authors = Author.find(:all, :include => {:post_about_thinking => :comments}, :order => 'authors.id DESC') assert_equal [authors(:mary), authors(:david)], authors -- cgit v1.2.3