Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add tests for create products classes from schema
Krzysztof Wolskif98c13ba7e3d
diff --git a/saleor/core/management/commands/populatedb.py b/saleor/core/management/commands/populatedb.pyindex 417df24fb2..15771a8e69 100644--- a/saleor/core/management/commands/populatedb.py+++ b/saleor/core/management/commands/populatedb.py@@ -4,7 +4,7 @@ from django.db import connectionfrom ...utils.random_data import (create_orders, create_users, create_shipping_methods,- create_items_by_schema)+ create_products_by_schema)from ...utils import create_superuser@@ -51,8 +51,8 @@ class Command(BaseCommand):create_images = not options['withoutimages']for msg in create_shipping_methods():self.stdout.write(msg)- create_items_by_schema(self.placeholders_dir, 10, create_images,- stdout=self.stdout)+ create_products_by_schema(self.placeholders_dir, 10, create_images,+ stdout=self.stdout)for msg in create_users(20):self.stdout.write(msg)for msg in create_orders(20):diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.pyindex 4efa25e596..4df45ab27b 100644--- a/saleor/core/utils/random_data.py+++ b/saleor/core/utils/random_data.py@@ -125,8 +125,8 @@ def set_product_attributes(product, product_class):def set_variant_attributes(variant, product_class):attr_dict = {}- existing_variants = variant.product.variants.values_list(- 'attributes', flat=True)+ existing_variants = variant.product.variants.values_list('attributes',+ flat=True)existing_variant_attributes = defaultdict(list)for variant_attrs in existing_variants:for attr_id, value_id in variant_attrs.items():@@ -166,9 +166,9 @@ def get_price_override(schema):return fake.price()-def create_items_by_class(product_class, schema,- placeholder_dir, how_many=10, create_images=True,- stdout=None):+def create_products_by_class(product_class, schema,+ placeholder_dir, how_many=10, create_images=True,+ stdout=None):category_name = schema.get('category') or DEFAULT_CATEGORYcategory = get_or_create_category(category_name)@@ -193,10 +193,10 @@ def create_items_by_class(product_class, schema,product, product_class.name, len(variant_combinations) or 1))-def create_items_by_schema(placeholder_dir, how_many, create_images, stdout,- schema=DEFAULT_SCHEMA):+def create_products_by_schema(placeholder_dir, how_many, create_images, stdout,+ schema=DEFAULT_SCHEMA):for product_class, class_schema in create_product_classes_by_schema(schema):- create_items_by_class(+ create_products_by_class(product_class, class_schema, placeholder_dir,how_many=how_many, create_images=create_images, stdout=stdout)diff --git a/tests/test_core.py b/tests/test_core.pyindex 95ad7b547f..41e93e81d1 100644--- a/tests/test_core.py+++ b/tests/test_core.py@@ -4,9 +4,11 @@ from mock import Mockfrom saleor.core.utils import (Country, get_country_by_ip, get_currency_for_country, create_superuser)from saleor.core.utils.random_data import (create_shipping_methods,- create_fake_user, create_users)+ create_fake_user, create_users,+ create_address, create_attribute,+ create_product_classes_by_schema)from saleor.shipping.models import ShippingMethod-from saleor.userprofile.models import User+from saleor.userprofile.models import User, Address@pytest.mark.parametrize('ip_data, expected_country', [@@ -72,3 +74,35 @@ def test_create_fake_users(db):passassert User.objects.all().count() == 5++def test_create_address(db):+ assert Address.objects.all().count() == 0+ create_address()+ assert Address.objects.all().count() == 1+++def test_create_attribute(db):+ data = {'name': 'best_attribute', 'display': 'Best attribute'}+ attribute = create_attribute(**data)+ assert attribute.name == data['name']+ assert attribute.display == data['display']+++def test_create_product_classes_by_schema(db):+ schema = {'T-Shirt': {+ 'category': 'Food',+ 'product_attributes': {+ 'Sweetness': ['Sweet', 'Sour'],+ 'Healthiness': ['Healthy', 'Not really']+ },+ 'variant_attributes': {+ 'GMO': ['Yes', 'No']+ },+ 'images_dir': 'candy/',+ 'is_shipping_required': True}}+ p_class = create_product_classes_by_schema(schema)[0][0]+ assert p_class.name == 'T-Shirt'+ assert p_class.product_attributes.count() == 2+ assert p_class.variant_attributes.count() == 1+ assert p_class.is_shipping_required+