From 355a8ff2cded22dafaa83c4578d21037eea2ca9c Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 19 Jan 2008 04:19:53 +0000 Subject: Introduce preload query strategy for eager :includes. Closes #9640. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8672 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/cases/associations/eager_test.rb | 45 +++++++++++++---- .../test/cases/associations/join_model_test.rb | 59 +++++++++++++++++++++- 2 files changed, 93 insertions(+), 11 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 0e351816d3..9ceb507e91 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -1,5 +1,6 @@ require 'abstract_unit' require 'models/post' +require 'models/tagging' require 'models/comment' require 'models/author' require 'models/category' @@ -9,7 +10,7 @@ require 'models/reader' class EagerAssociationTest < ActiveSupport::TestCase fixtures :posts, :comments, :authors, :categories, :categories_posts, - :companies, :accounts, :tags, :people, :readers + :companies, :accounts, :tags, :taggings, :people, :readers def test_loading_with_one_association posts = Post.find(:all, :include => :comments) @@ -56,6 +57,13 @@ class EagerAssociationTest < ActiveSupport::TestCase assert posts.first.comments.include?(comments(:greetings)) end + def test_duplicate_middle_objects + comments = Comment.find :all, :conditions => 'post_id = 1', :include => [:post => :author] + assert_no_queries do + comments.each {|comment| comment.post.author.name} + end + end + def test_loading_from_an_association posts = authors(:david).posts.find(:all, :include => :comments, :order => "posts.id") assert_equal 2, posts.first.comments.size @@ -353,6 +361,17 @@ class EagerAssociationTest < ActiveSupport::TestCase assert_equal posts(:sti_post_and_comments, :sti_comments), Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title) DESC, posts.id', :limit => 2, :offset => 1) end + def test_preload_with_interpolation + assert_equal [comments(:greetings)], Post.find(posts(:welcome).id, :include => :comments_with_interpolated_conditions).comments_with_interpolated_conditions + end + + def test_polymorphic_type_condition + post = Post.find(posts(:thinking).id, :include => :taggings) + assert post.taggings.include?(taggings(:thinking_general)) + post = SpecialPost.find(posts(:thinking).id, :include => :taggings) + assert post.taggings.include?(taggings(:thinking_general)) + end + def test_eager_with_multiple_associations_with_same_table_has_many_and_habtm # Eager includes of has many and habtm associations aren't necessarily sorted in the same way def assert_equal_after_sort(item1, item2, item3 = nil) @@ -405,34 +424,40 @@ class EagerAssociationTest < ActiveSupport::TestCase def test_preconfigured_includes_with_belongs_to author = posts(:welcome).author_with_posts - assert_equal 5, author.posts.size + assert_no_queries {assert_equal 5, author.posts.size} end def test_preconfigured_includes_with_has_one comment = posts(:sti_comments).very_special_comment_with_post - assert_equal posts(:sti_comments), comment.post + assert_no_queries {assert_equal posts(:sti_comments), comment.post} end def test_preconfigured_includes_with_has_many posts = authors(:david).posts_with_comments one = posts.detect { |p| p.id == 1 } - assert_equal 5, posts.size - assert_equal 2, one.comments.size + assert_no_queries do + assert_equal 5, posts.size + assert_equal 2, one.comments.size + end end def test_preconfigured_includes_with_habtm posts = authors(:david).posts_with_categories one = posts.detect { |p| p.id == 1 } - assert_equal 5, posts.size - assert_equal 2, one.categories.size + assert_no_queries do + assert_equal 5, posts.size + assert_equal 2, one.categories.size + end end def test_preconfigured_includes_with_has_many_and_habtm posts = authors(:david).posts_with_comments_and_categories one = posts.detect { |p| p.id == 1 } - assert_equal 5, posts.size - assert_equal 2, one.comments.size - assert_equal 2, one.categories.size + assert_no_queries do + assert_equal 5, posts.size + assert_equal 2, one.comments.size + assert_equal 2, one.categories.size + end end def test_count_with_include diff --git a/activerecord/test/cases/associations/join_model_test.rb b/activerecord/test/cases/associations/join_model_test.rb index 23ed9d1c5e..c8ae42d78c 100644 --- a/activerecord/test/cases/associations/join_model_test.rb +++ b/activerecord/test/cases/associations/join_model_test.rb @@ -317,7 +317,7 @@ class AssociationsJoinModelTest < ActiveSupport::TestCase assert_equal posts(:welcome, :thinking), tags(:general).taggables end assert_raise ActiveRecord::EagerLoadPolymorphicError do - assert_equal posts(:welcome, :thinking), tags(:general).taggings.find(:all, :include => :taggable) + assert_equal posts(:welcome, :thinking), tags(:general).taggings.find(:all, :include => :taggable, :conditions => 'bogus_table.column = 1') end end @@ -331,6 +331,7 @@ class AssociationsJoinModelTest < ActiveSupport::TestCase assert_no_queries do assert_equal desired, tag_with_include.tagged_posts end + assert_equal 4, tag_with_include.taggings.length end def test_has_many_through_has_many_find_all @@ -546,6 +547,62 @@ class AssociationsJoinModelTest < ActiveSupport::TestCase assert_equal comment_ids.sort.reverse, authors(:david).ordered_uniq_comments_desc.map(&:id) end + def test_polymorphic_has_many + expected = taggings(:welcome_general) + p = Post.find(posts(:welcome).id, :include => :taggings) + assert_no_queries {assert p.taggings.include?(expected)} + assert posts(:welcome).taggings.include?(taggings(:welcome_general)) + end + + def test_polymorphic_has_one + expected = posts(:welcome) + + tagging = Tagging.find(taggings(:welcome_general).id, :include => :taggable) + assert_no_queries { assert_equal expected, tagging.taggable} + end + + def test_polymorphic_belongs_to + p = Post.find(posts(:welcome).id, :include => {:taggings => :taggable}) + assert_no_queries {assert_equal posts(:welcome), p.taggings.first.taggable} + end + + def test_preload_polymorphic_has_many_through + posts = Post.find(:all, :order => 'posts.id') + posts_with_tags = Post.find(:all, :include => :tags, :order => 'posts.id') + assert_equal posts.length, posts_with_tags.length + posts.length.times do |i| + assert_equal posts[i].tags.length, assert_no_queries { posts_with_tags[i].tags.length } + end + end + + def test_preload_polymorph_many_types + taggings = Tagging.find :all, :include => :taggable, :conditions => ['taggable_type != ?', 'FakeModel'] + assert_no_queries do + taggings.first.taggable.id + taggings[1].taggable.id + end + + taggables = taggings.map(&:taggable) + assert taggables.include?(items(:dvd)) + assert taggables.include?(posts(:welcome)) + end + + def test_preload_polymorphic_has_many + posts = Post.find(:all, :order => 'posts.id') + posts_with_taggings = Post.find(:all, :include => :taggings, :order => 'posts.id') + assert_equal posts.length, posts_with_taggings.length + posts.length.times do |i| + assert_equal posts[i].taggings.length, assert_no_queries { posts_with_taggings[i].taggings.length } + end + end + + def test_belongs_to_shared_parent + comments = Comment.find(:all, :include => :post, :conditions => 'post_id = 1') + assert_no_queries do + assert_equal comments.first.post, comments[1].post + end + end + private # create dynamic Post models to allow different dependency options def find_post_with_dependency(post_id, association, association_name, dependency) -- cgit v1.2.3