Skip to content

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

Fix failing export when attribute has no values

IKarbowiakcad053ccefb4
diff --git a/saleor/csv/tests/export/products_data/test_handle_relations_data.py b/saleor/csv/tests/export/products_data/test_handle_relations_data.py
index f01c375af1..9bd354e192 100644
--- a/saleor/csv/tests/export/products_data/test_handle_relations_data.py
+++ b/saleor/csv/tests/export/products_data/test_handle_relations_data.py
@@ -290,6 +290,29 @@ def test_prepare_products_relations_data_only_channel_ids(
assert result == expected_result
+def test_prepare_products_relations_data_attribute_without_values(
+ product,
+ channel_USD,
+ channel_PLN,
+):
+ # given
+ pk = product.pk
+
+ attribute_product = product.attributes.first()
+ attribute_product.values.clear()
+ attribute = attribute_product.assignment.attribute
+
+ qs = Product.objects.all()
+ fields = {"name"}
+ attribute_ids = [str(attribute.pk)]
+
+ # when
+ result = prepare_products_relations_data(qs, fields, attribute_ids, [])
+
+ # then
+ assert result == {pk: {f"{attribute.slug} (product attribute)": ""}}
+
+
@patch("saleor.csv.utils.products_data.prepare_variants_relations_data")
def test_get_variants_relations_data(prepare_variants_data_mocked, product_list):
# given
@@ -836,6 +859,30 @@ def test_add_attribute_info_to_data_no_slug(product):
assert result == input_data
+def test_add_attribute_info_when_no_value(product):
+ # given
+ pk = product.pk
+ slug = "test_attribute_slug"
+ attribute_data = AttributeData(
+ slug=slug,
+ value=None,
+ file_url=None,
+ input_type="dropdown",
+ entity_type=None,
+ rich_text=None,
+ )
+ input_data = {pk: {}}
+
+ # when
+ result = add_attribute_info_to_data(
+ product.pk, attribute_data, "product attribute", input_data
+ )
+
+ # then
+ expected_header = f"{slug} (product attribute)"
+ assert result[pk][expected_header] == {""}
+
+
def test_add_file_attribute_info_to_data(product):
# given
pk = product.pk
diff --git a/saleor/csv/utils/products_data.py b/saleor/csv/utils/products_data.py
index 422d361eae..66d9657e20 100644
--- a/saleor/csv/utils/products_data.py
+++ b/saleor/csv/utils/products_data.py
@@ -396,7 +396,7 @@ def add_attribute_info_to_data(
elif input_type == AttributeInputType.RICH_TEXT:
value = clean_editor_js(attribute_data.rich_text, to_string=True)
else:
- value = attribute_data.value
+ value = attribute_data.value if attribute_data.value else ""
if header in result_data[pk]:
result_data[pk][header].add(value) # type: ignore
else: