3
require 'models/person'
4
require 'models/reference'
6
require 'models/reader'
7
require 'models/comment'
9
require 'models/tagging'
10
require 'models/author'
11
require 'models/owner'
14
require 'models/contract'
15
require 'models/company'
16
require 'models/developer'
18
class HasManyThroughAssociationsTest < ActiveRecord::TestCase
19
fixtures :posts, :readers, :people, :comments, :authors, :owners, :pets, :toys,
22
def test_associate_existing
23
assert_queries(2) { posts(:thinking);people(:david) }
25
posts(:thinking).people
28
posts(:thinking).people << people(:david)
32
assert posts(:thinking).people.include?(people(:david))
35
assert posts(:thinking).reload.people(true).include?(people(:david))
38
def test_associating_new
39
assert_queries(1) { posts(:thinking) }
40
new_person = nil # so block binding catches it
43
new_person = Person.new :first_name => 'bob'
46
# Associating new records always saves them
47
# Thus, 1 query for the new person record, 1 query for the new join table record
49
posts(:thinking).people << new_person
53
assert posts(:thinking).people.include?(new_person)
56
assert posts(:thinking).reload.people(true).include?(new_person)
59
def test_associate_new_by_building
60
assert_queries(1) { posts(:thinking) }
63
posts(:thinking).people.build(:first_name=>"Bob")
64
posts(:thinking).people.new(:first_name=>"Ted")
67
# Should only need to load the association once
69
assert posts(:thinking).people.collect(&:first_name).include?("Bob")
70
assert posts(:thinking).people.collect(&:first_name).include?("Ted")
73
# 2 queries for each new record (1 to save the record itself, 1 for the join model)
75
# + 1 query to save the actual post = 5
77
posts(:thinking).body += '-changed'
81
assert posts(:thinking).reload.people(true).collect(&:first_name).include?("Bob")
82
assert posts(:thinking).reload.people(true).collect(&:first_name).include?("Ted")
85
def test_delete_association
86
assert_queries(2){posts(:welcome);people(:michael); }
89
posts(:welcome).people.delete(people(:michael))
93
assert posts(:welcome).people.empty?
96
assert posts(:welcome).reload.people(true).empty?
99
def test_destroy_association
100
assert_difference "Person.count", -1 do
101
posts(:welcome).people.destroy(people(:michael))
104
assert posts(:welcome).reload.people.empty?
105
assert posts(:welcome).people(true).empty?
109
assert_difference "Person.count", -1 do
110
posts(:welcome).people.destroy_all
113
assert posts(:welcome).reload.people.empty?
114
assert posts(:welcome).people(true).empty?
117
def test_replace_association
118
assert_queries(4){posts(:welcome);people(:david);people(:michael); posts(:welcome).people(true)}
120
# 1 query to delete the existing reader (michael)
121
# 1 query to associate the new reader (david)
123
posts(:welcome).people = [people(:david)]
127
assert posts(:welcome).people.include?(people(:david))
128
assert !posts(:welcome).people.include?(people(:michael))
131
assert posts(:welcome).reload.people(true).include?(people(:david))
132
assert !posts(:welcome).reload.people(true).include?(people(:michael))
135
def test_replace_order_is_preserved
136
posts(:welcome).people.clear
137
posts(:welcome).people = [people(:david), people(:michael)]
138
assert_equal [people(:david).id, people(:michael).id], posts(:welcome).readers.all(:order => 'id').map(&:person_id)
140
# Test the inverse order in case the first success was a coincidence
141
posts(:welcome).people.clear
142
posts(:welcome).people = [people(:michael), people(:david)]
143
assert_equal [people(:michael).id, people(:david).id], posts(:welcome).readers.all(:order => 'id').map(&:person_id)
146
def test_replace_by_id_order_is_preserved
147
posts(:welcome).people.clear
148
posts(:welcome).person_ids = [people(:david).id, people(:michael).id]
149
assert_equal [people(:david).id, people(:michael).id], posts(:welcome).readers.all(:order => 'id').map(&:person_id)
151
# Test the inverse order in case the first success was a coincidence
152
posts(:welcome).people.clear
153
posts(:welcome).person_ids = [people(:michael).id, people(:david).id]
154
assert_equal [people(:michael).id, people(:david).id], posts(:welcome).readers.all(:order => 'id').map(&:person_id)
157
def test_associate_with_create
158
assert_queries(1) { posts(:thinking) }
160
# 1 query for the new record, 1 for the join table record
161
# No need to update the actual collection yet!
163
posts(:thinking).people.create(:first_name=>"Jeb")
166
# *Now* we actually need the collection so it's loaded
168
assert posts(:thinking).people.collect(&:first_name).include?("Jeb")
171
assert posts(:thinking).reload.people(true).collect(&:first_name).include?("Jeb")
174
def test_associate_with_create_and_no_options
175
peeps = posts(:thinking).people.count
176
posts(:thinking).people.create(:first_name => 'foo')
177
assert_equal peeps + 1, posts(:thinking).people.count
180
def test_associate_with_create_exclamation_and_no_options
181
peeps = posts(:thinking).people.count
182
posts(:thinking).people.create!(:first_name => 'foo')
183
assert_equal peeps + 1, posts(:thinking).people.count
186
def test_associate_with_create_and_invalid_options
187
peeps = companies(:first_firm).developers.count
188
assert_nothing_raised { companies(:first_firm).developers.create(:name => '0') }
189
assert_equal peeps, companies(:first_firm).developers.count
192
def test_associate_with_create_and_valid_options
193
peeps = companies(:first_firm).developers.count
194
assert_nothing_raised { companies(:first_firm).developers.create(:name => 'developer') }
195
assert_equal peeps + 1, companies(:first_firm).developers.count
198
def test_associate_with_create_bang_and_invalid_options
199
peeps = companies(:first_firm).developers.count
200
assert_raises(ActiveRecord::RecordInvalid) { companies(:first_firm).developers.create!(:name => '0') }
201
assert_equal peeps, companies(:first_firm).developers.count
204
def test_associate_with_create_bang_and_valid_options
205
peeps = companies(:first_firm).developers.count
206
assert_nothing_raised { companies(:first_firm).developers.create!(:name => 'developer') }
207
assert_equal peeps + 1, companies(:first_firm).developers.count
210
def test_clear_associations
211
assert_queries(2) { posts(:welcome);posts(:welcome).people(true) }
214
posts(:welcome).people.clear
218
assert posts(:welcome).people.empty?
221
assert posts(:welcome).reload.people(true).empty?
224
def test_association_callback_ordering
227
post = posts(:thinking)
229
post.people_with_callbacks << people(:michael)
231
[:added, :before, "Michael"],
232
[:added, :after, "Michael"]
235
post.people_with_callbacks.push(people(:david), Person.create!(:first_name => "Bob"), Person.new(:first_name => "Lary"))
237
[:added, :before, "David"],
238
[:added, :after, "David"],
239
[:added, :before, "Bob"],
240
[:added, :after, "Bob"],
241
[:added, :before, "Lary"],
242
[:added, :after, "Lary"]
245
post.people_with_callbacks.build(:first_name => "Ted")
247
[:added, :before, "Ted"],
248
[:added, :after, "Ted"]
251
post.people_with_callbacks.create(:first_name => "Sam")
253
[:added, :before, "Sam"],
254
[:added, :after, "Sam"]
257
post.people_with_callbacks = [people(:michael),people(:david), Person.new(:first_name => "Julian"), Person.create!(:first_name => "Roger")]
258
assert_equal (%w(Ted Bob Sam Lary) * 2).sort, log[-12..-5].collect(&:last).sort
260
[:added, :before, "Julian"],
261
[:added, :after, "Julian"],
262
[:added, :before, "Roger"],
263
[:added, :after, "Roger"]
266
post.people_with_callbacks.clear
267
assert_equal (%w(Michael David Julian Roger) * 2).sort, log.last(8).collect(&:last).sort
270
def test_dynamic_find_should_respect_association_include
271
# SQL error in sort clause if :include is not included
272
# due to Unknown column 'comments.id'
273
assert Person.find(1).posts_with_comments_sorted_by_comment_id.find_by_title('Welcome to the weblog')
276
def test_count_with_include_should_alias_join_table
277
assert_equal 2, people(:michael).posts.count(:include => :readers)
280
def test_inner_join_with_quoted_table_name
281
assert_equal 2, people(:michael).jobs.size
285
assert_equal [posts(:welcome).id, posts(:authorless).id].sort, people(:michael).post_ids.sort
288
def test_get_ids_for_loaded_associations
289
person = people(:michael)
297
def test_get_ids_for_unloaded_associations_does_not_load_them
298
person = people(:michael)
299
assert !person.posts.loaded?
300
assert_equal [posts(:welcome).id, posts(:authorless).id].sort, person.post_ids.sort
301
assert !person.posts.loaded?
304
def test_association_proxy_transaction_method_starts_transaction_in_association_class
305
Tag.expects(:transaction)
306
Post.find(:first).tags.transaction do
311
def test_has_many_association_through_a_belongs_to_association_where_the_association_doesnt_exist
312
author = authors(:mary)
313
post = Post.create!(:title => "TITLE", :body => "BODY")
314
assert_equal [], post.author_favorites
317
def test_has_many_association_through_a_belongs_to_association
318
author = authors(:mary)
319
post = Post.create!(:author => author, :title => "TITLE", :body => "BODY")
320
author.author_favorites.create(:favorite_author_id => 1)
321
author.author_favorites.create(:favorite_author_id => 2)
322
author.author_favorites.create(:favorite_author_id => 3)
323
assert_equal post.author.author_favorites, post.author_favorites
326
def test_has_many_association_through_a_has_many_association_with_nonstandard_primary_keys
327
assert_equal 1, owners(:blackbeard).toys.count
330
def test_find_on_has_many_association_collection_with_include_and_conditions
331
post_with_no_comments = people(:michael).posts_with_no_comments.first
332
assert_equal post_with_no_comments, posts(:authorless)
335
def test_has_many_through_has_one_reflection
336
assert_equal [comments(:eager_sti_on_associations_vs_comment)], authors(:david).very_special_comments
339
def test_modifying_has_many_through_has_one_reflection_should_raise
341
lambda { authors(:david).very_special_comments = [VerySpecialComment.create!(:body => "Gorp!", :post_id => 1011), VerySpecialComment.create!(:body => "Eep!", :post_id => 1012)] },
342
lambda { authors(:david).very_special_comments << VerySpecialComment.create!(:body => "Hoohah!", :post_id => 1013) },
343
lambda { authors(:david).very_special_comments.delete(authors(:david).very_special_comments.first) },
344
].each {|block| assert_raise(ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection, &block) }