From 23e4048608da8f1a2899fcd21690e80bd576b549 Mon Sep 17 00:00:00 2001 From: Dave Sims Date: Tue, 18 Apr 2017 08:25:06 -0500 Subject: [PATCH 01/13] Add bang support for validates_around --- lib/action_logic/action_validation.rb | 4 ++++ spec/action_logic/action_task_spec.rb | 8 ++++---- spec/fixtures/tasks.rb | 9 +++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/action_logic/action_validation.rb b/lib/action_logic/action_validation.rb index 4946808..43c178f 100644 --- a/lib/action_logic/action_validation.rb +++ b/lib/action_logic/action_validation.rb @@ -17,6 +17,10 @@ def validates_around(args) @validates_around = args end + def validates_around!(args) + @validates_around = args + end + def get_validates_before @validates_before ||= {} end diff --git a/spec/action_logic/action_task_spec.rb b/spec/action_logic/action_task_spec.rb index 60c9e79..bfa7389 100644 --- a/spec/action_logic/action_task_spec.rb +++ b/spec/action_logic/action_task_spec.rb @@ -21,18 +21,18 @@ module ActionLogic end describe "around validations" do - describe "required attributes and type validation" do + describe "required attributes and type validation with bang" do it "does not raise error if context has required keys and values are of the correct type" do - expect { ValidateAroundTestTask.execute(Constants::VALID_ATTRIBUTES) }.to_not raise_error + expect { ValidateAroundTestTaskWithBang.execute(Constants::VALID_ATTRIBUTES) }.to_not raise_error end it "raises error if context is missing required keys" do - expect { ValidateAroundTestTask.execute() }.to\ + expect { ValidateAroundTestTaskWithBang.execute() }.to\ raise_error(ActionLogic::MissingAttributeError) end it "raises error if context has required keys but values are not of correct type" do - expect { ValidateAroundTestTask.execute(Constants::INVALID_ATTRIBUTES) }.to\ + expect { ValidateAroundTestTaskWithBang.execute(Constants::INVALID_ATTRIBUTES) }.to\ raise_error(ActionLogic::AttributeTypeError) end end diff --git a/spec/fixtures/tasks.rb b/spec/fixtures/tasks.rb index b858ac5..c2cd429 100644 --- a/spec/fixtures/tasks.rb +++ b/spec/fixtures/tasks.rb @@ -10,6 +10,15 @@ def call end end +class ValidateAroundTestTaskWithBang + include ActionLogic::ActionTask + + validates_around! Constants::ALL_VALIDATIONS + + def call + end +end + class ValidateAroundTestTask include ActionLogic::ActionTask From ba72c3c9531f0d58484fb714e7a76246904dcb78 Mon Sep 17 00:00:00 2001 From: Dave Sims Date: Wed, 19 Apr 2017 08:59:42 -0500 Subject: [PATCH 02/13] WIP!! Experiment with extra attr on validations for raising --- lib/action_logic/action_validation.rb | 18 +++++++++--------- .../action_validation/attribute_validation.rb | 8 ++++++-- .../action_validation/presence_validation.rb | 14 +++++++++----- .../action_validation/type_validation.rb | 10 +++++++--- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/lib/action_logic/action_validation.rb b/lib/action_logic/action_validation.rb index 43c178f..6ae6d78 100644 --- a/lib/action_logic/action_validation.rb +++ b/lib/action_logic/action_validation.rb @@ -6,31 +6,31 @@ module ActionLogic module ActionValidation module ClassMethods def validates_before(args) - @validates_before = args + @validates_before = args.merge(raise_action_logic_exception: true) end def validates_after(args) - @validates_after = args + @validates_after = args.merge(raise_action_logic_exception: true) end - def validates_around(args) - @validates_around = args + def validates_around!(args) + @validates_around = args.merge(raise_action_logic_exception: true) end - def validates_around!(args) - @validates_around = args + def validates_around(args) + @validates_around = args.merge(raise_action_logic_exception: true) end def get_validates_before - @validates_before ||= {} + @validates_before ||= { raise_action_logic_exception: true } end def get_validates_after - @validates_after ||= {} + @validates_after ||= { raise_action_logic_exception: true } end def get_validates_around - @validates_around ||= {} + @validates_around ||= { raise_action_logic_exception: true } end end diff --git a/lib/action_logic/action_validation/attribute_validation.rb b/lib/action_logic/action_validation/attribute_validation.rb index d07a2ba..29bc51b 100644 --- a/lib/action_logic/action_validation/attribute_validation.rb +++ b/lib/action_logic/action_validation/attribute_validation.rb @@ -6,11 +6,15 @@ module ActionValidation class AttributeValidation < BaseValidation def self.validate!(validation_rules, context) + tmp_rules = validation_rules.clone + raise_exception = tmp_rules.delete(:raise_action_logic_exception) existing_attributes = context.to_h.keys - expected_attributes = validation_rules.keys || [] + expected_attributes = tmp_rules.keys || [] missing_attributes = expected_attributes - existing_attributes - raise ActionLogic::MissingAttributeError.new(error_message_format(missing_attributes.join(", ") + " attributes are missing")) if missing_attributes.any? + if raise_exception + raise ActionLogic::MissingAttributeError.new(error_message_format(missing_attributes.join(", ") + " attributes are missing")) if missing_attributes.any? + end end end end diff --git a/lib/action_logic/action_validation/presence_validation.rb b/lib/action_logic/action_validation/presence_validation.rb index ab42c45..b2f55b1 100644 --- a/lib/action_logic/action_validation/presence_validation.rb +++ b/lib/action_logic/action_validation/presence_validation.rb @@ -6,13 +6,17 @@ module ActionValidation class PresenceValidation < BaseValidation def self.validate!(validation_rules, context) - return unless validation_rules.values.find { |expected_validation| expected_validation[:presence] } - errors = presence_errors(validation_rules, context) - raise ActionLogic::PresenceError.new(errors) if errors.any? + tmp_rules = validation_rules.clone + raise_exception = tmp_rules.delete(:raise_action_logic_exception) + return unless tmp_rules.values.find { |expected_validation| expected_validation[:presence] } + errors = presence_errors(tmp_rules, context) + if raise_exception + raise ActionLogic::PresenceError.new(errors) if errors.any? + end end - def self.presence_errors(validation_rules, context) - validation_rules.reduce([]) do |error_collection, (expected_attribute, expected_validation)| + def self.presence_errors(tmp_rules, context) + tmp_rules.reduce([]) do |error_collection, (expected_attribute, expected_validation)| next unless expected_validation[:presence] error_collection << error_message(expected_attribute, expected_validation, context) error_collection diff --git a/lib/action_logic/action_validation/type_validation.rb b/lib/action_logic/action_validation/type_validation.rb index 094218d..c5773ed 100644 --- a/lib/action_logic/action_validation/type_validation.rb +++ b/lib/action_logic/action_validation/type_validation.rb @@ -6,9 +6,11 @@ module ActionValidation class TypeValidation < BaseValidation def self.validate!(validation_rules, context) - return unless validation_rules.values.find { |expected_validation| expected_validation[:type] } + tmp_rules = validation_rules.clone + raise_exception = tmp_rules.delete(:raise_action_logic_exception) + return unless tmp_rules.values.find { |expected_validation| expected_validation[:type] } - type_errors = validation_rules.reduce([]) do |collection, (expected_attribute, expected_validation)| + type_errors = tmp_rules.reduce([]) do |collection, (expected_attribute, expected_validation)| next unless expected_validation[:type] if context.to_h[expected_attribute].class != expected_validation[:type] @@ -17,7 +19,9 @@ def self.validate!(validation_rules, context) collection end - raise ActionLogic::AttributeTypeError.new(error_message_format(type_errors.join(", "))) if type_errors.any? + if raise_exception + raise ActionLogic::AttributeTypeError.new(error_message_format(type_errors.join(", "))) if type_errors.any? + end end end end From b28da4c537a96168df8ed80c70dbf19cf42be95c Mon Sep 17 00:00:00 2001 From: Dave Sims Date: Wed, 19 Apr 2017 14:54:13 -0500 Subject: [PATCH 03/13] Default validates_around to not raise errors; update tests --- lib/action_logic/action_validation.rb | 2 +- spec/action_logic/action_task_spec.rb | 30 ++++++++++++++++++----- spec/action_logic/active_use_case_spec.rb | 20 +++++++-------- 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/lib/action_logic/action_validation.rb b/lib/action_logic/action_validation.rb index 6ae6d78..b028039 100644 --- a/lib/action_logic/action_validation.rb +++ b/lib/action_logic/action_validation.rb @@ -18,7 +18,7 @@ def validates_around!(args) end def validates_around(args) - @validates_around = args.merge(raise_action_logic_exception: true) + @validates_around = args.merge(raise_action_logic_exception: false) end def get_validates_before diff --git a/spec/action_logic/action_task_spec.rb b/spec/action_logic/action_task_spec.rb index bfa7389..0352e4a 100644 --- a/spec/action_logic/action_task_spec.rb +++ b/spec/action_logic/action_task_spec.rb @@ -21,6 +21,24 @@ module ActionLogic end describe "around validations" do + describe "required attributes and type validation" do + it "does not have errors if context has required keys and values are of the correct type" do + result = ValidateAroundTestTask.execute(Constants::VALID_ATTRIBUTES) + expect(result.message).to be_nil + expect(result.success?).to be_truthy + end + + it "does not raise error if context is missing required keys" do + expect { ValidateAroundTestTask.execute() }.to_not\ + raise_error(ActionLogic::MissingAttributeError) + end + + it "raises error if context has required keys but values are not of correct type" do + expect { ValidateAroundTestTask.execute(Constants::INVALID_ATTRIBUTES) }.to_not\ + raise_error(ActionLogic::AttributeTypeError) + end + end + describe "required attributes and type validation with bang" do it "does not raise error if context has required keys and values are of the correct type" do expect { ValidateAroundTestTaskWithBang.execute(Constants::VALID_ATTRIBUTES) }.to_not raise_error @@ -42,8 +60,8 @@ module ActionLogic expect { ValidateAroundCustomTypeTestTask.execute(:custom_type => CustomType1.new) }.to_not raise_error end - it "raises error if context has custom type attribute but value is not correct custom type" do - expect { ValidateAroundCustomTypeTestTask.execute(:custom_type => CustomType2.new) }.to\ + it "doesn't raise error if context has custom type attribute but value is not correct custom type" do + expect { ValidateAroundCustomTypeTestTask.execute(:custom_type => CustomType2.new) }.to_not\ raise_error(ActionLogic::AttributeTypeError) end end @@ -53,8 +71,8 @@ module ActionLogic expect { ValidateAroundPresenceTestTask.execute(:integer_test => 1) }.to_not raise_error end - it "raises error if context has required key but value is not defined when validation requires presence" do - expect { ValidateAroundPresenceTestTask.execute(:integer_test => nil) }.to\ + it "doesn't raise error if context has required key but value is not defined when validation requires presence" do + expect { ValidateAroundPresenceTestTask.execute(:integer_test => nil) }.to_not\ raise_error(ActionLogic::PresenceError) end end @@ -64,8 +82,8 @@ module ActionLogic expect { ValidateAroundCustomPresenceTestTask.execute(:array_test => [1]) }.to_not raise_error end - it "raises error if custom presence validation is not satisfied" do - expect { ValidateAroundCustomPresenceTestTask.execute(:array_test => []) }.to\ + it "doesn't raise error if custom presence validation is not satisfied" do + expect { ValidateAroundCustomPresenceTestTask.execute(:array_test => []) }.to_not\ raise_error(ActionLogic::PresenceError) end diff --git a/spec/action_logic/active_use_case_spec.rb b/spec/action_logic/active_use_case_spec.rb index 004df10..66d91b0 100644 --- a/spec/action_logic/active_use_case_spec.rb +++ b/spec/action_logic/active_use_case_spec.rb @@ -48,13 +48,13 @@ module ActionLogic expect { ValidateAroundTestUseCase.execute(Constants::VALID_ATTRIBUTES) }.to_not raise_error end - it "raises error if context is missing required keys" do - expect { ValidateAroundTestUseCase.execute() }.to\ + it "doesn't raise error if context is missing required keys" do + expect { ValidateAroundTestUseCase.execute() }.to_not\ raise_error(ActionLogic::MissingAttributeError) end - it "raises error if context has required keys but values are not of correct type" do - expect { ValidateAroundTestUseCase.execute(Constants::INVALID_ATTRIBUTES) }.to\ + it "doesn't raise error if context has required keys but values are not of correct type" do + expect { ValidateAroundTestUseCase.execute(Constants::INVALID_ATTRIBUTES) }.to_not\ raise_error(ActionLogic::AttributeTypeError) end end @@ -64,8 +64,8 @@ module ActionLogic expect { ValidateAroundCustomTypeTestUseCase.execute(:custom_type => CustomType1.new) }.to_not raise_error end - it "raises error if context has custom type attribute but value is not correct custom type" do - expect { ValidateAroundCustomTypeTestUseCase.execute(:custom_type => CustomType2.new) }.to\ + it "doesn't raise error if context has custom type attribute but value is not correct custom type" do + expect { ValidateAroundCustomTypeTestUseCase.execute(:custom_type => CustomType2.new) }.to_not\ raise_error(ActionLogic::AttributeTypeError) end end @@ -75,8 +75,8 @@ module ActionLogic expect { ValidateAroundPresenceTestUseCase.execute(:integer_test => 1) }.to_not raise_error end - it "raises error if context has required key but value is not defined when validation requires presence" do - expect { ValidateAroundPresenceTestUseCase.execute(:integer_test => nil) }.to\ + it "doesn't raise error if context has required key but value is not defined when validation requires presence" do + expect { ValidateAroundPresenceTestUseCase.execute(:integer_test => nil) }.to_not\ raise_error(ActionLogic::PresenceError) end end @@ -86,8 +86,8 @@ module ActionLogic expect { ValidateAroundCustomPresenceTestUseCase.execute(:array_test => [1]) }.to_not raise_error end - it "raises error if custom presence validation is not satisfied" do - expect { ValidateAroundCustomPresenceTestUseCase.execute(:array_test => []) }.to\ + it "doesn't raise error when custom presence validation is not satisfied" do + expect { ValidateAroundCustomPresenceTestUseCase.execute(:array_test => []) }.to_not\ raise_error(ActionLogic::PresenceError) end From 1e2018ad88b1d3fd14eb9194651a28fea155568b Mon Sep 17 00:00:00 2001 From: Dave Sims Date: Thu, 20 Apr 2017 07:47:30 -0500 Subject: [PATCH 04/13] Test for exceptions thrown with around validations for custom types --- spec/action_logic/action_task_spec.rb | 11 +++++++++++ spec/fixtures/tasks.rb | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/spec/action_logic/action_task_spec.rb b/spec/action_logic/action_task_spec.rb index 0352e4a..14d7382 100644 --- a/spec/action_logic/action_task_spec.rb +++ b/spec/action_logic/action_task_spec.rb @@ -66,6 +66,17 @@ module ActionLogic end end + describe "custom types with bang" do + it "allows validation against custom defined types" do + expect { ValidateAroundCustomTypeTestTaskWithBang.execute(:custom_type => CustomType1.new) }.to_not raise_error + end + + it "raises error if context has custom type attribute but value is not correct custom type" do + expect { ValidateAroundCustomTypeTestTaskWithBang.execute(:custom_type => CustomType2.new) }.to\ + raise_error(ActionLogic::AttributeTypeError) + end + end + describe "presence" do it "validates presence if presence is specified" do expect { ValidateAroundPresenceTestTask.execute(:integer_test => 1) }.to_not raise_error diff --git a/spec/fixtures/tasks.rb b/spec/fixtures/tasks.rb index c2cd429..e1eb0f8 100644 --- a/spec/fixtures/tasks.rb +++ b/spec/fixtures/tasks.rb @@ -28,6 +28,15 @@ def call end end +class ValidateAroundCustomTypeTestTaskWithBang + include ActionLogic::ActionTask + + validates_around! :custom_type => { :type => CustomType1, :presence => true } + + def call + end +end + class ValidateAroundCustomTypeTestTask include ActionLogic::ActionTask From 00578ba0e1f79cbb4a0541a94c969c29fc68dfc0 Mon Sep 17 00:00:00 2001 From: Dave Sims Date: Thu, 20 Apr 2017 07:50:59 -0500 Subject: [PATCH 05/13] Added tests for ValidateAroundPresenceTestTaskWithBang --- spec/action_logic/action_task_spec.rb | 11 +++++++++++ spec/fixtures/tasks.rb | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/spec/action_logic/action_task_spec.rb b/spec/action_logic/action_task_spec.rb index 14d7382..670def8 100644 --- a/spec/action_logic/action_task_spec.rb +++ b/spec/action_logic/action_task_spec.rb @@ -88,6 +88,17 @@ module ActionLogic end end + describe "presence with bang" do + it "validates presence if presence is specified" do + expect { ValidateAroundPresenceTestTaskWithBang.execute(:integer_test => 1) }.to_not raise_error + end + + it "raises error if context has required key but value is not defined when validation requires presence" do + expect { ValidateAroundPresenceTestTaskWithBang.execute(:integer_test => nil) }.to\ + raise_error(ActionLogic::PresenceError) + end + end + describe "custom presence" do it "allows custom presence validation if custom presence is defined" do expect { ValidateAroundCustomPresenceTestTask.execute(:array_test => [1]) }.to_not raise_error diff --git a/spec/fixtures/tasks.rb b/spec/fixtures/tasks.rb index e1eb0f8..659aa8f 100644 --- a/spec/fixtures/tasks.rb +++ b/spec/fixtures/tasks.rb @@ -55,6 +55,15 @@ def call end end +class ValidateAroundPresenceTestTaskWithBang + include ActionLogic::ActionTask + + validates_around! :integer_test => { :presence => true } + + def call + end +end + class ValidateAroundPresenceTestTask include ActionLogic::ActionTask From dbd4f6d94578250baa4c2aad59c2c9c3a4c3ec18 Mon Sep 17 00:00:00 2001 From: Dave Sims Date: Thu, 20 Apr 2017 07:56:49 -0500 Subject: [PATCH 06/13] Test for around validations with custom presence --- spec/action_logic/action_task_spec.rb | 16 ++++++++++++++++ spec/fixtures/tasks.rb | 22 ++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/spec/action_logic/action_task_spec.rb b/spec/action_logic/action_task_spec.rb index 670def8..69357d0 100644 --- a/spec/action_logic/action_task_spec.rb +++ b/spec/action_logic/action_task_spec.rb @@ -114,6 +114,22 @@ module ActionLogic raise_error(ActionLogic::UnrecognizablePresenceValidatorError) end end + + describe "custom presence with bang" do + it "allows custom presence validation if custom presence is defined" do + expect { ValidateAroundCustomPresenceTestTaskWithBang.execute(:array_test => [1]) }.to_not raise_error + end + + it "raises error if custom presence validation is not satisfied" do + expect { ValidateAroundCustomPresenceTestTaskWithBang.execute(:array_test => []) }.to\ + raise_error(ActionLogic::PresenceError) + end + + it "raises error if custom presence validation is not supported" do + expect { ValidateAroundUnrecognizablePresenceTestTaskWithBang.execute(:integer_test => 1) }.to\ + raise_error(ActionLogic::UnrecognizablePresenceValidatorError) + end + end end describe "before validations" do diff --git a/spec/fixtures/tasks.rb b/spec/fixtures/tasks.rb index 659aa8f..9be7846 100644 --- a/spec/fixtures/tasks.rb +++ b/spec/fixtures/tasks.rb @@ -46,6 +46,15 @@ def call end end +class ValidateAroundUnrecognizablePresenceTestTaskWithBang + include ActionLogic::ActionTask + + validates_around! :integer_test => { :presence => :true } + + def call + end +end + class ValidateAroundUnrecognizablePresenceTestTask include ActionLogic::ActionTask @@ -73,6 +82,19 @@ def call end end +class ValidateAroundCustomPresenceTestTaskWithBang + include ActionLogic::ActionTask + + validates_around! :array_test => { :presence => ->(array_test) { array_test.any? } } + + def call + end + + def tasks + [] + end +end + class ValidateAroundCustomPresenceTestTask include ActionLogic::ActionTask From dcf12deebd8f4de2ba692d99a48779d6db72ddd4 Mon Sep 17 00:00:00 2001 From: Dave Sims Date: Thu, 20 Apr 2017 08:09:46 -0500 Subject: [PATCH 07/13] Added test for ValidateAroundTestUseCaseWithBang required attributes --- spec/action_logic/active_use_case_spec.rb | 16 ++++++++++++++++ spec/fixtures/use_cases.rb | 14 ++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/spec/action_logic/active_use_case_spec.rb b/spec/action_logic/active_use_case_spec.rb index 66d91b0..88f447b 100644 --- a/spec/action_logic/active_use_case_spec.rb +++ b/spec/action_logic/active_use_case_spec.rb @@ -59,6 +59,22 @@ module ActionLogic end end + describe "required attributes and type validation with bang" do + it "does not raise error if context has required keys and values are of the correct type" do + expect { ValidateAroundTestUseCaseWithBang.execute(Constants::VALID_ATTRIBUTES) }.to_not raise_error + end + + it "raises error if context is missing required keys" do + expect { ValidateAroundTestUseCaseWithBang.execute() }.to\ + raise_error(ActionLogic::MissingAttributeError) + end + + it "raises error if context has required keys but values are not of correct type" do + expect { ValidateAroundTestUseCaseWithBang.execute(Constants::INVALID_ATTRIBUTES) }.to\ + raise_error(ActionLogic::AttributeTypeError) + end + end + describe "custom types" do it "allows validation against custom defined types" do expect { ValidateAroundCustomTypeTestUseCase.execute(:custom_type => CustomType1.new) }.to_not raise_error diff --git a/spec/fixtures/use_cases.rb b/spec/fixtures/use_cases.rb index b9fe6e7..99b45d0 100644 --- a/spec/fixtures/use_cases.rb +++ b/spec/fixtures/use_cases.rb @@ -63,6 +63,20 @@ def tasks end end +class ValidateAroundTestUseCaseWithBang + include ActionLogic::ActionUseCase + + validates_around! Constants::ALL_VALIDATIONS + + def call + end + + def tasks + [UseCaseTestTask1, + UseCaseTestTask2] + end +end + class ValidateAroundCustomTypeTestUseCase include ActionLogic::ActionUseCase From 0a90d514861e4683d63847a45c6b5da1cb6a2964 Mon Sep 17 00:00:00 2001 From: Dave Sims Date: Thu, 20 Apr 2017 08:15:18 -0500 Subject: [PATCH 08/13] ActionUseCase test for around validations with custom types --- spec/action_logic/active_use_case_spec.rb | 11 +++++++++++ spec/fixtures/use_cases.rb | 14 ++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/spec/action_logic/active_use_case_spec.rb b/spec/action_logic/active_use_case_spec.rb index 88f447b..679cfda 100644 --- a/spec/action_logic/active_use_case_spec.rb +++ b/spec/action_logic/active_use_case_spec.rb @@ -86,6 +86,17 @@ module ActionLogic end end + describe "custom types with bang" do + it "allows validation against custom defined types" do + expect { ValidateAroundCustomTypeTestUseCaseWithBang.execute(:custom_type => CustomType1.new) }.to_not raise_error + end + + it "raises error if context has custom type attribute but value is not correct custom type" do + expect { ValidateAroundCustomTypeTestUseCaseWithBang.execute(:custom_type => CustomType2.new) }.to\ + raise_error(ActionLogic::AttributeTypeError) + end + end + describe "presence" do it "validates presence if presence is specified" do expect { ValidateAroundPresenceTestUseCase.execute(:integer_test => 1) }.to_not raise_error diff --git a/spec/fixtures/use_cases.rb b/spec/fixtures/use_cases.rb index 99b45d0..fef10aa 100644 --- a/spec/fixtures/use_cases.rb +++ b/spec/fixtures/use_cases.rb @@ -91,6 +91,20 @@ def tasks end end +class ValidateAroundCustomTypeTestUseCaseWithBang + include ActionLogic::ActionUseCase + + validates_around! :custom_type => { :type => CustomType1, :presence => true } + + def call + end + + def tasks + [UseCaseTestTask1, + UseCaseTestTask2] + end +end + class ValidateAroundUnrecognizablePresenceTestUseCase include ActionLogic::ActionUseCase From 995ba45f91c259e410764c3a8d31792fab7d950e Mon Sep 17 00:00:00 2001 From: Dave Sims Date: Thu, 20 Apr 2017 08:17:30 -0500 Subject: [PATCH 09/13] ActiveUseCase around validation with presence test --- spec/action_logic/active_use_case_spec.rb | 11 +++++++++++ spec/fixtures/use_cases.rb | 14 ++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/spec/action_logic/active_use_case_spec.rb b/spec/action_logic/active_use_case_spec.rb index 679cfda..c7e5393 100644 --- a/spec/action_logic/active_use_case_spec.rb +++ b/spec/action_logic/active_use_case_spec.rb @@ -108,6 +108,17 @@ module ActionLogic end end + describe "presence with bang" do + it "validates presence if presence is specified" do + expect { ValidateAroundPresenceTestUseCaseWithBang.execute(:integer_test => 1) }.to_not raise_error + end + + it "raises error if context has required key but value is not defined when validation requires presence" do + expect { ValidateAroundPresenceTestUseCaseWithBang.execute(:integer_test => nil) }.to\ + raise_error(ActionLogic::PresenceError) + end + end + describe "custom presence" do it "allows custom presence validation if custom presence is defined" do expect { ValidateAroundCustomPresenceTestUseCase.execute(:array_test => [1]) }.to_not raise_error diff --git a/spec/fixtures/use_cases.rb b/spec/fixtures/use_cases.rb index fef10aa..832a7b3 100644 --- a/spec/fixtures/use_cases.rb +++ b/spec/fixtures/use_cases.rb @@ -119,6 +119,20 @@ def tasks end end +class ValidateAroundPresenceTestUseCaseWithBang + include ActionLogic::ActionUseCase + + validates_around! :integer_test => { :presence => true } + + def call + end + + def tasks + [UseCaseTestTask1, + UseCaseTestTask2] + end +end + class ValidateAroundPresenceTestUseCase include ActionLogic::ActionUseCase From 4172fd4e99118c4d09d826732595205578eb29de Mon Sep 17 00:00:00 2001 From: Dave Sims Date: Thu, 20 Apr 2017 08:22:46 -0500 Subject: [PATCH 10/13] ActiveUseCase tests for around validation with custom presence --- spec/action_logic/active_use_case_spec.rb | 16 +++++++++++++ spec/fixtures/use_cases.rb | 28 +++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/spec/action_logic/active_use_case_spec.rb b/spec/action_logic/active_use_case_spec.rb index c7e5393..d87328f 100644 --- a/spec/action_logic/active_use_case_spec.rb +++ b/spec/action_logic/active_use_case_spec.rb @@ -134,6 +134,22 @@ module ActionLogic raise_error(ActionLogic::UnrecognizablePresenceValidatorError) end end + + describe "custom presence with bang" do + it "allows custom presence validation if custom presence is defined" do + expect { ValidateAroundCustomPresenceTestUseCaseWithBang.execute(:array_test => [1]) }.to_not raise_error + end + + it "raises error when custom presence validation is not satisfied" do + expect { ValidateAroundCustomPresenceTestUseCaseWithBang.execute(:array_test => []) }.to\ + raise_error(ActionLogic::PresenceError) + end + + it "raises error if custom presence validation is not supported" do + expect { ValidateAroundUnrecognizablePresenceTestUseCaseWithBang.execute(:integer_test => 1) }.to\ + raise_error(ActionLogic::UnrecognizablePresenceValidatorError) + end + end end describe "before validations" do diff --git a/spec/fixtures/use_cases.rb b/spec/fixtures/use_cases.rb index 832a7b3..09a174e 100644 --- a/spec/fixtures/use_cases.rb +++ b/spec/fixtures/use_cases.rb @@ -105,6 +105,20 @@ def tasks end end +class ValidateAroundUnrecognizablePresenceTestUseCaseWithBang + include ActionLogic::ActionUseCase + + validates_around! :integer_test => { :presence => :true } + + def call + end + + def tasks + [UseCaseTestTask1, + UseCaseTestTask2] + end +end + class ValidateAroundUnrecognizablePresenceTestUseCase include ActionLogic::ActionUseCase @@ -147,6 +161,20 @@ def tasks end end +class ValidateAroundCustomPresenceTestUseCaseWithBang + include ActionLogic::ActionUseCase + + validates_around! :array_test => { :presence => ->(array_test) { array_test.any? } } + + def call + end + + def tasks + [UseCaseTestTask1, + UseCaseTestTask2] + end +end + class ValidateAroundCustomPresenceTestUseCase include ActionLogic::ActionUseCase From a78b5d20366944e9cfdc44257920340ed726ca12 Mon Sep 17 00:00:00 2001 From: Dave Sims Date: Wed, 15 Dec 2021 07:13:27 -0600 Subject: [PATCH 11/13] update to ruby 3 --- .ruby-version | 1 + Gemfile.lock | 50 ++++++++++++------------- spec/action_logic/configuration_spec.rb | 2 +- 3 files changed, 25 insertions(+), 28 deletions(-) create mode 100644 .ruby-version diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..b502146 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +3.0.2 diff --git a/Gemfile.lock b/Gemfile.lock index 70b6249..8d4f3d5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,43 +6,39 @@ PATH GEM remote: https://rubygems.org/ specs: - codecov (0.1.4) + codecov (0.2.12) json simplecov - url - coderay (1.1.0) - diff-lcs (1.2.5) + coderay (1.1.3) + diff-lcs (1.4.4) docile (1.1.5) - json (1.8.3) - method_source (0.8.2) - pry (0.10.3) - coderay (~> 1.1.0) - method_source (~> 0.8.1) - slop (~> 3.4) - rake (10.4.2) - rspec (3.3.0) - rspec-core (~> 3.3.0) - rspec-expectations (~> 3.3.0) - rspec-mocks (~> 3.3.0) - rspec-core (3.3.2) - rspec-support (~> 3.3.0) - rspec-expectations (3.3.1) + json (1.8.6) + method_source (1.0.0) + pry (0.14.1) + coderay (~> 1.1) + method_source (~> 1.0) + rake (10.5.0) + rspec (3.10.0) + rspec-core (~> 3.10.0) + rspec-expectations (~> 3.10.0) + rspec-mocks (~> 3.10.0) + rspec-core (3.10.1) + rspec-support (~> 3.10.0) + rspec-expectations (3.10.1) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.3.0) - rspec-mocks (3.3.2) + rspec-support (~> 3.10.0) + rspec-mocks (3.10.2) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.3.0) - rspec-support (3.3.0) + rspec-support (~> 3.10.0) + rspec-support (3.10.3) simplecov (0.10.0) docile (~> 1.1.0) json (~> 1.8) simplecov-html (~> 0.10.0) - simplecov-html (0.10.0) - slop (3.6.0) - url (0.3.2) + simplecov-html (0.10.2) PLATFORMS - ruby + x86_64-darwin-19 DEPENDENCIES action_logic! @@ -53,4 +49,4 @@ DEPENDENCIES simplecov (~> 0.10.0) BUNDLED WITH - 1.10.6 + 2.2.33 diff --git a/spec/action_logic/configuration_spec.rb b/spec/action_logic/configuration_spec.rb index e76a2a3..b753c0d 100644 --- a/spec/action_logic/configuration_spec.rb +++ b/spec/action_logic/configuration_spec.rb @@ -63,7 +63,7 @@ class CustomFormatter; end end it "uses a custom benchmark handler if one is provided" do - custom_benchmark_handler = -> { yield } + custom_benchmark_handler = -> { } described_class.configure do |config| config.benchmark_handler = custom_benchmark_handler From 8b75a60e639724a07a777cc59878e3d8a881b6b2 Mon Sep 17 00:00:00 2001 From: Dave Sims Date: Wed, 15 Dec 2021 07:44:53 -0600 Subject: [PATCH 12/13] Convert current before methods to bang! --- lib/action_logic/action_validation.rb | 2 +- spec/action_logic/active_use_case_spec.rb | 20 ++++++++++---------- spec/fixtures/coordinators.rb | 10 +++++----- spec/fixtures/tasks.rb | 12 ++++++------ spec/fixtures/use_cases.rb | 20 ++++++++++---------- 5 files changed, 32 insertions(+), 32 deletions(-) diff --git a/lib/action_logic/action_validation.rb b/lib/action_logic/action_validation.rb index b028039..0a6bd2d 100644 --- a/lib/action_logic/action_validation.rb +++ b/lib/action_logic/action_validation.rb @@ -5,7 +5,7 @@ module ActionLogic module ActionValidation module ClassMethods - def validates_before(args) + def validates_before!(args) @validates_before = args.merge(raise_action_logic_exception: true) end diff --git a/spec/action_logic/active_use_case_spec.rb b/spec/action_logic/active_use_case_spec.rb index d87328f..51fdc00 100644 --- a/spec/action_logic/active_use_case_spec.rb +++ b/spec/action_logic/active_use_case_spec.rb @@ -155,54 +155,54 @@ module ActionLogic describe "before validations" do describe "required attributes and type validation" do it "does not raise error if context has required keys and values are of the correct type" do - expect { ValidateBeforeTestUseCase.execute(Constants::VALID_ATTRIBUTES) }.to_not raise_error + expect { ValidateBeforeTestUseCaseWithBang.execute(Constants::VALID_ATTRIBUTES) }.to_not raise_error end it "raises error if context is missing required keys" do - expect { ValidateBeforeTestUseCase.execute() }.to\ + expect { ValidateBeforeTestUseCaseWithBang.execute() }.to\ raise_error(ActionLogic::MissingAttributeError) end it "raises error if context has required key but is not of correct type" do - expect { ValidateBeforeTestUseCase.execute(Constants::INVALID_ATTRIBUTES) }.to\ + expect { ValidateBeforeTestUseCaseWithBang.execute(Constants::INVALID_ATTRIBUTES) }.to\ raise_error(ActionLogic::AttributeTypeError) end end describe "custom types" do it "allows validation against custom defined types" do - expect { ValidateBeforeCustomTypeTestUseCase.execute(Constants::CUSTOM_TYPE_ATTRIBUTES1) }.to_not raise_error + expect { ValidateBeforeCustomTypeTestUseCaseWithBang.execute(Constants::CUSTOM_TYPE_ATTRIBUTES1) }.to_not raise_error end it "raises error if context has custom type attribute but value is not correct custom type" do - expect { ValidateBeforeCustomTypeTestUseCase.execute(Constants::CUSTOM_TYPE_ATTRIBUTES2) }.to\ + expect { ValidateBeforeCustomTypeTestUseCaseWithBang.execute(Constants::CUSTOM_TYPE_ATTRIBUTES2) }.to\ raise_error(ActionLogic::AttributeTypeError) end end describe "presence" do it "validates presence if presence is specified" do - expect { ValidateBeforePresenceTestUseCase.execute(:integer_test => 1) }.to_not raise_error + expect { ValidateBeforePresenceTestUseCaseWithBang.execute(:integer_test => 1) }.to_not raise_error end it "raises error if context has required key but value is not defined when validation requires presence" do - expect { ValidateBeforePresenceTestUseCase.execute(:integer_test => nil) }.to\ + expect { ValidateBeforePresenceTestUseCaseWithBang.execute(:integer_test => nil) }.to\ raise_error(ActionLogic::PresenceError) end end describe "custom presence" do it "allows custom presence validation if custom presence is defined" do - expect { ValidateBeforeCustomPresenceTestUseCase.execute(:array_test => [1]) }.to_not raise_error + expect { ValidateBeforeCustomPresenceTestUseCaseWithBang.execute(:array_test => [1]) }.to_not raise_error end it "raises error if custom presence validation is not satisfied" do - expect { ValidateBeforeCustomPresenceTestUseCase.execute(:array_test => []) }.to\ + expect { ValidateBeforeCustomPresenceTestUseCaseWithBang.execute(:array_test => []) }.to\ raise_error(ActionLogic::PresenceError) end it "raises error if custom presence validation is not supported" do - expect { ValidateBeforeUnrecognizablePresenceTestUseCase.execute(:integer_test => 1) }.to\ + expect { ValidateBeforeUnrecognizablePresenceTestUseCaseWithBang.execute(:integer_test => 1) }.to\ raise_error(ActionLogic::UnrecognizablePresenceValidatorError) end end diff --git a/spec/fixtures/coordinators.rb b/spec/fixtures/coordinators.rb index ac82e90..5d6c0c2 100644 --- a/spec/fixtures/coordinators.rb +++ b/spec/fixtures/coordinators.rb @@ -61,7 +61,7 @@ def plan class ValidateBeforeTestCoordinator include ActionLogic::ActionCoordinator - validates_before Constants::ALL_VALIDATIONS + validates_before! Constants::ALL_VALIDATIONS def call end @@ -80,7 +80,7 @@ def plan class ValidateBeforeCustomTypeTestCoordinator include ActionLogic::ActionCoordinator - validates_before Constants::CUSTOM_TYPE_VALIDATION1 + validates_before! Constants::CUSTOM_TYPE_VALIDATION1 def call end @@ -99,7 +99,7 @@ def plan class ValidateBeforePresenceTestCoordinator include ActionLogic::ActionCoordinator - validates_before Constants::PRESENCE_VALIDATION + validates_before! Constants::PRESENCE_VALIDATION def call end @@ -118,7 +118,7 @@ def plan class ValidateBeforeCustomPresenceTestCoordinator include ActionLogic::ActionCoordinator - validates_before Constants::CUSTOM_PRESENCE_VALIDATION + validates_before! Constants::CUSTOM_PRESENCE_VALIDATION def call end @@ -137,7 +137,7 @@ def plan class ValidateBeforeUnrecognizablePresenceTestCoordinator include ActionLogic::ActionCoordinator - validates_before :integer_test => { :presence => :true } + validates_before! :integer_test => { :presence => :true } def call end diff --git a/spec/fixtures/tasks.rb b/spec/fixtures/tasks.rb index 9be7846..efe33bb 100644 --- a/spec/fixtures/tasks.rb +++ b/spec/fixtures/tasks.rb @@ -111,7 +111,7 @@ def tasks class ValidateBeforeTestTask include ActionLogic::ActionTask - validates_before Constants::ALL_VALIDATIONS + validates_before! Constants::ALL_VALIDATIONS def call end @@ -120,7 +120,7 @@ def call class ValidateBeforeCustomTypeTestTask include ActionLogic::ActionTask - validates_before :custom_type => { :type => CustomType1, :presence => true } + validates_before! :custom_type => { :type => CustomType1, :presence => true } def call end @@ -129,7 +129,7 @@ def call class ValidateBeforeUnrecognizablePresenceTestTask include ActionLogic::ActionTask - validates_before :integer_test => { :presence => :true } + validates_before! :integer_test => { :presence => :true } def call end @@ -138,7 +138,7 @@ def call class ValidateBeforePresenceTestTask include ActionLogic::ActionTask - validates_before :integer_test => { :presence => true } + validates_before! :integer_test => { :presence => true } def call end @@ -147,7 +147,7 @@ def call class ValidateBeforeCustomPresenceTestTask include ActionLogic::ActionTask - validates_before :array_test => { :presence => ->(array_test) { array_test.any? } } + validates_before! :array_test => { :presence => ->(array_test) { array_test.any? } } def call end @@ -285,7 +285,7 @@ def error(e) class ErrorHandlerInvalidAttributesBeforeTestTask include ActionLogic::ActionTask - validates_before Constants::ALL_VALIDATIONS + validates_before! Constants::ALL_VALIDATIONS def call raise diff --git a/spec/fixtures/use_cases.rb b/spec/fixtures/use_cases.rb index 09a174e..f6c4605 100644 --- a/spec/fixtures/use_cases.rb +++ b/spec/fixtures/use_cases.rb @@ -189,10 +189,10 @@ def tasks end end -class ValidateBeforeTestUseCase +class ValidateBeforeTestUseCaseWithBang include ActionLogic::ActionUseCase - validates_before Constants::ALL_VALIDATIONS + validates_before! Constants::ALL_VALIDATIONS def call end @@ -203,10 +203,10 @@ def tasks end end -class ValidateBeforePresenceTestUseCase +class ValidateBeforePresenceTestUseCaseWithBang include ActionLogic::ActionUseCase - validates_before Constants::PRESENCE_VALIDATION + validates_before! Constants::PRESENCE_VALIDATION def call end @@ -217,10 +217,10 @@ def tasks end end -class ValidateBeforeCustomPresenceTestUseCase +class ValidateBeforeCustomPresenceTestUseCaseWithBang include ActionLogic::ActionUseCase - validates_before Constants::CUSTOM_PRESENCE_VALIDATION + validates_before! Constants::CUSTOM_PRESENCE_VALIDATION def call end @@ -231,10 +231,10 @@ def tasks end end -class ValidateBeforeCustomTypeTestUseCase +class ValidateBeforeCustomTypeTestUseCaseWithBang include ActionLogic::ActionUseCase - validates_before Constants::CUSTOM_TYPE_VALIDATION1 + validates_before! Constants::CUSTOM_TYPE_VALIDATION1 def call end @@ -245,10 +245,10 @@ def tasks end end -class ValidateBeforeUnrecognizablePresenceTestUseCase +class ValidateBeforeUnrecognizablePresenceTestUseCaseWithBang include ActionLogic::ActionUseCase - validates_before :integer_test => { :presence => :true } + validates_before! :integer_test => { :presence => :true } def call end From 3625f2cecca9b14580008f5786b25b90c2e5a2f7 Mon Sep 17 00:00:00 2001 From: Dave Sims Date: Wed, 15 Dec 2021 08:10:19 -0600 Subject: [PATCH 13/13] Adding tests for before validation, no bang --- lib/action_logic/action_validation.rb | 4 ++ spec/action_logic/active_use_case_spec.rb | 19 +++++- spec/fixtures/use_cases.rb | 72 ++++++++++++++++++++++- 3 files changed, 93 insertions(+), 2 deletions(-) diff --git a/lib/action_logic/action_validation.rb b/lib/action_logic/action_validation.rb index 0a6bd2d..76e091d 100644 --- a/lib/action_logic/action_validation.rb +++ b/lib/action_logic/action_validation.rb @@ -9,6 +9,10 @@ def validates_before!(args) @validates_before = args.merge(raise_action_logic_exception: true) end + def validates_before(args) + @validates_before = args.merge(raise_action_logic_exception: false) + end + def validates_after(args) @validates_after = args.merge(raise_action_logic_exception: true) end diff --git a/spec/action_logic/active_use_case_spec.rb b/spec/action_logic/active_use_case_spec.rb index 51fdc00..6f31954 100644 --- a/spec/action_logic/active_use_case_spec.rb +++ b/spec/action_logic/active_use_case_spec.rb @@ -153,7 +153,7 @@ module ActionLogic end describe "before validations" do - describe "required attributes and type validation" do + describe "required attributes and type validation with bang" do it "does not raise error if context has required keys and values are of the correct type" do expect { ValidateBeforeTestUseCaseWithBang.execute(Constants::VALID_ATTRIBUTES) }.to_not raise_error end @@ -169,6 +169,23 @@ module ActionLogic end end + describe "required attributes and type validation" do + it "does not raise error if context has required keys and values are of the correct type" do + expect { ValidateBeforeTestUseCase.execute(Constants::VALID_ATTRIBUTES) }.to_not raise_error + end + + it "doesn't raise error if context is missing required keys" do + expect { ValidateBeforeTestUseCase.execute() }.to_not\ + raise_error(ActionLogic::MissingAttributeError) + end + + it "raises error if context has required key but is not of correct type" do + expect { ValidateBeforeTestUseCase.execute(Constants::INVALID_ATTRIBUTES) }.to_not\ + raise_error(ActionLogic::AttributeTypeError) + end + end + + describe "custom types" do it "allows validation against custom defined types" do expect { ValidateBeforeCustomTypeTestUseCaseWithBang.execute(Constants::CUSTOM_TYPE_ATTRIBUTES1) }.to_not raise_error diff --git a/spec/fixtures/use_cases.rb b/spec/fixtures/use_cases.rb index f6c4605..d1f6feb 100644 --- a/spec/fixtures/use_cases.rb +++ b/spec/fixtures/use_cases.rb @@ -189,6 +189,76 @@ def tasks end end +class ValidateBeforeTestUseCase + include ActionLogic::ActionUseCase + + validates_before Constants::ALL_VALIDATIONS + + def call + end + + def tasks + [UseCaseTestTask1, + UseCaseTestTask2] + end +end + +class ValidateBeforePresenceTestUseCase + include ActionLogic::ActionUseCase + + validates_before Constants::PRESENCE_VALIDATION + + def call + end + + def tasks + [UseCaseTestTask1, + UseCaseTestTask2] + end +end + +class ValidateBeforeCustomPresenceTestUseCase + include ActionLogic::ActionUseCase + + validates_before Constants::CUSTOM_PRESENCE_VALIDATION + + def call + end + + def tasks + [UseCaseTestTask1, + UseCaseTestTask2] + end +end + +class ValidateBeforeCustomTypeTestUseCase + include ActionLogic::ActionUseCase + + validates_before Constants::CUSTOM_TYPE_VALIDATION1 + + def call + end + + def tasks + [UseCaseTestTask1, + UseCaseTestTask2] + end +end + +class ValidateBeforeUnrecognizablePresenceTestUseCase + include ActionLogic::ActionUseCase + + validates_before :integer_test => { :presence => :true } + + def call + end + + def tasks + [UseCaseTestTask1, + UseCaseTestTask2] + end +end + class ValidateBeforeTestUseCaseWithBang include ActionLogic::ActionUseCase @@ -248,7 +318,7 @@ def tasks class ValidateBeforeUnrecognizablePresenceTestUseCaseWithBang include ActionLogic::ActionUseCase - validates_before! :integer_test => { :presence => :true } + validates_before :integer_test => { :presence => :true } def call end