Froze rails gems
[depot.git] / vendor / rails / activerecord / test / cases / associations / has_many_through_associations_test.rb
1 require "cases/helper"
2 require 'models/post'
3 require 'models/person'
4 require 'models/reader'
5 require 'models/comment'
6
7 class HasManyThroughAssociationsTest < ActiveRecord::TestCase
8 fixtures :posts, :readers, :people, :comments, :authors
9
10 def test_associate_existing
11 assert_queries(2) { posts(:thinking);people(:david) }
12
13 posts(:thinking).people
14
15 assert_queries(1) do
16 posts(:thinking).people << people(:david)
17 end
18
19 assert_queries(1) do
20 assert posts(:thinking).people.include?(people(:david))
21 end
22
23 assert posts(:thinking).reload.people(true).include?(people(:david))
24 end
25
26 def test_associating_new
27 assert_queries(1) { posts(:thinking) }
28 new_person = nil # so block binding catches it
29
30 assert_queries(0) do
31 new_person = Person.new :first_name => 'bob'
32 end
33
34 # Associating new records always saves them
35 # Thus, 1 query for the new person record, 1 query for the new join table record
36 assert_queries(2) do
37 posts(:thinking).people << new_person
38 end
39
40 assert_queries(1) do
41 assert posts(:thinking).people.include?(new_person)
42 end
43
44 assert posts(:thinking).reload.people(true).include?(new_person)
45 end
46
47 def test_associate_new_by_building
48 assert_queries(1) { posts(:thinking) }
49
50 assert_queries(0) do
51 posts(:thinking).people.build(:first_name=>"Bob")
52 posts(:thinking).people.new(:first_name=>"Ted")
53 end
54
55 # Should only need to load the association once
56 assert_queries(1) do
57 assert posts(:thinking).people.collect(&:first_name).include?("Bob")
58 assert posts(:thinking).people.collect(&:first_name).include?("Ted")
59 end
60
61 # 2 queries for each new record (1 to save the record itself, 1 for the join model)
62 # * 2 new records = 4
63 # + 1 query to save the actual post = 5
64 assert_queries(5) do
65 posts(:thinking).body += '-changed'
66 posts(:thinking).save
67 end
68
69 assert posts(:thinking).reload.people(true).collect(&:first_name).include?("Bob")
70 assert posts(:thinking).reload.people(true).collect(&:first_name).include?("Ted")
71 end
72
73 def test_delete_association
74 assert_queries(2){posts(:welcome);people(:michael); }
75
76 assert_queries(1) do
77 posts(:welcome).people.delete(people(:michael))
78 end
79
80 assert_queries(1) do
81 assert posts(:welcome).people.empty?
82 end
83
84 assert posts(:welcome).reload.people(true).empty?
85 end
86
87 def test_replace_association
88 assert_queries(4){posts(:welcome);people(:david);people(:michael); posts(:welcome).people(true)}
89
90 # 1 query to delete the existing reader (michael)
91 # 1 query to associate the new reader (david)
92 assert_queries(2) do
93 posts(:welcome).people = [people(:david)]
94 end
95
96 assert_queries(0){
97 assert posts(:welcome).people.include?(people(:david))
98 assert !posts(:welcome).people.include?(people(:michael))
99 }
100
101 assert posts(:welcome).reload.people(true).include?(people(:david))
102 assert !posts(:welcome).reload.people(true).include?(people(:michael))
103 end
104
105 def test_associate_with_create
106 assert_queries(1) { posts(:thinking) }
107
108 # 1 query for the new record, 1 for the join table record
109 # No need to update the actual collection yet!
110 assert_queries(2) do
111 posts(:thinking).people.create(:first_name=>"Jeb")
112 end
113
114 # *Now* we actually need the collection so it's loaded
115 assert_queries(1) do
116 assert posts(:thinking).people.collect(&:first_name).include?("Jeb")
117 end
118
119 assert posts(:thinking).reload.people(true).collect(&:first_name).include?("Jeb")
120 end
121
122 def test_associate_with_create_and_no_options
123 peeps = posts(:thinking).people.count
124 posts(:thinking).people.create(:first_name => 'foo')
125 assert_equal peeps + 1, posts(:thinking).people.count
126 end
127
128 def test_associate_with_create_exclamation_and_no_options
129 peeps = posts(:thinking).people.count
130 posts(:thinking).people.create!(:first_name => 'foo')
131 assert_equal peeps + 1, posts(:thinking).people.count
132 end
133
134 def test_clear_associations
135 assert_queries(2) { posts(:welcome);posts(:welcome).people(true) }
136
137 assert_queries(1) do
138 posts(:welcome).people.clear
139 end
140
141 assert_queries(0) do
142 assert posts(:welcome).people.empty?
143 end
144
145 assert posts(:welcome).reload.people(true).empty?
146 end
147
148 def test_association_callback_ordering
149 Post.reset_log
150 log = Post.log
151 post = posts(:thinking)
152
153 post.people_with_callbacks << people(:michael)
154 assert_equal [
155 [:added, :before, "Michael"],
156 [:added, :after, "Michael"]
157 ], log.last(2)
158
159 post.people_with_callbacks.push(people(:david), Person.create!(:first_name => "Bob"), Person.new(:first_name => "Lary"))
160 assert_equal [
161 [:added, :before, "David"],
162 [:added, :after, "David"],
163 [:added, :before, "Bob"],
164 [:added, :after, "Bob"],
165 [:added, :before, "Lary"],
166 [:added, :after, "Lary"]
167 ],log.last(6)
168
169 post.people_with_callbacks.build(:first_name => "Ted")
170 assert_equal [
171 [:added, :before, "Ted"],
172 [:added, :after, "Ted"]
173 ], log.last(2)
174
175 post.people_with_callbacks.create(:first_name => "Sam")
176 assert_equal [
177 [:added, :before, "Sam"],
178 [:added, :after, "Sam"]
179 ], log.last(2)
180
181 post.people_with_callbacks = [people(:michael),people(:david), Person.new(:first_name => "Julian"), Person.create!(:first_name => "Roger")]
182 assert_equal (%w(Ted Bob Sam Lary) * 2).sort, log[-12..-5].collect(&:last).sort
183 assert_equal [
184 [:added, :before, "Julian"],
185 [:added, :after, "Julian"],
186 [:added, :before, "Roger"],
187 [:added, :after, "Roger"]
188 ], log.last(4)
189
190 post.people_with_callbacks.clear
191 assert_equal (%w(Michael David Julian Roger) * 2).sort, log.last(8).collect(&:last).sort
192 end
193
194 def test_dynamic_find_should_respect_association_include
195 # SQL error in sort clause if :include is not included
196 # due to Unknown column 'comments.id'
197 assert Person.find(1).posts_with_comments_sorted_by_comment_id.find_by_title('Welcome to the weblog')
198 end
199
200 def test_count_with_include_should_alias_join_table
201 assert_equal 2, people(:michael).posts.count(:include => :readers)
202 end
203
204 def test_get_ids
205 assert_equal [posts(:welcome).id, posts(:authorless).id].sort, people(:michael).post_ids.sort
206 end
207
208 def test_get_ids_for_loaded_associations
209 person = people(:michael)
210 person.posts(true)
211 assert_queries(0) do
212 person.post_ids
213 person.post_ids
214 end
215 end
216
217 def test_get_ids_for_unloaded_associations_does_not_load_them
218 person = people(:michael)
219 assert !person.posts.loaded?
220 assert_equal [posts(:welcome).id, posts(:authorless).id].sort, person.post_ids.sort
221 assert !person.posts.loaded?
222 end
223
224 uses_mocha 'mocking Tag.transaction' do
225 def test_association_proxy_transaction_method_starts_transaction_in_association_class
226 Tag.expects(:transaction)
227 Post.find(:first).tags.transaction do
228 # nothing
229 end
230 end
231 end
232
233 def test_has_many_association_through_a_belongs_to_association_where_the_association_doesnt_exist
234 author = authors(:mary)
235 post = Post.create!(:title => "TITLE", :body => "BODY")
236 assert_equal [], post.author_favorites
237 end
238
239 def test_has_many_association_through_a_belongs_to_association
240 author = authors(:mary)
241 post = Post.create!(:author => author, :title => "TITLE", :body => "BODY")
242 author.author_favorites.create(:favorite_author_id => 1)
243 author.author_favorites.create(:favorite_author_id => 2)
244 author.author_favorites.create(:favorite_author_id => 3)
245 assert_equal post.author.author_favorites, post.author_favorites
246 end
247 end