Do not treat ResetAbsent as a decorator anymore

This fully encapsulates it's logic and reduces its tight coupling with
EntryProcessor.
This commit is contained in:
Pau Perez
2018-09-25 21:03:01 +02:00
parent 95ae18a1ba
commit f04fa4ed63
4 changed files with 98 additions and 162 deletions

View File

@@ -69,11 +69,7 @@ module ProductImport
end
def reset_absent
@reset_absent ||= ResetAbsent.new(
self,
Settings.new(import_settings),
strategy_factory
)
@reset_absent ||= ResetAbsent.new(self, settings, strategy)
end
def strategy_factory

View File

@@ -1,32 +1,30 @@
require 'delegate'
module ProductImport
class ResetAbsent < SimpleDelegator
class ResetAbsent
attr_reader :products_reset_count
def initialize(decorated, settings, strategy_factory)
super(decorated)
@products_reset_count = 0
def initialize(entry_processor, settings, strategy)
@entry_processor = entry_processor
@settings = settings
@strategy_factory = strategy_factory
@strategy = strategy
@products_reset_count = 0
end
# For selected enterprises; set stock to zero for all products/inventory
# that were not listed in the newly uploaded spreadsheet
def call
settings.enterprises_to_reset.each do |enterprise_id|
next unless permission_by_id?(enterprise_id)
next unless entry_processor.permission_by_id?(enterprise_id)
strategy << enterprise_id.to_i
end
reset_stock if strategy.supplier_ids
reset_stock if strategy.supplier_ids.present?
end
private
attr_reader :settings, :strategy_factory
attr_reader :settings, :strategy, :entry_processor
def reset_stock
@products_reset_count += strategy.reset(

View File

@@ -67,31 +67,65 @@ describe ProductImport::EntryProcessor do
ProductImport::Settings,
data_for_stock_reset?: true,
reset_all_absent?: true,
importing_into_inventory?: true
updated_ids: [1]
)
end
context 'when importing into inventory' do
let(:strategy) { instance_double(ProductImport::InventoryReset) }
it 'delegates to ResetAbsent' do
entry_processor.reset_absent_items
before do
allow(settings).to receive(:importing_into_inventory?) { true }
expect(ProductImport::ResetAbsent)
.to have_received(:new)
.with(entry_processor, settings, ProductImport::InventoryReset)
allow(ProductImport::InventoryReset)
.to receive(:new).with([1]) { strategy }
end
it 'delegates to ResetAbsent passing the appropriate strategy' do
entry_processor.reset_absent_items
expect(ProductImport::ResetAbsent)
.to have_received(:new)
.with(entry_processor, settings, strategy)
end
end
context 'when not importing into inventory' do
let(:strategy) { instance_double(ProductImport::ProductsReset) }
before do
allow(settings).to receive(:importing_into_inventory?) { false }
allow(ProductImport::ProductsReset)
.to receive(:new).with([1]) { strategy }
end
it 'delegates to ResetAbsent passing the appropriate strategy' do
entry_processor.reset_absent_items
expect(ProductImport::ResetAbsent)
.to have_received(:new)
.with(entry_processor, settings, strategy)
end
end
end
end
describe '#products_reset_count' do
let(:reset_absent) { instance_double(ProductImport::ResetAbsent) }
let(:settings) do
instance_double(
ProductImport::Settings,
importing_into_inventory?: false,
updated_ids: [1]
)
end
before do
allow(ProductImport::Settings).to receive(:new) { settings }
allow(ProductImport::ResetAbsent)
.to receive(:new)
.and_return(reset_absent)
.to receive(:new).and_return(reset_absent)
allow(reset_absent).to receive(:products_reset_count)
allow(import_settings).to receive(:[]).with(:settings)
end
it 'delegates to ResetAbsent' do

View File

