Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Add tests for market-based indexing, currency filtering, translation fallback

Damian Legawiec61ec35ada221
diff --git a/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
index 60de0a2f72..bd03e46bd9 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
@@ -484,6 +484,66 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller do
expect(mock_index).to have_received(:search)
expect(json_response['data'].size).to eq(2)
end
+
+ it 'filters by current locale in Meilisearch' do
+ allow(mock_index).to receive(:search).and_return({
+ 'hits' => [{ 'product_id' => product.prefixed_id }],
+ 'estimatedTotalHits' => 1,
+ 'facetDistribution' => {}
+ })
+
+ get :index
+
+ expect(response).to have_http_status(:ok)
+ expect(mock_index).to have_received(:search).with(anything, hash_including(
+ filter: include("locale = '#{store.default_locale}'")
+ ))
+ end
+
+ it 'filters by current currency in Meilisearch' do
+ allow(mock_index).to receive(:search).and_return({
+ 'hits' => [{ 'product_id' => product.prefixed_id }],
+ 'estimatedTotalHits' => 1,
+ 'facetDistribution' => {}
+ })
+
+ get :index
+
+ expect(response).to have_http_status(:ok)
+ expect(mock_index).to have_received(:search).with(anything, hash_including(
+ filter: include("currency = '#{store.default_currency}'")
+ ))
+ end
+
+ it 'filters by store_id in Meilisearch' do
+ allow(mock_index).to receive(:search).and_return({
+ 'hits' => [{ 'product_id' => product.prefixed_id }],
+ 'estimatedTotalHits' => 1,
+ 'facetDistribution' => {}
+ })
+
+ get :index
+
+ expect(response).to have_http_status(:ok)
+ expect(mock_index).to have_received(:search).with(anything, hash_including(
+ filter: include("store_ids = '#{store.id}'")
+ ))
+ end
+
+ it 'always filters by active status in Meilisearch' do
+ allow(mock_index).to receive(:search).and_return({
+ 'hits' => [{ 'product_id' => product.prefixed_id }],
+ 'estimatedTotalHits' => 1,
+ 'facetDistribution' => {}
+ })
+
+ get :index
+
+ expect(response).to have_http_status(:ok)
+ expect(mock_index).to have_received(:search).with(anything, hash_including(
+ filter: include("status = 'active'")
+ ))
+ end
end
end
end
diff --git a/spree/core/app/models/spree/search_provider/meilisearch.rb b/spree/core/app/models/spree/search_provider/meilisearch.rb
index ea2f44e912..17e9dcada7 100644
--- a/spree/core/app/models/spree/search_provider/meilisearch.rb
+++ b/spree/core/app/models/spree/search_provider/meilisearch.rb
@@ -239,7 +239,7 @@ module Spree
if facet_distribution['option_value_ids'].present?
prefixed_ids = facet_distribution['option_value_ids'].keys
raw_ids = prefixed_ids.filter_map { |pid| Spree::OptionValue.decode_prefixed_id(pid) }
- option_values = Spree::OptionValue.where(id: raw_ids).includes(:option_type).index_by(&:prefixed_id)
+ option_values = Spree::OptionValue.where(id: raw_ids).includes(:option_type).preload_associations_lazily.index_by(&:prefixed_id)
# Group by option type
by_option_type = {}
diff --git a/spree/core/app/presenters/spree/search_provider/product_presenter.rb b/spree/core/app/presenters/spree/search_provider/product_presenter.rb
index df473c8c56..4633a1ccc8 100644
--- a/spree/core/app/presenters/spree/search_provider/product_presenter.rb
+++ b/spree/core/app/presenters/spree/search_provider/product_presenter.rb
@@ -14,6 +14,9 @@ module Spree
documents = []
market_locale_pairs.each do |market, locale|
+ # Skip if product has no price in this currency
+ next unless lowest_price(market.currency)
+
Mobility.with_locale(locale) do
documents << build_document(locale, market.currency, default_locale)
end
diff --git a/spree/core/spec/presenters/spree/search_provider/product_presenter_spec.rb b/spree/core/spec/presenters/spree/search_provider/product_presenter_spec.rb
index bb2cb6de1b..9a7dff3b72 100644
--- a/spree/core/spec/presenters/spree/search_provider/product_presenter_spec.rb
+++ b/spree/core/spec/presenters/spree/search_provider/product_presenter_spec.rb
@@ -71,20 +71,92 @@ module Spree
let!(:us_market) { create(:market, store: store, name: 'US', currency: 'USD', default_locale: 'en') }
let!(:eu_market) { create(:market, store: store, name: 'EU', currency: 'EUR', default_locale: 'de', supported_locales: 'de,fr') }
+ # Product with prices in both currencies
+ let(:product) do
+ p = create(:product, name: 'Test Shirt', stores: [store])
+ create(:price, variant: p.master, amount: 29.99, currency: 'EUR')
+ p
+ end
+
# Reload store to pick up new markets
let(:presenter) { described_class.new(product, store.reload) }
- it 'creates documents for each market × locale combination' do
+ it 'creates one document per market × locale where product has a price' do
locales_currencies = documents.map { |d| [d[:locale], d[:currency]] }
expect(locales_currencies).to include(['en', 'USD'])
expect(locales_currencies).to include(['de', 'EUR'])
expect(locales_currencies).to include(['fr', 'EUR'])
+ expect(documents.size).to eq(3)
+ end
+
+ it 'each document has the correct currency and locale' do
+ usd_doc = documents.find { |d| d[:currency] == 'USD' }
+ eur_doc = documents.find { |d| d[:currency] == 'EUR' && d[:locale] == 'de' }
+
+ expect(usd_doc[:locale]).to eq('en')
+ expect(eur_doc[:locale]).to eq('de')
end
it 'each document has a unique composite prefixed_id' do
ids = documents.map { |d| d[:prefixed_id] }
expect(ids.uniq.size).to eq(ids.size)
end
+
+ it 'composite prefixed_id includes product_id, locale, and currency' do
+ doc = documents.first
+ expect(doc[:prefixed_id]).to match(/\A#{product.prefixed_id}_\w+_[A-Z]{3}\z/)
+ end
+
+ it 'includes the price for each currency' do
+ usd_doc = documents.find { |d| d[:currency] == 'USD' }
+ eur_doc = documents.find { |d| d[:currency] == 'EUR' }
+
+ expect(usd_doc[:price]).to be_present
+ expect(eur_doc[:price]).to eq(29.99)
+ end
+
+ it 'all documents share the same product_id' do
+ product_ids = documents.map { |d| d[:product_id] }.uniq
+ expect(product_ids).to eq([product.prefixed_id])
+ end
+ end
+
+ context 'with market but product has no price in that currency' do
+ let!(:us_market) { create(:market, store: store, name: 'US', currency: 'USD', default_locale: 'en') }
+ let!(:eu_market) { create(:market, store: store, name: 'EU', currency: 'EUR', default_locale: 'de') }
+
+ # Product with USD price only — no EUR price
+ let(:product) { create(:product, name: 'USD Only Product', stores: [store]) }
+ let(:presenter) { described_class.new(product, store.reload) }
+
+ it 'skips markets where product has no price' do
+ currencies = documents.map { |d| d[:currency] }
+ expect(currencies).to include('USD')
+ expect(currencies).not_to include('EUR')
+ end
+
+ it 'only creates documents for markets with matching prices' do
+ expect(documents.size).to eq(1)
+ expect(documents.first[:currency]).to eq('USD')
+ end
+ end
+
+ context 'translation fallback' do
+ let!(:us_market) { create(:market, store: store, name: 'US', currency: 'USD', default_locale: 'en') }
+ let!(:eu_market) { create(:market, store: store, name: 'EU', currency: 'EUR', default_locale: 'de') }
+
+ let(:product) do
+ p = create(:product, name: 'English Name', stores: [store])
+ create(:price, variant: p.master, amount: 19.99, currency: 'EUR')
+ p
+ end
+
+ let(:presenter) { described_class.new(product, store.reload) }
+
+ it 'falls back to default locale name when translation is missing' do
+ de_doc = documents.find { |d| d[:locale] == 'de' }
+ expect(de_doc[:name]).to eq('English Name')
+ end
end
context 'with option values' do