From 100239c4e6ff90a559a8b575622ca81f1a9487db Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Tue, 12 Mar 2024 15:26:49 +1100 Subject: [PATCH] Fix #bulk_product duplicate Remove duplicate when a product has mutiple variant in the same category (taxon) --- app/queries/product_scope_query.rb | 2 +- spec/queries/product_scope_query_spec.rb | 30 ++++++++++++++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/app/queries/product_scope_query.rb b/app/queries/product_scope_query.rb index bc8b6953b2b..00f4a401e81 100644 --- a/app/queries/product_scope_query.rb +++ b/app/queries/product_scope_query.rb @@ -20,7 +20,7 @@ def bulk_products product_query. ransack(query_params_with_defaults). - result + result(distinct: true) end def find_product diff --git a/spec/queries/product_scope_query_spec.rb b/spec/queries/product_scope_query_spec.rb index d1efd12d985..9674a201f5e 100755 --- a/spec/queries/product_scope_query_spec.rb +++ b/spec/queries/product_scope_query_spec.rb @@ -12,7 +12,7 @@ before { current_api_user.enterprise_roles.create(enterprise: supplier2) } - describe 'bulk update' do + describe '#bulk_products' do let!(:product3) { create(:product, supplier: supplier2) } it "returns a list of products" do @@ -28,13 +28,29 @@ expect(subject).not_to include(product2, product3) end - it "filters results by product category" do - subject = ProductScopeQuery - .new(current_api_user, { q: { variants_primary_taxon_id_eq: taxon.id } }) - .bulk_products + describe "by variant category" do + it "filters results by product category" do + create(:variant, product: product2, primary_taxon: taxon) + + subject = ProductScopeQuery + .new(current_api_user, { q: { variants_primary_taxon_id_eq: taxon.id } }) + .bulk_products + + expect(subject).to match_array([product, product2]) + expect(subject).not_to include(product3) + end - expect(subject).to include(product, product2) - expect(subject).not_to include(product3) + context "with mutiple variant in the same category" do + it "doesn't duplicate products" do + create(:variant, product: product2, primary_taxon: taxon) + + subject = ProductScopeQuery + .new(current_api_user, { q: { variants_primary_taxon_id_eq: taxon.id } }) + .bulk_products + + expect(subject).to match_array([product, product2]) + end + end end it "filters results by import_date" do