-
Notifications
You must be signed in to change notification settings - Fork 8
Cast AccountNumber string to integer #158
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
base: development
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,15 @@ module Types | |
| AccountNumber = Strict::Int | ||
| .constrained(gteq: 0, lteq: 9999) | ||
| .optional | ||
| .constructor do |input| | ||
| unless input.nil? || input.to_s.empty? | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these cases tested? |
||
| Integer(input) | ||
| else | ||
| input | ||
| end | ||
| rescue ArgumentError | ||
| input | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this case tested?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not all valid/invalid cases are tested. We could test for everything but right now, with my suggested changes, we do test the two main cases: Things that respond to to_s and has a valid value will convert, the rest will cause a Dry types constraint error... |
||
| end | ||
|
|
||
| ArticleType = Strict::String | ||
| .constrained(included_in: ArticleTypes.values) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,4 +28,16 @@ | |
| context 'when AccountNumber created with a negative number' do | ||
| include_examples 'raises ConstraintError', -1 | ||
|
||
| end | ||
|
|
||
| context 'when AccountNumber created with an invalid string' do | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Firstly this should be Basically this test could be anything that we don't explicitly allow, so make it
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it should be |
||
| include_examples 'raises ConstraintError', 'foo' | ||
| end | ||
|
|
||
| context 'when AccountNumber created with valid string' do | ||
| subject { klass['1234'] } | ||
|
|
||
| it 'casts it to a number' do | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't really care HOW it does something here, just that we get the correct result. So this could be: context 'created with valid string' do
subject { klass['1234'] }
it { is_expected.to eq 1234 }
endUtilizing the readability of RSpecs one line syntax.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually if we consider how we do things we can change it to |
||
| is_expected.to eq 1234 | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This constructor is now 9 lines. Can we create a separate method for this? I'm pretty sure Rubocop will complain about this :)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Look ma, one-liner ...
Optimizing the logic for brevity and skipping the rescue. No one should send random user input directly at this and if they do that is their problem. We are not end user facing, we are developer facing and can expect that the input has already been sanitised. So allow for reasonable things - like
niland string/symbol versions - where it makes sense, but do not try to defend from all bad inputs.