From 835bf751253769af17ffd548faea04aa44259f52 Mon Sep 17 00:00:00 2001 From: Carlos Figueiredo Date: Mon, 27 Feb 2017 17:06:32 -0500 Subject: Make required by default test for belongs_to association clearer Since #18937 `belongs_to` associations receive a setting to determine if it should be or not treated as `required` by default. While the tests were still passing, it was not evident that the "default" behaviour for `required` could change in fuction of a setting, that is set by default for fresh Rails5 apps, but not for upgraded apps. This commit try to relate them to make it clear what is the behaviour expected when the setting is set as `true` or not set. --- .../test/cases/associations/required_test.rb | 26 +++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'activerecord/test/cases/associations/required_test.rb') diff --git a/activerecord/test/cases/associations/required_test.rb b/activerecord/test/cases/associations/required_test.rb index f8b686721e..0daa35ea83 100644 --- a/activerecord/test/cases/associations/required_test.rb +++ b/activerecord/test/cases/associations/required_test.rb @@ -22,7 +22,10 @@ class RequiredAssociationsTest < ActiveRecord::TestCase @connection.drop_table "children", if_exists: true end - test "belongs_to associations are not required by default" do + test "belongs_to associations can be optional by default" do + original_value = ActiveRecord::Base.belongs_to_required_by_default + ActiveRecord::Base.belongs_to_required_by_default = false + model = subclass_of(Child) do belongs_to :parent, inverse_of: false, class_name: "RequiredAssociationsTest::Parent" @@ -30,6 +33,8 @@ class RequiredAssociationsTest < ActiveRecord::TestCase assert model.new.save assert model.new(parent: Parent.new).save + + ActiveRecord::Base.belongs_to_required_by_default = original_value end test "required belongs_to associations have presence validated" do @@ -46,6 +51,25 @@ class RequiredAssociationsTest < ActiveRecord::TestCase assert record.save end + test "belongs_to associations can be required by default" do + original_value = ActiveRecord::Base.belongs_to_required_by_default + ActiveRecord::Base.belongs_to_required_by_default = true + + model = subclass_of(Child) do + belongs_to :parent, inverse_of: false, + class_name: "RequiredAssociationsTest::Parent" + end + + record = model.new + assert_not record.save + assert_equal ["Parent must exist"], record.errors.full_messages + + record.parent = Parent.new + assert record.save + + ActiveRecord::Base.belongs_to_required_by_default = original_value + end + test "has_one associations are not required by default" do model = subclass_of(Parent) do has_one :child, inverse_of: false, -- cgit v1.2.3 From 6ff851886890b74ae31519bee86457e67c8a513a Mon Sep 17 00:00:00 2001 From: Carlos Figueiredo Date: Mon, 27 Feb 2017 23:23:27 -0500 Subject: Ensure that tests will reset belongs_to_requierd_by_default config --- .../test/cases/associations/required_test.rb | 50 ++++++++++++---------- 1 file changed, 27 insertions(+), 23 deletions(-) (limited to 'activerecord/test/cases/associations/required_test.rb') diff --git a/activerecord/test/cases/associations/required_test.rb b/activerecord/test/cases/associations/required_test.rb index 0daa35ea83..45e1803858 100644 --- a/activerecord/test/cases/associations/required_test.rb +++ b/activerecord/test/cases/associations/required_test.rb @@ -23,18 +23,20 @@ class RequiredAssociationsTest < ActiveRecord::TestCase end test "belongs_to associations can be optional by default" do - original_value = ActiveRecord::Base.belongs_to_required_by_default - ActiveRecord::Base.belongs_to_required_by_default = false + begin + original_value = ActiveRecord::Base.belongs_to_required_by_default + ActiveRecord::Base.belongs_to_required_by_default = false - model = subclass_of(Child) do - belongs_to :parent, inverse_of: false, - class_name: "RequiredAssociationsTest::Parent" - end - - assert model.new.save - assert model.new(parent: Parent.new).save + model = subclass_of(Child) do + belongs_to :parent, inverse_of: false, + class_name: "RequiredAssociationsTest::Parent" + end - ActiveRecord::Base.belongs_to_required_by_default = original_value + assert model.new.save + assert model.new(parent: Parent.new).save + ensure + ActiveRecord::Base.belongs_to_required_by_default = original_value + end end test "required belongs_to associations have presence validated" do @@ -52,22 +54,24 @@ class RequiredAssociationsTest < ActiveRecord::TestCase end test "belongs_to associations can be required by default" do - original_value = ActiveRecord::Base.belongs_to_required_by_default - ActiveRecord::Base.belongs_to_required_by_default = true - - model = subclass_of(Child) do - belongs_to :parent, inverse_of: false, - class_name: "RequiredAssociationsTest::Parent" - end + begin + original_value = ActiveRecord::Base.belongs_to_required_by_default + ActiveRecord::Base.belongs_to_required_by_default = true - record = model.new - assert_not record.save - assert_equal ["Parent must exist"], record.errors.full_messages + model = subclass_of(Child) do + belongs_to :parent, inverse_of: false, + class_name: "RequiredAssociationsTest::Parent" + end - record.parent = Parent.new - assert record.save + record = model.new + assert_not record.save + assert_equal ["Parent must exist"], record.errors.full_messages - ActiveRecord::Base.belongs_to_required_by_default = original_value + record.parent = Parent.new + assert record.save + ensure + ActiveRecord::Base.belongs_to_required_by_default = original_value + end end test "has_one associations are not required by default" do -- cgit v1.2.3