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.
This commit is contained in:
Maikel Linke
2024-03-15 15:03:26 +11:00
parent 3af7fa7521
commit d2d2db8489
2 changed files with 8 additions and 21 deletions

View File

@@ -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

View File

@@ -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")