From dd8f0aafabd6b9e1528ea76ccd2e1fc02f5d9aa5 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Wed, 22 May 2024 22:09:22 +1000 Subject: [PATCH] Fix ProductImport::EntryProcessor#count_existing_items Plus spec --- app/models/product_import/entry_processor.rb | 5 +- .../product_import/entry_processor_spec.rb | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/app/models/product_import/entry_processor.rb b/app/models/product_import/entry_processor.rb index 4c585a4b32..8ce73489a9 100644 --- a/app/models/product_import/entry_processor.rb +++ b/app/models/product_import/entry_processor.rb @@ -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 diff --git a/spec/models/product_import/entry_processor_spec.rb b/spec/models/product_import/entry_processor_spec.rb index 1306080ef8..13022d2207 100644 --- a/spec/models/product_import/entry_processor_spec.rb +++ b/spec/models/product_import/entry_processor_spec.rb @@ -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