Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 9233

Show
Ignore:
Timestamp:
04/06/08 03:01:09 (8 months ago)
Author:
pratik
Message:

Split associations_test.rb into multiple files based on association type. [Pratik]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/test/cases/associations_test.rb

    r9232 r9233  
    180180end 
    181181 
    182 class HasOneAssociationsTest < ActiveRecord::TestCase 
    183   fixtures :accounts, :companies, :developers, :projects, :developers_projects 
    184  
    185   def setup 
    186     Account.destroyed_account_ids.clear 
    187   end 
    188  
    189   def test_has_one 
    190     assert_equal companies(:first_firm).account, Account.find(1) 
    191     assert_equal Account.find(1).credit_limit, companies(:first_firm).account.credit_limit 
    192   end 
    193    
    194   def test_has_one_cache_nils 
    195     firm = companies(:another_firm) 
    196     assert_queries(1) { assert_nil firm.account } 
    197     assert_queries(0) { assert_nil firm.account } 
    198  
    199     firms = Firm.find(:all, :include => :account) 
    200     assert_queries(0) { firms.each(&:account) } 
    201   end 
    202  
    203   def test_can_marshal_has_one_association_with_nil_target 
    204     firm = Firm.new 
    205     assert_nothing_raised do 
    206       assert_equal firm.attributes, Marshal.load(Marshal.dump(firm)).attributes 
    207     end 
    208  
    209     firm.account 
    210     assert_nothing_raised do 
    211       assert_equal firm.attributes, Marshal.load(Marshal.dump(firm)).attributes 
    212     end 
    213   end 
    214  
    215   def test_proxy_assignment 
    216     company = companies(:first_firm) 
    217     assert_nothing_raised { company.account = company.account } 
    218   end 
    219  
    220   def test_triple_equality 
    221     assert Account === companies(:first_firm).account 
    222     assert companies(:first_firm).account === Account 
    223   end 
    224  
    225   def test_type_mismatch 
    226     assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).account = 1 } 
    227     assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).account = Project.find(1) } 
    228   end 
    229  
    230   def test_natural_assignment 
    231     apple = Firm.create("name" => "Apple") 
    232     citibank = Account.create("credit_limit" => 10) 
    233     apple.account = citibank 
    234     assert_equal apple.id, citibank.firm_id 
    235   end 
    236  
    237   def test_natural_assignment_to_nil 
    238     old_account_id = companies(:first_firm).account.id 
    239     companies(:first_firm).account = nil 
    240     companies(:first_firm).save 
    241     assert_nil companies(:first_firm).account 
    242     # account is dependent, therefore is destroyed when reference to owner is lost 
    243     assert_raises(ActiveRecord::RecordNotFound) { Account.find(old_account_id) } 
    244   end 
    245  
    246   def test_assignment_without_replacement 
    247     apple = Firm.create("name" => "Apple") 
    248     citibank = Account.create("credit_limit" => 10) 
    249     apple.account = citibank 
    250     assert_equal apple.id, citibank.firm_id 
    251  
    252     hsbc = apple.build_account({ :credit_limit => 20}, false) 
    253     assert_equal apple.id, hsbc.firm_id 
    254     hsbc.save 
    255     assert_equal apple.id, citibank.firm_id 
    256  
    257     nykredit = apple.create_account({ :credit_limit => 30}, false) 
    258     assert_equal apple.id, nykredit.firm_id 
    259     assert_equal apple.id, citibank.firm_id 
    260     assert_equal apple.id, hsbc.firm_id 
    261   end 
    262  
    263   def test_assignment_without_replacement_on_create 
    264     apple = Firm.create("name" => "Apple") 
    265     citibank = Account.create("credit_limit" => 10) 
    266     apple.account = citibank 
    267     assert_equal apple.id, citibank.firm_id 
    268  
    269     hsbc = apple.create_account({:credit_limit => 10}, false) 
    270     assert_equal apple.id, hsbc.firm_id 
    271     hsbc.save 
    272     assert_equal apple.id, citibank.firm_id 
    273   end 
    274  
    275   def test_dependence 
    276     num_accounts = Account.count 
    277  
    278     firm = Firm.find(1) 
    279     assert !firm.account.nil? 
    280     account_id = firm.account.id 
    281     assert_equal [], Account.destroyed_account_ids[firm.id] 
    282  
    283     firm.destroy 
    284     assert_equal num_accounts - 1, Account.count 
    285     assert_equal [account_id], Account.destroyed_account_ids[firm.id] 
    286   end 
    287  
    288   def test_exclusive_dependence 
    289     num_accounts = Account.count 
    290  
    291     firm = ExclusivelyDependentFirm.find(9) 
    292     assert !firm.account.nil? 
    293     account_id = firm.account.id 
    294     assert_equal [], Account.destroyed_account_ids[firm.id] 
    295  
    296     firm.destroy 
    297     assert_equal num_accounts - 1, Account.count 
    298     assert_equal [], Account.destroyed_account_ids[firm.id] 
    299   end 
    300  
    301   def test_dependence_with_nil_associate 
    302     firm = DependentFirm.new(:name => 'nullify') 
    303     firm.save! 
    304     assert_nothing_raised { firm.destroy } 
    305   end 
    306  
    307   def test_succesful_build_association 
    308     firm = Firm.new("name" => "GlobalMegaCorp") 
    309     firm.save 
    310  
    311     account = firm.build_account("credit_limit" => 1000) 
    312     assert account.save 
    313     assert_equal account, firm.account 
    314   end 
    315  
    316   def test_failing_build_association 
    317     firm = Firm.new("name" => "GlobalMegaCorp") 
    318     firm.save 
    319  
    320     account = firm.build_account 
    321     assert !account.save 
    322     assert_equal "can't be empty", account.errors.on("credit_limit") 
    323   end 
    324  
    325   def test_build_association_twice_without_saving_affects_nothing 
    326     count_of_account = Account.count 
    327     firm = Firm.find(:first) 
    328     account1 = firm.build_account("credit_limit" => 1000) 
    329     account2 = firm.build_account("credit_limit" => 2000) 
    330  
    331     assert_equal count_of_account, Account.count 
    332   end 
    333  
    334   def test_create_association 
    335     firm = Firm.create(:name => "GlobalMegaCorp") 
    336     account = firm.create_account(:credit_limit => 1000) 
    337     assert_equal account, firm.reload.account 
    338   end 
    339  
    340   def test_build 
    341     firm = Firm.new("name" => "GlobalMegaCorp") 
    342     firm.save 
    343  
    344     firm.account = account = Account.new("credit_limit" => 1000) 
    345     assert_equal account, firm.account 
    346     assert account.save 
    347     assert_equal account, firm.account 
    348   end 
    349  
    350   def test_build_before_child_saved 
    351     firm = Firm.find(1) 
    352  
    353     account = firm.account.build("credit_limit" => 1000) 
    354     assert_equal account, firm.account 
    355     assert account.new_record? 
    356     assert firm.save 
    357     assert_equal account, firm.account 
    358     assert !account.new_record? 
    359   end 
    360  
    361   def test_build_before_either_saved 
    362     firm = Firm.new("name" => "GlobalMegaCorp") 
    363  
    364     firm.account = account = Account.new("credit_limit" => 1000) 
    365     assert_equal account, firm.account 
    366     assert account.new_record? 
    367     assert firm.save 
    368     assert_equal account, firm.account 
    369     assert !account.new_record? 
    370   end 
    371  
    372   def test_failing_build_association 
    373     firm = Firm.new("name" => "GlobalMegaCorp") 
    374     firm.save 
    375  
    376     firm.account = account = Account.new 
    377     assert_equal account, firm.account 
    378     assert !account.save 
    379     assert_equal account, firm.account 
    380     assert_equal "can't be empty", account.errors.on("credit_limit") 
    381   end 
    382  
    383   def test_create 
    384     firm = Firm.new("name" => "GlobalMegaCorp") 
    385     firm.save 
    386     firm.account = account = Account.create("credit_limit" => 1000) 
    387     assert_equal account, firm.account 
    388   end 
    389  
    390   def test_create_before_save 
    391     firm = Firm.new("name" => "GlobalMegaCorp") 
    392     firm.account = account = Account.create("credit_limit" => 1000) 
    393     assert_equal account, firm.account 
    394   end 
    395  
    396   def test_dependence_with_missing_association 
    397     Account.destroy_all 
    398     firm = Firm.find(1) 
    399     assert firm.account.nil? 
    400     firm.destroy 
    401   end 
    402  
    403   def test_dependence_with_missing_association_and_nullify 
    404     Account.destroy_all 
    405     firm = DependentFirm.find(:first) 
    406     assert firm.account.nil? 
    407     firm.destroy 
    408   end 
    409  
    410   def test_assignment_before_parent_saved 
    411     firm = Firm.new("name" => "GlobalMegaCorp") 
    412     firm.account = a = Account.find(1) 
    413     assert firm.new_record? 
    414     assert_equal a, firm.account 
    415     assert firm.save 
    416     assert_equal a, firm.account 
    417     assert_equal a, firm.account(true) 
    418   end 
    419  
    420   def test_finding_with_interpolated_condition 
    421     firm = Firm.find(:first) 
    422     superior = firm.clients.create(:name => 'SuperiorCo') 
    423     superior.rating = 10 
    424     superior.save 
    425     assert_equal 10, firm.clients_with_interpolated_conditions.first.rating 
    426   end 
    427  
    428   def test_assignment_before_child_saved 
    429     firm = Firm.find(1) 
    430     firm.account = a = Account.new("credit_limit" => 1000) 
    431     assert !a.new_record? 
    432     assert_equal a, firm.account 
    433     assert_equal a, firm.account 
    434     assert_equal a, firm.account(true) 
    435   end 
    436    
    437   def test_save_fails_for_invalid_has_one 
    438     firm = Firm.find(:first) 
    439     assert firm.valid? 
    440      
    441     firm.account = Account.new 
    442      
    443     assert !firm.account.valid? 
    444     assert !firm.valid? 
    445     assert !firm.save 
    446     assert_equal "is invalid", firm.errors.on("account") 
    447   end 
    448  
    449   def test_assignment_before_either_saved 
    450     firm = Firm.new("name" => "GlobalMegaCorp") 
    451     firm.account = a = Account.new("credit_limit" => 1000) 
    452     assert firm.new_record? 
    453     assert a.new_record? 
    454     assert_equal a, firm.account 
    455     assert firm.save 
    456     assert !firm.new_record? 
    457     assert !a.new_record? 
    458     assert_equal a, firm.account 
    459     assert_equal a, firm.account(true) 
    460   end 
    461  
    462   def test_not_resaved_when_unchanged 
    463     firm = Firm.find(:first, :include => :account) 
    464     firm.name += '-changed' 
    465     assert_queries(1) { firm.save! } 
    466  
    467     firm = Firm.find(:first) 
    468     firm.account = Account.find(:first) 
    469     assert_queries(Firm.partial_updates? ? 0 : 1) { firm.save! } 
    470  
    471     firm = Firm.find(:first).clone 
    472     firm.account = Account.find(:first) 
    473     assert_queries(2) { firm.save! } 
    474  
    475     firm = Firm.find(:first).clone 
    476     firm.account = Account.find(:first).clone 
    477     assert_queries(2) { firm.save! } 
    478   end 
    479  
    480   def test_save_still_works_after_accessing_nil_has_one 
    481     jp = Company.new :name => 'Jaded Pixel' 
    482     jp.dummy_account.nil? 
    483  
    484     assert_nothing_raised do 
    485       jp.save! 
    486     end 
    487   end 
    488  
    489   def test_cant_save_readonly_association 
    490     assert_raise(ActiveRecord::ReadOnlyRecord) { companies(:first_firm).readonly_account.save!  } 
    491     assert companies(:first_firm).readonly_account.readonly? 
    492   end 
    493  
    494 end 
    495  
    496 class HasOneThroughAssociationsTest < ActiveRecord::TestCase 
    497   fixtures :members, :clubs, :memberships, :sponsors 
    498    
    499   def setup 
    500     @member = members(:groucho) 
    501   end 
    502  
    503   def test_has_one_through_with_has_one 
    504     assert_equal clubs(:boring_club), @member.club 
    505   end 
    506  
    507   def test_has_one_through_with_has_many 
    508     assert_equal clubs(:moustache_club), @member.favourite_club 
    509   end 
    510    
    511   def test_creating_association_creates_through_record 
    512     new_member = Member.create(:name => "Chris") 
    513     new_member.club = Club.create(:name => "LRUG") 
    514     assert_not_nil new_member.current_membership 
    515     assert_not_nil new_member.club 
    516   end 
    517    
    518   def test_replace_target_record 
    519     new_club = Club.create(:name => "Marx Bros") 
    520     @member.club = new_club 
    521     @member.reload 
    522     assert_equal new_club, @member.club 
    523   end 
    524    
    525   def test_replacing_target_record_deletes_old_association 
    526     assert_no_difference "Membership.count" do 
    527       new_club = Club.create(:name => "Bananarama") 
    528       @member.club = new_club 
    529       @member.reload       
    530     end 
    531   end 
    532    
    533   def test_has_one_through_polymorphic 
    534     assert_equal clubs(:moustache_club), @member.sponsor_club 
    535   end 
    536    
    537   def has_one_through_to_has_many 
    538     assert_equal 2, @member.fellow_members.size 
    539   end 
    540    
    541   def test_has_one_through_eager_loading 
    542     members = Member.find(:all, :include => :club, :conditions => ["name = ?", "Groucho Marx"]) 
    543     assert_equal 1, members.size 
    544     assert_not_nil assert_no_queries {members[0].club} 
    545   end 
    546    
    547   def test_has_one_through_eager_loading_through_polymorphic 
    548     members = Member.find(:all, :include => :sponsor_club, :conditions => ["name = ?", "Groucho Marx"]) 
    549     assert_equal 1, members.size 
    550     assert_not_nil assert_no_queries {members[0].sponsor_club}     
    551   end 
    552  
    553   def test_has_one_through_polymorphic_with_source_type 
    554     assert_equal members(:groucho), clubs(:moustache_club).sponsored_member 
    555   end 
    556  
    557   def test_eager_has_one_through_polymorphic_with_source_type 
    558     clubs = Club.find(:all, :include => :sponsored_member, :conditions => ["name = ?","Moustache and Eyebrow Fancier Club"]) 
    559     # Only the eyebrow fanciers club has a sponsored_member 
    560     assert_not_nil assert_no_queries {clubs[0].sponsored_member} 
    561   end 
    562  
    563 end 
    564  
    565 class HasManyThroughAssociationsTest < ActiveRecord::TestCase 
    566   fixtures :posts, :readers, :people 
    567  
    568   def test_associate_existing 
    569     assert_queries(2) { posts(:thinking);people(:david) } 
    570      
    571     assert_queries(1) do 
    572        posts(:thinking).people << people(:david) 
    573     end 
    574      
    575     assert_queries(1) do 
    576       assert posts(:thinking).people.include?(people(:david)) 
    577     end 
    578      
    579     assert posts(:thinking).reload.people(true).include?(people(:david)) 
    580   end 
    581  
    582   def test_associating_new 
    583     assert_queries(1) { posts(:thinking) } 
    584     new_person = nil # so block binding catches it 
    585      
    586     assert_queries(0) do 
    587       new_person = Person.new 
    588     end 
    589      
    590     # Associating new records always saves them 
    591     # Thus, 1 query for the new person record, 1 query for the new join table record 
    592     assert_queries(2) do 
    593       posts(:thinking).people << new_person 
    594     end 
    595      
    596     assert_queries(1) do 
    597       assert posts(:thinking).people.include?(new_person) 
    598     end 
    599      
    600     assert posts(:thinking).reload.people(true).include?(new_person) 
    601   end 
    602  
    603   def test_associate_new_by_building 
    604     assert_queries(1) { posts(:thinking) } 
    605      
    606     assert_queries(0) do 
    607       posts(:thinking).people.build(:first_name=>"Bob") 
    608       posts(:thinking).people.new(:first_name=>"Ted") 
    609     end 
    610      
    611     # Should only need to load the association once 
    612     assert_queries(1) do 
    613       assert posts(:thinking).people.collect(&:first_name).include?("Bob") 
    614       assert posts(:thinking).people.collect(&:first_name).include?("Ted") 
    615     end 
    616      
    617     # 2 queries for each new record (1 to save the record itself, 1 for the join model) 
    618     #    * 2 new records = 4 
    619     # + 1 query to save the actual post = 5 
    620     assert_queries(5) do 
    621       posts(:thinking).save 
    622     end 
    623      
    624     assert posts(:thinking).reload.people(true).collect(&:first_name).include?("Bob") 
    625     assert posts(:thinking).reload.people(true).collect(&:first_name).include?("Ted") 
    626   end 
    627  
    628   def test_delete_association 
    629     assert_queries(2){posts(:welcome);people(:michael); } 
    630      
    631     assert_queries(1) do 
    632       posts(:welcome).people.delete(people(:michael)) 
    633     end 
    634      
    635     assert_queries(1) do 
    636       assert posts(:welcome).people.empty? 
    637     end 
    638      
    639     assert posts(:welcome).reload.people(true).empty? 
    640   end 
    641  
    642   def test_replace_association 
    643     assert_queries(4){posts(:welcome);people(:david);people(:michael); posts(:welcome).people(true)} 
    644      
    645     # 1 query to delete the existing reader (michael) 
    646     # 1 query to associate the new reader (david) 
    647     assert_queries(2) do 
    648       posts(:welcome).people = [people(:david)] 
    649     end 
    650      
    651     assert_queries(0){ 
    652       assert posts(:welcome).people.include?(people(:david)) 
    653       assert !posts(:welcome).people.include?(people(:michael)) 
    654     } 
    655      
    656     assert posts(:welcome).reload.people(true).include?(people(:david)) 
    657     assert !posts(:welcome).reload.people(true).include?(people(:michael)) 
    658   end 
    659  
    660   def test_associate_with_create 
    661     assert_queries(1) { posts(:thinking) } 
    662      
    663     # 1 query for the new record, 1 for the join table record 
    664     # No need to update the actual collection yet! 
    665     assert_queries(2) do 
    666       posts(:thinking).people.create(:first_name=>"Jeb") 
    667     end 
    668      
    669     # *Now* we actually need the collection so it's loaded 
    670     assert_queries(1) do 
    671       assert posts(:thinking).people.collect(&:first_name).include?("Jeb") 
    672     end 
    673      
    674     assert posts(:thinking).reload.people(true).collect(&:first_name).include?("Jeb") 
    675   end 
    676  
    677   def test_clear_associations 
    678     assert_queries(2) { posts(:welcome);posts(:welcome).people(true) } 
    679      
    680     assert_queries(1) do 
    681       posts(:welcome).people.clear 
    682     end 
    683      
    684     assert_queries(0) do 
    685       assert posts(:welcome).people.empty? 
    686     end 
    687      
    688     assert posts(:welcome).reload.people(true).empty? 
    689   end 
    690  
    691   def test_association_callback_ordering 
    692     Post.reset_log 
    693     log = Post.log 
    694     post = posts(:thinking) 
    695  
    696     post.people_with_callbacks << people(:michael) 
    697     assert_equal [ 
    698       [:added, :before, "Michael"], 
    699       [:added, :after, "Michael"] 
    700     ], log.last(2) 
    701  
    702     post.people_with_callbacks.push(people(:david), Person.create!(:first_name => "Bob"), Person.new(:first_name => "Lary")) 
    703     assert_equal [ 
    704       [:added, :before, "David"], 
    705       [:added, :after, "David"], 
    706       [:added, :before, "Bob"], 
    707       [:added, :after, "Bob"], 
    708       [:added, :before, "Lary"], 
    709       [:added, :after, "Lary"] 
    710     ],log.last(6) 
    711  
    712     post.people_with_callbacks.build(:first_name => "Ted") 
    713     assert_equal [ 
    714       [:added, :before, "Ted"], 
    715       [:added, :after, "Ted"] 
    716     ], log.last(2) 
    717  
    718     post.people_with_callbacks.create(:first_name => "Sam") 
    719     assert_equal [ 
    720       [:added, :before, "Sam"], 
    721       [:added, :after, "Sam"] 
    722     ], log.last(2) 
    723  
    724     post.people_with_callbacks = [people(:michael),people(:david), Person.new(:first_name => "Julian"), Person.create!(:first_name => "Roger")] 
    725     assert_equal (%w(Ted Bob Sam Lary) * 2).sort, log[-12..-5].collect(&:last).sort 
    726     assert_equal [ 
    727       [:added, :before, "Julian"], 
    728       [:added, :after, "Julian"], 
    729       [:added, :before, "Roger"], 
    730       [:added, :after, "Roger"] 
    731     ], log.last(4) 
    732  
    733     post.people_with_callbacks.clear 
    734     assert_equal (%w(Michael David Julian Roger) * 2).sort, log.last(8).collect(&:last).sort 
    735   end 
    736 end 
    737  
    738 class HasManyAssociationsTest < ActiveRecord::TestCase 
    739   fixtures :accounts, :categories, :companies, :developers, :projects, 
    740            :developers_projects, :topics, :authors, :comments, :author_addresses, 
    741            :people, :posts 
    742  
    743   def setup 
    744     Client.destroyed_client_ids.clear 
    745   end 
    746  
    747   def force_signal37_to_load_all_clients_of_firm 
    748     companies(:first_firm).clients_of_firm.each {|f| } 
    749   end 
    750  
    751   def test_counting_with_counter_sql 
    752     assert_equal 2, Firm.find(:first).clients.count 
    753   end 
    754  
    755   def test_counting 
    756     assert_equal 2, Firm.find(:first).plain_clients.count 
    757   end 
    758  
    759   def test_counting_with_single_conditions 
    760     assert_equal 2, Firm.find(:first).plain_clients.count(:conditions => '1=1') 
    761   end 
    762  
    763   def test_counting_with_single_hash 
    764     assert_equal 2, Firm.find(:first).plain_clients.count(:conditions => '1=1') 
    765   end 
    766  
    767   def test_counting_with_column_name_and_hash 
    768     assert_equal 2, Firm.find(:first).plain_clients.count(:all, :conditions => '1=1') 
    769   end 
    770  
    771   def test_finding 
    772     assert_equal 2, Firm.find(:first).clients.length 
    773   end 
    774  
    775   def test_find_many_with_merged_options 
    776     assert_equal 1, companies(:first_firm).limited_clients.size 
    777     assert_equal 1, companies(:first_firm).limited_clients.find(:all).size 
    778     assert_equal 2, companies(:first_firm).limited_clients.find(:all, :limit => nil).size 
    779   end 
    780  
    781   def test_dynamic_find_should_respect_association_order 
    782     assert_equal companies(:second_client), companies(:first_firm).clients_sorted_desc.find(:first, :conditions => "type = 'Client'") 
    783     assert_equal companies(:second_client), companies(:first_firm).clients_sorted_desc.find_by_type('Client') 
    784   end 
    785  
    786   def test_dynamic_find_order_should_override_association_order 
    787     assert_equal companies(:first_client), companies(:first_firm).clients_sorted_desc.find(:first, :conditions => "type = 'Client'", :order => 'id') 
    788     assert_equal companies(:first_client), companies(:first_firm).clients_sorted_desc.find_by_type('Client', :order => 'id') 
    789   end 
    790  
    791   def test_dynamic_find_all_should_respect_association_order 
    792     assert_equal [companies(:second_client), companies(:first_client)], companies(:first_firm).clients_sorted_desc.find(:all, :conditions => "type = 'Client'") 
    793     assert_equal [companies(:second_client), companies(:first_client)], companies(:first_firm).clients_sorted_desc.find_all_by_type('Client') 
    794   end 
    795  
    796   def test_dynamic_find_all_order_should_override_association_order 
    797     assert_equal [companies(:first_client), companies(:second_client)], companies(:first_firm).clients_sorted_desc.find(:all, :conditions => "type = 'Client'", :order => 'id') 
    798     assert_equal [companies(:first_client), companies(:second_client)], companies(:first_firm).clients_sorted_desc.find_all_by_type('Client', :order => 'id') 
    799   end 
    800  
    801   def test_dynamic_find_all_should_respect_association_limit 
    802     assert_equal 1, companies(:first_firm).limited_clients.find(:all, :conditions => "type = 'Client'").length 
    803     assert_equal 1, companies(:first_firm).limited_clients.find_all_by_type('Client').length 
    804   end 
    805  
    806   def test_dynamic_find_all_limit_should_override_association_limit 
    807     assert_equal 2, companies(:first_firm).limited_clients.find(:all, :conditions => "type = 'Client'", :limit => 9_000).length 
    808     assert_equal 2, companies(:first_firm).limited_clients.find_all_by_type('Client', :limit => 9_000).length 
    809   end 
    810  
    811   def test_dynamic_find_all_should_respect_readonly_access 
    812     companies(:first_firm).readonly_clients.find(:all).each { |c| assert_raise(ActiveRecord::ReadOnlyRecord) { c.save!  } } 
    813     companies(:first_firm).readonly_clients.find(:all).each { |c| assert c.readonly? } 
    814   end 
    815  
    816   def test_cant_save_has_many_readonly_association 
    817     authors(:david).readonly_comments.each { |c| assert_raise(ActiveRecord::ReadOnlyRecord) { c.save! } } 
    818     authors(:david).readonly_comments.each { |c| assert c.readonly? } 
    819   end 
    820  
    821   def test_triple_equality 
    822     assert !(Array === Firm.find(:first).clients) 
    823     assert Firm.find(:first).clients === Array 
    824   end 
    825  
    826   def test_finding_default_orders 
    827     assert_equal "Summit", Firm.find(:first).clients.first.name 
    828   end 
    829  
    830   def test_finding_with_different_class_name_and_order 
    831     assert_equal "Microsoft", Firm.find(:first).clients_sorted_desc.first.name 
    832   end 
    833  
    834   def test_finding_with_foreign_key 
    835     assert_equal "Microsoft", Firm.find(:first).clients_of_firm.first.name 
    836   end 
    837  
    838   def test_finding_with_condition 
    839     assert_equal "Microsoft", Firm.find(:first).clients_like_ms.first.name 
    840   end 
    841  
    842   def test_finding_with_condition_hash 
    843     assert_equal "Microsoft", Firm.find(:first).clients_like_ms_with_hash_conditions.first.name 
    844   end 
    845  
    846   def test_finding_using_sql 
    847     firm = Firm.find(:first) 
    848     first_client = firm.clients_using_sql.first 
    849     assert_not_nil first_client 
    850     assert_equal "Microsoft", first_client.name 
    851     assert_equal 1, firm.clients_using_sql.size 
    852     assert_equal 1, Firm.find(:first).clients_using_sql.size 
    853   end 
    854  
    855   def test_counting_using_sql 
    856     assert_equal 1, Firm.find(:first).clients_using_counter_sql.size 
    857     assert Firm.find(:first).clients_using_counter_sql.any? 
    858     assert_equal 0, Firm.find(:first).clients_using_zero_counter_sql.size 
    859     assert !Firm.find(:first).clients_using_zero_counter_sql.any? 
    860   end 
    861  
    862   def test_counting_non_existant_items_using_sql 
    863     assert_equal 0, Firm.find(:first).no_clients_using_counter_sql.size 
    864   end 
    865  
    866   def test_belongs_to_sanity 
    867     c = Client.new 
    868     assert_nil c.firm 
    869  
    870     if c.firm 
    871       assert false, "belongs_to failed if check" 
    872     end 
    873  
    874     unless c.firm 
    875     else 
    876       assert false,  "belongs_to failed unless check" 
    877     end 
    878   end 
    879  
    880   def test_find_ids 
    881     firm = Firm.find(:first) 
    882  
    883     assert_raises(ActiveRecord::RecordNotFound) { firm.clients.find } 
    884  
    885     client = firm.clients.find(2) 
    886     assert_kind_of Client, client 
    887  
    888     client_ary = firm.clients.find([2]) 
    889     assert_kind_of Array, client_ary 
    890     assert_equal client, client_ary.first 
    891  
    892     client_ary = firm.clients.find(2, 3) 
    893     assert_kind_of Array, client_ary 
    894     assert_equal 2, client_ary.size 
    895     assert_equal client, client_ary.first 
    896  
    897     assert_raises(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) } 
    898   end 
    899  
    900   def test_find_string_ids_when_using_finder_sql 
    901     firm = Firm.find(:first) 
    902  
    903     client = firm.clients_using_finder_sql.find("2") 
    904     assert_kind_of Client, client 
    905  
    906     client_ary = firm.clients_using_finder_sql.find(["2"]) 
    907     assert_kind_of Array, client_ary 
    908     assert_equal client, client_ary.first 
    909  
    910     client_ary = firm.clients_using_finder_sql.find("2", "3") 
    911     assert_kind_of Array, client_ary 
    912     assert_equal 2, client_ary.size 
    913     assert client_ary.include?(client) 
    914   end 
    915  
    916   def test_find_all 
    917     firm = Firm.find(:first) 
    918     assert_equal 2, firm.clients.find(:all, :conditions => "#{QUOTED_TYPE} = 'Client'").length 
    919     assert_equal 1, firm.clients.find(:all, :conditions => "name = 'Summit'").length 
    920   end 
    921  
    922   def test_find_all_sanitized 
    923     firm = Firm.find(:first) 
    924     summit = firm.clients.find(:all, :conditions => "name = 'Summit'") 
    925     assert_equal summit, firm.clients.find(:all, :conditions => ["name = ?", "Summit"]) 
    926     assert_equal summit, firm.clients.find(:all, :conditions => ["name = :name", { :name => "Summit" }]) 
    927   end 
    928  
    929   def test_find_first 
    930     firm = Firm.find(:first) 
    931     client2 = Client.find(2) 
    932     assert_equal firm.clients.first, firm.clients.find(:first) 
    933     assert_equal client2, firm.clients.find(:first, :conditions => "#{QUOTED_TYPE} = 'Client'") 
    934   end 
    935  
    936   def test_find_first_sanitized 
    937     firm = Firm.find(:first) 
    938     client2 = Client.find(2) 
    939     assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = ?", 'Client']) 
    940     assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = :type", { :type => 'Client' }]) 
    941   end 
    942  
    943   def test_find_in_collection 
    944     assert_equal Client.find(2).name, companies(:first_firm).clients.find(2).name 
    945     assert_raises(ActiveRecord::RecordNotFound) { companies(:first_firm).clients.find(6) } 
    946   end 
    947  
    948   def test_find_grouped 
    949     all_clients_of_firm1 = Client.find(:all, :conditions => "firm_id = 1") 
    950     grouped_clients_of_firm1 = Client.find(:all, :conditions => "firm_id = 1", :group => "firm_id", :select => 'firm_id, count(id) as clients_count') 
    951     assert_equal 2, all_clients_of_firm1.size 
    952     assert_equal 1, grouped_clients_of_firm1.size 
    953   end 
    954  
    955   def test_adding 
    956     force_signal37_to_load_all_clients_of_firm 
    957     natural = Client.new("name" => "Natural Company") 
    958     companies(:first_firm).clients_of_firm << natural 
    959     assert_equal 2, companies(:first_firm).clients_of_firm.size # checking via the collection 
    960     assert_equal 2, companies(:first_firm).clients_of_firm(true).size # checking using the db 
    961     assert_equal natural, companies(:first_firm).clients_of_firm.last 
    962   end 
    963  
    964   def test_adding_using_create 
    965     first_firm = companies(:first_firm) 
    966     assert_equal 2, first_firm.plain_clients.size 
    967     natural = first_firm.plain_clients.create(:name => "Natural Company") 
    968     assert_equal 3, first_firm.plain_clients.length 
    969     assert_equal 3, first_firm.plain_clients.size 
    970   end 
    971  
    972   def test_create_with_bang_on_has_many_when_parent_is_new_raises 
    973     assert_raises(ActiveRecord::RecordNotSaved) do 
    974       firm = Firm.new 
    975       firm.plain_clients.create! :name=>"Whoever" 
    976     end 
    977   end 
    978  
    979   def test_regular_create_on_has_many_when_parent_is_new_raises 
    980     assert_raises(ActiveRecord::RecordNotSaved) do 
    981       firm = Firm.new 
    982       firm.plain_clients.create :name=>"Whoever" 
    983     end 
    984   end 
    985  
    986   def test_create_with_bang_on_has_many_raises_when_record_not_saved 
    987     assert_raises(ActiveRecord::RecordInvalid) do 
    988       firm = Firm.find(:first) 
    989       firm.plain_clients.create! 
    990     end 
    991   end 
    992  
    993   def test_create_with_bang_on_habtm_when_parent_is_new_raises 
    994     assert_raises(ActiveRecord::RecordNotSaved) do 
    995       Developer.new("name" => "Aredridel").projects.create! 
    996     end 
    997   end 
    998  
    999   def test_adding_a_mismatch_class 
    1000     assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << nil } 
    1001     assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << 1 } 
    1002     assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << Topic.find(1) } 
    1003   end 
    1004  
    1005   def test_adding_a_collection 
    1006     force_signal37_to_load_all_clients_of_firm 
    1007     companies(:first_firm).clients_of_firm.concat([Client.new("name" => "Natural Company"), Client.new("name" => "Apple")]) 
    1008     assert_equal 3, companies(:first_firm).clients_of_firm.size 
    1009     assert_equal 3, companies(:first_firm).clients_of_firm(true).size 
    1010   end 
    1011  
    1012   def test_adding_before_save 
    1013     no_of_firms = Firm.count 
    1014     no_of_clients = Client.count 
    1015  
    1016     new_firm = Firm.new("name" => "A New Firm, Inc") 
    1017     c = Client.new("name" => "Apple") 
    1018  
    1019     new_firm.clients_of_firm.push Client.new("name" => "Natural Company") 
    1020     assert_equal 1, new_firm.clients_of_firm.size 
    1021     new_firm.clients_of_firm << c 
    1022     assert_equal 2, new_firm.clients_of_firm.size 
    1023  
    1024     assert_equal no_of_firms, Firm.count      # Firm was not saved to database. 
    1025     assert_equal no_of_clients, Client.count  # Clients were not saved to database. 
    1026     assert new_firm.save 
    1027     assert !new_firm.new_record? 
    1028     assert !c.new_record? 
    1029     assert_equal new_firm, c.firm 
    1030     assert_equal no_of_firms+1, Firm.count      # Firm was saved to database. 
    1031     assert_equal no_of_clients+2, Client.count  # Clients were saved to database. 
    1032  
    1033     assert_equal 2, new_firm.clients_of_firm.size 
    1034     assert_equal 2, new_firm.clients_of_firm(true).size 
    1035   end 
    1036  
    1037   def test_invalid_adding 
    1038     firm = Firm.find(1) 
    1039     assert !(firm.clients_of_firm << c = Client.new) 
    1040     assert c.new_record? 
    1041     assert !firm.valid? 
    1042     assert !firm.save 
    1043     assert c.new_record? 
    1044   end 
    1045  
    1046   def test_invalid_adding_before_save 
    1047     no_of_firms = Firm.count 
    1048     no_of_clients = Client.count 
    1049     new_firm = Firm.new("name" => "A New Firm, Inc") 
    1050     new_firm.clients_of_firm.concat([c = Client.new, Client.new("name" => "Apple")]) 
    1051     assert c.new_record? 
    1052     assert !c.valid? 
    1053     assert !new_firm.valid? 
    1054     assert !new_firm.save 
    1055     assert c.new_record? 
    1056     assert new_firm.new_record? 
    1057   end 
    1058  
    1059   def test_build 
    1060     company = companies(:first_firm) 
    1061     new_client = assert_no_queries { company.clients_of_firm.build("name" => "Another Client") } 
    1062     assert !company.clients_of_firm.loaded? 
    1063      
    1064     assert_equal "Another Client", new_client.name 
    1065     assert new_client.new_record? 
    1066     assert_equal new_client, company.clients_of_firm.last 
    1067     assert_queries(2) { assert company.save } 
    1068     assert !new_client.new_record? 
    1069     assert_equal 2, company.clients_of_firm(true).size 
    1070   end 
    1071  
    1072   def test_build_many 
    1073     company = companies(:first_firm) 
    1074     new_clients = assert_no_queries { company.clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}]) } 
    1075      
    1076     assert_equal 2, new_clients.size 
    1077     assert_queries(3) { assert company.save } 
    1078     assert_equal 3, company.clients_of_firm(true).size 
    1079   end 
    1080  
    1081   def test_build_followed_by_save_does_not_load_target 
    1082     new_client = companies(:first_firm).clients_of_firm.build("name" => "Another Client") 
    1083     assert companies(:first_firm).save 
    1084     assert !companies(:first_firm).clients_of_firm.loaded? 
    1085   end 
    1086  
    1087   def test_build_without_loading_association 
    1088     first_topic = topics(:first) 
    1089     Reply.column_names 
    1090  
    1091     assert_equal 1, first_topic.replies.length 
    1092  
    1093     assert_no_queries do 
    1094       first_topic.replies.build(:title => "Not saved", :content => "Superstars") 
    1095       assert_equal 2, first_topic.replies.size 
    1096     end 
    1097  
    1098     assert_equal 2, first_topic.replies.to_ary.size 
    1099   end 
    1100  
    1101   def test_create_without_loading_association 
    1102     first_firm  = companies(:first_firm) 
    1103     Firm.column_names 
    1104     Client.column_names 
    1105  
    1106     assert_equal 1, first_firm.clients_of_firm.size 
    1107     first_firm.clients_of_firm.reset 
    1108  
    1109     assert_queries(1) do 
    1110       first_firm.clients_of_firm.create(:name => "Superstars") 
    1111     end 
    1112  
    1113     assert_equal 2, first_firm.clients_of_firm.size 
    1114   end 
    1115  
    1116   def test_invalid_build 
    1117     new_client = companies(:first_firm).clients_of_firm.build 
    1118     assert new_client.new_record? 
    1119     assert !new_client.valid? 
    1120     assert_equal new_client, companies(:first_firm).clients_of_firm.last 
    1121     assert !companies(:first_firm).save 
    1122     assert new_client.new_record? 
    1123     assert_equal 1, companies(:first_firm).clients_of_firm(true).size 
    1124   end 
    1125  
    1126   def test_create 
    1127     force_signal37_to_load_all_clients_of_firm 
    1128     new_client = companies(:first_firm).clients_of_firm.create("name" => "Another Client") 
    1129     assert !new_client.new_record? 
    1130     assert_equal new_client, companies(:first_firm).clients_of_firm.last 
    1131     assert_equal new_client, companies(:first_firm).clients_of_firm(true).last 
    1132   end 
    1133  
    1134   def test_create_many 
    1135     companies(:first_firm).clients_of_firm.create([{"name" => "Another Client"}, {"name" => "Another Client II"}]) 
    1136     assert_equal 3, companies(:first_firm).clients_of_firm(true).size 
    1137   end 
    1138  
    1139   def test_create_followed_by_save_does_not_load_target 
    1140     new_client = companies(:first_firm).clients_of_firm.create("name" => "Another Client") 
    1141     assert companies(:first_firm).save 
    1142     assert !companies(:first_firm).clients_of_firm.loaded? 
    1143   end 
    1144  
    1145   def test_find_or_initialize 
    1146     the_client = companies(:first_firm).clients.find_or_initialize_by_name("Yet another client") 
    1147     assert_equal companies(:first_firm).id, the_client.firm_id 
    1148     assert_equal "Yet another client", the_client.name 
    1149     assert the_client.new_record? 
    1150   end 
    1151  
    1152   def test_find_or_create 
    1153     number_of_clients = companies(:first_firm).clients.size 
    1154     the_client = companies(:first_firm).clients.find_or_create_by_name("Yet another client") 
    1155     assert_equal number_of_clients + 1, companies(:first_firm, :reload).clients.size 
    1156     assert_equal the_client, companies(:first_firm).clients.find_or_create_by_name("Yet another client") 
    1157     assert_equal number_of_clients + 1, companies(:first_firm, :reload).clients.size 
    1158   end 
    1159  
    1160   def test_deleting 
    1161     force_signal37_to_load_all_clients_of_firm 
    1162     companies(:first_firm).clients_of_firm.delete(companies(:first_firm).clients_of_firm.first) 
    1163     assert_equal 0, companies(:first_firm).clients_of_firm.size 
    1164     assert_equal 0, companies(:first_firm).clients_of_firm(true).size 
    1165   end 
    1166  
    1167   def test_deleting_before_save 
    1168     new_firm = Firm.new("name" => "A New Firm, Inc.") 
    1169     new_client = new_firm.clients_of_firm.build("name" => "Another Client") 
    1170     assert_equal 1, new_firm.clients_of_firm.size 
    1171     new_firm.clients_of_firm.delete(new_client) 
    1172     assert_equal 0, new_firm.clients_of_firm.size 
    1173   end 
    1174  
    1175   def test_deleting_a_collection 
    1176     force_signal37_to_load_all_clients_of_firm 
    1177     companies(:first_firm).clients_of_firm.create("name" => "Another Client") 
    1178     assert_equal 2, companies(:first_firm).clients_of_firm.size 
    1179     companies(:first_firm).clients_of_firm.delete([companies(:first_firm).clients_of_firm[0], companies(:first_firm).clients_of_firm[1]]) 
    1180     assert_equal 0, companies(:first_firm).clients_of_firm.size 
    1181     assert_equal 0, companies(:first_firm).clients_of_firm(true).size 
    1182   end 
    1183  
    1184   def test_delete_all 
    1185     force_signal37_to_load_all_clients_of_firm 
    1186     companies(:first_firm).clients_of_firm.create("name" => "Another Client") 
    1187     assert_equal 2, companies(:first_firm).clients_of_firm.size 
    1188     companies(:first_firm).clients_of_firm.delete_all 
    1189     assert_equal 0, companies(:first_firm).clients_of_firm.size 
    1190     assert_equal 0, companies(:first_firm).clients_of_firm(true).size 
    1191   end 
    1192  
    1193   def test_delete_all_with_not_yet_loaded_association_collection 
    1194     force_signal37_to_load_all_clients_of_firm 
    1195     companies(:first_firm).clients_of_firm.create("name" => "Another Client") 
    1196     assert_equal 2, companies(:first_firm).clients_of_firm.size 
    1197     companies(:first_firm).clients_of_firm.reset 
    1198     companies(:first_firm).clients_of_firm.delete_all 
    1199     assert_equal 0, companies(:first_firm).clients_of_firm.size 
    1200     assert_equal 0, companies(:first_firm).clients_of_firm(true).size 
    1201   end 
    1202  
    1203   def test_clearing_an_association_collection 
    1204     firm = companies(:first_firm) 
    1205     client_id = firm.clients_of_firm.first.id 
    1206     assert_equal 1, firm.clients_of_firm.size 
    1207  
    1208     firm.clients_of_firm.clear 
    1209  
    1210     assert_equal 0, firm.clients_of_firm.size 
    1211     assert_equal 0, firm.clients_of_firm(true).size 
    1212     assert_equal [], Client.destroyed_client_ids[firm.id] 
    1213  
    1214     # Should not be destroyed since the association is not dependent. 
    1215     assert_nothing_raised do 
    1216       assert Client.find(client_id).firm.nil? 
    1217     end 
    1218   end 
    1219  
    1220   def test_clearing_a_dependent_association_collection 
    1221     firm = companies(:first_firm) 
    1222     client_id = firm.dependent_clients_of_firm.first.id 
    1223     assert_equal 1, firm.dependent_clients_of_firm.size 
    1224  
    1225     # :dependent means destroy is called on each client 
    1226     firm.dependent_clients_of_firm.clear 
    1227  
    1228     assert_equal 0, firm.dependent_clients_of_firm.size 
    1229     assert_equal 0, firm.dependent_clients_of_firm(true).size 
    1230     assert_equal [client_id], Client.destroyed_client_ids[firm.id] 
    1231  
    1232     # Should be destroyed since the association is dependent. 
    1233     assert Client.find_by_id(client_id).nil? 
    1234   end 
    1235  
    1236   def test_clearing_an_exclusively_dependent_association_collection 
    1237     firm = companies(:first_firm) 
    1238     client_id = firm.exclusively_dependent_clients_of_firm.first.id 
    1239     assert_equal 1, firm.exclusively_dependent_clients_of_firm.size 
    1240  
    1241     assert_equal [], Client.destroyed_client_ids[firm.id] 
    1242  
    1243     # :exclusively_dependent means each client is deleted directly from 
    1244     # the database without looping through them calling destroy. 
    1245     firm.exclusively_dependent_clients_of_firm.clear 
    1246  
    1247     assert_equal 0, firm.exclusively_dependent_clients_of_firm.size 
    1248     assert_equal 0, firm.exclusively_dependent_clients_of_firm(true).size 
    1249     # no destroy-filters should have been called 
    1250     assert_equal [], Client.destroyed_client_ids[firm.id] 
    1251  
    1252     # Should be destroyed since the association is exclusively dependent. 
    1253     assert Client.find_by_id(client_id).nil? 
    1254   end 
    1255  
    1256   def test_dependent_association_respects_optional_conditions_on_delete 
    1257     firm = companies(:odegy) 
    1258     Client.create(:client_of => firm.id, :name => "BigShot Inc.") 
    1259     Client.create(:client_of => firm.id, :name => "SmallTime Inc.") 
    1260     # only one of two clients is included in the association due to the :condit