Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Bang Validations #10

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Adding tests for before validation, no bang
davesims committed Dec 15, 2021
commit 3625f2cecca9b14580008f5786b25b90c2e5a2f7
4 changes: 4 additions & 0 deletions lib/action_logic/action_validation.rb
Original file line number Diff line number Diff line change
@@ -9,6 +9,10 @@ def validates_before!(args)
@validates_before = args.merge(raise_action_logic_exception: true)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will eventually eliminate this dupe, but for now I just wanted to see what it looked like to use the validations hash to signal to the validation classes whether to raise an exception or not.

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)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how I feel about using an extra attribute to signal the validation classes. This is just a rough draft. If this is the approach to take, it needs to be a very specific name to avoid potential collisions, hence "raise_action_logic_exception".

end
19 changes: 18 additions & 1 deletion spec/action_logic/active_use_case_spec.rb
Original file line number Diff line number Diff line change
@@ -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
72 changes: 71 additions & 1 deletion spec/fixtures/use_cases.rb
Original file line number Diff line number Diff line change
@@ -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