Skip to content

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

Add tests

akjanikd3d51fb4d3af
diff --git a/saleor/graphql/core/mutations.py b/saleor/graphql/core/mutations.py
index 847e21fde4..e494fc0692 100644
--- a/saleor/graphql/core/mutations.py
+++ b/saleor/graphql/core/mutations.py
@@ -124,7 +124,7 @@ class ModelMutation(BaseMutation):
cleaned_input[field_name] = instances
# handle ID field
- elif value is not None and (field.type == graphene.ID or field.type.of_type == graphene.ID):
+ elif value is not None and field.type == graphene.ID:
instance = get_node(info, value)
cleaned_input[field_name] = instance
diff --git a/saleor/graphql/product/mutations/attributes.py b/saleor/graphql/product/mutations/attributes.py
index 4818be89ce..62228cdb50 100644
--- a/saleor/graphql/product/mutations/attributes.py
+++ b/saleor/graphql/product/mutations/attributes.py
@@ -13,7 +13,7 @@ class ProductAttributesInput(graphene.InputObjectType):
class AttributeChoiceValueInput(graphene.InputObjectType):
attribute = graphene.ID(
- required=True,
+ required=False,
description='Attribute to which value will be assigned.')
slug = graphene.String(
required=True, description='Internal name.')
@@ -45,6 +45,8 @@ class ProductAttributeCreate(ModelMutation):
class ProductAttributeUpdate(ProductAttributeCreate):
class Arguments:
+ id = graphene.ID(
+ required=True, description='ID of a product attribute to update.')
input = ProductAttributesInput(
required=True,
description='Fields required to update a product attribute.')
@@ -87,6 +89,9 @@ class AttributeChoiceValueCreate(ModelMutation):
class AttributeChoiceValueUpdate(AttributeChoiceValueCreate):
class Arguments:
+ id = graphene.ID(
+ required=True,
+ description='ID of an attribute choice value to update.')
input = AttributeChoiceValueUpdateInput(
required=True,
description='Fields required to update an attribute choice value.')
diff --git a/tests/api/test_attributes.py b/tests/api/test_attributes.py
new file mode 100644
index 0000000000..d74f240f15
--- /dev/null
+++ b/tests/api/test_attributes.py
@@ -0,0 +1,158 @@
+import json
+
+import graphene
+import pytest
+from django.shortcuts import reverse
+from tests.utils import get_graphql_content
+
+
+def test_create_product_attribute(admin_api_client):
+ query = """
+ mutation createAttribute($name: String!, $slug: String!) {
+ productAttributeCreate(input: {name: $name, slug: $slug}) {
+ productAttribute {
+ name
+ slug
+ values {
+ name
+ slug
+ }
+ }
+ }
+ }
+ """
+ name = 'test name'
+ slug = 'test-slug'
+ variables = json.dumps({'name': name, 'slug': slug})
+ response = admin_api_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ data = content['data']['productAttributeCreate']['productAttribute']
+ assert data['name'] == name
+ assert data['slug'] == slug
+ assert not data['values']
+
+
+def test_update_product_attribute(admin_api_client, color_attribute):
+ attribute = color_attribute
+ query = """
+ mutation updateAttribute($id: ID!, $name: String!, $slug: String!) {
+ productAttributeUpdate(id: $id, input: {name: $name, slug: $slug}) {
+ productAttribute {
+ name
+ }
+ }
+ }
+ """
+ name = 'Wings name'
+ slug = attribute.slug
+ id = graphene.Node.to_global_id('ProductAttribute', attribute.id)
+ variables = json.dumps({'name': name, 'id': id, 'slug': slug})
+ response = admin_api_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ attribute.refresh_from_db()
+ assert 'errors' not in content
+ data = content['data']['productAttributeUpdate']['productAttribute']
+ assert data['name'] == name == attribute.name
+
+
+def test_delete_product_attribute(admin_api_client, color_attribute):
+ attribute = color_attribute
+ query = """
+ mutation deleteAttribute($id: ID!) {
+ productAttributeDelete(id: $id) {
+ productAttribute {
+ id
+ }
+ }
+ }
+ """
+ id = graphene.Node.to_global_id('ProductAttribute', attribute.id)
+ variables = json.dumps({'id': id})
+ response = admin_api_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ with pytest.raises(attribute._meta.model.DoesNotExist):
+ attribute.refresh_from_db()
+
+
+def test_create_attribute_choice_value(admin_api_client, color_attribute):
+ attribute = color_attribute
+ query = """
+ mutation createChoice($attribute: ID!, $name: String!, $slug: String!) {
+ attributeChoiceValueCreate(
+ input: {attribute: $attribute, name: $name, slug: $slug}) {
+ attributeChoiceValue {
+ name
+ slug
+ }
+ }
+ }
+ """
+ attribute_id = graphene.Node.to_global_id('ProductAttribute', attribute.id)
+ name = 'test name'
+ slug = 'test-slug'
+ variables = json.dumps(
+ {'name': name, 'slug': slug, 'attribute': attribute_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']['attributeChoiceValueCreate']['attributeChoiceValue']
+ assert data['name'] == name
+ assert data['slug'] == slug
+
+
+def test_update_attribute_choice_value(admin_api_client, pink_choice_value):
+ value = pink_choice_value
+ query = """
+ mutation updateChoice($id: ID!, $name: String!, $slug: String!) {
+ attributeChoiceValueUpdate(
+ id: $id, input: {name: $name, slug: $slug}) {
+ attributeChoiceValue {
+ name
+ slug
+ }
+ }
+ }
+ """
+ id = graphene.Node.to_global_id('ProductAttributeValue', value.id)
+ name = 'Crimson'
+ slug = value.slug
+ variables = json.dumps(
+ {'name': name, 'slug': slug, 'id': id})
+ response = admin_api_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ value.refresh_from_db()
+ data = content[
+ 'data']['attributeChoiceValueUpdate']['attributeChoiceValue']
+ assert data['name'] == name == value.name
+
+
+def test_delete_attribute_choice_value(admin_api_client, color_attribute, pink_choice_value):
+ value = pink_choice_value
+ value = color_attribute.values.get(name='Red')
+ query = """
+ mutation updateChoice($id: ID!) {
+ attributeChoiceValueDelete(id: $id) {
+ attributeChoiceValue {
+ name
+ slug
+ }
+ }
+ }
+ """
+ id = graphene.Node.to_global_id('ProductAttributeValue', value.id)
+ variables = json.dumps({'id': id})
+ response = admin_api_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ with pytest.raises(value._meta.model.DoesNotExist):
+ value.refresh_from_db()
diff --git a/tests/conftest.py b/tests/conftest.py
index d0e799d5e5..c04a140feb 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -231,6 +231,13 @@ def color_attribute(db): # pylint: disable=W0613
return attribute
+def pink_choice_value(color_attribute): # pylint: disable=W0613
+ value = AttributeChoiceValue.objects.create(
+ slug='pink', name='Color', attribute=color_attribute)
+ return value
+
+
@pytest.fixture
def size_attribute(db): # pylint: disable=W0613
attribute = ProductAttribute.objects.create(slug='size', name='Size')