Rely on Spree's default StockLocation

This commit is contained in:
Pau Perez
2019-01-11 15:46:41 +01:00
parent 730d5ff7d6
commit 10a678bdfe
3 changed files with 46 additions and 8 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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