3 require 'models/comment'
4 require 'models/author'
5 require 'models/category'
6 require 'models/categorization'
7 require 'models/company'
11 class CascadedEagerLoadingTest
< ActiveRecord
::TestCase
12 fixtures
:authors, :mixins, :companies, :posts, :topics, :accounts, :comments, :categorizations
14 def test_eager_association_loading_with_cascaded_two_levels
15 authors
= Author
.find(:all, :include=>{:posts=>:comments}, :order=>"authors.id")
16 assert_equal
2, authors
.size
17 assert_equal
5, authors
[0].posts
.size
18 assert_equal
1, authors
[1].posts
.size
19 assert_equal
9, authors
[0].posts
.collect
{|post
| post
.comments
.size
}.inject(0){|sum
,i
| sum
+i
}
22 def test_eager_association_loading_with_cascaded_two_levels_and_one_level
23 authors
= Author
.find(:all, :include=>[{:posts=>:comments}, :categorizations], :order=>"authors.id")
24 assert_equal
2, authors
.size
25 assert_equal
5, authors
[0].posts
.size
26 assert_equal
1, authors
[1].posts
.size
27 assert_equal
9, authors
[0].posts
.collect
{|post
| post
.comments
.size
}.inject(0){|sum
,i
| sum
+i
}
28 assert_equal
1, authors
[0].categorizations
.size
29 assert_equal
2, authors
[1].categorizations
.size
32 def test_eager_association_loading_with_cascaded_two_levels_with_two_has_many_associations
33 authors
= Author
.find(:all, :include=>{:posts=>[:comments, :categorizations]}, :order=>"authors.id")
34 assert_equal
2, authors
.size
35 assert_equal
5, authors
[0].posts
.size
36 assert_equal
1, authors
[1].posts
.size
37 assert_equal
9, authors
[0].posts
.collect
{|post
| post
.comments
.size
}.inject(0){|sum
,i
| sum
+i
}
40 def test_eager_association_loading_with_cascaded_two_levels_and_self_table_reference
41 authors
= Author
.find(:all, :include=>{:posts=>[:comments, :author]}, :order=>"authors.id")
42 assert_equal
2, authors
.size
43 assert_equal
5, authors
[0].posts
.size
44 assert_equal
authors(:david).name
, authors
[0].name
45 assert_equal
[authors(:david).name
], authors
[0].posts
.collect
{|post
| post
.author
.name
}.uniq
48 def test_eager_association_loading_with_cascaded_two_levels_with_condition
49 authors
= Author
.find(:all, :include=>{:posts=>:comments}, :conditions=>"authors.id=1", :order=>"authors.id")
50 assert_equal
1, authors
.size
51 assert_equal
5, authors
[0].posts
.size
54 def test_eager_association_loading_with_cascaded_three_levels_by_ping_pong
55 firms
= Firm
.find(:all, :include=>{:account=>{:firm=>:account}}, :order=>"companies.id")
56 assert_equal
2, firms
.size
57 assert_equal firms
.first
.account
, firms
.first
.account
.firm
.account
58 assert_equal
companies(:first_firm).account
, assert_no_queries
{ firms
.first
.account
.firm
.account
}
59 assert_equal
companies(:first_firm).account
.firm
.account
, assert_no_queries
{ firms
.first
.account
.firm
.account
}
62 def test_eager_association_loading_with_has_many_sti
63 topics
= Topic
.find(:all, :include => :replies, :order => 'topics.id')
64 first
, second
, = topics(:first).replies
.size
, topics(:second).replies
.size
66 assert_equal first
, topics
[0].replies
.size
67 assert_equal second
, topics
[1].replies
.size
71 def test_eager_association_loading_with_has_many_sti_and_subclasses
72 silly
= SillyReply
.new(:title => "gaga", :content => "boo-boo", :parent_id => 1)
76 topics
= Topic
.find(:all, :include => :replies, :order => 'topics.id, replies_topics.id')
78 assert_equal
2, topics
[0].replies
.size
79 assert_equal
0, topics
[1].replies
.size
83 def test_eager_association_loading_with_belongs_to_sti
84 replies
= Reply
.find(:all, :include => :topic, :order => 'topics.id')
85 assert replies
.include?(topics(:second))
86 assert
!replies
.include?(topics(:first))
87 assert_equal
topics(:first), assert_no_queries
{ replies
.first
.topic
}
90 def test_eager_association_loading_with_multiple_stis_and_order
91 author
= Author
.find(:first, :include => { :posts => [ :special_comments , :very_special_comment ] }, :order => 'authors.name, comments.body, very_special_comments_posts.body', :conditions => 'posts.id = 4')
92 assert_equal
authors(:david), author
94 author
.posts
.first
.special_comments
95 author
.posts
.first
.very_special_comment
99 def test_eager_association_loading_of_stis_with_multiple_references
100 authors
= Author
.find(:all, :include => { :posts => { :special_comments => { :post => [ :special_comments, :very_special_comment ] } } }, :order => 'comments.body, very_special_comments_posts.body', :conditions => 'posts.id = 4')
101 assert_equal
[authors(:david)], authors
103 authors
.first
.posts
.first
.special_comments
.first
.post
.special_comments
104 authors
.first
.posts
.first
.special_comments
.first
.post
.very_special_comment
108 def test_eager_association_loading_where_first_level_returns_nil
109 authors
= Author
.find(:all, :include => {:post_about_thinking => :comments}, :order => 'authors.id DESC')
110 assert_equal
[authors(:mary), authors(:david)], authors
112 authors
[1].post_about_thinking
.comments
.first
117 require 'models/vertex'
118 require 'models/edge'
119 class CascadedEagerLoadingTest
< ActiveRecord
::TestCase
120 fixtures
:edges, :vertices
122 def test_eager_association_loading_with_recursive_cascading_four_levels_has_many_through
123 source
= Vertex
.find(:first, :include=>{:sinks=>{:sinks=>{:sinks=>:sinks}}}, :order => 'vertices.id')
124 assert_equal
vertices(:vertex_4), assert_no_queries
{ source
.sinks
.first
.sinks
.first
.sinks
.first
}
127 def test_eager_association_loading_with_recursive_cascading_four_levels_has_and_belongs_to_many
128 sink
= Vertex
.find(:first, :include=>{:sources=>{:sources=>{:sources=>:sources}}}, :order => 'vertices.id DESC')
129 assert_equal
vertices(:vertex_1), assert_no_queries
{ sink
.sources
.first
.sources
.first
.sources
.first
.sources
.first
}