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 --- .../test/cases/associations/join_model_test.rb | 59 +++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations/join_model_test.rb') 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