Skip to content

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

Split API tests into different files

Adam Janik7c5c63c864ce
diff --git a/saleor/graphql/product/forms.py b/saleor/graphql/product/forms.py
index c46a85f1fe..34e7c364c8 100644
--- a/saleor/graphql/product/forms.py
+++ b/saleor/graphql/product/forms.py
@@ -12,4 +12,4 @@ class ProductForm(forms.ModelForm):
class ProductVariantForm(forms.ModelForm):
class Meta:
model = ProductVariant
- exclude = ['images' ,'name', 'product', 'quantity_allocated']
+ exclude = ['images', 'name', 'product', 'quantity_allocated']
diff --git a/saleor/graphql/product/mutations.py b/saleor/graphql/product/mutations.py
index 6423b3c68c..be1972c25c 100644
--- a/saleor/graphql/product/mutations.py
+++ b/saleor/graphql/product/mutations.py
@@ -174,7 +174,7 @@ class ProductVariantCreateMutation(
class Arguments:
attributes = graphene.Argument(graphene.List(AttributeValueInput))
- product_id= graphene.ID()
+ product_id = graphene.ID()
class Meta:
description = 'Creates a new variant for product'
@@ -197,7 +197,6 @@ class ProductVariantUpdateMutation(
class Arguments:
attributes = graphene.Argument(graphene.List(AttributeValueInput))
- # product_id= graphene.ID()
class Meta:
description = 'Updates an existing variant for product'
diff --git a/tests/api/__init__.py b/tests/api/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/tests/api/test_category.py b/tests/api/test_category.py
new file mode 100644
index 0000000000..637920f148
--- /dev/null
+++ b/tests/api/test_category.py
@@ -0,0 +1,176 @@
+import json
+
+import graphene
+import pytest
+from django.shortcuts import reverse
+from tests.utils import get_graphql_content
+
+from saleor.product.models import Category
+
+
+def test_category_query(client, product):
+ category = Category.objects.first()
+ query = '''
+ query {
+ category(id: "%(category_pk)s") {
+ id
+ name
+ ancestors {
+ edges {
+ node {
+ name
+ }
+ }
+ }
+ children {
+ edges {
+ node {
+ name
+ }
+ }
+ }
+ }
+ }
+ ''' % {'category_pk': graphene.Node.to_global_id('Category', category.pk)}
+ response = client.post(reverse('api'), {'query': query})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ category_data = content['data']['category']
+ assert category_data is not None
+ assert category_data['name'] == category.name
+ assert (
+ len(category_data['ancestors']['edges']) ==
+ category.get_ancestors().count())
+ assert (
+ len(category_data['children']['edges']) ==
+ category.get_children().count())
+
+
+def test_category_create_mutation(admin_client):
+ query = """
+ mutation($name: String!, $description: String, $parentId: ID) {
+ categoryCreate(
+ name: $name
+ description: $description
+ parentId: $parentId
+ ) {
+ category {
+ id
+ name
+ slug
+ description
+ parent {
+ name
+ id
+ }
+ }
+ errors {
+ field
+ message
+ }
+ }
+ }
+ """
+
+ category_name = 'Test category'
+ category_description = 'Test description'
+
+ # test creating root category
+ variables = json.dumps({
+ 'name': category_name, 'description': category_description})
+ response = admin_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ data = content['data']['categoryCreate']
+ assert data['errors'] == []
+ assert data['category']['name'] == category_name
+ assert data['category']['description'] == category_description
+ assert not data['category']['parent']
+
+ # test creating subcategory
+ parent_id = data['category']['id']
+ variables = json.dumps({
+ 'name': category_name, 'description': category_description,
+ 'parentId': parent_id})
+ response = admin_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ data = content['data']['categoryCreate']
+ assert data['errors'] == []
+ assert data['category']['parent']['id'] == parent_id
+
+
+def test_category_update_mutation(admin_client, default_category):
+ query = """
+ mutation($id: ID, $name: String!, $description: String) {
+ categoryUpdate(
+ id: $id
+ name: $name
+ description: $description
+ ) {
+ category {
+ id
+ name
+ description
+ parent {
+ id
+ }
+ }
+ errors {
+ field
+ message
+ }
+ }
+ }
+ """
+ # create child category and test that the update mutation won't change
+ # it's parent
+ child_category = default_category.children.create(name='child')
+
+ category_name = 'Updated name'
+ category_description = 'Updated description'
+
+ category_id = graphene.Node.to_global_id('Category', child_category.pk)
+ variables = json.dumps({
+ 'name': category_name, 'description': category_description,
+ 'id': category_id})
+ response = admin_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ data = content['data']['categoryUpdate']
+ assert data['errors'] == []
+ assert data['category']['id'] == category_id
+ assert data['category']['name'] == category_name
+ assert data['category']['description'] == category_description
+
+ parent_id = graphene.Node.to_global_id('Category', default_category.pk)
+ assert data['category']['parent']['id'] == parent_id
+
+
+def test_category_delete_mutation(admin_client, default_category):
+ query = """
+ mutation($id: ID!) {
+ categoryDelete(id: $id) {
+ category {
+ name
+ }
+ errors {
+ field
+ message
+ }
+ }
+ }
+ """
+ variables = json.dumps({
+ 'id': graphene.Node.to_global_id('Category', default_category.id)})
+ response = admin_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ data = content['data']['categoryDelete']
+ assert data['category']['name'] == default_category.name
+ with pytest.raises(default_category._meta.model.DoesNotExist):
+ default_category.refresh_from_db()
diff --git a/tests/api/test_graphql.py b/tests/api/test_graphql.py
new file mode 100644
index 0000000000..2074f535fe
--- /dev/null
+++ b/tests/api/test_graphql.py
@@ -0,0 +1,206 @@
+import json
+from unittest.mock import patch
+
+import graphene
+from django.shortcuts import reverse
+from tests.utils import get_graphql_content
+
+from saleor.graphql.core.mutations import (
+ ModelFormMutation, ModelFormUpdateMutation)
+
+
+def test_real_query(admin_client, product):
+ product_attr = product.product_type.product_attributes.first()
+ category = product.category
+ attr_value = product_attr.values.first()
+ filter_by = '%s:%s' % (product_attr.slug, attr_value.slug)
+ query = '''
+ query Root($categoryId: ID!, $sortBy: String, $first: Int, $attributesFilter: [AttributeScalar], $minPrice: Float, $maxPrice: Float) {
+ category(id: $categoryId) {
+ ...CategoryPageFragmentQuery
+ __typename
+ }
+ attributes(inCategory: $categoryId) {
+ edges {
+ node {
+ ...ProductFiltersFragmentQuery
+ __typename
+ }
+ }
+ }
+ }
+
+ fragment CategoryPageFragmentQuery on Category {
+ id
+ name
+ url
+ ancestors {
+ edges {
+ node {
+ name
+ id
+ url
+ __typename
+ }
+ }
+ }
+ children {
+ edges {
+ node {
+ name
+ id
+ url
+ slug
+ __typename
+ }
+ }
+ }
+ products(first: $first, sortBy: $sortBy, attributes: $attributesFilter, price_Gte: $minPrice, price_Lte: $maxPrice) {
+ ...ProductListFragmentQuery
+ __typename
+ }
+ __typename
+ }
+
+ fragment ProductListFragmentQuery on ProductCountableConnection {
+ edges {
+ node {
+ ...ProductFragmentQuery
+ __typename
+ }
+ __typename
+ }
+ pageInfo {
+ hasNextPage
+ __typename
+ }
+ __typename
+ }
+
+ fragment ProductFragmentQuery on Product {
+ id
+ name
+ price {
+ amount
+ currency
+ localized
+ __typename
+ }
+ availability {
+ ...ProductPriceFragmentQuery
+ __typename
+ }
+ thumbnailUrl1x: thumbnailUrl(size: "255x255")
+ thumbnailUrl2x: thumbnailUrl(size: "510x510")
+ url
+ __typename
+ }
+
+ fragment ProductPriceFragmentQuery on ProductAvailability {
+ available
+ discount {
+ gross {
+ amount
+ currency
+ __typename
+ }
+ __typename
+ }
+ priceRange {
+ stop {
+ gross {
+ amount
+ currency
+ localized
+ __typename
+ }
+ currency
+ __typename
+ }
+ start {
+ gross {
+ amount
+ currency
+ localized
+ __typename
+ }
+ currency
+ __typename
+ }
+ __typename
+ }
+ __typename
+ }
+
+ fragment ProductFiltersFragmentQuery on ProductAttribute {
+ id
+ name
+ slug
+ values {
+ id
+ name
+ slug
+ color
+ __typename
+ }
+ __typename
+ }
+ '''
+ response = admin_client.post(
+ reverse('api'), {
+ 'query': query,
+ 'variables': json.dumps(
+ {
+ 'categoryId': graphene.Node.to_global_id(
+ 'Category', category.id),
+ 'sortBy': 'name',
+ 'first': 1,
+ 'attributesFilter': [filter_by]})})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+
+
+@patch('saleor.graphql.core.mutations.convert_form_fields')
+@patch('saleor.graphql.core.mutations.convert_form_field')
+def test_model_form_mutation(
+ mocked_convert_form_field, mocked_convert_form_fields,
+ model_form_class):
+
+ mocked_convert_form_fields.return_value = {
+ model_form_class._meta.fields: mocked_convert_form_field.return_value}
+
+ class TestMutation(ModelFormMutation):
+ test_field = graphene.String()
+
+ class Arguments:
+ test_input = graphene.String()
+
+ class Meta:
+ form_class = model_form_class
+ return_field_name = 'test_return_field'
+
+ meta = TestMutation._meta
+ assert meta.form_class == model_form_class
+ assert meta.model == 'test_model'
+ assert meta.return_field_name == 'test_return_field'
+ arguments = meta.arguments
+ # check if declarative arguments are present
+ assert 'test_input' in arguments
+ # check if model form field is present
+ mocked_convert_form_fields.assert_called_with(model_form_class, None)
+ assert 'test_field' in arguments
+
+ output_fields = meta.fields
+ assert 'test_return_field' in output_fields
+ assert 'errors' in output_fields
+
+
+@patch('saleor.graphql.core.mutations')
+def test_model_form_update_mutation(model_form_class):
+ class TestUpdateMutation(ModelFormUpdateMutation):
+ class Meta:
+ form_class = model_form_class
+ return_field_name = 'test_return_field'
+
+ meta = TestUpdateMutation._meta
+ assert 'id' in meta.arguments
diff --git a/tests/api/test_page.py b/tests/api/test_page.py
new file mode 100644
index 0000000000..608981bf31
--- /dev/null
+++ b/tests/api/test_page.py
@@ -0,0 +1,100 @@
+import json
+
+import graphene
+import pytest
+from django.shortcuts import reverse
+from tests.utils import get_graphql_content
+
+
+def test_page_query(client, page):
+ page.is_visible = True
+ query = """
+ query PageQuery($id: ID!) {
+ page(id: $id) {
+ title
+ slug
+ }
+ }
+ """
+ variables = json.dumps({
+ 'id': graphene.Node.to_global_id('Page', page.id)})
+ response = client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ page_data = content['data']['page']
+ assert page_data['title'] == page.title
+ assert page_data['slug'] == page.slug
+
+
+def test_page_create_mutation(admin_client):
+ query = """
+ mutation CreatePage(
+ $slug: String!,
+ $title: String!,
+ $content: String!,
+ $isVisible: Boolean!) {
+ pageCreate(slug: $slug,
+ title: $title,
+ content: $content,
+ isVisible: $isVisible) {
+ page {
+ id
+ title
+ content
+ slug
+ isVisible
+ }
+ errors {
+ message
+ field
+ }
+ }
+ }
+ """
+ page_slug = 'test-slug'
+ page_content = 'test content'
+ page_title = 'test title'
+ page_isVisible = True
+
+ # test creating root page
+ variables = json.dumps({
+ 'title': page_title, 'content': page_content,
+ 'isVisible': page_isVisible, 'slug': page_slug})
+ response = admin_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ data = content['data']['pageCreate']
+ assert data['errors'] == []
+ assert data['page']['title'] == page_title
+ assert data['page']['content'] == page_content
+ assert data['page']['slug'] == page_slug
+ assert data['page']['isVisible'] == page_isVisible
+
+
+def test_page_delete_mutation(admin_client, page):
+ query = """
+ mutation DeletePage($id: ID!) {
+ pageDelete(id: $id) {
+ page {
+ title
+ id
+ }
+ errors {
+ field
+ message
+ }
+ }
+ }
+ """
+ variables = json.dumps({
+ 'id': graphene.Node.to_global_id('Page', page.id)})
+ response = admin_client.post(
+ reverse('api'), {'query': query, 'variables': variables})
+ content = get_graphql_content(response)
+ assert 'errors' not in content
+ data = content['data']['pageDelete']
+ assert data['page']['title'] == page.title
+ with pytest.raises(page._meta.model.DoesNotExist):
+ page.refresh_from_db()
diff --git a/tests/test_graphql.py b/tests/api/test_product.py
similarity index 99%
rename from tests/test_graphql.py
rename to tests/api/test_product.py
index 5f63f88591..09a48ff86d 100644
--- a/tests/test_graphql.py
+++ b/tests/api/test_product.py
@@ -1,5 +1,4 @@
import json
-from unittest.mock import patch
import graphene
import pytest
@@ -7,14 +6,9 @@ from django.forms.models import model_to_dict
from django.shortcuts import reverse
from django.utils.text import slugify
from prices import Money
+from tests.utils import get_graphql_content
-from saleor.graphql.core.mutations import (
- ModelFormMutation, ModelFormUpdateMutation)
-from saleor.page.models import Page
-from saleor.product.models import (
- Category, Product, ProductAttribute, ProductType)
-
-from .utils import get_graphql_content
+from saleor.product.models import Category, Product, ProductAttribute
def test_create_token_mutation(admin_client, staff_user):