@@ -1,28 +1,10 @@
require 'spec_helper'
describe ProductImport::ResetAbsent do
let(:importer) { double(:importer) }
let(:validator) { double(:validator) }
let(:spreadsheet_data) { double(:spreadsheet_data) }
let(:editable_enterprises) { double(:editable_enterprises) }
let(:import_time) { double(:import_time) }
let(:updated_ids) { double(:updated_ids) }
let(:import_settings) { double(:import_settings) }
let(:entry_processor) do
ProductImport::EntryProcessor.new(
importer,
validator,
import_settings,
spreadsheet_data,
editable_enterprises,
import_time,
updated_ids
)
end
let(:entry_processor) { instance_double(ProductImport::EntryProcessor) }
let(:reset_absent) do
described_class.new(entry_processor, settings, strategy_factory)
described_class.new(entry_processor, settings, strategy)
end
describe '#call' do
@@ -35,10 +17,8 @@ describe ProductImport::ResetAbsent do
)
end
let(:strategy_factory) { double(:strategy_factory) }
before do
allow(import_settings).to receive(:[]).with(:settings)
let(:strategy) do
instance_double(ProductImport::InventoryReset, supplier_ids: [])
end
it 'returns nil' do
@@ -47,161 +27,89 @@ describe ProductImport::ResetAbsent do
end
context 'when there are updated_ids and enterprises_to_reset' do
let(:enterprise) { instance_double(Enterprise, id: 1) }
let(:settings) do
instance_double(
ProductImport::Settings,
reset_all_absent?: true,
data_for_stock_reset?: true,
updated_ids: [0],
enterprises_to_reset: [enterprise.id.to_s]
)
end
let(:strategy) { instance_double(ProductImport::ProductsReset) }
before do
allow(entry_processor)
.to receive(:permission_by_id?).with(enterprise.id.to_s) { true }
allow(strategy).to receive(:<<).with(enterprise.id)
allow(strategy).to receive(:supplier_ids) { [enterprise.id] }
allow(strategy).to receive(:reset).with([0], [enterprise.id]) { 2 }
end
context 'and not importing into inventory' do
let(:variant) { create(:variant) }
let(:enterprise) { variant.product.supplier }
let(:strategy_factory) { ProductImport::ProductsReset }
before do
allow(entry_processor)
.to receive(:importing_into_inventory?) { false }
end
it 'returns the number of products reset' do
expect(reset_absent.call).to eq(2)
end
xit 'resets the products of the specified suppliers' do
reset_absent.call
expect(strategy.supplier_ids).to eq([enterprise.id])
end
it 'returns the number of products reset' do
expect(reset_absent.call).to eq(2)
end
context 'and importing into inventory' do
let(:variant) { create(:variant) }
let(:enterprise) { variant.product.supplier }
let(:variant_override) do
create(:variant_override, variant: variant, hub: enterprise)
end
let(:strategy_factory) { ProductImport::InventoryReset }
before do
variant_override
allow(entry_processor)
.to receive(:permission_by_id?).with(enterprise.id.to_s) { true }
end
before do
allow(entry_processor)
.to receive(:importing_into_inventory?) { true }
end
it 'returns the number of products reset' do
expect(reset_absent.call).to eq(1)
end
xit 'resets the inventories of the specified suppliers' do
reset_absent.call
expect(strategy.supplier_ids).to eq([enterprise.id])
end
it 'resets the products of the specified suppliers' do
expect(strategy).to receive(:reset).with([0], [enterprise.id]) { 2 }
reset_absent.call
end
end
context 'when the enterprise has no permission' do
let(:enterprise) { instance_double(Enterprise, id: 1) }
let(:settings) do
instance_double(
ProductImport::Settings,
reset_all_absent?: true,
data_for_stock_reset?: true,
updated_ids: [0],
enterprises_to_reset: ['1']
enterprises_to_reset: [enterprise.id.to_s]
)
end
let(:strategy_factory) { double(:strategy_factory) }
let(:strategy) { instance_double(ProductImport::InventoryReset) }
before do
allow(entry_processor)
.to receive(:permission_by_id?).with('1') { false }
.to receive(:permission_by_id?).with(enterprise.id.to_s) { false }
allow(strategy).to receive(:supplier_ids) { [] }
end
xit 'does not reset anything' do
it 'does not reset stock' do
expect(strategy).not_to receive(:reset)
reset_absent.call
expect(strategy.supplier_ids).to eq([])
end
end
end
describe '#products_reset_count' do
context 'and importing into inventory' do
let(:variant) { create(:variant) }
let(:enterprise) { variant.product.supplier }
let(:variant_override) do
create(:variant_override, variant: variant, hub: enterprise)
end
let(:enterprise) { instance_double(Enterprise, id: 1) }
let(:settings) do
instance_double(
ProductImport::Settings,
updated_ids: [0],
enterprises_to_reset: [enterprise.id.to_s]
)
end
let(:strategy_factory) { ProductImport::InventoryReset }
before do
variant_override
allow(entry_processor)
.to receive(:permission_by_id?).with(enterprise.id.to_s) { true }
end
before do
allow(entry_processor)
.to receive(:importing_into_inventory?) { true }
end
it 'returns the number of reset variant overrides' do
reset_absent.call
expect(reset_absent.products_reset_count).to eq(1)
end
let(:settings) do
instance_double(
ProductImport::Settings,
updated_ids: [0],
enterprises_to_reset: [enterprise.id.to_s]
)
end
context 'and not importing into inventory' do
let(:variant) { create(:variant) }
let(:enterprise_id) { variant.product.supplier_id }
let(:strategy) { instance_double(ProductImport::InventoryReset) }
before do
allow(entry_processor)
.to receive(:permission_by_id?).with(enterprise_id.to_s) { true }
before do
allow(entry_processor)
.to receive(:permission_by_id?).with(enterprise.id.to_s) { true }
allow(entry_processor)
.to receive(:importing_into_inventory?) { false }
end
allow(strategy).to receive(:<<).with(enterprise.id)
allow(strategy).to receive(:supplier_ids) { [enterprise.id] }
allow(strategy).to receive(:reset).with([0], [enterprise.id]) { 1 }
end
let(:settings) do
instance_double(
ProductImport::Settings,
updated_ids: [0],
enterprises_to_reset: [enterprise_id.to_s]
)
end
let(:strategy_factory) { ProductImport::ProductsReset }
it 'returns the number of reset products or variants' do
reset_absent.call
expect(reset_absent.products_reset_count).to eq(2)
end
it 'returns the number of reset variant overrides' do
reset_absent.call
expect(reset_absent.products_reset_count).to eq(1)
end
end
end