Skip to content

Commit

Permalink
Add assertions for nested through associations loaded by includes wit…
Browse files Browse the repository at this point in the history
…h conditions (uses the single-query strategy). Currently one failure to fix.
  • Loading branch information
jonleighton committed Oct 15, 2010
1 parent 06c64eb commit 1e2525b
Showing 1 changed file with 75 additions and 81 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -58,18 +58,16 @@ def test_has_many_through_has_many_with_has_many_through_source_reflection


assert_equal [general, general], authors(:david).tags assert_equal [general, general], authors(:david).tags


# Only David has a Post tagged with General assert_includes_and_joins_equal(
authors = Author.joins(:tags).where('tags.id' => tags(:general).id) Author.where('tags.id' => tags(:general).id),
assert_equal [authors(:david)], authors.uniq [authors(:david)], :tags
)


# This ensures that the polymorphism of taggings is being observed correctly # This ensures that the polymorphism of taggings is being observed correctly
authors = Author.joins(:tags).where('taggings.taggable_type' => 'FakeModel') authors = Author.joins(:tags).where('taggings.taggable_type' => 'FakeModel')
assert authors.empty? assert authors.empty?


assert_queries(5) do authors = assert_queries(5) { Author.includes(:tags).to_a }
authors = Author.includes(:tags).to_a
end

assert_no_queries do assert_no_queries do
assert_equal [general, general], authors.first.tags assert_equal [general, general], authors.first.tags
end end
Expand All @@ -85,13 +83,12 @@ def test_has_many_through_has_many_through_with_has_many_source_reflection
assert_equal [luke, david, david], author.subscribers assert_equal [luke, david, david], author.subscribers


# All authors with subscribers where one of the subscribers' nick is 'alterself' # All authors with subscribers where one of the subscribers' nick is 'alterself'
authors = Author.joins(:subscribers).where('subscribers.nick' => 'alterself') assert_includes_and_joins_equal(
assert_equal [authors(:david)], authors Author.where('subscribers.nick' => 'alterself'),

[authors(:david)], :subscribers
assert_queries(4) do )
authors = Author.includes(:subscribers).to_a
end


authors = assert_queries(4) { Author.includes(:subscribers).to_a }
assert_no_queries do assert_no_queries do
assert_equal [luke, david, david], authors.first.subscribers.sort_by(&:nick) assert_equal [luke, david, david], authors.first.subscribers.sort_by(&:nick)
end end
Expand All @@ -107,13 +104,12 @@ def test_has_many_through_has_one_with_has_one_through_source_reflection


assert_equal [founding], members(:groucho).nested_member_types assert_equal [founding], members(:groucho).nested_member_types


members = Member.joins(:nested_member_types).where('member_types.id' => founding.id) assert_includes_and_joins_equal(
assert_equal [members(:groucho)], members Member.where('member_types.id' => founding.id),

[members(:groucho)], :nested_member_types
assert_queries(4) do )
members = Member.includes(:nested_member_types).to_a
end


members = assert_queries(4) { Member.includes(:nested_member_types).to_a }
assert_no_queries do assert_no_queries do
assert_equal [founding], members.first.nested_member_types assert_equal [founding], members.first.nested_member_types
end end
Expand All @@ -127,13 +123,12 @@ def test_has_many_through_has_one_through_with_has_one_source_reflection


assert_equal [mustache], members(:groucho).nested_sponsors assert_equal [mustache], members(:groucho).nested_sponsors


members = Member.joins(:nested_sponsors).where('sponsors.id' => mustache.id) assert_includes_and_joins_equal(
assert_equal [members(:groucho)], members Member.where('sponsors.id' => mustache.id),

[members(:groucho)], :nested_sponsors
assert_queries(4) do )
members = Member.includes(:nested_sponsors).to_a
end


members = assert_queries(4) { Member.includes(:nested_sponsors).to_a }
assert_no_queries do assert_no_queries do
assert_equal [mustache], members.first.nested_sponsors assert_equal [mustache], members.first.nested_sponsors
end end
Expand All @@ -147,18 +142,16 @@ def test_has_many_through_has_one_with_has_many_through_source_reflection


assert_equal [groucho_details, other_details], members(:groucho).organization_member_details assert_equal [groucho_details, other_details], members(:groucho).organization_member_details


