Abstract OFN's default stock location into a class

This commit is contained in:
Pau Perez
2019-01-11 15:52:09 +01:00
parent 0eefd2455c
commit 9ebe112bcc
3 changed files with 50 additions and 1 deletions

View File

@@ -0,0 +1,15 @@
# 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
def self.create!
country = Spree::Country.find_by_iso(ENV['DEFAULT_COUNTRY_CODE'])
state = country.states.first
Spree::StockLocation.create!(name: NAME, country_id: country.id, state_id: state.id)
end
def self.destroy_all
Spree::StockLocation.where(name: NAME).destroy_all
end
end

View File

@@ -656,8 +656,10 @@ end
FactoryBot.modify do
factory :stock_location, class: Spree::StockLocation do
name { 'default' }
# keeps the test stock_location unique
initialize_with { Spree::StockLocation.find_or_create_by_name(name)}
initialize_with { DefaultStockLocation.find_or_create }
end
factory :shipment, class: Spree::Shipment do

View File

@@ -0,0 +1,32 @@
require 'spec_helper'
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')
end
it 'sets the location in the default country' do
default_country = Spree::Country.find_by_iso(ENV['DEFAULT_COUNTRY_CODE'])
stock_location = described_class.create!
expect(stock_location.country).to eq(default_country)
end
it 'sets the first state in the country' do
default_country = Spree::Country.find_by_iso(ENV['DEFAULT_COUNTRY_CODE'])
stock_location = described_class.create!
expect(stock_location.state).to eq(default_country.states.first)
end
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')
expect { described_class.destroy_all }
.to change { Spree::StockLocation.count }.from(2).to(0)
end
end
end