Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add tests for Attribute and AttributeValue mutations
Pacu2500ed1bba441
diff --git a/tests/api/test_attributes.py b/tests/api/test_attributes.pyindex f5580a3ae3..a75a967672 100644--- a/tests/api/test_attributes.py+++ b/tests/api/test_attributes.py@@ -1,13 +1,16 @@import json-import grapheneimport pytest++import graphenefrom django.shortcuts import reverse-from tests.api.utils import get_graphql_content-from saleor.product.models import (- Category, Attribute, AttributeValue)+from django.template.defaultfilters import slugify+from saleor.graphql.product.types import (+ AttributeValueType, resolve_attribute_value_type)from saleor.graphql.product.utils import attributes_to_hstore-from saleor.graphql.product.types import resolve_attribute_value_type, AttributeValueType+from saleor.product.models import Attribute, AttributeValue, Category+from tests.api.utils import get_graphql_content+from tests.utils import get_graphql_contentdef test_attributes_to_hstore(product, color_attribute):@@ -90,10 +93,9 @@ def test_attributes_in_category_query(user_api_client, product):CREATE_ATTRIBUTES_QUERY = """mutation createAttribute(- $name: String!, $slug: String!,- $values: [AttributeValueCreateInput]) {+ $name: String!, $values: [AttributeCreateValueInput]) {attributeCreate(- input: {name: $name, slug: $slug, values: $values}) {+ input: {name: $name, values: $values}) {errors {fieldmessage@@ -128,62 +130,161 @@ def test_create_attribute(admin_api_client):def test_create_attribute_and_attribute_values(admin_api_client):query = CREATE_ATTRIBUTES_QUERY+ attribute_name = 'Example name'name = 'Value name'- slug = 'value-slug'variables = json.dumps({- 'name': 'Example name', 'slug': 'example-slug',- 'values': [{'slug': slug, 'name': name, 'value': '#1231'}]})+ 'name': attribute_name, 'values': [{'name': name, 'value': '#1231'}]})response = admin_api_client.post(reverse('api'), {'query': query, 'variables': variables})content = get_graphql_content(response)assert not content['data']['attributeCreate']['errors']- data = content['data']['attributeCreate']['attribute']['values']- assert len(data) == 1- assert data[0]['name'] == name- assert data[0]['slug'] == slug---def test_create_attribute_and_attribute_values_errors(admin_api_client):+ data = content['data']['attributeCreate']['attribute']+ assert data['name'] == attribute_name+ assert data['slug'] == slugify(attribute_name)++ assert len(data['values']) == 1+ assert data['values'][0]['name'] == name+ assert data['values'][0]['slug'] == slugify(name)+++ 'name_1, name_2, error_msg', (+ (+ 'Red color', 'Red color',+ 'Duplicated AttributeValue names provided.'),+ (+ 'Red color', 'red color',+ 'Provided AttributeValue names are not unique.')))+def test_create_attribute_and_attribute_values_errors(+ admin_api_client, name_1, name_2, error_msg):query = CREATE_ATTRIBUTES_QUERYvariables = json.dumps({'name': 'Example name', 'slug': 'example-slug','values': [- {'slug': 'slug', 'name': 'Red Color', 'value': '#1231'},- {'slug': 'Incorrect slug', 'name': 'Red Color', 'value': '#121'}]})+ {'name': name_1, 'value': '#1231'},+ {'name': name_2, 'value': '#121'}]})response = admin_api_client.post(reverse('api'), {'query': query, 'variables': variables})content = get_graphql_content(response)errors = content['data']['attributeCreate']['errors']assert errorsassert errors[0]['field'] == 'values'- assert errors[0]['message'] == 'Duplicated attribute value names provided.'- assert errors[1]['field'] == 'values:slug'- assert errors[1]['message'] == (- 'Enter a valid \'slug\' consisting of letters, '- 'numbers, underscores or hyphens.')---def test_update_attribute(admin_api_client, color_attribute):- attribute = color_attribute- query = """- mutation updateAttribute($id: ID!, $name: String!, $slug: String!) {- attributeUpdate(id: $id, input: {name: $name, slug: $slug}) {- attribute {+ assert errors[0]['message'] == error_msg+++UPDATE_ATTRIBUTE_QUERY = """+ mutation updateAttribute(+ $id: ID!, $name: String!, $addValues: [AttributeCreateValueInput]!,+ $removeValues: [ID]!) {+ attributeUpdate(+ id: $id,+ input: {+ name: $name, addValues: $addValues,+ removeValues: $removeValues}) {+ errors {+ field+ message+ }+ attribute {+ name+ slug+ values {name+ slug}}}- """+}+"""+++def test_update_attribute_name(admin_api_client, color_attribute):+ query = UPDATE_ATTRIBUTE_QUERY+ attribute = color_attribute+ name = 'Wings name'+ id = graphene.Node.to_global_id('Attribute', attribute.id)+ variables = json.dumps({+ 'name': name, 'id': id, 'addValues': [], 'removeValues': []})+ response = admin_api_client.post(+ reverse('api'), {'query': query, 'variables': variables})+ content = get_graphql_content(response)+ attribute.refresh_from_db()+ data = content['data']['attributeUpdate']+ assert data['attribute']['name'] == name == attribute.name+++def test_update_attribute_remove_and_add_values(+ admin_api_client, color_attribute):+ query = UPDATE_ATTRIBUTE_QUERY+ attribute = color_attributename = 'Wings name'- slug = attribute.slug+ attribute_value_name = 'Red Color'id = graphene.Node.to_global_id('Attribute', attribute.id)- variables = json.dumps({'name': name, 'id': id, 'slug': slug})+ attribute_value_id = attribute.values.first().id+ value_id = graphene.Node.to_global_id(+ 'AttributeValue', attribute_value_id)+ variables = json.dumps({+ 'name': name, 'id': id,+ 'addValues': [{'name': attribute_value_name, 'value': '#1231'}],+ 'removeValues': [value_id]})response = admin_api_client.post(reverse('api'), {'query': query, 'variables': variables})content = get_graphql_content(response)attribute.refresh_from_db()- data = content['data']['attributeUpdate']['attribute']- assert data['name'] == name == attribute.name+ data = content['data']['attributeUpdate']+ assert not data['errors']+ assert data['attribute']['name'] == name == attribute.name+ assert not attribute.values.filter(pk=attribute_value_id).exists()+ assert attribute.values.filter(name=attribute_value_name).exists()+++ 'name_1, name_2, error_msg', (+ (+ 'Red color', 'Red color',+ 'Duplicated AttributeValue names provided.'),+ (+ 'Red color', 'red color',+ 'Provided AttributeValue names are not unique.')))+def test_update_attribute_and_add_attribute_values_errors(+ admin_api_client, name_1, name_2, error_msg, color_attribute):+ query = UPDATE_ATTRIBUTE_QUERY+ attribute = color_attribute+ id = graphene.Node.to_global_id('Attribute', attribute.id)+ variables = json.dumps({+ 'name': 'Example name', 'id': id,+ 'slug': 'example-slug', 'removeValues': [],+ 'addValues': [+ {'name': name_1, 'value': '#1'}, {'name': name_2, 'value': '#2'}]})+ response = admin_api_client.post(+ reverse('api'), {'query': query, 'variables': variables})+ content = get_graphql_content(response)+ errors = content['data']['attributeUpdate']['errors']+ assert errors+ assert errors[0]['field'] == 'addValues'+ assert errors[0]['message'] == error_msg+++def test_update_attribute_and_remove_others_attribute_value(+ admin_api_client, color_attribute, size_attribute):+ query = UPDATE_ATTRIBUTE_QUERY+ attribute = color_attribute+ id = graphene.Node.to_global_id('Attribute', attribute.id)+ size_attribute = size_attribute.values.first()+ attr_id = graphene.Node.to_global_id(+ 'AttributeValue', size_attribute.pk)+ variables = json.dumps({+ 'name': 'Example name', 'id': id, 'slug': 'example-slug',+ 'addValues': [], 'removeValues': [attr_id]})+ response = admin_api_client.post(+ reverse('api'), {'query': query, 'variables': variables})+ content = get_graphql_content(response)+ errors = content['data']['attributeUpdate']['errors']+ assert errors+ assert errors[0]['field'] == 'removeValues:%s' % str(+ size_attribute).lower()+ assert errors[0]['message'] == (+ 'AttributeValue does not belong to this Attribute.')def test_delete_attribute(admin_api_client, color_attribute):@@ -206,63 +307,144 @@ def test_delete_attribute(admin_api_client, color_attribute):attribute.refresh_from_db()-def test_create_attribute_value(admin_api_client, color_attribute):- attribute = color_attribute- query = """- mutation createChoice($attribute: ID!, $name: String!, $slug: String!, $value: String!) {- attributeValueCreate(- input: {attribute: $attribute, name: $name, slug: $slug, value: $value}) {- attributeValue {+CREATE_ATTRIBUTE_VALUE_QUERY = """+ mutation createAttributeValue(+ $id: ID!, $name: String!, $value: String!) {+ attributeValueCreate(+ attribute: $id, input: {name: $name, value: $value}) {+ errors {+ field+ message+ }+ attribute {+ values {name- slug- type- value}}+ attributeValue {+ name+ type+ slug+ value+ }}- """+}+"""+++def test_create_attribute_value(admin_api_client, color_attribute):+ attribute = color_attribute+ query = CREATE_ATTRIBUTE_VALUE_QUERYattribute_id = graphene.Node.to_global_id('Attribute', attribute.id)name = 'test name'- slug = 'test-slug'value = 'test-string'variables = json.dumps(- {'name': name, 'slug': slug, 'value': value, 'attribute': attribute_id})+ {'name': name, 'value': value, 'id': attribute_id})response = admin_api_client.post(reverse('api'), {'query': query, 'variables': variables})content = get_graphql_content(response)- data = content[- 'data']['attributeValueCreate']['attributeValue']- assert data['name'] == name- assert data['slug'] == slug- assert data['value'] == value- assert data['type'] == 'STRING'+ data = content['data']['attributeValueCreate']+ assert not data['errors']+ attr_data = data['attributeValue']+ assert attr_data['name'] == name+ assert attr_data['slug'] == slugify(name)+ assert attr_data['value'] == value+ assert attr_data['type'] == 'STRING'+ assert name in [value['name'] for value in data['attribute']['values']]-def test_update_attribute_value(admin_api_client, pink_attribute_value):- value = pink_attribute_value- query = """- mutation updateChoice($id: ID!, $name: String!, $slug: String!) {- attributeValueUpdate(- id: $id, input: {name: $name, slug: $slug}) {- attributeValue {++def test_create_attribute_value_not_unique_name(+ admin_api_client, color_attribute):+ attribute = color_attribute+ query = CREATE_ATTRIBUTE_VALUE_QUERY+ attribute_id = graphene.Node.to_global_id('Attribute', attribute.id)+ value_name = attribute.values.first().name+ variables = json.dumps(+ {'name': value_name, 'value': 'test-string', 'id': attribute_id})+ response = admin_api_client.post(+ reverse('api'), {'query': query, 'variables': variables})+ content = get_graphql_content(response)+ data = content['data']['attributeValueCreate']+ assert data['errors']+ assert data['errors'][0]['field'] == 'name'+ assert data['errors'][0]['message'] == 'Provided name is not unique.'+++UPDATE_ATTRIBUTE_VALUE_QUERY = """+mutation updateChoice(+ $id: ID!, $name: String!, $value: String!) {+ attributeValueUpdate(+ id: $id, input: {name: $name, value: $value}) {+ errors {+ field+ message+ }+ attributeValue {+ name+ slug+ value+ }+ attribute {+ values {name- slug}}}- """+}+"""+++def test_update_attribute_value(admin_api_client, pink_attribute_value):+ query = UPDATE_ATTRIBUTE_VALUE_QUERY+ value = pink_attribute_valueid = graphene.Node.to_global_id('AttributeValue', value.id)- name = 'Crimson'- slug = value.slug+ name = 'Crimson name'variables = json.dumps(- {'name': name, 'slug': slug, 'id': id})+ {'name': name, 'value': '#RED', 'id': id})response = admin_api_client.post(reverse('api'), {'query': query, 'variables': variables})content = get_graphql_content(response)value.refresh_from_db()- data = content[- 'data']['attributeValueUpdate']['attributeValue']- assert data['name'] == name == value.name+ data = content['data']['attributeValueUpdate']+ assert data['attributeValue']['name'] == name == value.name+ assert data['attributeValue']['slug'] == slugify(name)+ assert name in [value['name'] for value in data['attribute']['values']]+++def test_update_attribute_value_name_not_unique(+ admin_api_client, pink_attribute_value):+ query = UPDATE_ATTRIBUTE_VALUE_QUERY+ value = pink_attribute_value.attribute.values.create(+ name='Example Name', slug='example-name', value='#RED')+ id = graphene.Node.to_global_id('AttributeValue', value.id)+ variables = json.dumps(+ {'name': pink_attribute_value.name, 'value': '#RED', 'id': id})+ response = admin_api_client.post(+ reverse('api'), {'query': query, 'variables': variables})+ content = get_graphql_content(response)+ assert 'errors' not in content+ data = content['data']['attributeValueUpdate']+ assert data['errors']+ assert data['errors'][0]['field'] == 'name'+ assert data['errors'][0]['message'] == 'Provided name is not unique.'+++def test_update_same_attribute_value(+ admin_api_client, pink_attribute_value):+ query = UPDATE_ATTRIBUTE_VALUE_QUERY+ value = pink_attribute_value+ id = graphene.Node.to_global_id('AttributeValue', value.id)+ attr_value = '#BLUE'+ variables = json.dumps(+ {'name': value.name, 'value': attr_value, 'id': id})+ response = admin_api_client.post(+ reverse('api'), {'query': query, 'variables': variables})+ content = get_graphql_content(response)+ assert 'errors' not in content+ data = content['data']['attributeValueUpdate']+ assert not data['errors']+ assert data['attributeValue']['value'] == attr_valuedef test_delete_attribute_value(