members = Member.joins(:organization_member_details). assert_includes_and_joins_equal(
where('member_details.id' => member_details(:groucho).id) Member.where('member_details.id' => member_details(:groucho).id),
assert_equal [members(:groucho), members(:some_other_guy)], members [members(:groucho), members(:some_other_guy)], :organization_member_details
)


members = Member.joins(:organization_member_details). members = Member.joins(:organization_member_details).
where('member_details.id' => 9) where('member_details.id' => 9)
assert members.empty? assert members.empty?


assert_queries(4) do members = assert_queries(4) { Member.includes(:organization_member_details).to_a }
members = Member.includes(:organization_member_details).to_a
end

assert_no_queries do assert_no_queries do
assert_equal [groucho_details, other_details], members.first.organization_member_details assert_equal [groucho_details, other_details], members.first.organization_member_details
end end
Expand All @@ -172,18 +165,16 @@ def test_has_many_through_has_one_through_with_has_many_source_reflection


assert_equal [groucho_details, other_details], members(:groucho).organization_member_details_2 assert_equal [groucho_details, other_details], members(:groucho).organization_member_details_2


members = Member.joins(:organization_member_details_2). assert_includes_and_joins_equal(
where('member_details.id' => groucho_details.id) Member.where('member_details.id' => groucho_details.id),
assert_equal [members(:groucho), members(:some_other_guy)], members [members(:groucho), members(:some_other_guy)], :organization_member_details_2
)


members = Member.joins(:organization_member_details_2). members = Member.joins(:organization_member_details_2).
where('member_details.id' => 9) where('member_details.id' => 9)
assert members.empty? assert members.empty?


assert_queries(4) do members = assert_queries(4) { Member.includes(:organization_member_details_2).to_a }
members = Member.includes(:organization_member_details_2).to_a
end

assert_no_queries do assert_no_queries do
assert_equal [groucho_details, other_details], members.first.organization_member_details_2 assert_equal [groucho_details, other_details], members.first.organization_member_details_2
end end
Expand All @@ -197,13 +188,12 @@ def test_has_many_through_has_many_with_has_and_belongs_to_many_source_reflectio


assert_equal [general, cooking], authors(:bob).post_categories assert_equal [general, cooking], authors(:bob).post_categories


authors = Author.joins(:post_categories).where('categories.id' => cooking.id) assert_includes_and_joins_equal(
assert_equal [authors(:bob)], authors Author.where('categories.id' => cooking.id),

[authors(:bob)], :post_categories
assert_queries(3) do )
authors = Author.includes(:post_categories).to_a
end


authors = assert_queries(3) { Author.includes(:post_categories).to_a }
assert_no_queries do assert_no_queries do
assert_equal [general, cooking], authors[2].post_categories assert_equal [general, cooking], authors[2].post_categories
end end
Expand All @@ -217,13 +207,12 @@ def test_has_many_through_has_and_belongs_to_many_with_has_many_source_reflectio


assert_equal [greetings, more], categories(:technology).post_comments assert_equal [greetings, more], categories(:technology).post_comments


categories = Category.joins(:post_comments).where('comments.id' => more.id) assert_includes_and_joins_equal(
assert_equal [categories(:general), categories(:technology)], categories Category.where('comments.id' => more.id),

[categories(:general), categories(:technology)], :post_comments
assert_queries(3) do )
categories = Category.includes(:post_comments).to_a
end


categories = assert_queries(3) { Category.includes(:post_comments).to_a }
assert_no_queries do assert_no_queries do
assert_equal [greetings, more], categories[1].post_comments assert_equal [greetings, more], categories[1].post_comments
end end
Expand All @@ -237,13 +226,12 @@ def test_has_many_through_has_many_with_has_many_through_habtm_source_reflection


assert_equal [greetings, more], authors(:bob).category_post_comments assert_equal [greetings, more], authors(:bob).category_post_comments


authors = Author.joins(:category_post_comments).where('comments.id' => comments(:does_it_hurt).id) assert_includes_and_joins_equal(
assert_equal [authors(:david), authors(:mary)], authors Author.where('comments.id' => comments(:does_it_hurt).id),

[authors(:david), authors(:mary)], :category_post_comments
assert_queries(5) do )
authors = Author.includes(:category_post_comments).to_a
end


