0
@@ -18,7 +18,7 @@ require 'models/developer'
0
require 'models/project'
0
class EagerAssociationTest < ActiveRecord::TestCase
0
- fixtures :posts, :comments, :authors, :
categories, :categories_posts,
0
+ fixtures :posts, :comments, :authors, :
author_addresses, :categories, :categories_posts,
0
:companies, :accounts, :tags, :taggings, :people, :readers,
0
:owners, :pets, :author_favorites, :jobs, :references, :subscribers, :subscriptions, :books,
0
:developers, :projects, :developers_projects
0
@@ -111,6 +111,46 @@ class EagerAssociationTest < ActiveRecord::TestCase
0
+ def test_finding_with_includes_on_has_many_association_with_same_include_includes_only_once
0
+ author_id = authors(:david).id
0
+ author = assert_queries(3) { Author.find(author_id, :include => {:posts_with_comments => :comments}) } # find the author, then find the posts, then find the comments
0
+ author.posts_with_comments.each do |post_with_comments|
0
+ assert_equal post_with_comments.comments.length, post_with_comments.comments.count
0
+ assert_equal nil, post_with_comments.comments.uniq!
0
+ def test_finding_with_includes_on_has_one_assocation_with_same_include_includes_only_once
0
+ author = authors(:david)
0
+ post = author.post_about_thinking_with_last_comment
0
+ last_comment = post.last_comment
0
+ author = assert_queries(3) { Author.find(author.id, :include => {:post_about_thinking_with_last_comment => :last_comment})} # find the author, then find the posts, then find the comments
0
+ assert_equal post, author.post_about_thinking_with_last_comment
0
+ assert_equal last_comment, author.post_about_thinking_with_last_comment.last_comment
0
+ def test_finding_with_includes_on_belongs_to_association_with_same_include_includes_only_once
0
+ post = posts(:welcome)
0
+ author_address = author.author_address
0
+ post = assert_queries(3) { Post.find(post.id, :include => {:author_with_address => :author_address}) } # find the post, then find the author, then find the address
0
+ assert_equal author, post.author_with_address
0
+ assert_equal author_address, post.author_with_address.author_address
0
+ def test_finding_with_includes_on_null_belongs_to_association_with_same_include_includes_only_once
0
+ post = posts(:welcome)
0
+ post.update_attributes!(:author => nil)
0
+ post = assert_queries(2) { Post.find(post.id, :include => {:author_with_address => :author_address}) } # find the post, then find the author which is null so no query for the address
0
+ assert_equal nil, post.author_with_address
0
def test_loading_from_an_association
0
posts = authors(:david).posts.find(:all, :include => :comments, :order => "posts.id")
0
assert_equal 2, posts.first.comments.size