2
require 'models/developer'
3
require 'models/project'
4
require 'models/company'
7
require 'models/category'
9
require 'models/author'
10
require 'models/comment'
11
require 'models/person'
12
require 'models/reader'
13
require 'models/tagging'
15
class HasManyAssociationsTest < ActiveRecord::TestCase
16
fixtures :accounts, :categories, :companies, :developers, :projects,
17
:developers_projects, :topics, :authors, :comments, :author_addresses,
18
:people, :posts, :readers, :taggings
21
Client.destroyed_client_ids.clear
24
def force_signal37_to_load_all_clients_of_firm
25
companies(:first_firm).clients_of_firm.each {|f| }
28
def test_counting_with_counter_sql
29
assert_equal 2, Firm.find(:first).clients.count
33
assert_equal 2, Firm.find(:first).plain_clients.count
36
def test_counting_with_empty_hash_conditions
37
assert_equal 2, Firm.find(:first).plain_clients.count(:conditions => {})
40
def test_counting_with_single_conditions
41
assert_equal 1, Firm.find(:first).plain_clients.count(:conditions => ['name=?', "Microsoft"])
44
def test_counting_with_single_hash
45
assert_equal 1, Firm.find(:first).plain_clients.count(:conditions => {:name => "Microsoft"})
48
def test_counting_with_column_name_and_hash
49
assert_equal 2, Firm.find(:first).plain_clients.count(:name)
52
def test_counting_with_association_limit
53
firm = companies(:first_firm)
54
assert_equal firm.limited_clients.length, firm.limited_clients.size
55
assert_equal firm.limited_clients.length, firm.limited_clients.count
59
assert_equal 2, Firm.find(:first).clients.length
62
def test_find_with_blank_conditions
63
[[], {}, nil, ""].each do |blank|
64
assert_equal 2, Firm.find(:first).clients.find(:all, :conditions => blank).size
68
def test_find_many_with_merged_options
69
assert_equal 1, companies(:first_firm).limited_clients.size
70
assert_equal 1, companies(:first_firm).limited_clients.find(:all).size
71
assert_equal 2, companies(:first_firm).limited_clients.find(:all, :limit => nil).size
74
def test_dynamic_find_last_without_specified_order
75
assert_equal companies(:second_client), companies(:first_firm).unsorted_clients.find_last_by_type('Client')
78
def test_dynamic_find_should_respect_association_order
79
assert_equal companies(:second_client), companies(:first_firm).clients_sorted_desc.find(:first, :conditions => "type = 'Client'")
80
assert_equal companies(:second_client), companies(:first_firm).clients_sorted_desc.find_by_type('Client')
83
def test_dynamic_find_order_should_override_association_order
84
assert_equal companies(:first_client), companies(:first_firm).clients_sorted_desc.find(:first, :conditions => "type = 'Client'", :order => 'id')
85
assert_equal companies(:first_client), companies(:first_firm).clients_sorted_desc.find_by_type('Client', :order => 'id')
88
def test_dynamic_find_all_should_respect_association_order
89
assert_equal [companies(:second_client), companies(:first_client)], companies(:first_firm).clients_sorted_desc.find(:all, :conditions => "type = 'Client'")
90
assert_equal [companies(:second_client), companies(:first_client)], companies(:first_firm).clients_sorted_desc.find_all_by_type('Client')
93
def test_dynamic_find_all_order_should_override_association_order
94
assert_equal [companies(:first_client), companies(:second_client)], companies(:first_firm).clients_sorted_desc.find(:all, :conditions => "type = 'Client'", :order => 'id')
95
assert_equal [companies(:first_client), companies(:second_client)], companies(:first_firm).clients_sorted_desc.find_all_by_type('Client', :order => 'id')
98
def test_dynamic_find_all_should_respect_association_limit
99
assert_equal 1, companies(:first_firm).limited_clients.find(:all, :conditions => "type = 'Client'").length
100
assert_equal 1, companies(:first_firm).limited_clients.find_all_by_type('Client').length
103
def test_dynamic_find_all_limit_should_override_association_limit
104
assert_equal 2, companies(:first_firm).limited_clients.find(:all, :conditions => "type = 'Client'", :limit => 9_000).length
105
assert_equal 2, companies(:first_firm).limited_clients.find_all_by_type('Client', :limit => 9_000).length
108
def test_dynamic_find_all_should_respect_readonly_access
109
companies(:first_firm).readonly_clients.find(:all).each { |c| assert_raise(ActiveRecord::ReadOnlyRecord) { c.save! } }
110
companies(:first_firm).readonly_clients.find(:all).each { |c| assert c.readonly? }
113
def test_cant_save_has_many_readonly_association
114
authors(:david).readonly_comments.each { |c| assert_raise(ActiveRecord::ReadOnlyRecord) { c.save! } }
115
authors(:david).readonly_comments.each { |c| assert c.readonly? }
118
def test_triple_equality
119
assert !(Array === Firm.find(:first).clients)
120
assert Firm.find(:first).clients === Array
123
def test_finding_default_orders
124
assert_equal "Summit", Firm.find(:first).clients.first.name
127
def test_finding_with_different_class_name_and_order
128
assert_equal "Microsoft", Firm.find(:first).clients_sorted_desc.first.name
131
def test_finding_with_foreign_key
132
assert_equal "Microsoft", Firm.find(:first).clients_of_firm.first.name
135
def test_finding_with_condition
136
assert_equal "Microsoft", Firm.find(:first).clients_like_ms.first.name
139
def test_finding_with_condition_hash
140
assert_equal "Microsoft", Firm.find(:first).clients_like_ms_with_hash_conditions.first.name
143
def test_finding_using_primary_key
144
assert_equal "Summit", Firm.find(:first).clients_using_primary_key.first.name
147
def test_finding_using_sql
148
firm = Firm.find(:first)
149
first_client = firm.clients_using_sql.first
150
assert_not_nil first_client
151
assert_equal "Microsoft", first_client.name
152
assert_equal 1, firm.clients_using_sql.size
153
assert_equal 1, Firm.find(:first).clients_using_sql.size
156
def test_counting_using_sql
157
assert_equal 1, Firm.find(:first).clients_using_counter_sql.size
158
assert Firm.find(:first).clients_using_counter_sql.any?
159
assert_equal 0, Firm.find(:first).clients_using_zero_counter_sql.size
160
assert !Firm.find(:first).clients_using_zero_counter_sql.any?
163
def test_counting_non_existant_items_using_sql
164
assert_equal 0, Firm.find(:first).no_clients_using_counter_sql.size
167
def test_belongs_to_sanity
172
assert false, "belongs_to failed if check"
177
assert false, "belongs_to failed unless check"
182
firm = Firm.find(:first)
184
assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find }
186
client = firm.clients.find(2)
187
assert_kind_of Client, client
189
client_ary = firm.clients.find([2])
190
assert_kind_of Array, client_ary
191
assert_equal client, client_ary.first
193
client_ary = firm.clients.find(2, 3)
194
assert_kind_of Array, client_ary
195
assert_equal 2, client_ary.size
196
assert_equal client, client_ary.first
198
assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) }
201
def test_find_string_ids_when_using_finder_sql
202
firm = Firm.find(:first)
204
client = firm.clients_using_finder_sql.find("2")
205
assert_kind_of Client, client
207
client_ary = firm.clients_using_finder_sql.find(["2"])
208
assert_kind_of Array, client_ary
209
assert_equal client, client_ary.first
211
client_ary = firm.clients_using_finder_sql.find("2", "3")
212
assert_kind_of Array, client_ary
213
assert_equal 2, client_ary.size
214
assert client_ary.include?(client)
218
firm = Firm.find(:first)
219
assert_equal 2, firm.clients.find(:all, :conditions => "#{QUOTED_TYPE} = 'Client'").length
220
assert_equal 1, firm.clients.find(:all, :conditions => "name = 'Summit'").length
224
firm = companies(:first_firm)
226
assert ! firm.clients.loaded?
229
firm.clients.find_each(:batch_size => 1) {|c| assert_equal firm.id, c.firm_id }
232
assert ! firm.clients.loaded?
235
def test_find_each_with_conditions
236
firm = companies(:first_firm)
239
firm.clients.find_each(:batch_size => 1, :conditions => {:name => "Microsoft"}) do |c|
240
assert_equal firm.id, c.firm_id
241
assert_equal "Microsoft", c.name
245
assert ! firm.clients.loaded?
248
def test_find_in_batches
249
firm = companies(:first_firm)
251
assert ! firm.clients.loaded?
254
firm.clients.find_in_batches(:batch_size => 2) do |clients|
255
clients.each {|c| assert_equal firm.id, c.firm_id }
259
assert ! firm.clients.loaded?
262
def test_find_all_sanitized
263
firm = Firm.find(:first)
264
summit = firm.clients.find(:all, :conditions => "name = 'Summit'")
265
assert_equal summit, firm.clients.find(:all, :conditions => ["name = ?", "Summit"])
266
assert_equal summit, firm.clients.find(:all, :conditions => ["name = :name", { :name => "Summit" }])
270
firm = Firm.find(:first)
271
client2 = Client.find(2)
272
assert_equal firm.clients.first, firm.clients.find(:first)
273
assert_equal client2, firm.clients.find(:first, :conditions => "#{QUOTED_TYPE} = 'Client'")
276
def test_find_first_sanitized
277
firm = Firm.find(:first)
278
client2 = Client.find(2)
279
assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = ?", 'Client'])
280
assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = :type", { :type => 'Client' }])
283
def test_find_all_with_include_and_conditions
284
assert_nothing_raised do
285
Developer.find(:all, :joins => :audit_logs, :conditions => {'audit_logs.message' => nil, :name => 'Smith'})
289
def test_find_in_collection
290
assert_equal Client.find(2).name, companies(:first_firm).clients.find(2).name
291
assert_raise(ActiveRecord::RecordNotFound) { companies(:first_firm).clients.find(6) }
294
def test_find_grouped
295
all_clients_of_firm1 = Client.find(:all, :conditions => "firm_id = 1")
296
grouped_clients_of_firm1 = Client.find(:all, :conditions => "firm_id = 1", :group => "firm_id", :select => 'firm_id, count(id) as clients_count')
297
assert_equal 2, all_clients_of_firm1.size
298
assert_equal 1, grouped_clients_of_firm1.size
301
def test_find_scoped_grouped
302
assert_equal 1, companies(:first_firm).clients_grouped_by_firm_id.size
303
assert_equal 1, companies(:first_firm).clients_grouped_by_firm_id.length
304
assert_equal 2, companies(:first_firm).clients_grouped_by_name.size
305
assert_equal 2, companies(:first_firm).clients_grouped_by_name.length
308
def test_find_scoped_grouped_having
309
assert_equal 1, authors(:david).popular_grouped_posts.length
310
assert_equal 0, authors(:mary).popular_grouped_posts.length
314
force_signal37_to_load_all_clients_of_firm
315
natural = Client.new("name" => "Natural Company")
316
companies(:first_firm).clients_of_firm << natural
317
assert_equal 2, companies(:first_firm).clients_of_firm.size # checking via the collection
318
assert_equal 2, companies(:first_firm).clients_of_firm(true).size # checking using the db
319
assert_equal natural, companies(:first_firm).clients_of_firm.last
322
def test_adding_using_create
323
first_firm = companies(:first_firm)
324
assert_equal 2, first_firm.plain_clients.size
325
natural = first_firm.plain_clients.create(:name => "Natural Company")
326
assert_equal 3, first_firm.plain_clients.length
327
assert_equal 3, first_firm.plain_clients.size
330
def test_create_with_bang_on_has_many_when_parent_is_new_raises
331
assert_raise(ActiveRecord::RecordNotSaved) do
333
firm.plain_clients.create! :name=>"Whoever"
337
def test_regular_create_on_has_many_when_parent_is_new_raises
338
assert_raise(ActiveRecord::RecordNotSaved) do
340
firm.plain_clients.create :name=>"Whoever"
344
def test_create_with_bang_on_has_many_raises_when_record_not_saved
345
assert_raise(ActiveRecord::RecordInvalid) do
346
firm = Firm.find(:first)
347
firm.plain_clients.create!
351
def test_create_with_bang_on_habtm_when_parent_is_new_raises
352
assert_raise(ActiveRecord::RecordNotSaved) do
353
Developer.new("name" => "Aredridel").projects.create!
357
def test_adding_a_mismatch_class
358
assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << nil }
359
assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << 1 }
360
assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << Topic.find(1) }
363
def test_adding_a_collection
364
force_signal37_to_load_all_clients_of_firm
365
companies(:first_firm).clients_of_firm.concat([Client.new("name" => "Natural Company"), Client.new("name" => "Apple")])
366
assert_equal 3, companies(:first_firm).clients_of_firm.size
367
assert_equal 3, companies(:first_firm).clients_of_firm(true).size
371
company = companies(:first_firm)
372
new_client = assert_no_queries { company.clients_of_firm.build("name" => "Another Client") }
373
assert !company.clients_of_firm.loaded?
375
assert_equal "Another Client", new_client.name
376
assert new_client.new_record?
377
assert_equal new_client, company.clients_of_firm.last
380
def test_collection_size_after_building
381
company = companies(:first_firm) # company already has one client
382
company.clients_of_firm.build("name" => "Another Client")
383
company.clients_of_firm.build("name" => "Yet Another Client")
384
assert_equal 3, company.clients_of_firm.size
387
def test_collection_size_twice_for_regressions
388
post = posts(:thinking)
389
assert_equal 0, post.readers.size
390
# This test needs a post that has no readers, we assert it to ensure it holds,
391
# but need to reload the post because the very call to #size hides the bug.
394
size1 = post.readers.size
395
size2 = post.readers.size
396
assert_equal size1, size2
400
company = companies(:first_firm)
401
new_clients = assert_no_queries { company.clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}]) }
402
assert_equal 2, new_clients.size
405
def test_build_followed_by_save_does_not_load_target
406
new_client = companies(:first_firm).clients_of_firm.build("name" => "Another Client")
407
assert companies(:first_firm).save
408
assert !companies(:first_firm).clients_of_firm.loaded?
411
def test_build_without_loading_association
412
first_topic = topics(:first)
415
assert_equal 1, first_topic.replies.length
418
first_topic.replies.build(:title => "Not saved", :content => "Superstars")
419
assert_equal 2, first_topic.replies.size
422
assert_equal 2, first_topic.replies.to_ary.size
425
def test_build_via_block
426
company = companies(:first_firm)
427
new_client = assert_no_queries { company.clients_of_firm.build {|client| client.name = "Another Client" } }
428
assert !company.clients_of_firm.loaded?
430
assert_equal "Another Client", new_client.name
431
assert new_client.new_record?
432
assert_equal new_client, company.clients_of_firm.last
435
def test_build_many_via_block
436
company = companies(:first_firm)
437
new_clients = assert_no_queries do
438
company.clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}]) do |client|
439
client.name = "changed"
443
assert_equal 2, new_clients.size
444
assert_equal "changed", new_clients.first.name
445
assert_equal "changed", new_clients.last.name
448
def test_create_without_loading_association
449
first_firm = companies(:first_firm)
453
assert_equal 1, first_firm.clients_of_firm.size
454
first_firm.clients_of_firm.reset
457
first_firm.clients_of_firm.create(:name => "Superstars")
460
assert_equal 2, first_firm.clients_of_firm.size
464
force_signal37_to_load_all_clients_of_firm
465
new_client = companies(:first_firm).clients_of_firm.create("name" => "Another Client")
466
assert !new_client.new_record?
467
assert_equal new_client, companies(:first_firm).clients_of_firm.last
468
assert_equal new_client, companies(:first_firm).clients_of_firm(true).last
472
companies(:first_firm).clients_of_firm.create([{"name" => "Another Client"}, {"name" => "Another Client II"}])
473
assert_equal 3, companies(:first_firm).clients_of_firm(true).size
476
def test_create_followed_by_save_does_not_load_target
477
new_client = companies(:first_firm).clients_of_firm.create("name" => "Another Client")
478
assert companies(:first_firm).save
479
assert !companies(:first_firm).clients_of_firm.loaded?
482
def test_find_or_initialize
483
the_client = companies(:first_firm).clients.find_or_initialize_by_name("Yet another client")
484
assert_equal companies(:first_firm).id, the_client.firm_id
485
assert_equal "Yet another client", the_client.name
486
assert the_client.new_record?
489
def test_find_or_create
490
number_of_clients = companies(:first_firm).clients.size
491
the_client = companies(:first_firm).clients.find_or_create_by_name("Yet another client")
492
assert_equal number_of_clients + 1, companies(:first_firm, :reload).clients.size
493
assert_equal the_client, companies(:first_firm).clients.find_or_create_by_name("Yet another client")
494
assert_equal number_of_clients + 1, companies(:first_firm, :reload).clients.size
498
force_signal37_to_load_all_clients_of_firm
499
companies(:first_firm).clients_of_firm.delete(companies(:first_firm).clients_of_firm.first)
500
assert_equal 0, companies(:first_firm).clients_of_firm.size
501
assert_equal 0, companies(:first_firm).clients_of_firm(true).size
504
def test_deleting_before_save
505
new_firm = Firm.new("name" => "A New Firm, Inc.")
506
new_client = new_firm.clients_of_firm.build("name" => "Another Client")
507
assert_equal 1, new_firm.clients_of_firm.size
508
new_firm.clients_of_firm.delete(new_client)
509
assert_equal 0, new_firm.clients_of_firm.size
512
def test_deleting_updates_counter_cache
514
assert_equal topic.replies.to_a.size, topic.replies_count
516
topic.replies.delete(topic.replies.first)
518
assert_equal topic.replies.to_a.size, topic.replies_count
521
def test_deleting_updates_counter_cache_without_dependent_destroy
522
post = posts(:welcome)
524
assert_difference "post.reload.taggings_count", -1 do
525
post.taggings.delete(post.taggings.first)
529
def test_deleting_a_collection
530
force_signal37_to_load_all_clients_of_firm
531
companies(:first_firm).clients_of_firm.create("name" => "Another Client")
532
assert_equal 2, companies(:first_firm).clients_of_firm.size
533
companies(:first_firm).clients_of_firm.delete([companies(:first_firm).clients_of_firm[0], companies(:first_firm).clients_of_firm[1]])
534
assert_equal 0, companies(:first_firm).clients_of_firm.size
535
assert_equal 0, companies(:first_firm).clients_of_firm(true).size
539
force_signal37_to_load_all_clients_of_firm
540
companies(:first_firm).clients_of_firm.create("name" => "Another Client")
541
assert_equal 2, companies(:first_firm).clients_of_firm.size
542
companies(:first_firm).clients_of_firm.delete_all
543
assert_equal 0, companies(:first_firm).clients_of_firm.size
544
assert_equal 0, companies(:first_firm).clients_of_firm(true).size
547
def test_delete_all_with_not_yet_loaded_association_collection
548
force_signal37_to_load_all_clients_of_firm
549
companies(:first_firm).clients_of_firm.create("name" => "Another Client")
550
assert_equal 2, companies(:first_firm).clients_of_firm.size
551
companies(:first_firm).clients_of_firm.reset
552
companies(:first_firm).clients_of_firm.delete_all
553
assert_equal 0, companies(:first_firm).clients_of_firm.size
554
assert_equal 0, companies(:first_firm).clients_of_firm(true).size
557
def test_clearing_an_association_collection
558
firm = companies(:first_firm)
559
client_id = firm.clients_of_firm.first.id
560
assert_equal 1, firm.clients_of_firm.size
562
firm.clients_of_firm.clear
564
assert_equal 0, firm.clients_of_firm.size
565
assert_equal 0, firm.clients_of_firm(true).size
566
assert_equal [], Client.destroyed_client_ids[firm.id]
568
# Should not be destroyed since the association is not dependent.
569
assert_nothing_raised do
570
assert Client.find(client_id).firm.nil?
574
def test_clearing_updates_counter_cache
579
assert_equal 0, topic.replies_count
582
def test_clearing_a_dependent_association_collection
583
firm = companies(:first_firm)
584
client_id = firm.dependent_clients_of_firm.first.id
585
assert_equal 1, firm.dependent_clients_of_firm.size
587
# :dependent means destroy is called on each client
588
firm.dependent_clients_of_firm.clear
590
assert_equal 0, firm.dependent_clients_of_firm.size
591
assert_equal 0, firm.dependent_clients_of_firm(true).size
592
assert_equal [client_id], Client.destroyed_client_ids[firm.id]
594
# Should be destroyed since the association is dependent.
595
assert Client.find_by_id(client_id).nil?
598
def test_clearing_an_exclusively_dependent_association_collection
599
firm = companies(:first_firm)
600
client_id = firm.exclusively_dependent_clients_of_firm.first.id
601
assert_equal 1, firm.exclusively_dependent_clients_of_firm.size
603
assert_equal [], Client.destroyed_client_ids[firm.id]
605
# :exclusively_dependent means each client is deleted directly from
606
# the database without looping through them calling destroy.
607
firm.exclusively_dependent_clients_of_firm.clear
609
assert_equal 0, firm.exclusively_dependent_clients_of_firm.size
610
assert_equal 0, firm.exclusively_dependent_clients_of_firm(true).size
611
# no destroy-filters should have been called
612
assert_equal [], Client.destroyed_client_ids[firm.id]
614
# Should be destroyed since the association is exclusively dependent.
615
assert Client.find_by_id(client_id).nil?
618
def test_dependent_association_respects_optional_conditions_on_delete
619
firm = companies(:odegy)
620
Client.create(:client_of => firm.id, :name => "BigShot Inc.")
621
Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
622
# only one of two clients is included in the association due to the :conditions key
623
assert_equal 2, Client.find_all_by_client_of(firm.id).size
624
assert_equal 1, firm.dependent_conditional_clients_of_firm.size
626
# only the correctly associated client should have been deleted
627
assert_equal 1, Client.find_all_by_client_of(firm.id).size
630
def test_dependent_association_respects_optional_sanitized_conditions_on_delete
631
firm = companies(:odegy)
632
Client.create(:client_of => firm.id, :name => "BigShot Inc.")
633
Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
634
# only one of two clients is included in the association due to the :conditions key
635
assert_equal 2, Client.find_all_by_client_of(firm.id).size
636
assert_equal 1, firm.dependent_sanitized_conditional_clients_of_firm.size
638
# only the correctly associated client should have been deleted
639
assert_equal 1, Client.find_all_by_client_of(firm.id).size
642
def test_dependent_association_respects_optional_hash_conditions_on_delete
643
firm = companies(:odegy)
644
Client.create(:client_of => firm.id, :name => "BigShot Inc.")
645
Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
646
# only one of two clients is included in the association due to the :conditions key
647
assert_equal 2, Client.find_all_by_client_of(firm.id).size
648
assert_equal 1, firm.dependent_sanitized_conditional_clients_of_firm.size
650
# only the correctly associated client should have been deleted
651
assert_equal 1, Client.find_all_by_client_of(firm.id).size
654
def test_delete_all_association_with_primary_key_deletes_correct_records
655
firm = Firm.find(:first)
656
# break the vanilla firm_id foreign key
657
assert_equal 2, firm.clients.count
658
firm.clients.first.update_attribute(:firm_id, nil)
659
assert_equal 1, firm.clients(true).count
660
assert_equal 1, firm.clients_using_primary_key_with_delete_all.count
661
old_record = firm.clients_using_primary_key_with_delete_all.first
662
firm = Firm.find(:first)
664
assert Client.find_by_id(old_record.id).nil?
667
def test_creation_respects_hash_condition
668
ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.build
670
assert ms_client.save
671
assert_equal 'Microsoft', ms_client.name
673
another_ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.create
675
assert !another_ms_client.new_record?
676
assert_equal 'Microsoft', another_ms_client.name
679
def test_dependent_delete_and_destroy_with_belongs_to
680
author_address = author_addresses(:david_address)
681
assert_equal [], AuthorAddress.destroyed_author_address_ids[authors(:david).id]
683
assert_difference "AuthorAddress.count", -2 do
684
authors(:david).destroy
687
assert_equal nil, AuthorAddress.find_by_id(authors(:david).author_address_id)
688
assert_equal nil, AuthorAddress.find_by_id(authors(:david).author_address_extra_id)
691
def test_invalid_belongs_to_dependent_option_raises_exception
692
assert_raise ArgumentError do
693
Author.belongs_to :special_author_address, :dependent => :nullify
697
def test_clearing_without_initial_access
698
firm = companies(:first_firm)
700
firm.clients_of_firm.clear
702
assert_equal 0, firm.clients_of_firm.size
703
assert_equal 0, firm.clients_of_firm(true).size
706
def test_deleting_a_item_which_is_not_in_the_collection
707
force_signal37_to_load_all_clients_of_firm
708
summit = Client.find_by_name('Summit')
709
companies(:first_firm).clients_of_firm.delete(summit)
710
assert_equal 1, companies(:first_firm).clients_of_firm.size
711
assert_equal 1, companies(:first_firm).clients_of_firm(true).size
712
assert_equal 2, summit.client_of
715
def test_deleting_type_mismatch
716
david = Developer.find(1)
717
david.projects.reload
718
assert_raise(ActiveRecord::AssociationTypeMismatch) { david.projects.delete(1) }
721
def test_deleting_self_type_mismatch
722
david = Developer.find(1)
723
david.projects.reload
724
assert_raise(ActiveRecord::AssociationTypeMismatch) { david.projects.delete(Project.find(1).developers) }
728
force_signal37_to_load_all_clients_of_firm
730
assert_difference "Client.count", -1 do
731
companies(:first_firm).clients_of_firm.destroy(companies(:first_firm).clients_of_firm.first)
734
assert_equal 0, companies(:first_firm).reload.clients_of_firm.size
735
assert_equal 0, companies(:first_firm).clients_of_firm(true).size
738
def test_destroying_by_fixnum_id
739
force_signal37_to_load_all_clients_of_firm
741
assert_difference "Client.count", -1 do
742
companies(:first_firm).clients_of_firm.destroy(companies(:first_firm).clients_of_firm.first.id)
745
assert_equal 0, companies(:first_firm).reload.clients_of_firm.size
746
assert_equal 0, companies(:first_firm).clients_of_firm(true).size
749
def test_destroying_by_string_id
750
force_signal37_to_load_all_clients_of_firm
752
assert_difference "Client.count", -1 do
753
companies(:first_firm).clients_of_firm.destroy(companies(:first_firm).clients_of_firm.first.id.to_s)
756
assert_equal 0, companies(:first_firm).reload.clients_of_firm.size
757
assert_equal 0, companies(:first_firm).clients_of_firm(true).size
760
def test_destroying_a_collection
761
force_signal37_to_load_all_clients_of_firm
762
companies(:first_firm).clients_of_firm.create("name" => "Another Client")
763
assert_equal 2, companies(:first_firm).clients_of_firm.size
765
assert_difference "Client.count", -2 do
766
companies(:first_firm).clients_of_firm.destroy([companies(:first_firm).clients_of_firm[0], companies(:first_firm).clients_of_firm[1]])
769
assert_equal 0, companies(:first_firm).reload.clients_of_firm.size
770
assert_equal 0, companies(:first_firm).clients_of_firm(true).size
774
force_signal37_to_load_all_clients_of_firm
775
assert !companies(:first_firm).clients_of_firm.empty?, "37signals has clients after load"
776
companies(:first_firm).clients_of_firm.destroy_all
777
assert companies(:first_firm).clients_of_firm.empty?, "37signals has no clients after destroy all"
778
assert companies(:first_firm).clients_of_firm(true).empty?, "37signals has no clients after destroy all and refresh"
782
firm = companies(:first_firm)
783
assert_equal 2, firm.clients.size
785
assert Client.find(:all, :conditions => "firm_id=#{firm.id}").empty?
788
def test_dependence_for_associations_with_hash_condition
789
david = authors(:david)
790
post = posts(:thinking).id
791
assert_difference('Post.count', -1) { assert david.destroy }
794
def test_destroy_dependent_when_deleted_from_association
795
firm = Firm.find(:first)
796
assert_equal 2, firm.clients.size
798
client = firm.clients.first
799
firm.clients.delete(client)
801
assert_raise(ActiveRecord::RecordNotFound) { Client.find(client.id) }
802
assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(client.id) }
803
assert_equal 1, firm.clients.size
806
def test_three_levels_of_dependence
807
topic = Topic.create "title" => "neat and simple"
808
reply = topic.replies.create "title" => "neat and simple", "content" => "still digging it"
809
silly_reply = reply.replies.create "title" => "neat and simple", "content" => "ain't complaining"
811
assert_nothing_raised { topic.destroy }
814
uses_transaction :test_dependence_with_transaction_support_on_failure
815
def test_dependence_with_transaction_support_on_failure
816
firm = companies(:first_firm)
817
clients = firm.clients
818
assert_equal 2, clients.length
819
clients.last.instance_eval { def before_destroy() raise "Trigger rollback" end }
821
firm.destroy rescue "do nothing"
823
assert_equal 2, Client.find(:all, :conditions => "firm_id=#{firm.id}").size
826
def test_dependence_on_account
827
num_accounts = Account.count
828
companies(:first_firm).destroy
829
assert_equal num_accounts - 1, Account.count
832
def test_depends_and_nullify
833
num_accounts = Account.count
834
num_companies = Company.count
836
core = companies(:rails_core)
837
assert_equal accounts(:rails_core_account), core.account
838
assert_equal companies(:leetsoft, :jadedpixel), core.companies
840
assert_nil accounts(:rails_core_account).reload.firm_id
841
assert_nil companies(:leetsoft).reload.client_of
842
assert_nil companies(:jadedpixel).reload.client_of
845
assert_equal num_accounts, Account.count
848
def test_included_in_collection
849
assert companies(:first_firm).clients.include?(Client.find(2))
852
def test_adding_array_and_collection
853
assert_nothing_raised { Firm.find(:first).clients + Firm.find(:all).last.clients }
856
def test_find_all_without_conditions
857
firm = companies(:first_firm)
858
assert_equal 2, firm.clients.find(:all).length
861
def test_replace_with_less
862
firm = Firm.find(:first)
863
firm.clients = [companies(:first_client)]
864
assert firm.save, "Could not save firm"
866
assert_equal 1, firm.clients.length
869
def test_replace_with_less_and_dependent_nullify
870
num_companies = Company.count
871
companies(:rails_core).companies = []
872
assert_equal num_companies, Company.count
875
def test_replace_with_new
876
firm = Firm.find(:first)
877
firm.clients = [companies(:second_client), Client.new("name" => "New Client")]
880
assert_equal 2, firm.clients.length
881
assert !firm.clients.include?(:first_client)
885
assert_equal [companies(:first_client).id, companies(:second_client).id], companies(:first_firm).client_ids
888
def test_get_ids_for_loaded_associations
889
company = companies(:first_firm)
890
company.clients(true)
897
def test_get_ids_for_unloaded_associations_does_not_load_them
898
company = companies(:first_firm)
899
assert !company.clients.loaded?
900
assert_equal [companies(:first_client).id, companies(:second_client).id], company.client_ids
901
assert !company.clients.loaded?
904
def test_get_ids_for_unloaded_finder_sql_associations_loads_them
905
company = companies(:first_firm)
906
assert !company.clients_using_sql.loaded?
907
assert_equal [companies(:second_client).id], company.clients_using_sql_ids
908
assert company.clients_using_sql.loaded?
911
def test_assign_ids_ignoring_blanks
912
firm = Firm.create!(:name => 'Apple')
913
firm.client_ids = [companies(:first_client).id, nil, companies(:second_client).id, '']
916
assert_equal 2, firm.clients(true).size
917
assert firm.clients.include?(companies(:second_client))
920
def test_get_ids_for_through
921
assert_equal [comments(:eager_other_comment1).id], authors(:mary).comment_ids
924
def test_modifying_a_through_a_has_many_should_raise
926
lambda { authors(:mary).comment_ids = [comments(:greetings).id, comments(:more_greetings).id] },
927
lambda { authors(:mary).comments = [comments(:greetings), comments(:more_greetings)] },
928
lambda { authors(:mary).comments << Comment.create!(:body => "Yay", :post_id => 424242) },
929
lambda { authors(:mary).comments.delete(authors(:mary).comments.first) },
930
].each {|block| assert_raise(ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection, &block) }
933
def test_dynamic_find_should_respect_association_order_for_through
934
assert_equal Comment.find(10), authors(:david).comments_desc.find(:first, :conditions => "comments.type = 'SpecialComment'")
935
assert_equal Comment.find(10), authors(:david).comments_desc.find_by_type('SpecialComment')
938
def test_dynamic_find_order_should_override_association_order_for_through
939
assert_equal Comment.find(3), authors(:david).comments_desc.find(:first, :conditions => "comments.type = 'SpecialComment'", :order => 'comments.id')
940
assert_equal Comment.find(3), authors(:david).comments_desc.find_by_type('SpecialComment', :order => 'comments.id')
943
def test_dynamic_find_all_should_respect_association_order_for_through
944
assert_equal [Comment.find(10), Comment.find(7), Comment.find(6), Comment.find(3)], authors(:david).comments_desc.find(:all, :conditions => "comments.type = 'SpecialComment'")
945
assert_equal [Comment.find(10), Comment.find(7), Comment.find(6), Comment.find(3)], authors(:david).comments_desc.find_all_by_type('SpecialComment')
948
def test_dynamic_find_all_order_should_override_association_order_for_through
949
assert_equal [Comment.find(3), Comment.find(6), Comment.find(7), Comment.find(10)], authors(:david).comments_desc.find(:all, :conditions => "comments.type = 'SpecialComment'", :order => 'comments.id')
950
assert_equal [Comment.find(3), Comment.find(6), Comment.find(7), Comment.find(10)], authors(:david).comments_desc.find_all_by_type('SpecialComment', :order => 'comments.id')
953
def test_dynamic_find_all_should_respect_association_limit_for_through
954
assert_equal 1, authors(:david).limited_comments.find(:all, :conditions => "comments.type = 'SpecialComment'").length
955
assert_equal 1, authors(:david).limited_comments.find_all_by_type('SpecialComment').length
958
def test_dynamic_find_all_order_should_override_association_limit_for_through
959
assert_equal 4, authors(:david).limited_comments.find(:all, :conditions => "comments.type = 'SpecialComment'", :limit => 9_000).length
960
assert_equal 4, authors(:david).limited_comments.find_all_by_type('SpecialComment', :limit => 9_000).length
963
def test_find_all_include_over_the_same_table_for_through
964
assert_equal 2, people(:michael).posts.find(:all, :include => :people).length
967
def test_has_many_through_respects_hash_conditions
968
assert_equal authors(:david).hello_posts, authors(:david).hello_posts_with_hash_conditions
969
assert_equal authors(:david).hello_post_comments, authors(:david).hello_post_comments_with_hash_conditions
972
def test_include_uses_array_include_after_loaded
973
firm = companies(:first_firm)
974
firm.clients.class # force load target
976
client = firm.clients.first
979
assert firm.clients.loaded?
980
assert firm.clients.include?(client)
984
def test_include_checks_if_record_exists_if_target_not_loaded
985
firm = companies(:first_firm)
986
client = firm.clients.first
989
assert ! firm.clients.loaded?
991
assert firm.clients.include?(client)
993
assert ! firm.clients.loaded?
996
def test_include_loads_collection_if_target_uses_finder_sql
997
firm = companies(:first_firm)
998
client = firm.clients_using_sql.first
1001
assert ! firm.clients_using_sql.loaded?
1002
assert firm.clients_using_sql.include?(client)
1003
assert firm.clients_using_sql.loaded?
1007
def test_include_returns_false_for_non_matching_record_to_verify_scoping
1008
firm = companies(:first_firm)
1009
client = Client.create!(:name => 'Not Associated')
1011
assert ! firm.clients.loaded?
1012
assert ! firm.clients.include?(client)
1015
def test_calling_first_or_last_on_association_should_not_load_association
1016
firm = companies(:first_firm)
1019
assert !firm.clients.loaded?
1022
def test_calling_first_or_last_on_loaded_association_should_not_fetch_with_query
1023
firm = companies(:first_firm)
1024
firm.clients.class # force load target
1025
assert firm.clients.loaded?
1027
assert_no_queries do
1029
assert_equal 2, firm.clients.first(2).size
1031
assert_equal 2, firm.clients.last(2).size
1035
def test_calling_first_or_last_on_existing_record_with_build_should_load_association
1036
firm = companies(:first_firm)
1037
firm.clients.build(:name => 'Foo')
1038
assert !firm.clients.loaded?
1045
assert firm.clients.loaded?
1048
def test_calling_first_or_last_on_existing_record_with_create_should_not_load_association
1049
firm = companies(:first_firm)
1050
firm.clients.create(:name => 'Foo')
1051
assert !firm.clients.loaded?
1058
assert !firm.clients.loaded?
1061
def test_calling_first_or_last_on_new_record_should_not_run_queries
1064
assert_no_queries do
1070
def test_calling_first_or_last_with_find_options_on_loaded_association_should_fetch_with_query
1071
firm = companies(:first_firm)
1072
firm.clients.class # force load target
1075
assert firm.clients.loaded?
1076
firm.clients.first(:order => 'name')
1077
firm.clients.last(:order => 'name')
1081
def test_calling_first_or_last_with_integer_on_association_should_load_association
1082
firm = companies(:first_firm)
1085
firm.clients.first(2)
1086
firm.clients.last(2)
1089
assert firm.clients.loaded?
1092
def test_joins_with_namespaced_model_should_use_correct_type
1093
old = ActiveRecord::Base.store_full_sti_class
1094
ActiveRecord::Base.store_full_sti_class = true
1096
firm = Namespaced::Firm.create({ :name => 'Some Company' })
1097
firm.clients.create({ :name => 'Some Client' })
1099
stats = Namespaced::Firm.find(firm.id, {
1100
:select => "#{Namespaced::Firm.table_name}.id, COUNT(#{Namespaced::Client.table_name}.id) AS num_clients",
1102
:group => "#{Namespaced::Firm.table_name}.id"
1104
assert_equal 1, stats.num_clients.to_i
1107
ActiveRecord::Base.store_full_sti_class = old
1110
def test_association_proxy_transaction_method_starts_transaction_in_association_class
1111
Comment.expects(:transaction)
1112
Post.find(:first).comments.transaction do
1117
def test_sending_new_to_association_proxy_should_have_same_effect_as_calling_new
1118
client_association = companies(:first_firm).clients
1119
assert_equal client_association.new.attributes, client_association.send(:new).attributes
1122
def test_respond_to_private_class_methods
1123
client_association = companies(:first_firm).clients
1124
assert !client_association.respond_to?(:private_method)
1125
assert client_association.respond_to?(:private_method, true)
1128
def test_creating_using_primary_key
1129
firm = Firm.find(:first)
1130
client = firm.clients_using_primary_key.create!(:name => 'test')
1131
assert_equal firm.name, client.firm_name