2005-04-03 17:50:11 +00:00
|
|
|
require 'abstract_unit'
|
|
|
|
require 'fixtures/post'
|
|
|
|
require 'fixtures/comment'
|
|
|
|
require 'fixtures/author'
|
2005-04-10 17:24:56 +00:00
|
|
|
require 'fixtures/category'
|
2005-04-03 17:50:11 +00:00
|
|
|
|
|
|
|
class EagerAssociationTest < Test::Unit::TestCase
|
2005-04-10 17:24:56 +00:00
|
|
|
fixtures :posts, :comments, :authors, :categories, :categories_posts
|
2005-04-03 17:50:11 +00:00
|
|
|
|
|
|
|
def test_loading_with_one_association
|
|
|
|
posts = Post.find(:all, :include => :comments)
|
|
|
|
assert_equal 2, posts.first.comments.size
|
|
|
|
assert_equal @greetings.body, posts.first.comments.first.body
|
|
|
|
|
|
|
|
post = Post.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'")
|
|
|
|
assert_equal 2, post.comments.size
|
|
|
|
assert_equal @greetings.body, post.comments.first.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_loading_with_multiple_associations
|
2005-04-10 17:24:56 +00:00
|
|
|
posts = Post.find(:all, :include => [ :comments, :author, :categories ])
|
2005-04-03 17:50:11 +00:00
|
|
|
assert_equal 2, posts.first.comments.size
|
2005-04-10 17:24:56 +00:00
|
|
|
assert_equal 2, posts.first.categories.size
|
2005-04-03 17:50:11 +00:00
|
|
|
assert_equal @greetings.body, posts.first.comments.first.body
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_loading_from_an_association
|
|
|
|
posts = @david.posts.find(:all, :include => :comments)
|
|
|
|
assert_equal 2, posts.first.comments.size
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_eager_association_loading_with_belongs_to
|
|
|
|
comments = Comment.find(:all, :include => :post)
|
|
|
|
assert_equal @welcome.title, comments.first.post.title
|
|
|
|
assert_equal @thinking.title, comments.last.post.title
|
|
|
|
end
|
2005-04-10 17:24:56 +00:00
|
|
|
|
|
|
|
def test_eager_association_loading_with_habtm
|
|
|
|
posts = Post.find(:all, :include => :categories)
|
|
|
|
assert_equal 2, posts.first.categories.size
|
|
|
|
assert_equal 1, posts.last.categories.size
|
|
|
|
assert_equal @technology.name, posts.first.categories.last.name
|
|
|
|
assert_equal @general.name, posts.last.categories.first.name
|
|
|
|
end
|
2005-04-10 15:49:49 +00:00
|
|
|
|
|
|
|
def test_eager_with_inheritance
|
|
|
|
posts = SpecialPost.find(:all, :include => [ :comments ])
|
2005-04-10 17:24:56 +00:00
|
|
|
end
|
2005-04-03 17:50:11 +00:00
|
|
|
end
|
|
|
|
|