Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add tests
Adam Janikfd930e603b30
diff --git a/saleor/graphql/product/mutations.py b/saleor/graphql/product/mutations.pyindex a57f188900..15ec538dde 100644--- a/saleor/graphql/product/mutations.py+++ b/saleor/graphql/product/mutations.py@@ -73,7 +73,7 @@ class CollectionCreateMutation(StaffMemberRequiredMixin, ModelFormMutation):product_ids = input.pop('products', None)kwargs = super().get_form_kwargs(root, info, **input)if product_ids:- products = get_nodes(product_ids, Product)+ products = set(get_nodes(product_ids, Product))kwargs['data']['products'] = productsreturn kwargs@@ -98,9 +98,9 @@ class CollectionDelete(StaffMemberRequiredMixin, ModelDeleteMutation):class CollectionAddProducts(BaseMutation):class Arguments:collection_id = graphene.Argument(- graphene.ID, description='ID of a collection.')+ graphene.ID, required=True, description='ID of a collection.')products = graphene.List(- graphene.ID, description='List of product IDs.')+ graphene.ID, required=True, description='List of product IDs.')collection = graphene.Field(Collection,@@ -120,9 +120,9 @@ class CollectionAddProducts(BaseMutation):class CollectionRemoveProducts(BaseMutation):class Arguments:collection_id = graphene.Argument(- graphene.ID, description='ID of a collection.')+ graphene.ID, required=True, description='ID of a collection.')products = graphene.List(- graphene.ID, description='List of product IDs.')+ graphene.ID, required=True, description='List of product IDs.')collection = graphene.Field(Collection,diff --git a/tests/api/test_graphql.py b/tests/api/test_graphql.pyindex 25a8b04502..63b1cc8341 100644--- a/tests/api/test_graphql.py+++ b/tests/api/test_graphql.py@@ -18,7 +18,7 @@ from saleor.graphql.product.types import Productfrom saleor.graphql.utils import get_nodes-def test_jwt_middleware(admin_client, admin_user):+def test_jwt_middleware(admin_user):def get_response(request):return HttpResponse()@@ -236,7 +236,8 @@ def test_model_form_update_mutation(model_form_class):assert 'id' in meta.argumentsdef test_get_nodes(product_list):- global_ids = [to_global_id('Product', product.pk) for product in product_list]+ global_ids = [+ to_global_id('Product', product.pk) for product in product_list]# Make sure function works even if duplicated ids are providedglobal_ids.append(to_global_id('Product', product_list[0].pk))# Return products corresponding to global idsdiff --git a/tests/api/test_product.py b/tests/api/test_product.pyindex 65f8ba8e94..b9ae04d1ad 100644--- a/tests/api/test_product.py+++ b/tests/api/test_product.py@@ -2,9 +2,9 @@ import jsonimport grapheneimport pytest-from django.shortcuts import reversefrom django.utils.text import slugify+from graphql_relay import to_global_idfrom prices import Moneyfrom tests.utils import create_image, get_graphql_content@@ -829,3 +829,170 @@ def test_product_image_create_mutation(admin_api_client, product):file_name = data['productImage']['image']product.refresh_from_db()assert product.images.first().image.name == file_name+++def test_collections_query(user_api_client, collection):+ query = """+ query Collections {+ collections(first: 1) {+ edges {+ node {+ name+ slug+ products {+ totalCount+ }+ }+ }+ }+ }+ """+ response = user_api_client.post(reverse('api'), {'query': query})+ content = get_graphql_content(response)+ assert 'errors' not in content+ data = content['data']['collections']['edges'][0]['node']+ assert data['name'] == collection.name+ assert data['slug'] == collection.slug+ assert data['products']['totalCount'] == collection.products.count()+++def test_create_collection(admin_api_client, product_list):+ query = """+ mutation createCollection(+ $name: String!, $slug: String!, $products: [ID]) {+ collectionCreate(+ name: $name, slug: $slug, products: $products) {+ collection {+ name+ slug+ products {+ totalCount+ }+ }+ }+ }+ """+ product_ids = [+ to_global_id('Product', product.pk) for product in product_list]+ name = 'test-name'+ slug = 'test-slug'+ variables = json.dumps(+ {'name': name, 'slug': slug, 'products': product_ids})+ response = admin_api_client.post(+ reverse('api'), {'query': query, 'variables': variables})+ content = get_graphql_content(response)+ assert 'errors' not in content+ data = content['data']['collectionCreate']['collection']+ assert data['name'] == name+ assert data['slug'] == slug+ assert data['products']['totalCount'] == len(product_ids)+++def test_update_collection(admin_api_client, collection):+ query = """+ mutation createCollection(+ $name: String!, $slug: String!, $id: ID!) {+ collectionUpdate(+ name: $name, slug: $slug, id: $id) {+ collection {+ name+ slug+ }+ }+ }+ """+ collection_id = to_global_id('Collection', collection.id)+ name = 'new-name'+ slug = 'new-slug'+ variables = json.dumps(+ {'name': name, 'slug': slug, 'id': collection_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']['collectionUpdate']['collection']+ assert data['name'] == name+ assert data['slug'] == slug+++def test_delete_collection(admin_api_client, collection):+ query = """+ mutation deleteCollection(+ $id: ID!) {+ collectionDelete(+ id: $id) {+ collection {+ name+ }+ }+ }+ """+ collection_id = to_global_id('Collection', collection.id)+ variables = json.dumps({'id': collection_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']['collectionDelete']['collection']+ assert data['name'] == collection.name+ with pytest.raises(collection._meta.model.DoesNotExist):+ collection.refresh_from_db()+++def test_add_products_to_collection(+ admin_api_client, collection, product_list):+ query = """+ mutation collectionAddProducts(+ $id: ID!, $products: [ID]!) {+ collectionAddProducts(collectionId: $id, products: $products) {+ collection {+ products {+ totalCount+ }+ }+ }+ }+ """+ collection_id = to_global_id('Collection', collection.id)+ product_ids = [+ to_global_id('Product', product.pk) for product in product_list]+ no_products_before = collection.products.count()+ variables = json.dumps(+ {'id': collection_id, 'products': product_ids})+ response = admin_api_client.post(+ reverse('api'), {'query': query, 'variables': variables})+ content = get_graphql_content(response)+ assert 'errors' not in content+ data = content['data']['collectionAddProducts']['collection']+ assert data[+ 'products']['totalCount'] == no_products_before + len(product_ids)+++def test_remove_products_to_collection(+ admin_api_client, collection, product_list):+ query = """+ mutation collectionRemoveProducts(+ $id: ID!, $products: [ID]!) {+ collectionRemoveProducts(collectionId: $id, products: $products) {+ collection {+ products {+ totalCount+ }+ }+ }+ }+ """+ collection.products.add(*product_list)+ collection_id = to_global_id('Collection', collection.id)+ product_ids = [+ to_global_id('Product', product.pk) for product in product_list]+ no_products_before = collection.products.count()+ variables = json.dumps(+ {'id': collection_id, 'products': product_ids})+ response = admin_api_client.post(+ reverse('api'), {'query': query, 'variables': variables})+ content = get_graphql_content(response)+ assert 'errors' not in content+ data = content['data']['collectionRemoveProducts']['collection']+ assert data[+ 'products']['totalCount'] == no_products_before - len(product_ids)