mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-10 03:30:22 +00:00
Improve inventory reset strategy specs' performance
This commit is contained in:
@@ -4,24 +4,21 @@ describe ProductImport::InventoryResetStrategy do
|
||||
let(:inventory_reset) { described_class.new(excluded_items_ids) }
|
||||
|
||||
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
|
||||
|
||||
context 'when there are excluded_items_ids' do
|
||||
let(:enterprise) { variant.product.supplier }
|
||||
let(:variant) { build_stubbed(:variant) }
|
||||
let!(:variant_override) do
|
||||
build_stubbed(
|
||||
:variant_override,
|
||||
count_on_hand: 10,
|
||||
hub: enterprise,
|
||||
variant: variant
|
||||
)
|
||||
end
|
||||
let(:excluded_items_ids) { [variant_override.id] }
|
||||
|
||||
context 'and supplier_ids is []' do
|
||||
let(:supplier_ids) { [] }
|
||||
let(:relation) do
|
||||
instance_double(ActiveRecord::Relation, update_all: true)
|
||||
end
|
||||
@@ -29,13 +26,12 @@ describe ProductImport::InventoryResetStrategy do
|
||||
before { allow(VariantOverride).to receive(:where) { relation } }
|
||||
|
||||
it 'does not update any DB record' do
|
||||
inventory_reset.reset(supplier_ids)
|
||||
inventory_reset.reset([])
|
||||
expect(relation).not_to have_received(:update_all)
|
||||
end
|
||||
end
|
||||
|
||||
context 'and supplier_ids is nil' do
|
||||
let(:supplier_ids) { nil }
|
||||
let(:relation) do
|
||||
instance_double(ActiveRecord::Relation, update_all: true)
|
||||
end
|
||||
@@ -43,14 +39,24 @@ describe ProductImport::InventoryResetStrategy do
|
||||
before { allow(VariantOverride).to receive(:where) { relation } }
|
||||
|
||||
it 'does not update any DB record' do
|
||||
inventory_reset.reset(supplier_ids)
|
||||
inventory_reset.reset(nil)
|
||||
expect(relation).not_to have_received(:update_all)
|
||||
end
|
||||
end
|
||||
|
||||
context 'and supplier_ids is set' do
|
||||
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
|
||||
it 'does not update the count_on_hand of the excluded items' do
|
||||
inventory_reset.reset(supplier_ids)
|
||||
inventory_reset.reset(enterprise.id)
|
||||
expect(variant_override.reload.count_on_hand).to eq(10)
|
||||
end
|
||||
|
||||
@@ -61,7 +67,7 @@ describe ProductImport::InventoryResetStrategy do
|
||||
hub: enterprise,
|
||||
variant: variant
|
||||
)
|
||||
inventory_reset.reset(supplier_ids)
|
||||
inventory_reset.reset(enterprise.id)
|
||||
expect(non_excluded_variant_override.reload.count_on_hand).to eq(0)
|
||||
end
|
||||
end
|
||||
@@ -71,7 +77,6 @@ describe ProductImport::InventoryResetStrategy do
|
||||
let(:excluded_items_ids) { [] }
|
||||
|
||||
context 'and supplier_ids is []' do
|
||||
let(:supplier_ids) { [] }
|
||||
let(:relation) do
|
||||
instance_double(ActiveRecord::Relation, update_all: true)
|
||||
end
|
||||
@@ -79,13 +84,12 @@ describe ProductImport::InventoryResetStrategy do
|
||||
before { allow(VariantOverride).to receive(:where) { relation } }
|
||||
|
||||
it 'does not update any DB record' do
|
||||
inventory_reset.reset(supplier_ids)
|
||||
inventory_reset.reset([])
|
||||
expect(relation).not_to have_received(:update_all)
|
||||
end
|
||||
end
|
||||
|
||||
context 'and supplier_ids is nil' do
|
||||
let(:supplier_ids) { nil }
|
||||
let(:relation) do
|
||||
instance_double(ActiveRecord::Relation, update_all: true)
|
||||
end
|
||||
@@ -93,14 +97,24 @@ describe ProductImport::InventoryResetStrategy do
|
||||
before { allow(VariantOverride).to receive(:where) { relation } }
|
||||
|
||||
it 'does not update any DB record' do
|
||||
inventory_reset.reset(supplier_ids)
|
||||
inventory_reset.reset(nil)
|
||||
expect(relation).not_to have_received(:update_all)
|
||||
end
|
||||
end
|
||||
|
||||
context 'and supplier_ids is set' do
|
||||
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
|
||||
it 'sets all count_on_hand to 0' do
|
||||
inventory_reset.reset(supplier_ids)
|
||||
inventory_reset.reset(enterprise.id)
|
||||
expect(variant_override.reload.count_on_hand).to eq(0)
|
||||
end
|
||||
end
|
||||
@@ -110,7 +124,6 @@ describe ProductImport::InventoryResetStrategy do
|
||||
let(:excluded_items_ids) { nil }
|
||||
|
||||
context 'and supplier_ids is []' do
|
||||
let(:supplier_ids) { [] }
|
||||
let(:relation) do
|
||||
instance_double(ActiveRecord::Relation, update_all: true)
|
||||
end
|
||||
@@ -118,13 +131,12 @@ describe ProductImport::InventoryResetStrategy do
|
||||
before { allow(VariantOverride).to receive(:where) { relation } }
|
||||
|
||||
it 'does not update any DB record' do
|
||||
inventory_reset.reset(supplier_ids)
|
||||
inventory_reset.reset([])
|
||||
expect(relation).not_to have_received(:update_all)
|
||||
end
|
||||
end
|
||||
|
||||
context 'and supplier_ids is nil' do
|
||||
let(:supplier_ids) { nil }
|
||||
let(:relation) do
|
||||
instance_double(ActiveRecord::Relation, update_all: true)
|
||||
end
|
||||
@@ -132,14 +144,24 @@ describe ProductImport::InventoryResetStrategy do
|
||||
before { allow(VariantOverride).to receive(:where) { relation } }
|
||||
|
||||
it 'does not update any DB record' do
|
||||
inventory_reset.reset(supplier_ids)
|
||||
inventory_reset.reset(nil)
|
||||
expect(relation).not_to have_received(:update_all)
|
||||
end
|
||||
end
|
||||
|
||||
context 'and supplier_ids is set' do
|
||||
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
|
||||
it 'sets all count_on_hand to 0' do
|
||||
inventory_reset.reset(supplier_ids)
|
||||
inventory_reset.reset(enterprise.id)
|
||||
expect(variant_override.reload.count_on_hand).to eq(0)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user