From 336ff8a97e9391d4111e923a9b841376110d04c9 Mon Sep 17 00:00:00 2001 From: Manoj Date: Sat, 28 Jan 2012 00:18:59 +0530 Subject: has_many/has_one, :dependent => :restrict, deprecation added. --- .../associations/has_many_associations_test.rb | 67 ++++++++++++++++++---- .../associations/has_one_associations_test.rb | 48 +++++++++++++++- 2 files changed, 101 insertions(+), 14 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index f1a341437f..a710900054 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1140,16 +1140,41 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_nil companies(:leetsoft).reload.client_of assert_nil companies(:jadedpixel).reload.client_of - assert_equal num_accounts, Account.count end def test_restrict - firm = RestrictedFirm.new(:name => 'restrict') - firm.save! + # ActiveRecord::Base.dependent_restrict_raises = true, by default + + firm = RestrictedFirm.create!(:name => 'restrict') firm.companies.create(:name => 'child') + assert !firm.companies.empty? assert_raise(ActiveRecord::DeleteRestrictionError) { firm.destroy } + assert RestrictedFirm.exists?(:name => 'restrict') + assert firm.companies.exists?(:name => 'child') + end + + def test_restrict_when_dependent_restrict_raises_config_set_to_false + # ActiveRecord::Base.dependent_restrict_raises = true, by default + + ActiveRecord::Base.dependent_restrict_raises = false + # add an error on the model instead of raising ActiveRecord::DeleteRestrictionError + + firm = RestrictedFirm.create!(:name => 'restrict') + firm.companies.create(:name => 'child') + + assert !firm.companies.empty? + + firm.destroy + + assert !firm.errors.empty? + + assert_equal "Cannot delete record because dependent companies exist", firm.errors[:base].first + assert RestrictedFirm.exists?(:name => 'restrict') + assert firm.companies.exists?(:name => 'child') + ensure + ActiveRecord::Base.dependent_restrict_raises = true end def test_included_in_collection @@ -1401,29 +1426,29 @@ class HasManyAssociationsTest < ActiveRecord::TestCase firm.clients.last end end - + def test_custom_primary_key_on_new_record_should_fetch_with_query author = Author.new(:name => "David") assert !author.essays.loaded? - - assert_queries 1 do + + assert_queries 1 do assert_equal 1, author.essays.size end - + assert_equal author.essays, Essay.find_all_by_writer_id("David") - + end - + def test_has_many_custom_primary_key david = authors(:david) assert_equal david.essays, Essay.find_all_by_writer_id("David") end - + def test_blank_custom_primary_key_on_new_record_should_not_run_queries author = Author.new assert !author.essays.loaded? - - assert_queries 0 do + + assert_queries 0 do assert_equal 0, author.essays.size end end @@ -1649,4 +1674,22 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal [bulb2], car.bulbs assert_equal [bulb2], car.reload.bulbs end + + def test_building_has_many_association_with_restrict_dependency + assert_deprecated do + class_eval <<-EOF + class RestrictedFirm < ActiveRecord::Base + has_many :companies, :dependent => :restrict + end + EOF + end + + assert_not_deprecated do + class_eval <<-EOF + class Firm < ActiveRecord::Base + has_many :companies + end + EOF + end + end end diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index 26931e3e85..a17f33c8df 100644 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -157,11 +157,37 @@ class HasOneAssociationsTest < ActiveRecord::TestCase end def test_dependence_with_restrict - firm = RestrictedFirm.new(:name => 'restrict') - firm.save! + # ActiveRecord::Base.dependent_restrict_raises = true, by default + + firm = RestrictedFirm.create!(:name => 'restrict') firm.create_account(:credit_limit => 10) + assert_not_nil firm.account + assert_raise(ActiveRecord::DeleteRestrictionError) { firm.destroy } + assert RestrictedFirm.exists?(:name => 'restrict') + assert firm.account.present? + end + + def test_dependence_with_restrict_with_dependent_restrict_raises_config_set_to_false + # ActiveRecord::Base.dependent_restrict_raises = true, by default + + ActiveRecord::Base.dependent_restrict_raises = false + # adds an error on the model instead of raising ActiveRecord::DeleteRestrictionError + + firm = RestrictedFirm.create!(:name => 'restrict') + firm.create_account(:credit_limit => 10) + + assert_not_nil firm.account + + firm.destroy + + assert !firm.errors.empty? + assert_equal "Cannot delete record because dependent account exist", firm.errors[:base].first + assert RestrictedFirm.exists?(:name => 'restrict') + assert firm.account.present? + ensure + ActiveRecord::Base.dependent_restrict_raises = true end def test_successful_build_association @@ -456,4 +482,22 @@ class HasOneAssociationsTest < ActiveRecord::TestCase assert_equal car.id, bulb.attributes_after_initialize['car_id'] end + + def test_building_has_one_association_with_dependent_restrict + assert_deprecated do + class_eval <<-EOF + class RestrictedFirm < ActiveRecord::Base + has_one :account, :dependent => :restrict + end + EOF + end + + assert_not_deprecated do + class_eval <<-EOF + class Firm < ActiveRecord::Base + has_one :account + end + EOF + end + end end -- cgit v1.2.3 From 23074c81a5e0b1e08e2e6555053678e8d656f484 Mon Sep 17 00:00:00 2001 From: Manoj Date: Tue, 31 Jan 2012 13:46:07 +0530 Subject: suggested fixes for :dependent => :restrict deprecation. --- .../associations/has_many_associations_test.rb | 21 +++++---------------- .../cases/associations/has_one_associations_test.rb | 21 +++++---------------- 2 files changed, 10 insertions(+), 32 deletions(-) (limited to 'activerecord/test/cases/associations') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index a710900054..8c7956406a 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1170,7 +1170,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert !firm.errors.empty? - assert_equal "Cannot delete record because dependent companies exist", firm.errors[:base].first + assert_equal "Cannot delete record because dependent company exists", firm.errors[:base].first assert RestrictedFirm.exists?(:name => 'restrict') assert firm.companies.exists?(:name => 'child') ensure @@ -1676,20 +1676,9 @@ class HasManyAssociationsTest < ActiveRecord::TestCase end def test_building_has_many_association_with_restrict_dependency - assert_deprecated do - class_eval <<-EOF - class RestrictedFirm < ActiveRecord::Base - has_many :companies, :dependent => :restrict - end - EOF - end - - assert_not_deprecated do - class_eval <<-EOF - class Firm < ActiveRecord::Base - has_many :companies - end - EOF - end + klass = Class.new(ActiveRecord::Base) + + assert_deprecated { klass.has_many :companies, :dependent => :restrict } + assert_not_deprecated { klass.has_many :companies } end end diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index a17f33c8df..7c6736fb95 100644 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -183,7 +183,7 @@ class HasOneAssociationsTest < ActiveRecord::TestCase firm.destroy assert !firm.errors.empty? - assert_equal "Cannot delete record because dependent account exist", firm.errors[:base].first + assert_equal "Cannot delete record because dependent account exists", firm.errors[:base].first assert RestrictedFirm.exists?(:name => 'restrict') assert firm.account.present? ensure @@ -484,20 +484,9 @@ class HasOneAssociationsTest < ActiveRecord::TestCase end def test_building_has_one_association_with_dependent_restrict - assert_deprecated do - class_eval <<-EOF - class RestrictedFirm < ActiveRecord::Base - has_one :account, :dependent => :restrict - end - EOF - end - - assert_not_deprecated do - class_eval <<-EOF - class Firm < ActiveRecord::Base - has_one :account - end - EOF - end + klass = Class.new(ActiveRecord::Base) + + assert_deprecated { klass.has_one :account, :dependent => :restrict } + assert_not_deprecated { klass.has_one :account } end end -- cgit v1.2.3