Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix attributes field in the variant mutations
NyanKiyoshi2a613121163e
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.pyindex d28e38e121..3df4dc93a4 100644--- a/saleor/graphql/product/mutations/products.py+++ b/saleor/graphql/product/mutations/products.py@@ -31,8 +31,8 @@ from ...core.utils import (perform_reordering,validate_image_file,)-from ...product.types import AttributeValuefrom ..types import (+ Attribute,Category,Collection,MoveProductInput,@@ -678,9 +678,9 @@ class ProductVariantCreate(ModelMutation):if attr_id:attr_id = from_global_id_strict_type(- info, attr_id, only_type=AttributeValue, field="attributes"+ info, attr_id, only_type=Attribute, field="attributes")- input_id_map[attr_id] = values+ input_id_map[int(attr_id)] = valueselif slug:input_slug_map[slug] = valueselse:diff --git a/tests/api/test_product.py b/tests/api/test_product.pyindex 8ce4c8271e..e4d7735d65 100644--- a/tests/api/test_product.py+++ b/tests/api/test_product.py@@ -978,6 +978,7 @@ def test_update_product(permission_manage_products,settings,monkeypatch,+ color_attribute,):query = """mutation updateProduct(@@ -1022,10 +1023,12 @@ def test_update_product(}attributes {attribute {+ idname}values {name+ slug}}}@@ -1056,6 +1059,8 @@ def test_update_product(lambda self, x: TaxType(description="", code=product_tax_rate),)+ attribute_id = graphene.Node.to_global_id("Attribute", color_attribute.pk)+variables = {"productId": product_id,"categoryId": category_id,@@ -1065,6 +1070,7 @@ def test_update_product("chargeTaxes": product_charge_taxes,"taxCode": product_tax_rate,"basePrice": product_price,+ "attributes": [{"id": attribute_id, "values": ["Rainbow"]}],}response = staff_api_client.post_graphql(@@ -1080,6 +1086,15 @@ def test_update_product(assert data["product"]["taxType"]["taxCode"] == product_tax_rateassert not data["product"]["category"]["name"] == category.name+ attributes = data["product"]["attributes"]++ assert len(attributes) == 1+ assert len(attributes[0]["values"]) == 1++ assert attributes[0]["attribute"]["id"] == attribute_id+ assert attributes[0]["values"][0]["name"] == "Rainbow"+ assert attributes[0]["values"][0]["slug"] == "rainbow"+SET_ATTRIBUTES_TO_PRODUCT_QUERY = """mutation updateProduct($productId: ID!, $attributes: [AttributeValueInput!]) {diff --git a/tests/api/test_variant.py b/tests/api/test_variant.pyindex 67ce6c04a9..a3d5f64f7a 100644--- a/tests/api/test_variant.py+++ b/tests/api/test_variant.py@@ -194,6 +194,92 @@ def test_create_product_variant_not_all_attributes(assert not product.variants.filter(sku=sku).exists()+def test_create_product_variant_update_with_new_attributes(+ staff_api_client, permission_manage_products, product, size_attribute+):+ query = """+ mutation VariantUpdate(+ $id: ID!+ $attributes: [AttributeValueInput]+ $costPrice: Decimal+ $priceOverride: Decimal+ $sku: String+ $quantity: Int+ $trackInventory: Boolean!+ ) {+ productVariantUpdate(+ id: $id+ input: {+ attributes: $attributes+ costPrice: $costPrice+ priceOverride: $priceOverride+ sku: $sku+ quantity: $quantity+ trackInventory: $trackInventory+ }+ ) {+ errors {+ field+ message+ }+ productVariant {+ id+ attributes {+ attribute {+ id+ name+ slug+ values {+ id+ name+ slug+ __typename+ }+ __typename+ }+ value {+ id+ name+ slug+ __typename+ }+ __typename+ }+ }+ }+ }+ """++ size_attribute_id = graphene.Node.to_global_id("Attribute", size_attribute.pk)+ variant_id = graphene.Node.to_global_id(+ "ProductVariant", product.variants.first().pk+ )++ variables = {+ "attributes": [{"id": size_attribute_id, "values": ["XXXL"]}],+ "costPrice": 10,+ "id": variant_id,+ "priceOverride": 0,+ "quantity": 4,+ "sku": "21599567",+ "trackInventory": True,+ }++ data = get_graphql_content(+ staff_api_client.post_graphql(+ query, variables, permissions=[permission_manage_products]+ )+ )["data"]["productVariantUpdate"]+ assert not data["errors"]+ assert data["productVariant"]["id"] == variant_id++ attributes = data["productVariant"]["attributes"]+ assert len(attributes) == 1+ assert attributes[0]["attribute"]["id"] == size_attribute_id+ assert attributes[0]["value"]["name"] == "XXXL"+ assert attributes[0]["value"]["slug"] == "xxxl"++def test_update_product_variant(staff_api_client, product, permission_manage_products):query = """mutation updateVariant (@@ -253,6 +339,10 @@ def test_update_product_variant(staff_api_client, product, permission_manage_prodef test_update_product_variant_not_all_attributes(staff_api_client, product, product_type, color_attribute, permission_manage_products):+ """Ensures updating a variant with missing attributes (all attributes must+ be provided) raises an error. We expect the color attribute+ to be flagged as missing."""+query = """mutation updateVariant ($id: ID!,@@ -294,7 +384,7 @@ def test_update_product_variant_not_all_attributes(content = get_graphql_content(response)assert content["data"]["productVariantUpdate"]["errors"]assert content["data"]["productVariantUpdate"]["errors"][0]["field"] == (- "attributes"+ "attributes:color")assert not product.variants.filter(sku=sku).exists()