From 001530580ca8c269f0470bfedfbbaf043bb8e287 Mon Sep 17 00:00:00 2001 From: Artyom Dudarev Date: Tue, 12 Feb 2019 15:43:26 +0200 Subject: [PATCH] Price range filter should use pricelists from search criteria (#195) --- .../Search/ProductSearchRequestBuilder.cs | 4 +- .../ProductSearchRequestBuilderTests.cs | 66 +++++++++++++++++++ .../VirtoCommerce.CatalogModule.Test.csproj | 1 + 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 VirtoCommerce.CatalogModule.Test/ProductSearchRequestBuilderTests.cs diff --git a/VirtoCommerce.CatalogModule.Data/Search/ProductSearchRequestBuilder.cs b/VirtoCommerce.CatalogModule.Data/Search/ProductSearchRequestBuilder.cs index 9c1ac2b0e..85701d44a 100644 --- a/VirtoCommerce.CatalogModule.Data/Search/ProductSearchRequestBuilder.cs +++ b/VirtoCommerce.CatalogModule.Data/Search/ProductSearchRequestBuilder.cs @@ -43,7 +43,7 @@ public virtual SearchRequest BuildRequest(SearchCriteriaBase criteria) Aggregations = _aggregationConverter?.GetAggregationRequests(productSearchCriteria, allFilters), IsFuzzySearch = productSearchCriteria.IsFuzzySearch, RawQuery = productSearchCriteria.RawQuery - }; + }; } return request; @@ -162,7 +162,7 @@ protected virtual IList GetPermanentFilters(ProductSearchCriteria crite if (criteria.PriceRange != null) { var range = criteria.PriceRange; - result.Add(FiltersHelper.CreatePriceRangeFilter(criteria.Currency, null, range.Lower, range.Upper, range.IncludeLower, range.IncludeUpper)); + result.Add(FiltersHelper.CreatePriceRangeFilter(criteria.Currency, criteria.Pricelists, range.Lower, range.Upper, range.IncludeLower, range.IncludeUpper)); } if (criteria.GeoDistanceFilter != null) diff --git a/VirtoCommerce.CatalogModule.Test/ProductSearchRequestBuilderTests.cs b/VirtoCommerce.CatalogModule.Test/ProductSearchRequestBuilderTests.cs new file mode 100644 index 000000000..7844ef7ed --- /dev/null +++ b/VirtoCommerce.CatalogModule.Test/ProductSearchRequestBuilderTests.cs @@ -0,0 +1,66 @@ +using System; +using System.Globalization; +using System.Linq; +using VirtoCommerce.CatalogModule.Data.Search; +using VirtoCommerce.Domain.Catalog.Model.Search; +using VirtoCommerce.Domain.Commerce.Model.Search; +using VirtoCommerce.Domain.Search; +using Xunit; + +namespace VirtoCommerce.CatalogModule.Test +{ + [Trait("Category", "Unit")] + [CLSCompliant(false)] + public class ProductSearchRequestBuilderTests : BrowseFiltersTestBase + { + [Theory] + [InlineData(null, null, null, null, "price:")] + [InlineData(null, null, "", null, "price:")] + [InlineData("0", null, "", null, "price:(0 TO)")] + [InlineData(null, "100", "", null, "price:(TO 100)")] + [InlineData("0", "100", "", null, "price:(0 TO 100)")] + [InlineData("100", "200", "", null, "price:(100 TO 200)")] + [InlineData("100", "200", "USD", null, "price_usd:(100 TO 200)")] + [InlineData("100", "200", "USD", "1", "price_usd_1:(100 TO 200)")] + [InlineData("100", "200", "USD", "1;2", "(price_usd_1:(100 TO 200) OR (NOT(price_usd_1:(0 TO)) AND price_usd_2:(100 TO 200)))")] + [InlineData("100", "200", "USD", "1;2;3", "(price_usd_1:(100 TO 200) OR (NOT(price_usd_1:(0 TO)) AND (price_usd_2:(100 TO 200) OR (NOT(price_usd_2:(0 TO)) AND price_usd_3:(100 TO 200)))))")] + public void TestPriceRangeFilter(string lower, string upper, string currency, string pricelists, string expectedFilter) + { + var criteria = new ProductSearchCriteria + { + PriceRange = new NumericRange + { + Lower = ParseDecimal(lower), + Upper = ParseDecimal(upper), + }, + Currency = currency, + Pricelists = pricelists?.Split(';'), + }; + + var termFilterBuilder = GetSearchRequestBuilder(); + + var searchRequest = termFilterBuilder.BuildRequest(criteria); + + var priceFilter = (searchRequest.Filter as AndFilter)?.ChildFilters.Last().ToString(); + Assert.Equal(expectedFilter, priceFilter); + } + + + private static decimal? ParseDecimal(string str) + { + decimal? result = null; + + if (decimal.TryParse(str, NumberStyles.Number, CultureInfo.InvariantCulture, out var value)) + { + result = value; + } + + return result; + } + + private static ISearchRequestBuilder GetSearchRequestBuilder() + { + return new ProductSearchRequestBuilder(null, GetTermFilterBuilder(), null); + } + } +} diff --git a/VirtoCommerce.CatalogModule.Test/VirtoCommerce.CatalogModule.Test.csproj b/VirtoCommerce.CatalogModule.Test/VirtoCommerce.CatalogModule.Test.csproj index f1fa786b0..9edc1a212 100644 --- a/VirtoCommerce.CatalogModule.Test/VirtoCommerce.CatalogModule.Test.csproj +++ b/VirtoCommerce.CatalogModule.Test/VirtoCommerce.CatalogModule.Test.csproj @@ -137,6 +137,7 @@ +