diff --git a/app/models/product_import/entry_validator.rb b/app/models/product_import/entry_validator.rb index 9710cb37dc4..a759898e320 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 new_variant.persisted? + 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 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 }