mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Fix ProductImport::EntryProcessor#count_existing_items
Plus spec
This commit is contained in:
@@ -54,10 +54,7 @@ module ProductImport
|
||||
if settings.importing_into_inventory?
|
||||
VariantOverride.for_hubs([enterprise_id]).count
|
||||
else
|
||||
Spree::Variant.
|
||||
joins(:product).
|
||||
where(spree_products: { supplier_id: enterprise_id }).
|
||||
count
|
||||
Spree::Variant.where(supplier_id: enterprise_id).count
|
||||
end
|
||||
|
||||
@enterprise_products[enterprise_id] = products_count
|
||||
|
||||
@@ -150,4 +150,50 @@ RSpec.describe ProductImport::EntryProcessor do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#count_existing_items" do
|
||||
let(:settings) { instance_double(ProductImport::Settings, importing_into_inventory?: false) }
|
||||
let(:editable_enterprises) do
|
||||
{
|
||||
"#{supplier1.name}": supplier1.id,
|
||||
"#{supplier2.name}": supplier2.id
|
||||
}
|
||||
end
|
||||
let(:supplier1) { create(:supplier_enterprise) }
|
||||
let(:supplier2) { create(:supplier_enterprise) }
|
||||
let!(:products) { create_list(:simple_product, 3, supplier_id: supplier1.id) }
|
||||
|
||||
before do
|
||||
allow(ProductImport::Settings).to receive(:new) { settings }
|
||||
|
||||
enterprises = {
|
||||
"#{supplier1.name}": { id: supplier1.id },
|
||||
"#{supplier2.name}": { id: supplier2.id }
|
||||
}
|
||||
allow(spreadsheet_data).to receive(:enterprises_index).and_return(enterprises)
|
||||
|
||||
create_list(:simple_product, 2, supplier_id: supplier2.id)
|
||||
end
|
||||
|
||||
it "returns the total of existing variants for the given enterprises" do
|
||||
entry_processor.count_existing_items
|
||||
|
||||
expect(entry_processor.total_enterprise_products).to eq(5)
|
||||
end
|
||||
|
||||
context "when importing into inventory" do
|
||||
let(:settings) { instance_double(ProductImport::Settings, importing_into_inventory?: true) }
|
||||
|
||||
it "returns the total of existing variant override for the given enterprises" do
|
||||
products.each do |p|
|
||||
variant = p.variants.first
|
||||
create(:variant_override, variant:, hub: variant.supplier)
|
||||
end
|
||||
|
||||
entry_processor.count_existing_items
|
||||
|
||||
expect(entry_processor.total_enterprise_products).to eq(3)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user