From d6c099b7033a336376d74b2568c5b4b21ba38cd0 Mon Sep 17 00:00:00 2001 From: Samuel Nilsson Date: Mon, 4 Jun 2018 20:46:21 +0200 Subject: [PATCH 1/3] Cast AccountNumber string to integer --- lib/fortnox/api/types.rb | 1 + spec/fortnox/api/types/account_number_spec.rb | 12 +++++------- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/fortnox/api/types.rb b/lib/fortnox/api/types.rb index ce9c2ef6..2cea8006 100644 --- a/lib/fortnox/api/types.rb +++ b/lib/fortnox/api/types.rb @@ -38,6 +38,7 @@ module Types AccountNumber = Strict::Int .constrained(gteq: 0, lteq: 9999) .optional + .constructor(Dry::Types::Coercions::Form.method(:to_int)) ArticleType = Strict::String .constrained(included_in: ArticleTypes.values) diff --git a/spec/fortnox/api/types/account_number_spec.rb b/spec/fortnox/api/types/account_number_spec.rb index f5d6af46..ecb6701b 100644 --- a/spec/fortnox/api/types/account_number_spec.rb +++ b/spec/fortnox/api/types/account_number_spec.rb @@ -13,19 +13,17 @@ it { is_expected.to be_nil } end - context 'when AccountNumber created with empty string' do - include_examples 'raises ConstraintError', '' - end - context 'when AccountNumber created with valid number' do - include_examples 'equals input', 1234 + subject { klass['1234'] } + + it { is_expected.to eq 1234 } end context 'when AccountNumber created with a too large number' do - include_examples 'raises ConstraintError', 10_000 + include_examples 'raises ConstraintError', '10000' end context 'when AccountNumber created with a negative number' do - include_examples 'raises ConstraintError', -1 + include_examples 'raises ConstraintError', '-1' end end From 26042da0017e2902df2ce836b6bb7927e811f3ea Mon Sep 17 00:00:00 2001 From: Samuel Nilsson Date: Fri, 15 Jun 2018 08:33:04 +0200 Subject: [PATCH 2/3] Improve tests for AccountNumber --- spec/fortnox/api/types/account_number_spec.rb | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/spec/fortnox/api/types/account_number_spec.rb b/spec/fortnox/api/types/account_number_spec.rb index ecb6701b..f48f33a3 100644 --- a/spec/fortnox/api/types/account_number_spec.rb +++ b/spec/fortnox/api/types/account_number_spec.rb @@ -14,16 +14,26 @@ end context 'when AccountNumber created with valid number' do - subject { klass['1234'] } - - it { is_expected.to eq 1234 } + include_examples 'equals input', 1234 end context 'when AccountNumber created with a too large number' do - include_examples 'raises ConstraintError', '10000' + include_examples 'raises ConstraintError', 10_000 end context 'when AccountNumber created with a negative number' do - include_examples 'raises ConstraintError', '-1' + include_examples 'raises ConstraintError', -1 + end + + context 'when AccountNumber created with an invalid string' do + include_examples 'raises ConstraintError', 'foo' + end + + context 'when AccountNumber created with valid string' do + subject { klass['1234'] } + + it 'casts it to a number' do + is_expected.to eq 1234 + end end end From 51bbce1987c0fdc4e13e830a512841346bbc8887 Mon Sep 17 00:00:00 2001 From: Samuel Nilsson Date: Fri, 15 Jun 2018 17:28:03 +0200 Subject: [PATCH 3/3] Raise in case of empty string --- lib/fortnox/api/types.rb | 10 +++++++++- spec/fortnox/api/types/account_number_spec.rb | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/fortnox/api/types.rb b/lib/fortnox/api/types.rb index 2cea8006..6798e9d6 100644 --- a/lib/fortnox/api/types.rb +++ b/lib/fortnox/api/types.rb @@ -38,7 +38,15 @@ module Types AccountNumber = Strict::Int .constrained(gteq: 0, lteq: 9999) .optional - .constructor(Dry::Types::Coercions::Form.method(:to_int)) + .constructor do |input| + unless input.nil? || input.to_s.empty? + Integer(input) + else + input + end + rescue ArgumentError + input + end ArticleType = Strict::String .constrained(included_in: ArticleTypes.values) diff --git a/spec/fortnox/api/types/account_number_spec.rb b/spec/fortnox/api/types/account_number_spec.rb index f48f33a3..e7d5e84c 100644 --- a/spec/fortnox/api/types/account_number_spec.rb +++ b/spec/fortnox/api/types/account_number_spec.rb @@ -13,6 +13,10 @@ it { is_expected.to be_nil } end + context 'when AccountNumber created with empty string' do + include_examples 'raises ConstraintError', '' + end + context 'when AccountNumber created with valid number' do include_examples 'equals input', 1234 end