mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-26 01:33:22 +00:00
Merge pull request #10506 from jibees/9868-product-import-empty-variant-names-recorded-differently-leads-to-variant-duplication
Product import: consider `display_name` with `null` or `empty` as value as equal
This commit is contained in:
@@ -271,10 +271,16 @@ module ProductImport
|
||||
end
|
||||
|
||||
def entry_matches_existing_variant?(entry, existing_variant)
|
||||
existing_variant.display_name == entry.display_name &&
|
||||
display_name_are_the_same?(entry, existing_variant) &&
|
||||
existing_variant.unit_value == entry.unit_value.to_f
|
||||
end
|
||||
|
||||
def display_name_are_the_same?(entry, existing_variant)
|
||||
return true if entry.display_name.blank? && existing_variant.display_name.blank?
|
||||
|
||||
existing_variant.display_name == entry.display_name
|
||||
end
|
||||
|
||||
def category_validation(entry)
|
||||
category_name = entry.category
|
||||
|
||||
|
||||
@@ -43,6 +43,10 @@ describe ProductImport::ProductImporter do
|
||||
create(:variant, product_id: product.id, price: '8.50', on_hand: '100', unit_value: '500',
|
||||
display_name: 'Preexisting Banana')
|
||||
}
|
||||
let!(:variant_with_empty_display_name) {
|
||||
create(:variant, product_id: product.id, price: '8.50', on_hand: '100', unit_value: '500',
|
||||
display_name: '')
|
||||
}
|
||||
let!(:product2) {
|
||||
create(:simple_product, supplier: enterprise, on_hand: '100', name: 'Beans', unit_value: '500',
|
||||
primary_taxon_id: category.id, description: nil)
|
||||
@@ -377,6 +381,28 @@ describe ProductImport::ProductImporter do
|
||||
end
|
||||
end
|
||||
|
||||
describe "updating variant having an nil display name with CSV with empty display name" do
|
||||
let(:csv_data) {
|
||||
CSV.generate do |csv|
|
||||
csv << ["name", "producer", "category", "on_hand", "price", "units", "unit_type",
|
||||
"display_name", "shipping_category"]
|
||||
csv << ["Hypothetical Cake", enterprise2.name, "Cake", "5", "5.50", "1", "g",
|
||||
"", shipping_category.name]
|
||||
end
|
||||
}
|
||||
let(:importer) { import_data csv_data }
|
||||
|
||||
it "consider both existing and imported as the same and should be then updated" do
|
||||
importer.validate_entries
|
||||
entries = JSON.parse(importer.entries_json)
|
||||
|
||||
expect(filter('valid', entries)).to eq 1
|
||||
expect(filter('invalid', entries)).to eq 0
|
||||
expect(filter('create_product', entries)).to eq 0
|
||||
expect(filter('update_product', entries)).to eq 1
|
||||
end
|
||||
end
|
||||
|
||||
describe "adding new product and sub-variant at the same time" do
|
||||
let(:csv_data) {
|
||||
CSV.generate do |csv|
|
||||
|
||||
Reference in New Issue
Block a user