From 42a0a973ba6f98dd8aa8518f4bf48ef1f48848d4 Mon Sep 17 00:00:00 2001 From: SarvarKhalimov Date: Sun, 13 Feb 2022 18:28:21 +0500 Subject: [PATCH 1/3] Fix product import on existing, empty unit_type and variant_unit --- app/models/product_import/entry_validator.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/app/models/product_import/entry_validator.rb b/app/models/product_import/entry_validator.rb index 9710cb37dc4..f3babe9c115 100644 --- a/app/models/product_import/entry_validator.rb +++ b/app/models/product_import/entry_validator.rb @@ -73,11 +73,13 @@ def mark_as_new_variant(entry, product_id) 'variant_unit_scale', 'primary_taxon_id') ) new_variant.save - if entry.attributes['on_demand'].present? - new_variant.on_demand = entry.attributes['on_demand'] - end - if entry.attributes['on_hand'].present? - new_variant.on_hand = entry.attributes['on_hand'] + if entry.attributes['unit_type'].present? || entry.attributes['variant_unit_name'].present? + if entry.attributes['on_demand'].present? + new_variant.on_demand = entry.attributes['on_demand'] + end + if entry.attributes['on_hand'].present? + new_variant.on_hand = entry.attributes['on_hand'] + end end new_variant.product_id = product_id From d69f42e940976b0ba3dfda7b7ca3db6c31dc4749 Mon Sep 17 00:00:00 2001 From: SarvarKhalimov Date: Tue, 15 Feb 2022 00:10:32 +0500 Subject: [PATCH 2/3] Update app/models/product_import/entry_validator.rb Co-authored-by: Maikel --- app/models/product_import/entry_validator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/product_import/entry_validator.rb b/app/models/product_import/entry_validator.rb index f3babe9c115..a759898e320 100644 --- a/app/models/product_import/entry_validator.rb +++ b/app/models/product_import/entry_validator.rb @@ -73,7 +73,7 @@ def mark_as_new_variant(entry, product_id) 'variant_unit_scale', 'primary_taxon_id') ) new_variant.save - if entry.attributes['unit_type'].present? || entry.attributes['variant_unit_name'].present? + if new_variant.persisted? if entry.attributes['on_demand'].present? new_variant.on_demand = entry.attributes['on_demand'] end From 22de758e3af3485aa1f709c20197423ce27bb0aa Mon Sep 17 00:00:00 2001 From: SarvarKhalimov Date: Wed, 16 Feb 2022 13:31:27 +0500 Subject: [PATCH 3/3] Add spec for product import validation --- .../product_import/entry_validator_spec.rb | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/spec/models/product_import/entry_validator_spec.rb b/spec/models/product_import/entry_validator_spec.rb index ff8075f690f..44b9738706f 100644 --- a/spec/models/product_import/entry_validator_spec.rb +++ b/spec/models/product_import/entry_validator_spec.rb @@ -55,6 +55,54 @@ ) end + describe "variant validation" do + let(:potatoes) { + create( + :simple_product, + supplier: enterprise, + on_hand: '100', + name: 'Potatoes', + unit_value: 1000, + variant_unit_scale: 1000, + variant_unit: 'weight' + ) + } + + let(:potato_variant) do + ProductImport::SpreadsheetEntry.new( + unscaled_units: "1", + units: "1", + unit_type: "", + variant_unit_name: "", + name: potatoes.name, + display_name: 'Potatoes', + enterprise: enterprise, + enterprise_id: enterprise.id, + producer: enterprise, + producer_id: enterprise.id, + ) + end + + before do + allow(import_settings).to receive(:dig) + allow(spreadsheet_data).to receive(:tax_index) + allow(spreadsheet_data).to receive(:shipping_index) + allow(spreadsheet_data).to receive(:categories_index) + allow(entry_validator).to receive(:enterprise_validation) + allow(entry_validator).to receive(:tax_and_shipping_validation) + allow(entry_validator).to receive(:variant_of_product_validation) + allow(entry_validator).to receive(:category_validation) + allow(entry_validator).to receive(:shipping_presence_validation) + allow(entry_validator).to receive(:product_validation) + end + + it "validates a product" do + entries = [potato_variant] + entry_validator.validate_all(entries) + expect(potato_variant.errors.count).to eq 1 + end + end + describe "inventory validation" do before do allow(entry_validator).to receive(:import_into_inventory?) { true }