Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fix filtering by numeric values (#11438)

Iga Karbowiak884db7eb1c85
diff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.py
index 087badc7b7..e0b21edd55 100644
--- a/saleor/graphql/product/filters.py
+++ b/saleor/graphql/product/filters.py
@@ -114,13 +114,13 @@ def _clean_product_attributes_range_filter_input(filter_value, queries):
)
attributes_map: Dict[str, int] = {}
- values_map: Dict[str, Dict[str, int]] = defaultdict(dict)
+ values_map: Dict[str, Dict[str, int]] = defaultdict(lambda: defaultdict(list))
for value_data in values.values_list(
"attribute_id", "attribute__slug", "pk", "numeric_value"
):
attr_pk, attr_slug, pk, numeric_value = value_data
attributes_map[attr_slug] = attr_pk
- values_map[attr_slug][numeric_value] = pk
+ values_map[attr_slug][numeric_value].append(pk)
for attr_name, val_range in filter_value:
if attr_name not in attributes_map:
@@ -131,8 +131,9 @@ def _clean_product_attributes_range_filter_input(filter_value, queries):
matching_values = [
value for value in attr_values.keys() if gte <= value and lte >= value
]
- attr_val_pks = [attr_values[value] for value in matching_values]
- queries[attr_pk] += attr_val_pks
+ queries[attr_pk] = []
+ for value in matching_values:
+ queries[attr_pk] += attr_values[value]
def _clean_product_attributes_date_time_range_filter_input(filter_value):
diff --git a/saleor/graphql/product/tests/queries/test_products_query_with_filter.py b/saleor/graphql/product/tests/queries/test_products_query_with_filter.py
index 025d178c36..b6dfc95e4a 100644
--- a/saleor/graphql/product/tests/queries/test_products_query_with_filter.py
+++ b/saleor/graphql/product/tests/queries/test_products_query_with_filter.py
@@ -89,14 +89,15 @@ def test_products_query_with_filter_attributes(
@pytest.mark.parametrize(
"gte, lte, expected_products_index",
[
- (None, 8, [1]),
- (0, 8, [1]),
+ (None, 8, [1, 2]),
+ (0, 8, [1, 2]),
(7, 8, []),
- (5, None, [0, 1]),
+ (5, None, [0, 1, 2]),
(8, 10, [0]),
(12, None, [0]),
(20, None, []),
(20, 8, []),
+ (5, 5, [1, 2]),
],
)
def test_products_query_with_filter_numeric_attributes(
@@ -131,15 +132,28 @@ def test_products_query_with_filter_numeric_attributes(
category=category,
)
attr_value = AttributeValue.objects.create(
- attribute=numeric_attribute, name="5.2", slug="5_2"
+ attribute=numeric_attribute, name="5", slug="5"
)
associate_attribute_values_to_instance(
second_product, numeric_attribute, attr_value
)
+ third_product = Product.objects.create(
+ name="Third product",
+ slug="third-product",
+ product_type=product_type,
+ category=category,
+ )
+ attr_value = AttributeValue.objects.create(
+ attribute=numeric_attribute, name="5", slug="5_X"
+ )
+
+ associate_attribute_values_to_instance(third_product, numeric_attribute, attr_value)
+
second_product.refresh_from_db()
- products_instances = [product, second_product]
+ third_product.refresh_from_db()
+ products_instances = [product, second_product, third_product]
products_ids = [
graphene.Node.to_global_id("Product", p.pk) for p in products_instances
]