Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add more tests
Marcin Gębalac432c9a4e069
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.pyindex e8bd14aeb9..0b0b3d8f94 100644--- a/saleor/graphql/api.py+++ b/saleor/graphql/api.py@@ -70,10 +70,7 @@ class CategoryType(DjangoObjectType):than or equal to the given value"""),price_gte=graphene.Argument(graphene.Float, description="""Get the products with price greater- than or equal to the given value"""),- subcategories=graphene.Argument(- graphene.List(graphene.String, description="""A list of- subcategories slugs to filter the products by""")))+ than or equal to the given value"""))products_count = graphene.Int()url = graphene.String()ancestors = graphene.List(lambda: CategoryType)diff --git a/tests/conftest.py b/tests/conftest.pyindex f4340cdd6e..ac9ddf08a5 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -4,6 +4,7 @@ from __future__ import unicode_literalsfrom decimal import Decimalimport pytestfrom django.contrib.auth.models import AnonymousUser+from django.utils.encoding import smart_textfrom mock import Mockfrom saleor.cart import utils@@ -92,18 +93,22 @@ def shipping_method(db): # pylint: disable=W0613@pytest.fixturedef color_attribute(db): # pylint: disable=W0613- attribute = ProductAttribute.objects.create(name='color',- display='Display')- AttributeChoiceValue.objects.create(display='Red', attribute=attribute)- AttributeChoiceValue.objects.create(display='Blue', attribute=attribute)+ attribute = ProductAttribute.objects.create(+ name='color', display='Color')+ AttributeChoiceValue.objects.create(+ attribute=attribute, display='Red', slug='red')+ AttributeChoiceValue.objects.create(+ attribute=attribute, display='Blue', slug='blue')return attribute@pytest.fixturedef size_attribute(db): # pylint: disable=W0613attribute = ProductAttribute.objects.create(name='size', display='Size')- AttributeChoiceValue.objects.create(display='Small', attribute=attribute)- AttributeChoiceValue.objects.create(display='Big', attribute=attribute)+ AttributeChoiceValue.objects.create(+ attribute=attribute, display='Small', slug='small')+ AttributeChoiceValue.objects.create(+ attribute=attribute, display='Big', slug='big')return attribute@@ -114,8 +119,8 @@ def default_category(db): # pylint: disable=W0613@pytest.fixturedef product_class(color_attribute, size_attribute):- product_class = ProductClass.objects.create(name='Default Class',- has_variants=False)+ product_class = ProductClass.objects.create(+ name='Default Class', has_variants=False)product_class.product_attributes.add(color_attribute)product_class.variant_attributes.add(size_attribute)return product_class@@ -123,10 +128,15 @@ def product_class(color_attribute, size_attribute):@pytest.fixturedef product_in_stock(product_class, default_category):+ product_attr = product_class.product_attributes.first()+ attr_value = product_attr.values.first()+ attributes = {smart_text(product_attr.pk): smart_text(attr_value.pk)}+product = Product.objects.create(name='Test product', price=Decimal('10.00'), weight=1,- product_class=product_class)+ product_class=product_class, attributes=attributes)product.categories.add(default_category)+variant = ProductVariant.objects.create(product=product, sku='123')warehouse_1 = StockLocation.objects.create(name='Warehouse 1')warehouse_2 = StockLocation.objects.create(name='Warehouse 2')diff --git a/tests/test_graphql.py b/tests/test_graphql.pyindex f9d9eeae2f..baad04f70b 100644--- a/tests/test_graphql.py+++ b/tests/test_graphql.py@@ -1,24 +1,24 @@import jsonimport pytest-from saleor.product.models import Category+from saleor.product.models import Category, ProductAttributedef get_content(response):return json.loads(response.content.decode('utf8'))-def assert_success_response(content):+def assert_success(content):assert 'errors' not in contentassert 'data' in contentassert 'viewer' in content['data']-def assert_corresponding_categories(data, queryset):- assert type(data) == list- assert len(data) == queryset.count()- for (category_data, category) in zip(data, queryset):- assert category_data['name'] == category.name+def assert_corresponding_fields(iterable_data, queryset, fields):+ assert len(iterable_data) == queryset.count()+ for (data, obj) in zip(iterable_data, queryset):+ for field in fields:+ assert str(data[field]) == str(getattr(obj, field))@pytest.mark.django_db()@@ -40,18 +40,18 @@ def test_category_query(client, product_in_stock):""" % {'category_pk': category.pk}response = client.post('/graphql/', {'query': query})content = get_content(response)- assert_success_response(content)+ assert_success(content)category_data = content['data']['viewer']['category']assert category_data is not Noneassert int(category_data['pk']) == category.pkassert category_data['name'] == category.nameassert category_data['productsCount'] == category.products.count()- assert_corresponding_categories(- category_data['ancestors'], category.get_ancestors())- assert_corresponding_categories(- category_data['children'], category.get_children())- assert_corresponding_categories(- category_data['siblings'], category.get_siblings())+ assert_corresponding_fields(+ category_data['ancestors'], category.get_ancestors(), ['name'])+ assert_corresponding_fields(+ category_data['children'], category.get_children(), ['name'])+ assert_corresponding_fields(+ category_data['siblings'], category.get_siblings(), ['name'])@pytest.mark.django_db()@@ -79,6 +79,8 @@ def test_product_query(client, product_in_stock):priceRange {minPrice {gross+ net+ currency}}}@@ -91,7 +93,7 @@ def test_product_query(client, product_in_stock):""" % {'category_pk': category.pk}response = client.post('/graphql/', {'query': query})content = get_content(response)- assert_success_response(content)+ assert_success(content)assert content['data']['viewer']['category'] is not Noneproduct_edges_data = content['data']['viewer']['category']['products']['edges']assert len(product_edges_data) == category.products.count()@@ -99,3 +101,83 @@ def test_product_query(client, product_in_stock):assert int(product_data['pk']) == product.pkassert product_data['name'] == product.nameassert product_data['url'] == product.get_absolute_url()+ gross = product_data['availability']['priceRange']['minPrice']['gross']+ assert float(gross) == float(product.price.gross)++[email protected]_db()+def test_filter_product_by_attributes(client, product_in_stock):+ category = Category.objects.first()+ product_attr = product_in_stock.product_class.product_attributes.first()+ attr_value = product_attr.values.first()+ filter_by = "%s:%s" % (product_attr.name, attr_value.slug)+ query = """+ query {+ viewer {+ category(pk: %(category_pk)s) {+ products(attributes: ["%(filter_by)s"]) {+ edges {+ node {+ name+ }+ }+ }+ }+ }+ }+ """ % {'category_pk': category.pk, 'filter_by': filter_by}+ response = client.post('/graphql/', {'query': query})+ content = get_content(response)+ assert_success(content)+ product_data = content['data']['viewer']['category']['products']['edges'][0]['node']+ assert product_data['name'] == product_in_stock.name++[email protected]_db()+def test_attributes_query(client, product_in_stock):+ attributes = ProductAttribute.objects.prefetch_related('values')+ query = """+ query {+ viewer {+ attributes {+ pk+ name+ display+ values {+ pk+ display+ slug+ }+ }+ }+ }+ """+ response = client.post('/graphql/', {'query': query})+ content = get_content(response)+ assert_success(content)+ attributes_data = content['data']['viewer']['attributes']+ assert_corresponding_fields(attributes_data, attributes, ['pk', 'name'])++[email protected]_db()+def test_attributes_in_category_query(client, product_in_stock):+ category = Category.objects.first()+ query = """+ query {+ viewer {+ attributes(categoryPk: %(category_pk)s) {+ pk+ name+ display+ values {+ pk+ display+ slug+ }+ }+ }+ }+ """ % {'category_pk': category.pk}+ response = client.post('/graphql/', {'query': query})+ content = get_content(response)+ assert_success(content)