Skip to content

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

Fix failing products boolean filter (#16112)

Maciej Korycinski628700778707
diff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.py
index 8d0d0d3739..01f3e40cf3 100644
--- a/saleor/graphql/product/filters.py
+++ b/saleor/graphql/product/filters.py
@@ -220,10 +220,14 @@ def _clean_product_attributes_boolean_filter_input(
for attr in attributes
}
- for attr_slug, val in filter_value:
- attr_pk = values_map[attr_slug]["pk"]
- value_pk = values_map[attr_slug]["values"].get(val)
- if value_pk:
+ for attr_slug, value in filter_value:
+ if attr_slug not in values_map:
+ raise ValueError(f"Unknown attribute name: {attr_slug}")
+ attr_pk = values_map[attr_slug].get("pk")
+ value_pk = values_map[attr_slug]["values"].get(value)
+ if not value_pk:
+ raise ValueError(f"Requested value for attribute {attr_slug} doesn't exist")
+ if attr_pk and value_pk:
queries[attr_pk] += [value_pk]
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 927861c3bc..db44acef8a 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
@@ -265,6 +265,75 @@ def test_products_query_with_filter_boolean_attributes(
}
+ "filter_value",
+ [
+ False,
+ True,
+ ],
+)
+def test_products_query_with_filter_non_existing_boolean_attributes(
+ filter_value,
+ query_products_with_filter,
+ staff_api_client,
+ product,
+ permission_manage_products,
+):
+ # given
+
+ variables = {
+ "filter": {
+ "attributes": [{"slug": "non-existing-atr", "boolean": filter_value}]
+ }
+ }
+
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+
+ # when
+ response = staff_api_client.post_graphql(query_products_with_filter, variables)
+
+ # then
+ content = get_graphql_content(response)
+ products = content["data"]["products"]["edges"]
+ assert len(products) == 0
+
+
+ "filter_value",
+ [
+ False,
+ True,
+ ],
+)
+def test_products_query_with_filter_boolean_attributes_not_assigned_to_product(
+ filter_value,
+ query_products_with_filter,
+ staff_api_client,
+ product,
+ category,
+ boolean_attribute,
+ permission_manage_products,
+):
+ # given
+ boolean_attribute.values.all().delete()
+ product.product_type.product_attributes.add(boolean_attribute)
+
+ variables = {
+ "filter": {
+ "attributes": [{"slug": boolean_attribute.slug, "boolean": filter_value}]
+ }
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+
+ # when
+ response = staff_api_client.post_graphql(query_products_with_filter, variables)
+
+ # then
+ content = get_graphql_content(response)
+ products = content["data"]["products"]["edges"]
+ assert len(products) == 0
+
+
def test_products_query_with_filter_by_attributes_values_and_range(
query_products_with_filter,
staff_api_client,