Skip to content

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

Add tests for defining product with product reference attributes

IKarbowiakee6bc36d09fc
diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.py
index 47adbe442e..c8e8f9a6e2 100644
--- a/saleor/graphql/product/tests/test_product.py
+++ b/saleor/graphql/product/tests/test_product.py
@@ -2416,6 +2416,74 @@ def test_create_product_with_page_reference_attribute(
assert page_reference_attribute.values.count() == values_count + 1
+def test_create_product_with_product_reference_attribute(
+ staff_api_client,
+ product_type,
+ category,
+ color_attribute,
+ product_reference_attribute,
+ permission_manage_products,
+ product,
+):
+ query = CREATE_PRODUCT_MUTATION
+
+ values_count = product_reference_attribute.values.count()
+
+ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
+ category_id = graphene.Node.to_global_id("Category", category.pk)
+ product_name = "test name"
+ product_slug = "product-test-slug"
+
+ # Add second attribute
+ product_type.product_attributes.add(product_reference_attribute)
+ reference_attr_id = graphene.Node.to_global_id(
+ "Attribute", product_reference_attribute.id
+ )
+ reference = graphene.Node.to_global_id("Product", product.pk)
+
+ # test creating root product
+ variables = {
+ "input": {
+ "productType": product_type_id,
+ "category": category_id,
+ "name": product_name,
+ "slug": product_slug,
+ "attributes": [{"id": reference_attr_id, "references": [reference]}],
+ }
+ }
+
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["productCreate"]
+ assert data["productErrors"] == []
+ assert data["product"]["name"] == product_name
+ assert data["product"]["slug"] == product_slug
+ assert data["product"]["productType"]["name"] == product_type.name
+ assert data["product"]["category"]["name"] == category.name
+ _, product_id = graphene.Node.from_global_id(data["product"]["id"])
+ expected_attributes_data = [
+ {"attribute": {"slug": color_attribute.slug}, "values": []},
+ {
+ "attribute": {"slug": product_reference_attribute.slug},
+ "values": [
+ {
+ "slug": f"{product_id}_{product.id}",
+ "name": product.name,
+ "file": None,
+ "reference": reference,
+ }
+ ],
+ },
+ ]
+ for attr_data in data["product"]["attributes"]:
+ assert attr_data in expected_attributes_data
+
+ product_reference_attribute.refresh_from_db()
+ assert product_reference_attribute.values.count() == values_count + 1
+
+
def test_create_product_with_file_attribute_new_attribute_value(
staff_api_client,
product_type,
@@ -2634,6 +2702,55 @@ def test_create_product_with_page_reference_attribute_required_no_references(
]
+def test_create_product_with_product_reference_attribute_required_no_references(
+ staff_api_client,
+ product_type,
+ category,
+ product_reference_attribute,
+ permission_manage_products,
+):
+ query = CREATE_PRODUCT_MUTATION
+
+ product_reference_attribute.value_required = True
+ product_reference_attribute.save(update_fields=["value_required"])
+
+ product_type_id = graphene.Node.to_global_id("ProductType", product_type.pk)
+ category_id = graphene.Node.to_global_id("Category", category.pk)
+ product_name = "test name"
+ product_slug = "product-test-slug"
+
+ # Add second attribute
+ product_type.product_attributes.add(product_reference_attribute)
+ reference_attr_id = graphene.Node.to_global_id(
+ "Attribute", product_reference_attribute.id
+ )
+
+ # test creating root product
+ variables = {
+ "input": {
+ "productType": product_type_id,
+ "category": category_id,
+ "name": product_name,
+ "slug": product_slug,
+ "attributes": [{"id": reference_attr_id, "references": []}],
+ }
+ }
+
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["productCreate"]
+ errors = data["productErrors"]
+ assert not data["product"]
+ assert len(errors) == 1
+ assert errors[0]["code"] == ProductErrorCode.REQUIRED.name
+ assert errors[0]["field"] == "attributes"
+ assert errors[0]["attributes"] == [
+ graphene.Node.to_global_id("Attribute", product_reference_attribute.pk)
+ ]
+
+
def test_create_product_no_values_given(
staff_api_client,
product_type,
@@ -3524,7 +3641,7 @@ def test_update_product_rating(
@patch("saleor.plugins.manager.PluginsManager.product_updated")
-def test_update_product_with_reference_attribute_value(
+def test_update_product_with_page_reference_attribute_value(
updated_webhook_mock,
staff_api_client,
page_reference_attribute,
@@ -3583,7 +3700,7 @@ def test_update_product_with_reference_attribute_value(
@patch("saleor.plugins.manager.PluginsManager.product_updated")
-def test_update_product_with_reference_attribute_existing_value(
+def test_update_product_with_page_reference_attribute_existing_value(
updated_webhook_mock,
staff_api_client,
page_reference_attribute,
@@ -3650,7 +3767,7 @@ def test_update_product_with_reference_attribute_existing_value(
@patch("saleor.plugins.manager.PluginsManager.product_updated")
-def test_update_product_with_reference_attribute_value_not_given(
+def test_update_product_with_page_reference_attribute_value_not_given(
updated_webhook_mock,
staff_api_client,
page_reference_attribute,
@@ -3692,6 +3809,183 @@ def test_update_product_with_reference_attribute_value_not_given(
updated_webhook_mock.assert_not_called()
+@patch("saleor.plugins.manager.PluginsManager.product_updated")
+def test_update_product_with_product_reference_attribute_value(
+ updated_webhook_mock,
+ staff_api_client,
+ product_reference_attribute,
+ product_list,
+ product_type,
+ permission_manage_products,
+):
+ # given
+ query = MUTATION_UPDATE_PRODUCT
+
+ product = product_list[0]
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ product_ref = product_list[1]
+
+ attribute_id = graphene.Node.to_global_id(
+ "Attribute", product_reference_attribute.pk
+ )
+ product_type.product_attributes.add(product_reference_attribute)
+
+ values_count = product_reference_attribute.values.count()
+
+ reference = graphene.Node.to_global_id("Product", product_ref.pk)
+
+ variables = {
+ "productId": product_id,
+ "input": {"attributes": [{"id": attribute_id, "references": [reference]}]},
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["productUpdate"]
+ assert data["productErrors"] == []
+
+ attributes = data["product"]["attributes"]
+
+ assert len(attributes) == 2
+ expected_file_att_data = {
+ "attribute": {"id": attribute_id, "name": product_reference_attribute.name},
+ "values": [
+ {
+ "name": product_ref.name,
+ "slug": f"{product.id}_{product_ref.id}",
+ "file": None,
+ "reference": reference,
+ }
+ ],
+ }
+ assert expected_file_att_data in attributes
+
+ updated_webhook_mock.assert_called_once_with(product)
+
+ product_reference_attribute.refresh_from_db()
+ assert product_reference_attribute.values.count() == values_count + 1
+
+
+@patch("saleor.plugins.manager.PluginsManager.product_updated")
+def test_update_product_with_product_reference_attribute_existing_value(
+ updated_webhook_mock,
+ staff_api_client,
+ product_reference_attribute,
+ product_list,
+ product_type,
+ permission_manage_products,
+):
+ # given
+ query = MUTATION_UPDATE_PRODUCT
+
+ product = product_list[0]
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+ product_ref = product_list[1]
+
+ attribute_id = graphene.Node.to_global_id(
+ "Attribute", product_reference_attribute.pk
+ )
+ product_type.product_attributes.add(product_reference_attribute)
+ attr_value = AttributeValue.objects.create(
+ attribute=product_reference_attribute,
+ name=product_ref.name,
+ slug=f"{product.pk}_{product_ref.pk}",
+ )
+ associate_attribute_values_to_instance(
+ product, product_reference_attribute, attr_value
+ )
+
+ values_count = product_reference_attribute.values.count()
+
+ reference = graphene.Node.to_global_id("Product", product_ref.pk)
+
+ variables = {
+ "productId": product_id,
+ "input": {"attributes": [{"id": attribute_id, "references": [reference]}]},
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["productUpdate"]
+ assert data["productErrors"] == []
+
+ attributes = data["product"]["attributes"]
+
+ assert len(attributes) == 2
+ expected_file_att_data = {
+ "attribute": {"id": attribute_id, "name": product_reference_attribute.name},
+ "values": [
+ {
+ "name": product_ref.name,
+ "slug": f"{product.id}_{product_ref.id}",
+ "file": None,
+ "reference": reference,
+ }
+ ],
+ }
+ assert expected_file_att_data in attributes
+
+ updated_webhook_mock.assert_called_once_with(product)
+
+ product_reference_attribute.refresh_from_db()
+ assert product_reference_attribute.values.count() == values_count
+
+
+@patch("saleor.plugins.manager.PluginsManager.product_updated")
+def test_update_product_with_product_reference_attribute_value_not_given(
+ updated_webhook_mock,
+ staff_api_client,
+ product_reference_attribute,
+ product,
+ product_type,
+ permission_manage_products,
+):
+ # given
+ query = MUTATION_UPDATE_PRODUCT
+
+ product_reference_attribute.value_required = True
+ product_reference_attribute.save(update_fields=["value_required"])
+
+ product_id = graphene.Node.to_global_id("Product", product.pk)
+
+ attribute_id = graphene.Node.to_global_id(
+ "Attribute", product_reference_attribute.pk
+ )
+ product_type.product_attributes.add(product_reference_attribute)
+
+ variables = {
+ "productId": product_id,
+ "input": {"attributes": [{"id": attribute_id, "values": ["test"]}]},
+ }
+
+ # when
+ response = staff_api_client.post_graphql(
+ query, variables, permissions=[permission_manage_products]
+ )
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["productUpdate"]
+ errors = data["productErrors"]
+
+ assert not data["product"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "attributes"
+ assert errors[0]["code"] == AttributeErrorCode.REQUIRED.name
+
+ updated_webhook_mock.assert_not_called()
+
+
UPDATE_PRODUCT_SLUG_MUTATION = """
mutation($id: ID!, $slug: String) {
productUpdate(