Add specs for ResetAbsent strategies

This also fixes the case where there are no overrides to exclude.
This commit is contained in:
Pau Perez
2018-09-26 11:38:22 +02:00
parent d527f6265a
commit fd69c7672d
4 changed files with 147 additions and 19 deletions

View File

@@ -2,9 +2,9 @@ module ProductImport
class InventoryReset
attr_reader :supplier_ids
def initialize(updated_ids)
def initialize(excluded_items_ids)
@supplier_ids = []
@updated_ids = updated_ids
@excluded_items_ids = excluded_items_ids
end
def <<(values)
@@ -17,15 +17,13 @@ module ProductImport
private
attr_reader :updated_ids
attr_reader :excluded_items_ids
def relation
VariantOverride.where(
'variant_overrides.hub_id IN (?) ' \
'AND variant_overrides.id NOT IN (?)',
supplier_ids,
updated_ids
)
relation = VariantOverride.where(hub_id: supplier_ids)
return relation if excluded_items_ids.blank?
relation.where('id NOT IN (?)', excluded_items_ids)
end
end
end

View File

@@ -2,9 +2,9 @@ module ProductImport
class ProductsReset
attr_reader :supplier_ids
def initialize(updated_ids)
def initialize(excluded_items_ids)
@supplier_ids = []
@updated_ids = updated_ids
@excluded_items_ids = excluded_items_ids
end
def <<(values)
@@ -17,19 +17,19 @@ module ProductImport
private
attr_reader :updated_ids
attr_reader :excluded_items_ids
def relation
Spree::Variant
relation = Spree::Variant
.joins(:product)
.where(
'spree_products.supplier_id IN (?) ' \
'AND spree_variants.id NOT IN (?) ' \
'AND spree_variants.is_master = false ' \
'AND spree_variants.deleted_at IS NULL',
supplier_ids,
updated_ids
spree_products: { supplier_id: supplier_ids },
spree_variants: { is_master: false, deleted_at: nil }
)
return relation if excluded_items_ids.blank?
relation.where('spree_variants.id NOT IN (?)', excluded_items_ids)
end
end
end

View File

@@ -0,0 +1,70 @@
require 'spec_helper'
describe ProductImport::InventoryReset do
let(:inventory_reset) { described_class.new(excluded_items_ids) }
describe '#<<' do
let(:excluded_items_ids) { [] }
let(:supplier_ids) { 1 }
it 'stores the specified supplier_ids' do
inventory_reset << supplier_ids
expect(inventory_reset.supplier_ids).to eq([supplier_ids])
end
end
describe '#reset' do
let(:supplier_ids) { enterprise.id }
let(:enterprise) { variant.product.supplier }
let(:variant) { create(:variant) }
let!(:variant_override) do
create(
:variant_override,
count_on_hand: 10,
hub: enterprise,
variant: variant
)
end
before { inventory_reset << supplier_ids }
context 'when there are excluded_items_ids' do
let(:excluded_items_ids) { [variant_override.id] }
it 'does not update the count_on_hand of the excluded items' do
inventory_reset.reset
expect(variant_override.reload.count_on_hand).to eq(10)
end
it 'updates the count_on_hand of the non-excluded items' do
non_excluded_variant_override = create(
:variant_override,
count_on_hand: 3,
hub: enterprise,
variant: variant
)
inventory_reset.reset
expect(non_excluded_variant_override.reload.count_on_hand).to eq(0)
end
end
context 'when there are no excluded_items_ids' do
let(:excluded_items_ids) { [] }
it 'sets all count_on_hand to 0' do
inventory_reset.reset
expect(variant_override.reload.count_on_hand).to eq(0)
end
end
context 'when excluded_items_ids is nil' do
let(:excluded_items_ids) { nil }
it 'sets all count_on_hand to 0' do
inventory_reset.reset
expect(variant_override.reload.count_on_hand).to eq(0)
end
end
end
end

View File

@@ -0,0 +1,60 @@
require 'spec_helper'
describe ProductImport::ProductsReset do
let(:products_reset) { described_class.new(excluded_items_ids) }
describe '#<<' do
let(:excluded_items_ids) { [] }
let(:supplier_ids) { 1 }
it 'stores the specified supplier_ids' do
products_reset << supplier_ids
expect(products_reset.supplier_ids).to eq([supplier_ids])
end
end
describe '#reset' do
let(:supplier_ids) { enterprise.id }
let(:enterprise) { variant.product.supplier }
let(:variant) { create(:variant, count_on_hand: 2) }
before { products_reset << supplier_ids }
context 'when there are excluded_items_ids' do
let(:excluded_items_ids) { [variant.id] }
it 'does not update the count_on_hand of the excluded items' do
products_reset.reset
expect(variant.reload.count_on_hand).to eq(2)
end
it 'updates the count_on_hand of the non-excluded items' do
non_excluded_variant = create(
:variant,
count_on_hand: 3,
product: variant.product
)
products_reset.reset
expect(non_excluded_variant.reload.count_on_hand).to eq(0)
end
end
context 'when there are no excluded_items_ids' do
let(:excluded_items_ids) { [] }
it 'sets all count_on_hand to 0' do
products_reset.reset
expect(variant.reload.count_on_hand).to eq(0)
end
end
context 'when excluded_items_ids is nil' do
let(:excluded_items_ids) { nil }
it 'sets all count_on_hand to 0' do
products_reset.reset
expect(variant.reload.count_on_hand).to eq(0)
end
end
end
end