From 7b8ccccdc35bdb9e2c2c22694d5e374e92056926 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Thu, 2 Mar 2023 17:15:57 +0100 Subject: [PATCH 1/2] `display_name` can actually be `null` or `empty`: consider them as equal --- app/models/product_import/entry_validator.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/models/product_import/entry_validator.rb b/app/models/product_import/entry_validator.rb index 06c1311d3a..5ae5c61b30 100644 --- a/app/models/product_import/entry_validator.rb +++ b/app/models/product_import/entry_validator.rb @@ -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 From d9b534b8292ae6c471e35bb658f8fa6478213197 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Mon, 6 Mar 2023 14:52:32 +0100 Subject: [PATCH 2/2] + update spec as well --- spec/models/product_importer_spec.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/spec/models/product_importer_spec.rb b/spec/models/product_importer_spec.rb index 812181e3e1..59c440e3a8 100644 --- a/spec/models/product_importer_spec.rb +++ b/spec/models/product_importer_spec.rb @@ -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|