Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Move and split test utils
fowczrekf2ac9394f975
diff --git a/saleor/checkout/tests/test_checkout.py b/saleor/checkout/tests/test_checkout.pyindex dfc12c1382..4ab9e7991a 100644--- a/saleor/checkout/tests/test_checkout.py+++ b/saleor/checkout/tests/test_checkout.py@@ -36,7 +36,7 @@ from saleor.order import OrderEvents, OrderEventsEmailsfrom saleor.order.models import OrderEventfrom saleor.plugins.manager import get_plugins_managerfrom saleor.shipping.models import ShippingZone-from tests.utils import flush_post_commit_hooks+from saleor.tests.utils import flush_post_commit_hooksdef test_is_valid_shipping_method(checkout_with_item, address, shipping_zone):diff --git a/saleor/graphql/account/tests/test_account.py b/saleor/graphql/account/tests/test_account.pyindex 722cb4e3f8..4c803eb759 100644--- a/saleor/graphql/account/tests/test_account.py+++ b/saleor/graphql/account/tests/test_account.py@@ -31,13 +31,13 @@ from saleor.graphql.account.mutations.staff import ()from saleor.graphql.core.utils import str_to_enumfrom saleor.order.models import FulfillmentStatus, Order+from saleor.product.tests.utils import create_imagefrom tests.api.utils import (assert_no_permission,convert_dict_keys_to_camel_case,get_graphql_content,get_multipart_request_body,)-from tests.utils import create_image@pytest.fixturediff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.pyindex 35297b3c50..997d8e285d 100644--- a/saleor/graphql/checkout/tests/test_checkout.py+++ b/saleor/graphql/checkout/tests/test_checkout.py@@ -28,8 +28,8 @@ from saleor.payment import TransactionKindfrom saleor.payment.interface import GatewayResponsefrom saleor.plugins.manager import PluginsManagerfrom saleor.warehouse.models import Stock+from saleor.warehouse.tests.utils import get_available_quantity_for_stockfrom tests.api.utils import assert_no_permission, get_graphql_content-from tests.utils import get_available_quantity_for_stock@pytest.fixture(autouse=True)diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.pyindex ea9e95edbd..3588293afd 100644--- a/saleor/graphql/order/tests/test_order.py+++ b/saleor/graphql/order/tests/test_order.py@@ -29,8 +29,8 @@ from saleor.payment.models import Paymentfrom saleor.plugins.manager import PluginsManagerfrom saleor.shipping.models import ShippingMethodfrom saleor.warehouse.models import Allocation, Stock+from saleor.warehouse.tests.utils import get_available_quantity_for_stockfrom tests.api.utils import assert_no_permission, get_graphql_content-from tests.utils import get_available_quantity_for_stock@pytest.fixturediff --git a/saleor/graphql/product/tests/test_category.py b/saleor/graphql/product/tests/test_category.pyindex 3b602e606b..a61133dc64 100644--- a/saleor/graphql/product/tests/test_category.py+++ b/saleor/graphql/product/tests/test_category.py@@ -8,8 +8,8 @@ from graphql_relay import to_global_idfrom saleor.product.error_codes import ProductErrorCodefrom saleor.product.models import Category+from saleor.product.tests.utils import create_image, create_pdf_file_with_image_extfrom tests.api.utils import get_graphql_content, get_multipart_request_body-from tests.utils import create_image, create_pdf_file_with_image_extQUERY_CATEGORY = """query ($id: ID, $slug: String){diff --git a/saleor/graphql/product/tests/test_collection.py b/saleor/graphql/product/tests/test_collection.pyindex a89feec66b..8c5d236f38 100644--- a/saleor/graphql/product/tests/test_collection.py+++ b/saleor/graphql/product/tests/test_collection.py@@ -8,8 +8,8 @@ from graphql_relay import to_global_idfrom saleor.product.error_codes import ProductErrorCodefrom saleor.product.models import Collection+from saleor.product.tests.utils import create_image, create_pdf_file_with_image_extfrom tests.api.utils import get_graphql_content, get_multipart_request_body-from tests.utils import create_image, create_pdf_file_with_image_extQUERY_COLLECTION = """query ($id: ID, $slug: String){diff --git a/saleor/graphql/product/tests/test_digital_content.py b/saleor/graphql/product/tests/test_digital_content.pyindex 0aa9f130e6..8aab20e0a9 100644--- a/saleor/graphql/product/tests/test_digital_content.py+++ b/saleor/graphql/product/tests/test_digital_content.py@@ -2,8 +2,8 @@ import graphenefrom saleor.product.error_codes import ProductErrorCodefrom saleor.product.models import DigitalContent, ProductVariant+from saleor.product.tests.utils import create_imagefrom tests.api.utils import get_graphql_content, get_multipart_request_body-from tests.utils import create_imagedef test_fetch_all_digital_contents(diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.pyindex ec4fd5cb24..55954eb2c6 100644--- a/saleor/graphql/product/tests/test_product.py+++ b/saleor/graphql/product/tests/test_product.py@@ -29,6 +29,7 @@ from saleor.product.models import (ProductVariant,)from saleor.product.tasks import update_variants_names+from saleor.product.tests.utils import create_image, create_pdf_file_with_image_extfrom saleor.product.utils.attributes import associate_attribute_values_to_instancefrom saleor.warehouse.models import Allocation, Stock, Warehousefrom tests.api.utils import (@@ -36,7 +37,6 @@ from tests.api.utils import (get_graphql_content,get_multipart_request_body,)-from tests.utils import create_image, create_pdf_file_with_image_ext@pytest.fixturediff --git a/saleor/graphql/tests/fixtures.py b/saleor/graphql/tests/fixtures.pyindex b8f04c4753..39409cf634 100644--- a/saleor/graphql/tests/fixtures.py+++ b/saleor/graphql/tests/fixtures.py@@ -12,8 +12,8 @@ from graphql_jwt.shortcuts import get_tokenfrom saleor.account.models import Userfrom saleor.app.models import Appfrom saleor.graphql.views import handled_errors_logger, unhandled_errors_logger+from saleor.tests.utils import flush_post_commit_hooksfrom tests.api.utils import assert_no_permission-from tests.utils import flush_post_commit_hooksAPI_PATH = reverse("api")diff --git a/saleor/graphql/warehouse/tests/test_stock.py b/saleor/graphql/warehouse/tests/test_stock.pyindex cbffdecbad..d2d693402f 100644--- a/saleor/graphql/warehouse/tests/test_stock.py+++ b/saleor/graphql/warehouse/tests/test_stock.py@@ -2,8 +2,8 @@ import graphenefrom saleor.core.permissions import ProductPermissionsfrom saleor.warehouse.models import Stock+from saleor.warehouse.tests.utils import get_quantity_allocated_for_stockfrom tests.api.utils import assert_no_permission, get_graphql_content-from tests.utils import get_quantity_allocated_for_stockQUERY_STOCK = """query stock($id: ID!) {diff --git a/saleor/order/tests/test_order.py b/saleor/order/tests/test_order.pyindex 96e74045f1..e87f2c41d6 100644--- a/saleor/order/tests/test_order.py+++ b/saleor/order/tests/test_order.py@@ -32,7 +32,7 @@ from saleor.payment import ChargeStatusfrom saleor.payment.models import Paymentfrom saleor.product.models import Collectionfrom saleor.warehouse.models import Stock-from tests.utils import get_quantity_allocated_for_stock+from saleor.warehouse.tests.utils import get_quantity_allocated_for_stockdef test_total_setter():diff --git a/saleor/order/tests/test_order_actions.py b/saleor/order/tests/test_order_actions.pyindex de9c50f50c..4445b0e5ea 100644--- a/saleor/order/tests/test_order_actions.py+++ b/saleor/order/tests/test_order_actions.py@@ -17,8 +17,8 @@ from saleor.order.actions import (from saleor.order.models import Fulfillmentfrom saleor.payment import ChargeStatus, PaymentErrorfrom saleor.product.models import DigitalContent+from saleor.product.tests.utils import create_imagefrom saleor.warehouse.models import Allocation, Stock-from tests.utils import create_image@pytest.fixturediff --git a/saleor/product/tests/utils.py b/saleor/product/tests/utils.pynew file mode 100644index 0000000000..171d346399--- /dev/null+++ b/saleor/product/tests/utils.py@@ -0,0 +1,18 @@+from io import BytesIO++from django.core.files.uploadedfile import SimpleUploadedFile+from PIL import Image+++def create_image(image_name="product2"):+ img_data = BytesIO()+ image = Image.new("RGB", size=(1, 1), color=(255, 0, 0, 0))+ image.save(img_data, format="JPEG")+ image = SimpleUploadedFile(image_name + ".jpg", img_data.getvalue(), "image/png")+ return image, image_name+++def create_pdf_file_with_image_ext():+ file_name = "product.jpg"+ file_data = SimpleUploadedFile(file_name, b"product_data", "application/pdf")+ return file_data, file_namediff --git a/saleor/shipping/tests/test_shipping.py b/saleor/shipping/tests/test_shipping.pyindex 95e9b49eec..d7d2b93a5d 100644--- a/saleor/shipping/tests/test_shipping.py+++ b/saleor/shipping/tests/test_shipping.py@@ -4,7 +4,6 @@ from prices import Moneyfrom saleor.shipping.models import ShippingMethod, ShippingMethodType, ShippingZonefrom saleor.shipping.utils import default_shipping_zone_exists-from tests.utils import moneydef test_shipping_get_total(monkeypatch, shipping_zone):@@ -62,21 +61,21 @@ def test_applicable_shipping_methods_weight()assert "PL" in shipping_zone.countriesresult = ShippingMethod.objects.applicable_shipping_methods(- price=money(0), weight=weight, country_code="PL"+ price=Money("0", "USD"), weight=weight, country_code="PL")assert (method in result) == shipping_includeddef test_applicable_shipping_methods_country_code_outside_shipping_zone(shipping_zone):method = shipping_zone.shipping_methods.create(- minimum_order_price=money(1),- maximum_order_price=money(10),+ minimum_order_price=Money("1.0", "USD"),+ maximum_order_price=Money("10.0", "USD"),type=ShippingMethodType.PRICE_BASED,)shipping_zone.countries = ["DE"]shipping_zone.save()result = ShippingMethod.objects.applicable_shipping_methods(- price=money(5), weight=Weight(kg=0), country_code="PL"+ price=Money("5.0", "USD"), weight=Weight(kg=0), country_code="PL")assert method not in result@@ -86,19 +85,19 @@ def test_applicable_shipping_methods_inproper_shipping_method_type(shipping_zoneshipping method and the other way around."""price_method = shipping_zone.shipping_methods.create(- minimum_order_price=money(1),- maximum_order_price=money(10),+ minimum_order_price=Money("1.0", "USD"),+ maximum_order_price=Money("10.0", "USD"),minimum_order_weight=Weight(kg=100),type=ShippingMethodType.WEIGHT_BASED,)weight_method = shipping_zone.shipping_methods.create(minimum_order_weight=Weight(kg=1),maximum_order_weight=Weight(kg=10),- minimum_order_price=money(1000),+ minimum_order_price=Money("1000.0", "USD"),type=ShippingMethodType.PRICE_BASED,)result = ShippingMethod.objects.applicable_shipping_methods(- price=money(5), weight=Weight(kg=5), country_code="PL"+ price=Money("5.0", "USD"), weight=Weight(kg=5), country_code="PL")assert price_method not in resultassert weight_method not in result@@ -106,8 +105,8 @@ def test_applicable_shipping_methods_inproper_shipping_method_type(shipping_zonedef test_applicable_shipping_methods(shipping_zone):price_method = shipping_zone.shipping_methods.create(- minimum_order_price=money(1),- maximum_order_price=money(10),+ minimum_order_price=Money("1.0", "USD"),+ maximum_order_price=Money("10.0", "USD"),type=ShippingMethodType.PRICE_BASED,)weight_method = shipping_zone.shipping_methods.create(@@ -116,7 +115,7 @@ def test_applicable_shipping_methods(shipping_zone):type=ShippingMethodType.WEIGHT_BASED,)result = ShippingMethod.objects.applicable_shipping_methods(- price=money(5), weight=Weight(kg=5), country_code="PL"+ price=Money("5.0", "USD"), weight=Weight(kg=5), country_code="PL")assert price_method in resultassert weight_method in result@@ -133,7 +132,7 @@ def test_use_default_shipping_zone(shipping_zone):type=ShippingMethodType.WEIGHT_BASED,)result = ShippingMethod.objects.applicable_shipping_methods(- price=money(5), weight=Weight(kg=5), country_code="DE"+ price=Money("5.0", "USD"), weight=Weight(kg=5), country_code="DE")assert result[0] == weight_methoddiff --git a/saleor/tests/__init__.py b/saleor/tests/__init__.pynew file mode 100644index 0000000000..e69de29bb2diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.pyindex 1969c1c450..a73909c41f 100644--- a/saleor/tests/fixtures.py+++ b/saleor/tests/fixtures.py@@ -66,6 +66,7 @@ from saleor.product.models import (ProductVariant,ProductVariantTranslation,)+from saleor.product.tests.utils import create_imagefrom saleor.product.utils.attributes import associate_attribute_values_to_instancefrom saleor.shipping.models import (ShippingMethod,@@ -79,7 +80,6 @@ from saleor.warehouse.models import Allocation, Stock, Warehousefrom saleor.webhook.event_types import WebhookEventTypefrom saleor.webhook.models import Webhookfrom saleor.wishlist.models import Wishlist-from tests.utils import create_imageclass CaptureQueriesContext(BaseCaptureQueriesContext):diff --git a/saleor/tests/utils.py b/saleor/tests/utils.pynew file mode 100644index 0000000000..792c0dee7a--- /dev/null+++ b/saleor/tests/utils.py@@ -0,0 +1,14 @@+from django.db import connections, transaction+++def flush_post_commit_hooks():+ """Run all pending `transaction.on_commit()` callbacks.++ Forces all `on_commit()` hooks to run even if the transaction was not committed yet.+ """+ for alias in connections:+ connection = transaction.get_connection(alias)+ was_atomic = connection.in_atomic_block+ connection.in_atomic_block = False+ connection.run_and_clear_commit_hooks()+ connection.in_atomic_block = was_atomicdiff --git a/saleor/warehouse/tests/utils.py b/saleor/warehouse/tests/utils.pynew file mode 100644index 0000000000..33ad47eecb--- /dev/null+++ b/saleor/warehouse/tests/utils.py@@ -0,0 +1,15 @@+from django.db.models import Sum+from django.db.models.functions import Coalesce+++def get_quantity_allocated_for_stock(stock):+ """Count how many items are allocated for stock."""+ return stock.allocations.aggregate(+ quantity_allocated=Coalesce(Sum("quantity_allocated"), 0)+ )["quantity_allocated"]+++def get_available_quantity_for_stock(stock):+ """Count how many stock items are available."""+ quantity_allocated = get_quantity_allocated_for_stock(stock)+ return max(stock.quantity - quantity_allocated, 0)diff --git a/tests/utils.py b/tests/utils.pyindex b72c96be29..4facfc227b 100644--- a/tests/utils.py+++ b/tests/utils.py@@ -1,12 +1,6 @@-from io import BytesIOfrom typing import Dict, Set, Unionfrom urllib.parse import urlparse-from django.core.files.uploadedfile import SimpleUploadedFile-from django.db import connections, transaction-from django.db.models import Sum-from django.db.models.functions import Coalesce-from PIL import Imagefrom prices import Moneyfrom saleor.product.models import Product, ProductVariant@@ -27,20 +21,6 @@ def get_form_errors(response, form_name="form"):return errors.get("__all__") if errors else []-def create_image(image_name="product2"):- img_data = BytesIO()- image = Image.new("RGB", size=(1, 1), color=(255, 0, 0, 0))- image.save(img_data, format="JPEG")- image = SimpleUploadedFile(image_name + ".jpg", img_data.getvalue(), "image/png")- return image, image_name---def create_pdf_file_with_image_ext():- file_name = "product.jpg"- file_data = SimpleUploadedFile(file_name, b"product_data", "application/pdf")- return file_data, file_name--def money(amount):return Money(amount, "USD")@@ -65,29 +45,3 @@ def generate_attribute_map(obj: Union[Product, ProductVariant]) -> Dict[int, Setassignment.attribute.pk: {value.pk for value in assignment.values.all()}for assignment in qs}---def flush_post_commit_hooks():- """Run all pending `transaction.on_commit()` callbacks.-- Forces all `on_commit()` hooks to run even if the transaction was not committed yet.- """- for alias in connections:- connection = transaction.get_connection(alias)- was_atomic = connection.in_atomic_block- connection.in_atomic_block = False- connection.run_and_clear_commit_hooks()- connection.in_atomic_block = was_atomic---def get_quantity_allocated_for_stock(stock):- """Count how many items are allocated for stock."""- return stock.allocations.aggregate(- quantity_allocated=Coalesce(Sum("quantity_allocated"), 0)- )["quantity_allocated"]---def get_available_quantity_for_stock(stock):- """Count how many stock items are available."""- quantity_allocated = get_quantity_allocated_for_stock(stock)- return max(stock.quantity - quantity_allocated, 0)