Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Remove slug from AttributeValueInput
fowczarekd3ea4666c263
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.pyindex d98c52c02d..110e1bb500 100644--- a/saleor/graphql/product/mutations/products.py+++ b/saleor/graphql/product/mutations/products.py@@ -451,7 +451,6 @@ class CategoryClearPrivateMeta(ClearMetaBaseMutation):class AttributeValueInput(InputObjectType):id = graphene.ID(description="ID of the selected attribute.")- slug = graphene.String(description="Slug of the selected attribute.")values = graphene.List(graphene.String,required=True,diff --git a/tests/api/test_product.py b/tests/api/test_product.pyindex d4b4b0e9c9..2cfc84c5c0 100644--- a/tests/api/test_product.py+++ b/tests/api/test_product.py@@ -1,7 +1,7 @@import jsonfrom datetime import datetimefrom decimal import Decimal-from unittest.mock import Mock, patch+from unittest.mock import ANY, Mock, patchimport grapheneimport pytest@@ -15,6 +15,7 @@ from saleor.extensions.manager import ExtensionsManagerfrom saleor.graphql.core.enums import ReportingPeriodfrom saleor.graphql.product.enums import StockAvailabilityfrom saleor.product import AttributeInputType+from saleor.product.error_codes import ProductErrorCodefrom saleor.product.models import (Attribute,AttributeValue,@@ -1236,9 +1237,10 @@ def test_update_product(SET_ATTRIBUTES_TO_PRODUCT_QUERY = """mutation updateProduct($productId: ID!, $attributes: [AttributeValueInput!]) {productUpdate(id: $productId, input: { attributes: $attributes }) {- errors {+ productErrors {messagefield+ code}}}@@ -1270,11 +1272,8 @@ def test_update_product_can_only_assign_multiple_values_to_valid_input_types(data = get_graphql_content(staff_api_client.post_graphql(SET_ATTRIBUTES_TO_PRODUCT_QUERY, variables))["data"]["productUpdate"]- assert data["errors"] == [- {- "field": "attributes",- "message": "A dropdown attribute must take only one value",- }+ assert data["productErrors"] == [+ {"field": "attributes", "code": ProductErrorCode.INVALID.name, "message": ANY}]# Try to assign multiple values from a valid attribute@@ -1282,7 +1281,7 @@ def test_update_product_can_only_assign_multiple_values_to_valid_input_types(data = get_graphql_content(staff_api_client.post_graphql(SET_ATTRIBUTES_TO_PRODUCT_QUERY, variables))["data"]["productUpdate"]- assert not data["errors"]+ assert not data["productErrors"]def test_update_product_with_existing_attribute_value(@@ -1306,7 +1305,7 @@ def test_update_product_with_existing_attribute_value(data = get_graphql_content(staff_api_client.post_graphql(SET_ATTRIBUTES_TO_PRODUCT_QUERY, variables))["data"]["productUpdate"]- assert not data["errors"]+ assert not data["productErrors"]assert (color_attribute.values.count() == expected_attribute_values_count@@ -1322,6 +1321,7 @@ def test_update_product_without_supplying_required_product_attribute(staff_api_client.user.user_permissions.add(permission_manage_products)product_type = product.product_type+ color_attribute_id = graphene.Node.to_global_id("Attribute", color_attribute.id)# Create and assign a new attribute requiring a value to be always suppliedrequired_attribute = Attribute.objects.create(@@ -1332,43 +1332,25 @@ def test_update_product_without_supplying_required_product_attribute(# Try to assign multiple values from an attribute that does not support such thingsvariables = {"productId": graphene.Node.to_global_id("Product", product.pk),- "attributes": [{"slug": color_attribute.slug, "values": ["Blue"]}],+ "attributes": [{"id": color_attribute_id, "values": ["Blue"]}],}data = get_graphql_content(staff_api_client.post_graphql(SET_ATTRIBUTES_TO_PRODUCT_QUERY, variables))["data"]["productUpdate"]- assert data["errors"] == [- {- "field": "attributes",- "message": (- "All attributes flagged as having a value required must be supplied."- ),- }+ assert data["productErrors"] == [+ {"field": "attributes", "code": ProductErrorCode.REQUIRED.name, "message": ANY}]- "attributes_input, expected_message",- (- (- [{"id": "QXR0cmlidXRlOjA=", "values": ["hello"]}], # no such ID (id=0)- "Could not resolve to a node: ids=['QXR0cmlidXRlOjA='] and slugs=[]",- ),- (- [{"slug": "Oopsie.", "values": ["hello"]}], # no such slug- "Could not resolve to a node: ids=[] and slugs=['Oopsie.']",- ),- ),-)def test_update_product_with_non_existing_attribute(- staff_api_client,- product,- permission_manage_products,- color_attribute,- attributes_input,- expected_message,+ staff_api_client, product, permission_manage_products, color_attribute):+ non_existent_attribute_pk = 0+ invalid_attribute_id = graphene.Node.to_global_id(+ "Attribute", non_existent_attribute_pk+ )+"""Ensure assigning an existing value to a product doesn't create a newattribute value."""@@ -1377,13 +1359,15 @@ def test_update_product_with_non_existing_attribute(# Try to assign multiple values from an attribute that does not support such thingsvariables = {"productId": graphene.Node.to_global_id("Product", product.pk),- "attributes": attributes_input,+ "attributes": [{"id": invalid_attribute_id, "values": ["hello"]}],}data = get_graphql_content(staff_api_client.post_graphql(SET_ATTRIBUTES_TO_PRODUCT_QUERY, variables))["data"]["productUpdate"]- assert data["errors"] == [{"field": "attributes", "message": expected_message}]+ assert data["productErrors"] == [+ {"field": "attributes", "code": ProductErrorCode.NOT_FOUND.name, "message": ANY}+ ]def test_update_product_with_no_attribute_slug_or_id(@@ -1402,8 +1386,8 @@ def test_update_product_with_no_attribute_slug_or_id(data = get_graphql_content(staff_api_client.post_graphql(SET_ATTRIBUTES_TO_PRODUCT_QUERY, variables))["data"]["productUpdate"]- assert data["errors"] == [- {"field": "attributes", "message": "You must whether supply an ID or a slug"}+ assert data["productErrors"] == [+ {"field": "attributes", "code": ProductErrorCode.REQUIRED.name, "message": ANY}]diff --git a/tests/api/test_product_minimal_variant_price.py b/tests/api/test_product_minimal_variant_price.pyindex b17e737f04..7659698c31 100644--- a/tests/api/test_product_minimal_variant_price.py+++ b/tests/api/test_product_minimal_variant_price.py@@ -1,5 +1,6 @@from unittest.mock import patch+import grapheneimport pytestfrom freezegun import freeze_timefrom graphql_relay import from_global_id, to_global_id@@ -141,7 +142,7 @@ def test_product_variant_create_updates_minimal_variant_price(productVariant {name}- errors {+ productErrors {messagefield}@@ -151,13 +152,15 @@ def test_product_variant_create_updates_minimal_variant_price(product_id = to_global_id("Product", product.pk)sku = "1"price_override = "1.99"- variant_slug = product.product_type.variant_attributes.first().slug+ variant_id = graphene.Node.to_global_id(+ "Attribute", product.product_type.variant_attributes.first().pk+ )variant_value = "test-value"variables = {"productId": product_id,"sku": sku,"priceOverride": price_override,- "attributes": [{"slug": variant_slug, "values": [variant_value]}],+ "attributes": [{"id": variant_id, "values": [variant_value]}],}response = staff_api_client.post_graphql(query, variables, permissions=[permission_manage_products]@@ -166,7 +169,7 @@ def test_product_variant_create_updates_minimal_variant_price(content = get_graphql_content(response)data = content["data"]["productVariantCreate"]- assert data["errors"] == []+ assert data["productErrors"] == []mock_update_product_minimal_variant_price_task.delay.assert_called_once_with(product.pkdiff --git a/tests/api/test_variant.py b/tests/api/test_variant.pyindex f469f5f575..42d429c8ec 100644--- a/tests/api/test_variant.py+++ b/tests/api/test_variant.py@@ -1,4 +1,3 @@-from decimal import Decimalfrom unittest.mock import ANYfrom uuid import uuid4@@ -86,7 +85,7 @@ def test_create_variant(trackInventory: $trackInventory,weight: $weight}) {- errors {+ productErrors {fieldmessage}@@ -128,6 +127,9 @@ def test_create_variant(quantity = 10weight = 10.22variant_slug = product_type.variant_attributes.first().slug+ variant_id = graphene.Node.to_global_id(+ "Attribute", product_type.variant_attributes.first().pk+ )variant_value = "test-value"variables = {@@ -137,14 +139,14 @@ def test_create_variant("costPrice": cost_price,"priceOverride": price_override,"weight": weight,- "attributes": [{"slug": variant_slug, "values": [variant_value]}],+ "attributes": [{"id": variant_id, "values": [variant_value]}],"trackInventory": True,}response = staff_api_client.post_graphql(query, variables, permissions=[permission_manage_products])content = get_graphql_content(response)["data"]["productVariantCreate"]- assert not content["errors"]+ assert not content["productErrors"]data = content["productVariant"]assert data["name"] == variant_valueassert data["quantity"] == quantity@@ -171,8 +173,9 @@ def test_create_product_variant_not_all_attributes(sku: $sku,attributes: $attributes}) {- errors {+ productErrors {field+ codemessage}}@@ -181,23 +184,26 @@ def test_create_product_variant_not_all_attributes("""product_id = graphene.Node.to_global_id("Product", product.pk)sku = "1"- variant_slug = product_type.variant_attributes.first().slug+ variant_id = graphene.Node.to_global_id(+ "Attribute", product_type.variant_attributes.first().pk+ )variant_value = "test-value"product_type.variant_attributes.add(color_attribute)variables = {"productId": product_id,"sku": sku,- "attributes": [{"slug": variant_slug, "values": [variant_value]}],+ "attributes": [{"id": variant_id, "values": [variant_value]}],}response = staff_api_client.post_graphql(query, variables, permissions=[permission_manage_products])content = get_graphql_content(response)- assert content["data"]["productVariantCreate"]["errors"]- assert content["data"]["productVariantCreate"]["errors"][0] == {+ assert content["data"]["productVariantCreate"]["productErrors"]+ assert content["data"]["productVariantCreate"]["productErrors"][0] == {"field": "attributes",- "message": "All attributes must take a value",+ "code": ProductErrorCode.REQUIRED.name,+ "message": ANY,}assert not product.variants.filter(sku=sku).exists()@@ -648,52 +654,6 @@ PRODUCT_VARIANT_BULK_CREATE_MUTATION = """"""-def test_product_variant_bulk_create_by_attribute_slug(- staff_api_client, product, size_attribute, permission_manage_products-):- product_variant_count = ProductVariant.objects.count()- attribute_value_count = size_attribute.values.count()- product_id = graphene.Node.to_global_id("Product", product.pk)- attribute_value = size_attribute.values.last()- sku = str(uuid4())[:12]- quantity = 1000- cost_price = 15.5- price_override = 9.5- weight = 2.5- track_inventory = True- variants = [- {- "sku": sku,- "quantity": quantity,- "costPrice": cost_price,- "priceOverride": price_override,- "weight": weight,- "trackInventory": track_inventory,- "attributes": [- {"slug": size_attribute.slug, "values": [attribute_value.name]}- ],- }- ]-- variables = {"productId": product_id, "variants": variants}- staff_api_client.user.user_permissions.add(permission_manage_products)- response = staff_api_client.post_graphql(- PRODUCT_VARIANT_BULK_CREATE_MUTATION, variables- )- content = get_graphql_content(response)- data = content["data"]["productVariantBulkCreate"]- assert not data["bulkProductErrors"]- assert data["count"] == 1- assert product_variant_count + 1 == ProductVariant.objects.count()- assert attribute_value_count == size_attribute.values.count()- product_variant = ProductVariant.objects.get(sku=sku)- assert product_variant.quantity == quantity- assert product_variant.cost_price.amount == cost_price- assert product_variant.price_override.amount == Decimal(price_override)- assert product_variant.weight.value == weight- assert product_variant.track_inventory == track_inventory--def test_product_variant_bulk_create_by_attribute_id(staff_api_client, product, size_attribute, permission_manage_products):@@ -755,18 +715,17 @@ def test_product_variant_bulk_create_with_new_attribute_value():product_variant_count = ProductVariant.objects.count()attribute_value_count = size_attribute.values.count()+ size_attribute_id = graphene.Node.to_global_id("Attribute", size_attribute.pk)product_id = graphene.Node.to_global_id("Product", product.pk)attribute_value = size_attribute.values.last()variants = [{"sku": str(uuid4())[:12],- "attributes": [- {"slug": size_attribute.slug, "values": [attribute_value.name]}- ],+ "attributes": [{"id": size_attribute_id, "values": [attribute_value.name]}],},{"sku": str(uuid4())[:12],- "attributes": [{"slug": size_attribute.slug, "values": ["Test-attribute"]}],+ "attributes": [{"id": size_attribute_id, "values": ["Test-attribute"]}],},]@@ -788,16 +747,17 @@ def test_product_variant_bulk_create_negative_quantity():product_variant_count = ProductVariant.objects.count()product_id = graphene.Node.to_global_id("Product", product.pk)+ size_attribute_id = graphene.Node.to_global_id("Attribute", size_attribute.pk)variants = [{"sku": str(uuid4())[:12],"quantity": -1000,- "attributes": [{"slug": size_attribute.slug, "values": ["Test-value"]}],+ "attributes": [{"id": size_attribute_id, "values": ["Test-value"]}],},{"sku": str(uuid4())[:12],"quantity": 100,- "attributes": [{"slug": size_attribute.slug, "values": ["Test-value2"]}],+ "attributes": [{"id": size_attribute_id, "values": ["Test-value2"]}],},]@@ -825,17 +785,18 @@ def test_product_variant_bulk_create_duplicated_sku():product_variant_count = ProductVariant.objects.count()product_id = graphene.Node.to_global_id("Product", product.pk)+ size_attribute_id = graphene.Node.to_global_id("Attribute", size_attribute.pk)sku = product.variants.first().skusku2 = product_with_default_variant.variants.first().skuassert not sku == sku2variants = [{"sku": sku,- "attributes": [{"slug": size_attribute.slug, "values": ["Test-value"]}],+ "attributes": [{"id": size_attribute_id, "values": ["Test-value"]}],},{"sku": sku2,- "attributes": [{"slug": size_attribute.slug, "values": ["Test-valuee"]}],+ "attributes": [{"id": size_attribute_id, "values": ["Test-valuee"]}],},]@@ -860,15 +821,16 @@ def test_product_variant_bulk_create_duplicated_sku_in_input():product_variant_count = ProductVariant.objects.count()product_id = graphene.Node.to_global_id("Product", product.pk)+ size_attribute_id = graphene.Node.to_global_id("Attribute", size_attribute.pk)sku = str(uuid4())[:12]variants = [{"sku": sku,- "attributes": [{"slug": size_attribute.slug, "values": ["Test-value"]}],+ "attributes": [{"id": size_attribute_id, "values": ["Test-value"]}],},{"sku": sku,- "attributes": [{"slug": size_attribute.slug, "values": ["Test-value2"]}],+ "attributes": [{"id": size_attribute_id, "values": ["Test-value2"]}],},]@@ -887,69 +849,37 @@ def test_product_variant_bulk_create_duplicated_sku_in_input(assert product_variant_count == ProductVariant.objects.count()-def test_product_variant_bulk_create_invalid_attribute_slug(- staff_api_client, product, permission_manage_products-):- unasign_attribute = Attribute.objects.create(- slug="new_attribute", name="New attribute"- )- product_variant_count = ProductVariant.objects.count()- product_id = graphene.Node.to_global_id("Product", product.pk)- variants = [- {- "sku": str(uuid4())[:12],- "attributes": [{"slug": "invalid", "values": ["Test-value"]}],- },- {- "sku": str(uuid4())[:12],- "attributes": [{"slug": unasign_attribute.slug, "values": ["Test-value"]}],- },- ]-- variables = {"productId": product_id, "variants": variants}- staff_api_client.user.user_permissions.add(permission_manage_products)- response = staff_api_client.post_graphql(- PRODUCT_VARIANT_BULK_CREATE_MUTATION, variables- )- content = get_graphql_content(response)- data = content["data"]["productVariantBulkCreate"]- assert len(data["bulkProductErrors"]) == 2- errors = data["bulkProductErrors"]- assert errors[0]["field"] == "attributes"- assert errors[0]["code"] == ProductErrorCode.NOT_FOUND.name- assert errors[0]["index"] == 0- assert errors[1]["field"] == "attributes"- assert errors[1]["code"] == ProductErrorCode.NOT_FOUND.name- assert errors[1]["index"] == 1- assert product_variant_count == ProductVariant.objects.count()--def test_product_variant_bulk_create_many_errors(staff_api_client, product, size_attribute, permission_manage_products):product_variant_count = ProductVariant.objects.count()product_id = graphene.Node.to_global_id("Product", product.pk)+ size_attribute_id = graphene.Node.to_global_id("Attribute", size_attribute.pk)+ non_existent_attribute_pk = 0+ invalid_attribute_id = graphene.Node.to_global_id(+ "Attribute", non_existent_attribute_pk+ )sku = product.variants.first().skuvariants = [{"sku": str(uuid4())[:12],"quantity": -1000,- "attributes": [{"slug": size_attribute.slug, "values": ["Test-value1"]}],+ "attributes": [{"id": size_attribute_id, "values": ["Test-value1"]}],},{"sku": str(uuid4())[:12],"quantity": 100,- "attributes": [{"slug": size_attribute.slug, "values": ["Test-value4"]}],+ "attributes": [{"id": size_attribute_id, "values": ["Test-value4"]}],},{"sku": sku,"quantity": 100,- "attributes": [{"slug": size_attribute.slug, "values": ["Test-value2"]}],+ "attributes": [{"id": size_attribute_id, "values": ["Test-value2"]}],},{"sku": str(uuid4())[:12],"quantity": 100,- "attributes": [{"slug": "Invalid", "values": ["Test-value3"]}],+ "attributes": [{"id": invalid_attribute_id, "values": ["Test-value3"]}],},]