Merged updates from trunk into stable branch
[feedcatcher.git] / vendor / rails / activerecord / test / cases / autosave_association_test.rb
1 require 'cases/helper'
2 require 'models/bird'
3 require 'models/company'
4 require 'models/customer'
5 require 'models/developer'
6 require 'models/order'
7 require 'models/parrot'
8 require 'models/person'
9 require 'models/pirate'
10 require 'models/post'
11 require 'models/reader'
12 require 'models/ship'
13 require 'models/ship_part'
14 require 'models/treasure'
15
16 class TestAutosaveAssociationsInGeneral < ActiveRecord::TestCase
17 def test_autosave_should_be_a_valid_option_for_has_one
18 assert base.valid_keys_for_has_one_association.include?(:autosave)
19 end
20
21 def test_autosave_should_be_a_valid_option_for_belongs_to
22 assert base.valid_keys_for_belongs_to_association.include?(:autosave)
23 end
24
25 def test_autosave_should_be_a_valid_option_for_has_many
26 assert base.valid_keys_for_has_many_association.include?(:autosave)
27 end
28
29 def test_autosave_should_be_a_valid_option_for_has_and_belongs_to_many
30 assert base.valid_keys_for_has_and_belongs_to_many_association.include?(:autosave)
31 end
32
33 private
34
35 def base
36 ActiveRecord::Base
37 end
38 end
39
40 class TestDefaultAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCase
41 def test_save_fails_for_invalid_has_one
42 firm = Firm.find(:first)
43 assert firm.valid?
44
45 firm.account = Account.new
46
47 assert !firm.account.valid?
48 assert !firm.valid?
49 assert !firm.save
50 assert_equal "is invalid", firm.errors.on("account")
51 end
52
53 def test_save_succeeds_for_invalid_has_one_with_validate_false
54 firm = Firm.find(:first)
55 assert firm.valid?
56
57 firm.unvalidated_account = Account.new
58
59 assert !firm.unvalidated_account.valid?
60 assert firm.valid?
61 assert firm.save
62 end
63
64 def test_build_before_child_saved
65 firm = Firm.find(1)
66
67 account = firm.account.build("credit_limit" => 1000)
68 assert_equal account, firm.account
69 assert account.new_record?
70 assert firm.save
71 assert_equal account, firm.account
72 assert !account.new_record?
73 end
74
75 def test_build_before_either_saved
76 firm = Firm.new("name" => "GlobalMegaCorp")
77
78 firm.account = account = Account.new("credit_limit" => 1000)
79 assert_equal account, firm.account
80 assert account.new_record?
81 assert firm.save
82 assert_equal account, firm.account
83 assert !account.new_record?
84 end
85
86 def test_assignment_before_parent_saved
87 firm = Firm.new("name" => "GlobalMegaCorp")
88 firm.account = a = Account.find(1)
89 assert firm.new_record?
90 assert_equal a, firm.account
91 assert firm.save
92 assert_equal a, firm.account
93 assert_equal a, firm.account(true)
94 end
95
96 def test_assignment_before_either_saved
97 firm = Firm.new("name" => "GlobalMegaCorp")
98 firm.account = a = Account.new("credit_limit" => 1000)
99 assert firm.new_record?
100 assert a.new_record?
101 assert_equal a, firm.account
102 assert firm.save
103 assert !firm.new_record?
104 assert !a.new_record?
105 assert_equal a, firm.account
106 assert_equal a, firm.account(true)
107 end
108
109 def test_not_resaved_when_unchanged
110 firm = Firm.find(:first, :include => :account)
111 firm.name += '-changed'
112 assert_queries(1) { firm.save! }
113
114 firm = Firm.find(:first)
115 firm.account = Account.find(:first)
116 assert_queries(Firm.partial_updates? ? 0 : 1) { firm.save! }
117
118 firm = Firm.find(:first).clone
119 firm.account = Account.find(:first)
120 assert_queries(2) { firm.save! }
121
122 firm = Firm.find(:first).clone
123 firm.account = Account.find(:first).clone
124 assert_queries(2) { firm.save! }
125 end
126 end
127
128 class TestDefaultAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase
129 def test_save_fails_for_invalid_belongs_to
130 assert log = AuditLog.create(:developer_id => 0, :message => "")
131
132 log.developer = Developer.new
133 assert !log.developer.valid?
134 assert !log.valid?
135 assert !log.save
136 assert_equal "is invalid", log.errors.on("developer")
137 end
138
139 def test_save_succeeds_for_invalid_belongs_to_with_validate_false
140 assert log = AuditLog.create(:developer_id => 0, :message=> "")
141
142 log.unvalidated_developer = Developer.new
143 assert !log.unvalidated_developer.valid?
144 assert log.valid?
145 assert log.save
146 end
147
148 def test_assignment_before_parent_saved
149 client = Client.find(:first)
150 apple = Firm.new("name" => "Apple")
151 client.firm = apple
152 assert_equal apple, client.firm
153 assert apple.new_record?
154 assert client.save
155 assert apple.save
156 assert !apple.new_record?
157 assert_equal apple, client.firm
158 assert_equal apple, client.firm(true)
159 end
160
161 def test_assignment_before_either_saved
162 final_cut = Client.new("name" => "Final Cut")
163 apple = Firm.new("name" => "Apple")
164 final_cut.firm = apple
165 assert final_cut.new_record?
166 assert apple.new_record?
167 assert final_cut.save
168 assert !final_cut.new_record?
169 assert !apple.new_record?
170 assert_equal apple, final_cut.firm
171 assert_equal apple, final_cut.firm(true)
172 end
173
174 def test_store_two_association_with_one_save
175 num_orders = Order.count
176 num_customers = Customer.count
177 order = Order.new
178
179 customer1 = order.billing = Customer.new
180 customer2 = order.shipping = Customer.new
181 assert order.save
182 assert_equal customer1, order.billing
183 assert_equal customer2, order.shipping
184
185 order.reload
186
187 assert_equal customer1, order.billing
188 assert_equal customer2, order.shipping
189
190 assert_equal num_orders +1, Order.count
191 assert_equal num_customers +2, Customer.count
192 end
193
194 def test_store_association_in_two_relations_with_one_save
195 num_orders = Order.count
196 num_customers = Customer.count
197 order = Order.new
198
199 customer = order.billing = order.shipping = Customer.new
200 assert order.save
201 assert_equal customer, order.billing
202 assert_equal customer, order.shipping
203
204 order.reload
205
206 assert_equal customer, order.billing
207 assert_equal customer, order.shipping
208
209 assert_equal num_orders +1, Order.count
210 assert_equal num_customers +1, Customer.count
211 end
212
213 def test_store_association_in_two_relations_with_one_save_in_existing_object
214 num_orders = Order.count
215 num_customers = Customer.count
216 order = Order.create
217
218 customer = order.billing = order.shipping = Customer.new
219 assert order.save
220 assert_equal customer, order.billing
221 assert_equal customer, order.shipping
222
223 order.reload
224
225 assert_equal customer, order.billing
226 assert_equal customer, order.shipping
227
228 assert_equal num_orders +1, Order.count
229 assert_equal num_customers +1, Customer.count
230 end
231
232 def test_store_association_in_two_relations_with_one_save_in_existing_object_with_values
233 num_orders = Order.count
234 num_customers = Customer.count
235 order = Order.create
236
237 customer = order.billing = order.shipping = Customer.new
238 assert order.save
239 assert_equal customer, order.billing
240 assert_equal customer, order.shipping
241
242 order.reload
243
244 customer = order.billing = order.shipping = Customer.new
245
246 assert order.save
247 order.reload
248
249 assert_equal customer, order.billing
250 assert_equal customer, order.shipping
251
252 assert_equal num_orders +1, Order.count
253 assert_equal num_customers +2, Customer.count
254 end
255 end
256
257 class TestDefaultAutosaveAssociationOnAHasManyAssociation < ActiveRecord::TestCase
258 fixtures :companies, :people
259
260 def test_invalid_adding
261 firm = Firm.find(1)
262 assert !(firm.clients_of_firm << c = Client.new)
263 assert c.new_record?
264 assert !firm.valid?
265 assert !firm.save
266 assert c.new_record?
267 end
268
269 def test_invalid_adding_before_save
270 no_of_firms = Firm.count
271 no_of_clients = Client.count
272 new_firm = Firm.new("name" => "A New Firm, Inc")
273 new_firm.clients_of_firm.concat([c = Client.new, Client.new("name" => "Apple")])
274 assert c.new_record?
275 assert !c.valid?
276 assert !new_firm.valid?
277 assert !new_firm.save
278 assert c.new_record?
279 assert new_firm.new_record?
280 end
281
282 def test_invalid_adding_with_validate_false
283 firm = Firm.find(:first)
284 client = Client.new
285 firm.unvalidated_clients_of_firm << client
286
287 assert firm.valid?
288 assert !client.valid?
289 assert firm.save
290 assert client.new_record?
291 end
292
293 def test_valid_adding_with_validate_false
294 no_of_clients = Client.count
295
296 firm = Firm.find(:first)
297 client = Client.new("name" => "Apple")
298
299 assert firm.valid?
300 assert client.valid?
301 assert client.new_record?
302
303 firm.unvalidated_clients_of_firm << client
304
305 assert firm.save
306 assert !client.new_record?
307 assert_equal no_of_clients+1, Client.count
308 end
309
310 def test_invalid_build
311 new_client = companies(:first_firm).clients_of_firm.build
312 assert new_client.new_record?
313 assert !new_client.valid?
314 assert_equal new_client, companies(:first_firm).clients_of_firm.last
315 assert !companies(:first_firm).save
316 assert new_client.new_record?
317 assert_equal 1, companies(:first_firm).clients_of_firm(true).size
318 end
319
320 def test_adding_before_save
321 no_of_firms = Firm.count
322 no_of_clients = Client.count
323
324 new_firm = Firm.new("name" => "A New Firm, Inc")
325 c = Client.new("name" => "Apple")
326
327 new_firm.clients_of_firm.push Client.new("name" => "Natural Company")
328 assert_equal 1, new_firm.clients_of_firm.size
329 new_firm.clients_of_firm << c
330 assert_equal 2, new_firm.clients_of_firm.size
331
332 assert_equal no_of_firms, Firm.count # Firm was not saved to database.
333 assert_equal no_of_clients, Client.count # Clients were not saved to database.
334 assert new_firm.save
335 assert !new_firm.new_record?
336 assert !c.new_record?
337 assert_equal new_firm, c.firm
338 assert_equal no_of_firms+1, Firm.count # Firm was saved to database.
339 assert_equal no_of_clients+2, Client.count # Clients were saved to database.
340
341 assert_equal 2, new_firm.clients_of_firm.size
342 assert_equal 2, new_firm.clients_of_firm(true).size
343 end
344
345 def test_assign_ids
346 firm = Firm.new("name" => "Apple")
347 firm.client_ids = [companies(:first_client).id, companies(:second_client).id]
348 firm.save
349 firm.reload
350 assert_equal 2, firm.clients.length
351 assert firm.clients.include?(companies(:second_client))
352 end
353
354 def test_assign_ids_for_through_a_belongs_to
355 post = Post.new(:title => "Assigning IDs works!", :body => "You heared it here first, folks!")
356 post.person_ids = [people(:david).id, people(:michael).id]
357 post.save
358 post.reload
359 assert_equal 2, post.people.length
360 assert post.people.include?(people(:david))
361 end
362
363 def test_build_before_save
364 company = companies(:first_firm)
365 new_client = assert_no_queries { company.clients_of_firm.build("name" => "Another Client") }
366 assert !company.clients_of_firm.loaded?
367
368 company.name += '-changed'
369 assert_queries(2) { assert company.save }
370 assert !new_client.new_record?
371 assert_equal 2, company.clients_of_firm(true).size
372 end
373
374 def test_build_many_before_save
375 company = companies(:first_firm)
376 new_clients = assert_no_queries { company.clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}]) }
377
378 company.name += '-changed'
379 assert_queries(3) { assert company.save }
380 assert_equal 3, company.clients_of_firm(true).size
381 end
382
383 def test_build_via_block_before_save
384 company = companies(:first_firm)
385 new_client = assert_no_queries { company.clients_of_firm.build {|client| client.name = "Another Client" } }
386 assert !company.clients_of_firm.loaded?
387
388 company.name += '-changed'
389 assert_queries(2) { assert company.save }
390 assert !new_client.new_record?
391 assert_equal 2, company.clients_of_firm(true).size
392 end
393
394 def test_build_many_via_block_before_save
395 company = companies(:first_firm)
396 new_clients = assert_no_queries do
397 company.clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}]) do |client|
398 client.name = "changed"
399 end
400 end
401
402 company.name += '-changed'
403 assert_queries(3) { assert company.save }
404 assert_equal 3, company.clients_of_firm(true).size
405 end
406
407 def test_replace_on_new_object
408 firm = Firm.new("name" => "New Firm")
409 firm.clients = [companies(:second_client), Client.new("name" => "New Client")]
410 assert firm.save
411 firm.reload
412 assert_equal 2, firm.clients.length
413 assert firm.clients.include?(Client.find_by_name("New Client"))
414 end
415 end
416
417 class TestDestroyAsPartOfAutosaveAssociation < ActiveRecord::TestCase
418 self.use_transactional_fixtures = false
419
420 def setup
421 @pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
422 @ship = @pirate.create_ship(:name => 'Nights Dirty Lightning')
423 end
424
425 # reload
426 def test_a_marked_for_destruction_record_should_not_be_be_marked_after_reload
427 @pirate.mark_for_destruction
428 @pirate.ship.mark_for_destruction
429
430 assert !@pirate.reload.marked_for_destruction?
431 assert !@pirate.ship.marked_for_destruction?
432 end
433
434 # has_one
435 def test_should_destroy_a_child_association_as_part_of_the_save_transaction_if_it_was_marked_for_destroyal
436 assert !@pirate.ship.marked_for_destruction?
437
438 @pirate.ship.mark_for_destruction
439 id = @pirate.ship.id
440
441 assert @pirate.ship.marked_for_destruction?
442 assert Ship.find_by_id(id)
443
444 @pirate.save
445 assert_nil @pirate.reload.ship
446 assert_nil Ship.find_by_id(id)
447 end
448
449 def test_should_skip_validation_on_a_child_association_if_marked_for_destruction
450 @pirate.ship.name = ''
451 assert !@pirate.valid?
452
453 @pirate.ship.mark_for_destruction
454 assert_difference('Ship.count', -1) { @pirate.save! }
455 end
456
457 def test_should_rollback_destructions_if_an_exception_occurred_while_saving_a_child
458 # Stub the save method of the @pirate.ship instance to destroy and then raise an exception
459 class << @pirate.ship
460 def save(*args)
461 super
462 destroy
463 raise 'Oh noes!'
464 end
465 end
466
467 assert_raise(RuntimeError) { assert !@pirate.save }
468 assert_not_nil @pirate.reload.ship
469 end
470
471 # belongs_to
472 def test_should_destroy_a_parent_association_as_part_of_the_save_transaction_if_it_was_marked_for_destroyal
473 assert !@ship.pirate.marked_for_destruction?
474
475 @ship.pirate.mark_for_destruction
476 id = @ship.pirate.id
477
478 assert @ship.pirate.marked_for_destruction?
479 assert Pirate.find_by_id(id)
480
481 @ship.save
482 assert_nil @ship.reload.pirate
483 assert_nil Pirate.find_by_id(id)
484 end
485
486 def test_should_skip_validation_on_a_parent_association_if_marked_for_destruction
487 @ship.pirate.catchphrase = ''
488 assert !@ship.valid?
489
490 @ship.pirate.mark_for_destruction
491 assert_difference('Pirate.count', -1) { @ship.save! }
492 end
493
494 def test_should_rollback_destructions_if_an_exception_occurred_while_saving_a_parent
495 # Stub the save method of the @ship.pirate instance to destroy and then raise an exception
496 class << @ship.pirate
497 def save(*args)
498 super
499 destroy
500 raise 'Oh noes!'
501 end
502 end
503
504 assert_raise(RuntimeError) { assert !@ship.save }
505 assert_not_nil @ship.reload.pirate
506 end
507
508 # has_many & has_and_belongs_to
509 %w{ parrots birds }.each do |association_name|
510 define_method("test_should_destroy_#{association_name}_as_part_of_the_save_transaction_if_they_were_marked_for_destroyal") do
511 2.times { |i| @pirate.send(association_name).create!(:name => "#{association_name}_#{i}") }
512
513 assert !@pirate.send(association_name).any? { |child| child.marked_for_destruction? }
514
515 @pirate.send(association_name).each { |child| child.mark_for_destruction }
516 klass = @pirate.send(association_name).first.class
517 ids = @pirate.send(association_name).map(&:id)
518
519 assert @pirate.send(association_name).all? { |child| child.marked_for_destruction? }
520 ids.each { |id| assert klass.find_by_id(id) }
521
522 @pirate.save
523 assert @pirate.reload.send(association_name).empty?
524 ids.each { |id| assert_nil klass.find_by_id(id) }
525 end
526
527 define_method("test_should_skip_validation_on_the_#{association_name}_association_if_marked_for_destruction") do
528 2.times { |i| @pirate.send(association_name).create!(:name => "#{association_name}_#{i}") }
529 children = @pirate.send(association_name)
530
531 children.each { |child| child.name = '' }
532 assert !@pirate.valid?
533
534 children.each { |child| child.mark_for_destruction }
535 assert_difference("#{association_name.classify}.count", -2) { @pirate.save! }
536 end
537
538 define_method("test_should_rollback_destructions_if_an_exception_occurred_while_saving_#{association_name}") do
539 2.times { |i| @pirate.send(association_name).create!(:name => "#{association_name}_#{i}") }
540 before = @pirate.send(association_name).map { |c| c }
541
542 # Stub the save method of the first child to destroy and the second to raise an exception
543 class << before.first
544 def save(*args)
545 super
546 destroy
547 end
548 end
549 class << before.last
550 def save(*args)
551 super
552 raise 'Oh noes!'
553 end
554 end
555
556 assert_raise(RuntimeError) { assert !@pirate.save }
557 assert_equal before, @pirate.reload.send(association_name)
558 end
559
560 # Add and remove callbacks tests for association collections.
561 %w{ method proc }.each do |callback_type|
562 define_method("test_should_run_add_callback_#{callback_type}s_for_#{association_name}") do
563 association_name_with_callbacks = "#{association_name}_with_#{callback_type}_callbacks"
564
565 pirate = Pirate.new(:catchphrase => "Arr")
566 pirate.send(association_name_with_callbacks).build(:name => "Crowe the One-Eyed")
567
568 expected = [
569 "before_adding_#{callback_type}_#{association_name.singularize}_<new>",
570 "after_adding_#{callback_type}_#{association_name.singularize}_<new>"
571 ]
572
573 assert_equal expected, pirate.ship_log
574 end
575
576 define_method("test_should_run_remove_callback_#{callback_type}s_for_#{association_name}") do
577 association_name_with_callbacks = "#{association_name}_with_#{callback_type}_callbacks"
578
579 @pirate.send(association_name_with_callbacks).create!(:name => "Crowe the One-Eyed")
580 @pirate.send(association_name_with_callbacks).each { |c| c.mark_for_destruction }
581 child_id = @pirate.send(association_name_with_callbacks).first.id
582
583 @pirate.ship_log.clear
584 @pirate.save
585
586 expected = [
587 "before_removing_#{callback_type}_#{association_name.singularize}_#{child_id}",
588 "after_removing_#{callback_type}_#{association_name.singularize}_#{child_id}"
589 ]
590
591 assert_equal expected, @pirate.ship_log
592 end
593 end
594 end
595 end
596
597 class TestAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCase
598 self.use_transactional_fixtures = false
599
600 def setup
601 @pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
602 @ship = @pirate.create_ship(:name => 'Nights Dirty Lightning')
603 end
604
605 def test_should_still_work_without_an_associated_model
606 @ship.destroy
607 @pirate.reload.catchphrase = "Arr"
608 @pirate.save
609 assert 'Arr', @pirate.reload.catchphrase
610 end
611
612 def test_should_automatically_save_the_associated_model
613 @pirate.ship.name = 'The Vile Insanity'
614 @pirate.save
615 assert_equal 'The Vile Insanity', @pirate.reload.ship.name
616 end
617
618 def test_should_automatically_save_bang_the_associated_model
619 @pirate.ship.name = 'The Vile Insanity'
620 @pirate.save!
621 assert_equal 'The Vile Insanity', @pirate.reload.ship.name
622 end
623
624 def test_should_automatically_validate_the_associated_model
625 @pirate.ship.name = ''
626 assert !@pirate.valid?
627 assert !@pirate.errors.on(:ship_name).blank?
628 end
629
630 def test_should_merge_errors_on_the_associated_models_onto_the_parent_even_if_it_is_not_valid
631 @pirate.ship.name = nil
632 @pirate.catchphrase = nil
633 assert !@pirate.valid?
634 assert !@pirate.errors.on(:ship_name).blank?
635 assert !@pirate.errors.on(:catchphrase).blank?
636 end
637
638 def test_should_still_allow_to_bypass_validations_on_the_associated_model
639 @pirate.catchphrase = ''
640 @pirate.ship.name = ''
641 @pirate.save(false)
642 assert_equal ['', ''], [@pirate.reload.catchphrase, @pirate.ship.name]
643 end
644
645 def test_should_allow_to_bypass_validations_on_associated_models_at_any_depth
646 2.times { |i| @pirate.ship.parts.create!(:name => "part #{i}") }
647
648 @pirate.catchphrase = ''
649 @pirate.ship.name = ''
650 @pirate.ship.parts.each { |part| part.name = '' }
651 @pirate.save(false)
652
653 values = [@pirate.reload.catchphrase, @pirate.ship.name, *@pirate.ship.parts.map(&:name)]
654 assert_equal ['', '', '', ''], values
655 end
656
657 def test_should_still_raise_an_ActiveRecordRecord_Invalid_exception_if_we_want_that
658 @pirate.ship.name = ''
659 assert_raise(ActiveRecord::RecordInvalid) do
660 @pirate.save!
661 end
662 end
663
664 def test_should_rollback_any_changes_if_an_exception_occurred_while_saving
665 before = [@pirate.catchphrase, @pirate.ship.name]
666
667 @pirate.catchphrase = 'Arr'
668 @pirate.ship.name = 'The Vile Insanity'
669
670 # Stub the save method of the @pirate.ship instance to raise an exception
671 class << @pirate.ship
672 def save(*args)
673 super
674 raise 'Oh noes!'
675 end
676 end
677
678 assert_raise(RuntimeError) { assert !@pirate.save }
679 assert_equal before, [@pirate.reload.catchphrase, @pirate.ship.name]
680 end
681
682 def test_should_not_load_the_associated_model
683 assert_queries(1) { @pirate.catchphrase = 'Arr'; @pirate.save! }
684 end
685 end
686
687 class TestAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase
688 self.use_transactional_fixtures = false
689
690 def setup
691 @ship = Ship.create(:name => 'Nights Dirty Lightning')
692 @pirate = @ship.create_pirate(:catchphrase => "Don' botharrr talkin' like one, savvy?")
693 end
694
695 def test_should_still_work_without_an_associated_model
696 @pirate.destroy
697 @ship.reload.name = "The Vile Insanity"
698 @ship.save
699 assert 'The Vile Insanity', @ship.reload.name
700 end
701
702 def test_should_automatically_save_the_associated_model
703 @ship.pirate.catchphrase = 'Arr'
704 @ship.save
705 assert_equal 'Arr', @ship.reload.pirate.catchphrase
706 end
707
708 def test_should_automatically_save_bang_the_associated_model
709 @ship.pirate.catchphrase = 'Arr'
710 @ship.save!
711 assert_equal 'Arr', @ship.reload.pirate.catchphrase
712 end
713
714 def test_should_automatically_validate_the_associated_model
715 @ship.pirate.catchphrase = ''
716 assert !@ship.valid?
717 assert !@ship.errors.on(:pirate_catchphrase).blank?
718 end
719
720 def test_should_merge_errors_on_the_associated_model_onto_the_parent_even_if_it_is_not_valid
721 @ship.name = nil
722 @ship.pirate.catchphrase = nil
723 assert !@ship.valid?
724 assert !@ship.errors.on(:name).blank?
725 assert !@ship.errors.on(:pirate_catchphrase).blank?
726 end
727
728 def test_should_still_allow_to_bypass_validations_on_the_associated_model
729 @ship.pirate.catchphrase = ''
730 @ship.name = ''
731 @ship.save(false)
732 assert_equal ['', ''], [@ship.reload.name, @ship.pirate.catchphrase]
733 end
734
735 def test_should_still_raise_an_ActiveRecordRecord_Invalid_exception_if_we_want_that
736 @ship.pirate.catchphrase = ''
737 assert_raise(ActiveRecord::RecordInvalid) do
738 @ship.save!
739 end
740 end
741
742 def test_should_rollback_any_changes_if_an_exception_occurred_while_saving
743 before = [@ship.pirate.catchphrase, @ship.name]
744
745 @ship.pirate.catchphrase = 'Arr'
746 @ship.name = 'The Vile Insanity'
747
748 # Stub the save method of the @ship.pirate instance to raise an exception
749 class << @ship.pirate
750 def save(*args)
751 super
752 raise 'Oh noes!'
753 end
754 end
755
756 assert_raise(RuntimeError) { assert !@ship.save }
757 # TODO: Why does using reload on @ship looses the associated pirate?
758 assert_equal before, [@ship.pirate.reload.catchphrase, @ship.reload.name]
759 end
760
761 def test_should_not_load_the_associated_model
762 assert_queries(1) { @ship.name = 'The Vile Insanity'; @ship.save! }
763 end
764 end
765
766 module AutosaveAssociationOnACollectionAssociationTests
767 def test_should_automatically_save_the_associated_models
768 new_names = ['Grace OMalley', 'Privateers Greed']
769 @pirate.send(@association_name).each_with_index { |child, i| child.name = new_names[i] }
770
771 @pirate.save
772 assert_equal new_names, @pirate.reload.send(@association_name).map(&:name)
773 end
774
775 def test_should_automatically_save_bang_the_associated_models
776 new_names = ['Grace OMalley', 'Privateers Greed']
777 @pirate.send(@association_name).each_with_index { |child, i| child.name = new_names[i] }
778
779 @pirate.save!
780 assert_equal new_names, @pirate.reload.send(@association_name).map(&:name)
781 end
782
783 def test_should_automatically_validate_the_associated_models
784 @pirate.send(@association_name).each { |child| child.name = '' }
785
786 assert !@pirate.valid?
787 assert_equal "can't be blank", @pirate.errors.on("#{@association_name}_name")
788 assert @pirate.errors.on(@association_name).blank?
789 end
790
791 def test_should_not_use_default_invalid_error_on_associated_models
792 @pirate.send(@association_name).build(:name => '')
793
794 assert !@pirate.valid?
795 assert_equal "can't be blank", @pirate.errors.on("#{@association_name}_name")
796 assert @pirate.errors.on(@association_name).blank?
797 end
798
799 def test_should_merge_errors_on_the_associated_models_onto_the_parent_even_if_it_is_not_valid
800 @pirate.send(@association_name).each { |child| child.name = '' }
801 @pirate.catchphrase = nil
802
803 assert !@pirate.valid?
804 assert_equal "can't be blank", @pirate.errors.on("#{@association_name}_name")
805 assert !@pirate.errors.on(:catchphrase).blank?
806 end
807
808 def test_should_allow_to_bypass_validations_on_the_associated_models_on_update
809 @pirate.catchphrase = ''
810 @pirate.send(@association_name).each { |child| child.name = '' }
811
812 assert @pirate.save(false)
813 assert_equal ['', '', ''], [
814 @pirate.reload.catchphrase,
815 @pirate.send(@association_name).first.name,
816 @pirate.send(@association_name).last.name
817 ]
818 end
819
820 def test_should_validation_the_associated_models_on_create
821 assert_no_difference("#{ @association_name == :birds ? 'Bird' : 'Parrot' }.count") do
822 2.times { @pirate.send(@association_name).build }
823 @pirate.save(true)
824 end
825 end
826
827 def test_should_allow_to_bypass_validations_on_the_associated_models_on_create
828 assert_difference("#{ @association_name == :birds ? 'Bird' : 'Parrot' }.count", +2) do
829 2.times { @pirate.send(@association_name).build }
830 @pirate.save(false)
831 end
832 end
833
834 def test_should_rollback_any_changes_if_an_exception_occurred_while_saving
835 before = [@pirate.catchphrase, *@pirate.send(@association_name).map(&:name)]
836 new_names = ['Grace OMalley', 'Privateers Greed']
837
838 @pirate.catchphrase = 'Arr'
839 @pirate.send(@association_name).each_with_index { |child, i| child.name = new_names[i] }
840
841 # Stub the save method of the first child instance to raise an exception
842 class << @pirate.send(@association_name).first
843 def save(*args)
844 super
845 raise 'Oh noes!'
846 end
847 end
848
849 assert_raise(RuntimeError) { assert !@pirate.save }
850 assert_equal before, [@pirate.reload.catchphrase, *@pirate.send(@association_name).map(&:name)]
851 end
852
853 def test_should_still_raise_an_ActiveRecordRecord_Invalid_exception_if_we_want_that
854 @pirate.send(@association_name).each { |child| child.name = '' }
855 assert_raise(ActiveRecord::RecordInvalid) do
856 @pirate.save!
857 end
858 end
859
860 def test_should_not_load_the_associated_models_if_they_were_not_loaded_yet
861 assert_queries(1) { @pirate.catchphrase = 'Arr'; @pirate.save! }
862
863 @pirate.send(@association_name).class # hack to load the target
864
865 assert_queries(3) do
866 @pirate.catchphrase = 'Yarr'
867 new_names = ['Grace OMalley', 'Privateers Greed']
868 @pirate.send(@association_name).each_with_index { |child, i| child.name = new_names[i] }
869 @pirate.save!
870 end
871 end
872 end
873
874 class TestAutosaveAssociationOnAHasManyAssociation < ActiveRecord::TestCase
875 self.use_transactional_fixtures = false
876
877 def setup
878 @association_name = :birds
879
880 @pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
881 @child_1 = @pirate.birds.create(:name => 'Posideons Killer')
882 @child_2 = @pirate.birds.create(:name => 'Killer bandita Dionne')
883 end
884
885 include AutosaveAssociationOnACollectionAssociationTests
886 end
887
888 class TestAutosaveAssociationOnAHasAndBelongsToManyAssociation < ActiveRecord::TestCase
889 self.use_transactional_fixtures = false
890
891 def setup
892 @association_name = :parrots
893 @habtm = true
894
895 @pirate = Pirate.create(:catchphrase => "Don' botharrr talkin' like one, savvy?")
896 @child_1 = @pirate.parrots.create(:name => 'Posideons Killer')
897 @child_2 = @pirate.parrots.create(:name => 'Killer bandita Dionne')
898 end
899
900 include AutosaveAssociationOnACollectionAssociationTests
901 end