From 10a678bdfeb8a0e2964810a6263601226ab694f6 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Fri, 11 Jan 2019 15:46:41 +0100 Subject: [PATCH] Rely on Spree's default StockLocation --- app/services/default_stock_location.rb | 6 ++- spec/factories.rb | 5 ++- spec/services/default_stock_location_spec.rb | 43 +++++++++++++++++--- 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/app/services/default_stock_location.rb b/app/services/default_stock_location.rb index e2df33cb71..9269f17743 100644 --- a/app/services/default_stock_location.rb +++ b/app/services/default_stock_location.rb @@ -1,7 +1,7 @@ # Encapsulates the concept of default stock location that OFN has, as explained # in https://github.com/openfoodfoundation/openfoodnetwork/wiki/Spree-Upgrade%3A-Stock-locations class DefaultStockLocation - NAME = 'OFN default'.freeze + NAME = 'default'.freeze def self.create! country = Spree::Country.find_by_iso(ENV['DEFAULT_COUNTRY_CODE']) @@ -12,4 +12,8 @@ class DefaultStockLocation def self.destroy_all Spree::StockLocation.where(name: NAME).destroy_all end + + def self.find_or_create + Spree::StockLocation.find_or_create_by_name(NAME) + end end diff --git a/spec/factories.rb b/spec/factories.rb index 582dd4d98b..742f943ed7 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -656,10 +656,11 @@ end FactoryBot.modify do factory :stock_location, class: Spree::StockLocation do - name { 'default' } - # keeps the test stock_location unique initialize_with { DefaultStockLocation.find_or_create } + + # Ensures the name attribute is not assigned after instantiating the default location + transient { name 'default' } end factory :shipment, class: Spree::Shipment do diff --git a/spec/services/default_stock_location_spec.rb b/spec/services/default_stock_location_spec.rb index b2b9691267..8d91d7da83 100644 --- a/spec/services/default_stock_location_spec.rb +++ b/spec/services/default_stock_location_spec.rb @@ -4,7 +4,7 @@ describe DefaultStockLocation do describe '.create!' do it "names the location 'OFN default'" do stock_location = described_class.create! - expect(stock_location.name).to eq('OFN default') + expect(stock_location.name).to eq('default') end it 'sets the location in the default country' do @@ -21,12 +21,45 @@ describe DefaultStockLocation do end describe '.destroy_all' do - it "removes all stock locations named 'OFN default'" do - create(:stock_location, name: 'OFN default') - create(:stock_location, name: 'OFN default') + it "removes all stock locations named 'default'" do + create(:stock_location, name: 'default') expect { described_class.destroy_all } - .to change { Spree::StockLocation.count }.from(2).to(0) + .to change { Spree::StockLocation.count }.to(0) + end + end + + describe '.find_or_create' do + context 'when a location named default already exists' do + let!(:location) do + country = create(:country) + state = create(:state, country: country) + Spree::StockLocation.create!( + name: 'default', + country_id: country.id, + state_id: state.id + ) + end + + it 'returns the location' do + expect(described_class.find_or_create).to eq(location) + end + + it 'does not create any other location' do + expect { described_class.find_or_create }.not_to change(Spree::StockLocation, :count) + end + end + + context 'when a location named default does not exist' do + it 'returns the location' do + location = described_class.find_or_create + expect(location.name).to eq('default') + end + + it 'does not create any other location' do + expect { described_class.find_or_create } + .to change(Spree::StockLocation, :count).from(0).to(1) + end end end end