Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failing products boolean filter #16146

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions saleor/graphql/product/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,14 @@ def _clean_product_attributes_boolean_filter_input(filter_value, queries):
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]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,75 @@ def test_products_query_with_filter_boolean_attributes(
}


@pytest.mark.parametrize(
"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


@pytest.mark.parametrize(
"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,
Expand Down
Loading