Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix random_data and test_core
Maciej Korycinski377e3694afb7
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.pyindex c15c5c9262..38ef3ff818 100644--- a/saleor/core/utils/random_data.py+++ b/saleor/core/utils/random_data.py@@ -1,3 +1,4 @@+import itertoolsimport jsonimport osimport random@@ -10,14 +11,11 @@ from unittest.mock import patchfrom django.conf import settingsfrom django.contrib.sites.models import Sitefrom django.core.files import File-from django_countries.fields import Countryfrom faker import Factoryfrom faker.providers import BaseProviderfrom measurement.measures import Weightfrom prices import Money-from saleor.core.taxes.vatlayer import get_taxed_shipping_price, get_taxes_for_country-from ...account.models import Address, Userfrom ...account.utils import store_user_addressfrom ...checkout import AddressType@@ -27,7 +25,7 @@ from ...dashboard.menu.utils import update_menufrom ...discount import DiscountValueType, VoucherTypefrom ...discount.models import Sale, Voucherfrom ...menu.models import Menu-from ...order.models import Fulfillment, Order+from ...order.models import Fulfillment, Order, OrderLinefrom ...order.utils import update_order_statusfrom ...page.models import Pagefrom ...payment.utils import (@@ -53,6 +51,7 @@ from ...product.thumbnails import (create_product_thumbnails,)from ...shipping.models import ShippingMethod, ShippingMethodType, ShippingZone+from ..taxes import interface as tax_interfacefake = Factory.create()@@ -292,7 +291,7 @@ def create_address():street_address_1=fake.street_address(),city=fake.city(),postal_code=fake.postcode(),- country=fake.country_code(),+ country=settings.DEFAULT_COUNTRY,)return address@@ -348,27 +347,44 @@ def create_fake_payment(mock_email_confirmation, order):return payment-def create_order_line(order, discounts, taxes):- product = Product.objects.filter(variants__isnull=False).order_by("?")[0]- variant = product.variants.all()[0]- quantity = random.randrange(1, 5)- variant.quantity += quantity- variant.quantity_allocated += quantity- variant.save()- return order.lines.create(- product_name=variant.display_product(),- product_sku=variant.sku,- is_shipping_required=variant.is_shipping_required(),- quantity=quantity,- variant=variant,- unit_price=variant.get_price(discounts=discounts, taxes=taxes),- tax_rate=0,+def create_order_lines(order, discounts, how_many=10):+ variants = (+ ProductVariant.objects.filter()+ .order_by("?")+ .prefetch_related("product__product_type")[:how_many])---def create_order_lines(order, discounts, taxes, how_many=10):+ variants_iter = itertools.cycle(variants)+ lines = []for dummy in range(how_many):- yield create_order_line(order, discounts, taxes)+ variant = next(variants_iter)+ quantity = random.randrange(1, 5)+ variant.quantity += quantity+ variant.quantity_allocated += quantity+ unit_price = variant.get_price(discounts)+ lines.append(+ OrderLine(+ order=order,+ product_name=variant.display_product(),+ product_sku=variant.sku,+ is_shipping_required=variant.is_shipping_required(),+ quantity=quantity,+ variant=variant,+ unit_price_net=unit_price,+ unit_price_gross=unit_price,+ tax_rate=0,+ )+ )+ ProductVariant.objects.bulk_update(variants, ["quantity", "quantity_allocated"])+ lines = OrderLine.objects.bulk_create(lines)+ for line in lines:+ unit_price = tax_interface.refresh_order_line_unit_price(line)+ line.unit_price_net = unit_price.net+ line.unit_price_gross = unit_price.gross+ line.tax_rate = unit_price.tax / unit_price.net+ OrderLine.objects.bulk_update(+ lines, ["unit_price_net", "unit_price_gross", "tax_rate"]+ )+ return linesdef create_fulfillments(order):@@ -383,15 +399,16 @@ def create_fulfillments(order):update_order_status(order)-def create_fake_order(discounts, taxes):+def create_fake_order(discounts, max_order_lines=5):user = random.choice([None, User.objects.filter(is_superuser=False).order_by("?").first()])if user:+ address = user.default_shipping_addressorder_data = {"user": user,"billing_address": user.default_billing_address,- "shipping_address": user.default_shipping_address,+ "shipping_address": address,}else:address = create_address()@@ -403,16 +420,15 @@ def create_fake_order(discounts, taxes):shipping_method = ShippingMethod.objects.order_by("?").first()shipping_price = shipping_method.price- shipping_price = get_taxed_shipping_price(shipping_price, taxes)+ shipping_price = tax_interface.apply_taxes_to_shipping(shipping_price, address)order_data.update({"shipping_method_name": shipping_method.name, "shipping_price": shipping_price})order = Order.objects.create(**order_data)- lines = create_order_lines(order, discounts, taxes, random.randrange(1, 5))-- order.total = sum([line.get_total() for line in lines], order.shipping_price)+ lines = create_order_lines(order, discounts, random.randrange(1, max_order_lines))+ order.total = sum([line.get_total() for line in lines], shipping_price)weight = Weight(kg=0)for line in order:weight += line.variant.get_weight()@@ -442,12 +458,11 @@ def create_users(how_many=10):def create_orders(how_many=10):- taxes = get_taxes_for_country(Country(settings.DEFAULT_COUNTRY))discounts = Sale.objects.active(date.today()).prefetch_related("products", "categories", "collections")- for dummy in range(how_many):- order = create_fake_order(discounts, taxes)+ for _ in range(how_many):+ order = create_fake_order(discounts)yield "Order: %s" % (order,)diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 6a3316cd28..32f85cf2c2 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -277,7 +277,7 @@ def add_variant_to_order(line.quantity += quantityline.save(update_fields=["quantity"])except OrderLine.DoesNotExist:- unit_price_net = variant.get_price(discounts)+ unit_price = variant.get_price(discounts)product_name = variant.display_product()translated_product_name = variant.display_product(translated=True)if translated_product_name == product_name:@@ -288,8 +288,8 @@ def add_variant_to_order(product_sku=variant.sku,is_shipping_required=variant.is_shipping_required(),quantity=quantity,- unit_price_net=unit_price_net,- unit_price_gross=unit_price_net,+ unit_price_net=unit_price,+ unit_price_gross=unit_price,variant=variant,)diff --git a/tests/test_core.py b/tests/test_core.pyindex 1b2f92c8f3..e2ee06c0ac 100644--- a/tests/test_core.py+++ b/tests/test_core.py@@ -123,7 +123,7 @@ def test_create_address(db):assert Address.objects.all().count() == 1-def test_create_fake_order(db, monkeypatch, image, media_root):+def test_create_fake_order(db, monkeypatch, image, media_root, settings):# Tests shouldn't depend on images present in placeholder foldermonkeypatch.setattr("saleor.core.utils.random_data.get_image", Mock(return_value=image)@@ -131,11 +131,12 @@ def test_create_fake_order(db, monkeypatch, image, media_root):for _ in random_data.create_shipping_zones():passfor _ in random_data.create_users(3):- random_data.create_products_by_schema("/", 10)- how_many = 5+ pass+ random_data.create_products_by_schema("/", False)+ how_many = 2for _ in random_data.create_orders(how_many):pass- assert Order.objects.all().count() == 5+ assert Order.objects.all().count() == 2def test_create_product_sales(db):