Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Simplify ValidationError in Product, ProductType, Category and Collection
IKarbowiakb24ebcb52283
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.pyindex 32e12583a9..ea8f8a33ff 100644--- a/saleor/graphql/product/mutations/products.py+++ b/saleor/graphql/product/mutations/products.py@@ -99,15 +99,9 @@ class CategoryCreate(ModelMutation):cleaned_input = validate_slug_and_generate_if_needed(instance, "name", cleaned_input)- except ValidationError:- raise ValidationError(- {- "slug": ValidationError(- "Slug value cannot be blank.",- code=ProductErrorCode.REQUIRED.value,- )- }- )+ except ValidationError as error:+ error.code = ProductErrorCode.REQUIRED.value+ raise ValidationError({"slug": error})parent_id = data["parent_id"]if parent_id:parent = cls.get_node_or_error(@@ -220,15 +214,9 @@ class CollectionCreate(ModelMutation):cleaned_input = validate_slug_and_generate_if_needed(instance, "name", cleaned_input)- except ValidationError:- raise ValidationError(- {- "slug": ValidationError(- "Slug value cannot be blank.",- code=ProductErrorCode.REQUIRED.value,- )- }- )+ except ValidationError as error:+ error.code = ProductErrorCode.REQUIRED.value+ raise ValidationError({"slug": error})if data.get("background_image"):image_data = info.context.FILES.get(data["background_image"])validate_image_file(image_data, "background_image")@@ -817,16 +805,9 @@ class ProductCreate(ModelMutation):cleaned_input = validate_slug_and_generate_if_needed(instance, "name", cleaned_input)- except ValidationError:- raise ValidationError(- {- "slug": ValidationError(- "Slug value cannot be blank.",- code=ProductErrorCode.REQUIRED.value,- )- }- )-+ except ValidationError as error:+ error.code = ProductErrorCode.REQUIRED.value+ raise ValidationError({"slug": error})# Try to get price from "basePrice" or "price" field. Once "price" is removed# from the schema, only "basePrice" should be used here.price = data.get("base_price", data.get("price"))@@ -1360,15 +1341,9 @@ class ProductTypeCreate(ModelMutation):cleaned_input = validate_slug_and_generate_if_needed(instance, "name", cleaned_input)- except ValidationError:- raise ValidationError(- {- "slug": ValidationError(- "Slug value cannot be blank.",- code=ProductErrorCode.REQUIRED.value,- )- }- )+ except ValidationError as error:+ error.code = ProductErrorCode.REQUIRED.value+ raise ValidationError({"slug": error})# FIXME tax_rate logic should be dropped after we remove tax_rate from inputtax_rate = cleaned_input.pop("tax_rate", "")diff --git a/tests/api/test_category.py b/tests/api/test_category.pyindex 627620479d..24ea5ea61c 100644--- a/tests/api/test_category.py+++ b/tests/api/test_category.py@@ -6,6 +6,7 @@ import pytestfrom django.template.defaultfilters import slugifyfrom graphql_relay import to_global_id+from saleor.product.error_codes import ProductErrorCodefrom saleor.product.models import Categoryfrom tests.api.utils import get_graphql_content, get_multipart_request_bodyfrom tests.utils import create_image, create_pdf_file_with_image_ext@@ -164,9 +165,10 @@ def test_create_category_with_given_slug(nameslug}- errors {+ productErrors {fieldmessage+ code}}}@@ -178,7 +180,7 @@ def test_create_category_with_given_slug()content = get_graphql_content(response)data = content["data"]["categoryCreate"]- assert not data["errors"]+ assert not data["productErrors"]assert data["category"]["slug"] == expected_slug@@ -195,9 +197,10 @@ def test_category_create_mutation_without_background_image(},parent: $parentId) {- errors {+ productErrors {fieldmessage+ code}}}@@ -224,7 +227,7 @@ def test_category_create_mutation_without_background_image()content = get_graphql_content(response)data = content["data"]["categoryCreate"]- assert data["errors"] == []+ assert data["productErrors"] == []assert mock_create_thumbnails.call_count == 0@@ -401,9 +404,10 @@ UPDATE_CATEGORY_SLUG_MUTATION = """nameslug}- errors {+ productErrors {fieldmessage+ code}}}@@ -438,14 +442,14 @@ def test_update_category_slug()content = get_graphql_content(response)data = content["data"]["categoryUpdate"]- errors = data["errors"]+ errors = data["productErrors"]if not error_message:assert not errorsassert data["category"]["slug"] == expected_slugelse:assert errorsassert errors[0]["field"] == "slug"- assert errors[0]["message"] == error_message+ assert errors[0]["code"] == ProductErrorCode.REQUIRED.namedef test_update_category_slug_exists(@@ -468,10 +472,10 @@ def test_update_category_slug_exists()content = get_graphql_content(response)data = content["data"]["categoryUpdate"]- errors = data["errors"]+ errors = data["productErrors"]assert errorsassert errors[0]["field"] == "slug"- assert errors[0]["message"] == "Category with this Slug already exists."+ assert errors[0]["code"] == ProductErrorCode.UNIQUE.name@pytest.mark.parametrize(@@ -508,9 +512,10 @@ def test_update_category_slug_and_name(nameslug}- errors {+ productErrors {fieldmessage+ code}}}@@ -530,14 +535,14 @@ def test_update_category_slug_and_name(content = get_graphql_content(response)category.refresh_from_db()data = content["data"]["categoryUpdate"]- errors = data["errors"]+ errors = data["productErrors"]if not error_message:assert data["category"]["name"] == input_name == category.nameassert data["category"]["slug"] == input_slug == category.slugelse:assert errorsassert errors[0]["field"] == error_field- assert errors[0]["message"] == error_message+ assert errors[0]["code"] == ProductErrorCode.REQUIRED.nameMUTATION_CATEGORY_DELETE = """diff --git a/tests/api/test_collection.py b/tests/api/test_collection.pyindex 361973c256..918180950f 100644--- a/tests/api/test_collection.py+++ b/tests/api/test_collection.py@@ -4,9 +4,9 @@ from unittest.mock import Mockimport grapheneimport pytest-from django.utils.text import slugifyfrom graphql_relay import to_global_id+from saleor.product.error_codes import ProductErrorCodefrom saleor.product.models import Collectionfrom tests.utils import create_image, create_pdf_file_with_image_ext@@ -197,9 +197,10 @@ def test_create_collection_with_given_slug(nameslug}- errors {+ productErrors {fieldmessage+ code}}}@@ -211,7 +212,7 @@ def test_create_collection_with_given_slug()content = get_graphql_content(response)data = content["data"]["collectionCreate"]- assert not data["errors"]+ assert not data["productErrors"]assert data["collection"]["slug"] == expected_slug@@ -378,9 +379,10 @@ UPDATE_COLLECTION_SLUG_MUTATION = """nameslug}- errors {+ productErrors {fieldmessage+ code}}}@@ -415,14 +417,14 @@ def test_update_collection_slug()content = get_graphql_content(response)data = content["data"]["collectionUpdate"]- errors = data["errors"]+ errors = data["productErrors"]if not error_message:assert not errorsassert data["collection"]["slug"] == expected_slugelse:assert errorsassert errors[0]["field"] == "slug"- assert errors[0]["message"] == error_message+ assert errors[0]["code"] == ProductErrorCode.REQUIRED.namedef test_update_collection_slug_exists(@@ -446,10 +448,10 @@ def test_update_collection_slug_exists()content = get_graphql_content(response)data = content["data"]["collectionUpdate"]- errors = data["errors"]+ errors = data["productErrors"]assert errorsassert errors[0]["field"] == "slug"- assert errors[0]["message"] == "Collection with this Slug already exists."+ assert errors[0]["code"] == ProductErrorCode.UNIQUE.name@pytest.mark.parametrize(@@ -486,9 +488,10 @@ def test_update_collection_slug_and_name(nameslug}- errors {+ productErrors {fieldmessage+ code}}}@@ -508,14 +511,14 @@ def test_update_collection_slug_and_name(content = get_graphql_content(response)collection.refresh_from_db()data = content["data"]["collectionUpdate"]- errors = data["errors"]+ errors = data["productErrors"]if not error_message:assert data["collection"]["name"] == input_name == collection.nameassert data["collection"]["slug"] == input_slug == collection.slugelse:assert errorsassert errors[0]["field"] == error_field- assert errors[0]["message"] == error_message+ assert errors[0]["code"] == ProductErrorCode.REQUIRED.namedef test_delete_collection(staff_api_client, collection, permission_manage_products):@@ -540,32 +543,6 @@ def test_delete_collection(staff_api_client, collection, permission_manage_producollection.refresh_from_db()-def test_auto_create_slug_on_collection(- staff_api_client, product_list, permission_manage_products-):- query = """- mutation createCollection(- $name: String!, $isPublished: Boolean!) {- collectionCreate(- input: {name: $name, isPublished: $isPublished}) {- collection {- name- slug- }- }- }- """- name = "test name123"- variables = {"name": name, "isPublished": True}- response = staff_api_client.post_graphql(- query, variables, permissions=[permission_manage_products]- )- content = get_graphql_content(response)- data = content["data"]["collectionCreate"]["collection"]- assert data["name"] == name- assert data["slug"] == slugify(name)--def test_add_products_to_collection(staff_api_client, collection, product_list, permission_manage_products):diff --git a/tests/api/test_product.py b/tests/api/test_product.pyindex 1528d20d4f..d999a315df 100644--- a/tests/api/test_product.py+++ b/tests/api/test_product.py@@ -1362,9 +1362,10 @@ UPDATE_PRODUCT_SLUG_MUTATION = """nameslug}- errors {+ productErrors {fieldmessage+ code}}}@@ -1399,14 +1400,14 @@ def test_update_product_slug()content = get_graphql_content(response)data = content["data"]["productUpdate"]- errors = data["errors"]+ errors = data["productErrors"]if not error_message:assert not errorsassert data["product"]["slug"] == expected_slugelse:assert errorsassert errors[0]["field"] == "slug"- assert errors[0]["message"] == error_message+ assert errors[0]["code"] == ProductErrorCode.REQUIRED.namedef test_update_product_slug_exists(@@ -1429,10 +1430,10 @@ def test_update_product_slug_exists()content = get_graphql_content(response)data = content["data"]["productUpdate"]- errors = data["errors"]+ errors = data["productErrors"]assert errorsassert errors[0]["field"] == "slug"- assert errors[0]["message"] == "Product with this Slug already exists."+ assert errors[0]["code"] == ProductErrorCode.UNIQUE.name@pytest.mark.parametrize(@@ -1469,9 +1470,10 @@ def test_update_product_slug_and_name(nameslug}- errors {+ productErrors {fieldmessage+ code}}}@@ -1491,14 +1493,14 @@ def test_update_product_slug_and_name(content = get_graphql_content(response)product.refresh_from_db()data = content["data"]["productUpdate"]- errors = data["errors"]+ errors = data["productErrors"]if not error_message:assert data["product"]["name"] == input_name == product.nameassert data["product"]["slug"] == input_slug == product.slugelse:assert errorsassert errors[0]["field"] == error_field- assert errors[0]["message"] == error_message+ assert errors[0]["code"] == ProductErrorCode.REQUIRED.nameSET_ATTRIBUTES_TO_PRODUCT_QUERY = """@@ -2092,9 +2094,10 @@ def test_create_product_type_with_given_slug(nameslug}- errors {+ productErrors {fieldmessage+ code}}}@@ -2106,7 +2109,7 @@ def test_create_product_type_with_given_slug()content = get_graphql_content(response)data = content["data"]["productTypeCreate"]- assert not data["errors"]+ assert not data["productErrors"]assert data["productType"]["slug"] == expected_slug@@ -2190,9 +2193,10 @@ UPDATE_PRODUCT_TYPE_SLUG_MUTATION = """nameslug}- errors {+ productErrors {fieldmessage+ code}}}@@ -2227,14 +2231,14 @@ def test_update_product_type_slug()content = get_graphql_content(response)data = content["data"]["productTypeUpdate"]- errors = data["errors"]+ errors = data["productErrors"]if not error_message:assert not errorsassert data["productType"]["slug"] == expected_slugelse:assert errorsassert errors[0]["field"] == "slug"- assert errors[0]["message"] == error_message+ assert errors[0]["code"] == ProductErrorCode.REQUIRED.namedef test_update_product_type_slug_exists(@@ -2257,10 +2261,10 @@ def test_update_product_type_slug_exists()content = get_graphql_content(response)data = content["data"]["productTypeUpdate"]- errors = data["errors"]+ errors = data["productErrors"]assert errorsassert errors[0]["field"] == "slug"- assert errors[0]["message"] == "Product type with this Slug already exists."+ assert errors[0]["code"] == ProductErrorCode.UNIQUE.name@pytest.mark.parametrize(@@ -2297,9 +2301,10 @@ def test_update_product_type_slug_and_name(nameslug}- errors {+ productErrors {fieldmessage+ code}}}@@ -2319,14 +2324,14 @@ def test_update_product_type_slug_and_name(content = get_graphql_content(response)product_type.refresh_from_db()data = content["data"]["productTypeUpdate"]- errors = data["errors"]+ errors = data["productErrors"]if not error_message:assert data["productType"]["name"] == input_name == product_type.nameassert data["productType"]["slug"] == input_slug == product_type.slugelse:assert errorsassert errors[0]["field"] == error_field- assert errors[0]["message"] == error_message+ assert errors[0]["code"] == ProductErrorCode.REQUIRED.namedef test_product_type_delete_mutation(