Skip to content

Commit

Permalink
Merge pull request #8876 from SarvarKh/product-import-validation
Browse files Browse the repository at this point in the history
Fix product import on existing, empty unit_type and variant_unit
  • Loading branch information
filipefurtad0 authored Feb 21, 2022
2 parents 28ac9d9 + 22de758 commit 385cd49
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
12 changes: 7 additions & 5 deletions app/models/product_import/entry_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions spec/models/product_import/entry_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit 385cd49

Please sign in to comment.