Skip to content

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

Fix product search string generator for num attr

fowczrek521aa9e2d65e
diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.py
index a3ac769666..bfcf856f9d 100644
--- a/saleor/graphql/product/tests/test_product.py
+++ b/saleor/graphql/product/tests/test_product.py
@@ -2706,6 +2706,53 @@ def test_products_query_with_filter_search_by_numeric_attribute_value(
assert products[0]["node"]["name"] == product_with_numeric_attr.name
+def test_products_query_with_filter_search_by_numeric_attribute_value_without_unit(
+ query_products_with_filter,
+ staff_api_client,
+ product_list,
+ permission_manage_products,
+ channel_USD,
+ numeric_attribute_without_unit,
+):
+ # given
+ numeric_attribute = numeric_attribute_without_unit
+ product_with_numeric_attr = product_list[1]
+
+ product_type = product_with_numeric_attr.product_type
+ product_type.product_attributes.add(numeric_attribute)
+
+ numeric_attr_value = numeric_attribute.values.first()
+ numeric_attr_value.name = "13456"
+ numeric_attr_value.save(update_fields=["name"])
+
+ associate_attribute_values_to_instance(
+ product_with_numeric_attr, numeric_attribute, numeric_attr_value
+ )
+
+ product_with_numeric_attr.refresh_from_db()
+
+ product_with_numeric_attr.search_document = prepare_product_search_document_value(
+ product_with_numeric_attr
+ )
+ product_with_numeric_attr.save(update_fields=["search_document"])
+
+ variables = {"filter": {"search": "13456"}, "channel": channel_USD.slug}
+
+ # when
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+
+ # then
+ response = staff_api_client.post_graphql(query_products_with_filter, variables)
+ content = get_graphql_content(response)
+ products = content["data"]["products"]["edges"]
+
+ assert len(products) == 1
+ assert products[0]["node"]["id"] == graphene.Node.to_global_id(
+ "Product", product_with_numeric_attr.id
+ )
+ assert products[0]["node"]["name"] == product_with_numeric_attr.name
+
+
@pytest.mark.parametrize("search_value", ["2020", "2020 10 10", "2020-10-10"])
def test_products_query_with_filter_search_by_date_attribute_value(
search_value,
diff --git a/saleor/product/search.py b/saleor/product/search.py
index be35c2e8df..569ac7040f 100644
--- a/saleor/product/search.py
+++ b/saleor/product/search.py
@@ -102,7 +102,7 @@ def generate_attributes_search_document_value(
clean_editor_js(value.rich_text, to_string=True) for value in values
]
elif input_type == AttributeInputType.NUMERIC:
- unit = attribute.unit
+ unit = attribute.unit or ""
values_list = [value.name + unit for value in values]
elif input_type in [AttributeInputType.DATE, AttributeInputType.DATE_TIME]:
values_list = [value.date_time.isoformat() for value in values]
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index b1bd6988d9..188682f486 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -1319,6 +1319,22 @@ def numeric_attribute(db):
return attribute
+def numeric_attribute_without_unit(db):
+ attribute = Attribute.objects.create(
+ slug="count",
+ name="Count",
+ type=AttributeType.PRODUCT_TYPE,
+ input_type=AttributeInputType.NUMERIC,
+ filterable_in_storefront=True,
+ filterable_in_dashboard=True,
+ available_in_grid=True,
+ )
+ AttributeValue.objects.create(attribute=attribute, name="9", slug="9")
+ AttributeValue.objects.create(attribute=attribute, name="15", slug="15")
+ return attribute
+
+
@pytest.fixture
def file_attribute(db):
attribute = Attribute.objects.create(