diff options
author | Adam Mueller <adam.mathys@gmail.com> | 2020-07-21 14:09:17 -0700 |
---|---|---|
committer | Adam Mueller <adam.mathys@gmail.com> | 2020-07-21 14:09:17 -0700 |
commit | 13d41349671fe914d93a17eade80911aed887ca1 (patch) | |
tree | 7b4ae8c12d9528c7527095ab14d2769e251db166 | |
parent | e6af942deb798793324bdf8924b9c8e144184374 (diff) |
Fix handling of no results for address validation
According to the documentation, and some testing, the API will raise a
Taxjar::Error::NotFound error if the API finds no addresses. Instead of
raising this error to others, we should handle it ourselves and return
nil/an empty array like we originally intended.
https://developers.taxjar.com/api/reference/#post-validate-an-address
-rw-r--r-- | lib/super_good/solidus_taxjar/addresses.rb | 2 | ||||
-rw-r--r-- | spec/super_good/solidus_taxjar/addresses_spec.rb | 56 |
2 files changed, 42 insertions, 16 deletions
diff --git a/lib/super_good/solidus_taxjar/addresses.rb b/lib/super_good/solidus_taxjar/addresses.rb index 0993a03..de91af9 100644 --- a/lib/super_good/solidus_taxjar/addresses.rb +++ b/lib/super_good/solidus_taxjar/addresses.rb @@ -47,6 +47,8 @@ module SuperGood def taxjar_addresses(spree_address) api.validate_spree_address(spree_address) + rescue Taxjar::Error::NotFound + [] end def us diff --git a/spec/super_good/solidus_taxjar/addresses_spec.rb b/spec/super_good/solidus_taxjar/addresses_spec.rb index a5042d1..9a149b8 100644 --- a/spec/super_good/solidus_taxjar/addresses_spec.rb +++ b/spec/super_good/solidus_taxjar/addresses_spec.rb @@ -41,20 +41,25 @@ RSpec.describe SuperGood::SolidusTaxJar::Addresses do instance_double ::SuperGood::SolidusTaxJar::API } - before do - allow(dummy_api) - .to receive(:validate_spree_address) - .with(spree_address) - .and_return(results) - end - context "when there are no possibilities for the address" do - let(:results) { [] } + before do + allow(dummy_api) + .to receive(:validate_spree_address) + .with(spree_address) + .and_raise(Taxjar::Error::NotFound) + end it { is_expected.to be_nil } end context "when there is one possibility for the address" do + before do + allow(dummy_api) + .to receive(:validate_spree_address) + .with(spree_address) + .and_return(results) + end + let(:results) { [ Taxjar::Address.new( @@ -85,6 +90,13 @@ RSpec.describe SuperGood::SolidusTaxJar::Addresses do end context "when there are multiple possibilities for the address" do + before do + allow(dummy_api) + .to receive(:validate_spree_address) + .with(spree_address) + .and_return(results) + end + let(:results) { [ Taxjar::Address.new( @@ -162,20 +174,25 @@ RSpec.describe SuperGood::SolidusTaxJar::Addresses do instance_double ::SuperGood::SolidusTaxJar::API } - before do - allow(dummy_api) - .to receive(:validate_spree_address) - .with(spree_address) - .and_return(results) - end - context "when there are no possibilities for the address" do - let(:results) { [] } + before do + allow(dummy_api) + .to receive(:validate_spree_address) + .with(spree_address) + .and_raise(Taxjar::Error::NotFound) + end it { is_expected.to be_empty } end context "when there is one possibility for the address" do + before do + allow(dummy_api) + .to receive(:validate_spree_address) + .with(spree_address) + .and_return(results) + end + let(:results) { [ Taxjar::Address.new( @@ -206,6 +223,13 @@ RSpec.describe SuperGood::SolidusTaxJar::Addresses do end context "when there are multiple possibilities for the address" do + before do + allow(dummy_api) + .to receive(:validate_spree_address) + .with(spree_address) + .and_return(results) + end + let(:results) { [ Taxjar::Address.new( |