From d2d2db8489a07001fa0fe6208d299d59bd0eeb2f Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 15 Mar 2024 15:03:26 +1100 Subject: [PATCH] Assign random product category on import if missing Failing in this case may be desired in some circumstances but most of the time we want compatibility and easy interoperability even when not all data matches. --- .../app/services/supplied_product_builder.rb | 7 +++--- .../services/supplied_product_builder_spec.rb | 22 ++++--------------- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/engines/dfc_provider/app/services/supplied_product_builder.rb b/engines/dfc_provider/app/services/supplied_product_builder.rb index d722179d22..31c343b524 100644 --- a/engines/dfc_provider/app/services/supplied_product_builder.rb +++ b/engines/dfc_provider/app/services/supplied_product_builder.rb @@ -90,10 +90,11 @@ class SuppliedProductBuilder < DfcBuilder end def self.taxon(supplied_product) - return unless supplied_product.productType + dfc_id = supplied_product.productType&.semanticId - dfc_id = supplied_product.productType.semanticId - Spree::Taxon.find_by(dfc_id: ) + # Every product needs a primary taxon to be valid. So if we don't have + # one or can't find it we just take a random one. + Spree::Taxon.find_by(dfc_id:) || Spree::Taxon.first end private_class_method :product_type, :taxon diff --git a/engines/dfc_provider/spec/services/supplied_product_builder_spec.rb b/engines/dfc_provider/spec/services/supplied_product_builder_spec.rb index ccc618998f..08db382860 100644 --- a/engines/dfc_provider/spec/services/supplied_product_builder_spec.rb +++ b/engines/dfc_provider/spec/services/supplied_product_builder_spec.rb @@ -64,14 +64,6 @@ describe SuppliedProductBuilder do expect(product.productType).to eq soft_drink end - - context "when no taxon set" do - let(:taxon) { nil } - - it "returns nil" do - expect(product.productType).to be_nil - end - end end it "assigns an image_url type" do @@ -131,16 +123,6 @@ describe SuppliedProductBuilder do expect(product.primary_taxon).to eq(taxon) end - - describe "when no matching taxon" do - let(:product_type) { DfcLoader.connector.PRODUCT_TYPES.DRINK } - - it "set the taxon to nil" do - product = builder.import_product(supplied_product) - - expect(product.primary_taxon).to be_nil - end - end end end @@ -161,7 +143,10 @@ describe SuppliedProductBuilder do let(:product_type) { DfcLoader.connector.PRODUCT_TYPES.VEGETABLE.NON_LOCAL_VEGETABLE } it "creates a new Spree::Product and variant" do + create(:taxon) + expect(imported_variant).to be_a(Spree::Variant) + expect(imported_variant).to be_valid expect(imported_variant.id).to be_nil expect(imported_variant.semantic_links.size).to eq 1 @@ -170,6 +155,7 @@ describe SuppliedProductBuilder do imported_product = imported_variant.product expect(imported_product).to be_a(Spree::Product) + expect(imported_product).to be_valid expect(imported_product.id).to be_nil expect(imported_product.name).to eq("Tomato") expect(imported_product.description).to eq("Awesome tomato")