Froze rails gems
[depot.git] / vendor / rails / activerecord / test / cases / associations / cascaded_eager_loading_test.rb
1 require "cases/helper"
2 require 'models/post'
3 require 'models/comment'
4 require 'models/author'
5 require 'models/category'
6 require 'models/categorization'
7 require 'models/company'
8 require 'models/topic'
9 require 'models/reply'
10
11 class CascadedEagerLoadingTest < ActiveRecord::TestCase
12 fixtures :authors, :mixins, :companies, :posts, :topics, :accounts, :comments, :categorizations
13
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}
20 end
21
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
30 end
31
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}
38 end
39
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
46 end
47
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
52 end
53
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 }
60 end
61
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
65 assert_no_queries do
66 assert_equal first, topics[0].replies.size
67 assert_equal second, topics[1].replies.size
68 end
69 end
70
71 def test_eager_association_loading_with_has_many_sti_and_subclasses
72 silly = SillyReply.new(:title => "gaga", :content => "boo-boo", :parent_id => 1)
73 silly.parent_id = 1
74 assert silly.save
75
76 topics = Topic.find(:all, :include => :replies, :order => 'topics.id, replies_topics.id')
77 assert_no_queries do
78 assert_equal 2, topics[0].replies.size
79 assert_equal 0, topics[1].replies.size
80 end
81 end
82
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 }
88 end
89
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
93 assert_no_queries do
94 author.posts.first.special_comments
95 author.posts.first.very_special_comment
96 end
97 end
98
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
102 assert_no_queries do
103 authors.first.posts.first.special_comments.first.post.special_comments
104 authors.first.posts.first.special_comments.first.post.very_special_comment
105 end
106 end
107 end
108
109 require 'models/vertex'
110 require 'models/edge'
111 class CascadedEagerLoadingTest < ActiveRecord::TestCase
112 fixtures :edges, :vertices
113
114 def test_eager_association_loading_with_recursive_cascading_four_levels_has_many_through
115 source = Vertex.find(:first, :include=>{:sinks=>{:sinks=>{:sinks=>:sinks}}}, :order => 'vertices.id')
116 assert_equal vertices(:vertex_4), assert_no_queries { source.sinks.first.sinks.first.sinks.first }
117 end
118
119 def test_eager_association_loading_with_recursive_cascading_four_levels_has_and_belongs_to_many
120 sink = Vertex.find(:first, :include=>{:sources=>{:sources=>{:sources=>:sources}}}, :order => 'vertices.id DESC')
121 assert_equal vertices(:vertex_1), assert_no_queries { sink.sources.first.sources.first.sources.first.sources.first }
122 end
123 end