1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add some tests for functionality in JoinAssociation which already exists but was previously untested

This commit is contained in:
Jon Leighton 2010-10-13 11:56:15 +01:00 committed by Aaron Patterson
parent 603406dc53
commit 076b75bf98
7 changed files with 43 additions and 12 deletions

View file

@ -16,7 +16,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
assert_equal 2, authors.size
assert_equal 5, authors[0].posts.size
assert_equal 1, authors[1].posts.size
assert_equal 9, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
assert_equal 10, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
end
def test_eager_association_loading_with_cascaded_two_levels_and_one_level
@ -24,7 +24,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
assert_equal 2, authors.size
assert_equal 5, authors[0].posts.size
assert_equal 1, authors[1].posts.size
assert_equal 9, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
assert_equal 10, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
assert_equal 1, authors[0].categorizations.size
assert_equal 2, authors[1].categorizations.size
end
@ -35,7 +35,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
end
authors = Author.joins(:posts).eager_load(:comments).where(:posts => {:taggings_count => 1}).all
assert_equal 1, assert_no_queries { authors.size }
assert_equal 9, assert_no_queries { authors[0].comments.size }
assert_equal 10, assert_no_queries { authors[0].comments.size }
end
def test_eager_association_loading_grafts_stashed_associations_to_correct_parent
@ -57,7 +57,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
assert_equal 2, authors.size
assert_equal 5, authors[0].posts.size
assert_equal 1, authors[1].posts.size
assert_equal 9, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
assert_equal 10, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
end
def test_eager_association_loading_with_cascaded_two_levels_and_self_table_reference

View file

@ -174,7 +174,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
def test_eager_association_loading_with_belongs_to
comments = Comment.find(:all, :include => :post)
assert_equal 10, comments.length
assert_equal 11, comments.length
titles = comments.map { |c| c.post.title }
assert titles.include?(posts(:welcome).title)
assert titles.include?(posts(:sti_post_and_comments).title)
@ -532,7 +532,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
def test_eager_has_many_with_association_inheritance
post = Post.find(4, :include => [ :special_comments ])
post.special_comments.each do |special_comment|
assert_equal "SpecialComment", special_comment.class.to_s
assert special_comment.is_a?(SpecialComment)
end
end
@ -726,8 +726,8 @@ class EagerAssociationTest < ActiveRecord::TestCase
posts = assert_queries(2) do
Post.find(:all, :joins => :comments, :include => :author, :order => 'comments.id DESC')
end
assert_equal posts(:eager_other), posts[0]
assert_equal authors(:mary), assert_no_queries { posts[0].author}
assert_equal posts(:eager_other), posts[1]
assert_equal authors(:mary), assert_no_queries { posts[1].author}
end
def test_eager_loading_with_conditions_on_joined_table_preloads

View file

@ -4,9 +4,12 @@ require 'models/comment'
require 'models/author'
require 'models/category'
require 'models/categorization'
require 'models/tagging'
require 'models/tag'
class InnerJoinAssociationTest < ActiveRecord::TestCase
fixtures :authors, :posts, :comments, :categories, :categories_posts, :categorizations
fixtures :authors, :posts, :comments, :categories, :categories_posts, :categorizations,
:taggings, :tags
def test_construct_finder_sql_applies_aliases_tables_on_association_conditions
result = Author.joins(:thinking_posts, :welcome_posts).to_a
@ -62,4 +65,23 @@ class InnerJoinAssociationTest < ActiveRecord::TestCase
authors_with_welcoming_post_titles = Author.calculate(:count, 'authors.id', :joins => :posts, :distinct => true, :conditions => "posts.title like 'Welcome%'")
assert_equal real_count, authors_with_welcoming_post_titles, "inner join and conditions should have only returned authors posting titles starting with 'Welcome'"
end
def test_find_with_sti_join
scope = Post.joins(:special_comments).where(:id => posts(:sti_comments).id)
# The join should match SpecialComment and its subclasses only
assert scope.where("comments.type" => "Comment").empty?
assert !scope.where("comments.type" => "SpecialComment").empty?
assert !scope.where("comments.type" => "SubSpecialComment").empty?
end
def test_find_with_conditions_on_reflection
assert !posts(:welcome).comments.empty?
assert Post.joins(:nonexistant_comments).where(:id => posts(:welcome).id).empty? # [sic!]
end
def test_find_with_conditions_on_through_reflection
assert !posts(:welcome).tags.empty?
assert Post.joins(:misc_tags).where(:id => posts(:welcome).id).empty?
end
end

View file

@ -406,7 +406,7 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
author = Author.find :first, :conditions => ['name = ?', 'David'], :include => :comments, :order => 'comments.id'
SpecialComment.new; VerySpecialComment.new
assert_no_queries do
assert_equal [1,2,3,5,6,7,8,9,10], author.comments.collect(&:id)
assert_equal [1,2,3,5,6,7,8,9,10,12], author.comments.collect(&:id)
end
end
@ -508,7 +508,7 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
def test_has_many_through_collection_size_doesnt_load_target_if_not_loaded
author = authors(:david)
assert_equal 9, author.comments.size
assert_equal 10, author.comments.size
assert !author.comments.loaded?
end

View file

@ -259,7 +259,7 @@ class FinderTest < ActiveRecord::TestCase
end
def test_find_on_association_proxy_conditions
assert_equal [1, 2, 3, 5, 6, 7, 8, 9, 10], Comment.find_all_by_post_id(authors(:david).posts).map(&:id).sort
assert_equal [1, 2, 3, 5, 6, 7, 8, 9, 10, 12], Comment.find_all_by_post_id(authors(:david).posts).map(&:id).sort
end
def test_find_on_hash_conditions_with_range

View file

@ -57,3 +57,9 @@ eager_other_comment1:
post_id: 7
body: go crazy
type: SpecialComment
sub_special_comment:
id: 12
post_id: 4
body: Sub special comment
type: SubSpecialComment

View file

@ -23,6 +23,9 @@ class SpecialComment < Comment
end
end
class SubSpecialComment < SpecialComment
end
class VerySpecialComment < Comment
def self.what_are_you
'a very special comment...'