authors = assert_queries(5) { Author.includes(:category_post_comments).to_a }
assert_no_queries do assert_no_queries do
assert_equal [greetings, more], authors[2].category_post_comments assert_equal [greetings, more], authors[2].category_post_comments
end end
Expand All @@ -257,13 +245,12 @@ def test_has_many_through_has_many_through_with_belongs_to_source_reflection


assert_equal [general, general], authors(:david).tagging_tags assert_equal [general, general], authors(:david).tagging_tags


authors = Author.joins(:tagging_tags).where('tags.id' => tags(:general).id) assert_includes_and_joins_equal(
assert_equal [authors(:david)], authors.uniq Author.where('tags.id' => tags(:general).id),

[authors(:david)], :tagging_tags
assert_queries(5) do )
authors = Author.includes(:tagging_tags).to_a
end


authors = assert_queries(5) { Author.includes(:tagging_tags).to_a }
assert_no_queries do assert_no_queries do
assert_equal [general, general], authors.first.tagging_tags assert_equal [general, general], authors.first.tagging_tags
end end
Expand All @@ -277,13 +264,12 @@ def test_has_many_through_belongs_to_with_has_many_through_source_reflection


assert_equal [welcome_general, thinking_general], categorizations(:david_welcome_general).post_taggings assert_equal [welcome_general, thinking_general], categorizations(:david_welcome_general).post_taggings


categorizations = Categorization.joins(:post_taggings).where('taggings.id' => welcome_general.id) assert_includes_and_joins_equal(
assert_equal [categorizations(:david_welcome_general)], categorizations Categorization.where('taggings.id' => welcome_general.id),

[categorizations(:david_welcome_general)], :post_taggings
assert_queries(4) do )
categorizations = Categorization.includes(:post_taggings).to_a
end


categorizations = assert_queries(4) { Categorization.includes(:post_taggings).to_a }
assert_no_queries do assert_no_queries do
assert_equal [welcome_general, thinking_general], categorizations.first.post_taggings assert_equal [welcome_general, thinking_general], categorizations.first.post_taggings
end end
Expand All @@ -297,13 +283,12 @@ def test_has_one_through_has_one_with_has_one_through_source_reflection


assert_equal founding, members(:groucho).nested_member_type assert_equal founding, members(:groucho).nested_member_type


members = Member.joins(:nested_member_type).where('member_types.id' => founding.id) assert_includes_and_joins_equal(
assert_equal [members(:groucho)], members Member.where('member_types.id' => founding.id),

[members(:groucho)], :nested_member_type
assert_queries(4) do )
members = Member.includes(:nested_member_type).to_a
end


members = assert_queries(4) { Member.includes(:nested_member_type).to_a }
assert_no_queries do assert_no_queries do
assert_equal founding, members.first.nested_member_type assert_equal founding, members.first.nested_member_type
end end
Expand All @@ -317,13 +302,12 @@ def test_has_one_through_has_one_through_with_belongs_to_source_reflection


assert_equal general, members(:groucho).club_category assert_equal general, members(:groucho).club_category


members = Member.joins(:club_category).where('categories.id' => categories(:technology).id) assert_includes_and_joins_equal(
assert_equal [members(:blarpy_winkup)], members Member.where('categories.id' => categories(:technology).id),

[members(:blarpy_winkup)], :club_category
assert_queries(4) do )
members = Member.includes(:club_category).to_a
end


members = assert_queries(4) { Member.includes(:club_category).to_a }
assert_no_queries do assert_no_queries do
assert_equal general, members.first.club_category assert_equal general, members.first.club_category
end end
Expand Down Expand Up @@ -379,4 +363,14 @@ def test_has_many_through_with_sti_on_through_reflection
assert !scope.where("comments.type" => "SpecialComment").empty? assert !scope.where("comments.type" => "SpecialComment").empty?
assert !scope.where("comments.type" => "SubSpecialComment").empty? assert !scope.where("comments.type" => "SubSpecialComment").empty?
end end

private

def assert_includes_and_joins_equal(query, expected, association)
actual = assert_queries(1) { query.joins(association).to_a.uniq }
assert_equal expected, actual

actual = assert_queries(1) { query.includes(association).to_a.uniq }
assert_equal expected, actual
end
end end

0 comments on commit 1e2525b

Please sign in to comment.