Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Split fixtures in checkout module (#16875)

Piotr Zabieglik2426cbbeb708
diff --git a/conftest.py b/conftest.py
index b02cedb6e1..8e78729e23 100644
--- a/conftest.py
+++ b/conftest.py
@@ -25,10 +25,8 @@ prepare_test_db_connections()
pytest_plugins = [
"saleor.tests.fixtures",
"saleor.app.tests.fixtures",
- "saleor.discount.tests.test_utils.fixtures",
"saleor.plugins.tests.fixtures",
"saleor.graphql.tests.fixtures",
- "saleor.graphql.checkout.tests.benchmark.fixtures",
"saleor.graphql.account.tests.benchmark.fixtures",
"saleor.webhook.tests.fixtures",
"saleor.tax.tests.fixtures",
@@ -44,6 +42,7 @@ pytest_plugins = [
"saleor.permission.tests.fixtures",
"saleor.giftcard.tests.fixtures",
"saleor.discount.tests.fixtures",
+ "saleor.checkout.tests.fixtures",
]
diff --git a/saleor/checkout/tests/conftest.py b/saleor/checkout/tests/conftest.py
deleted file mode 100644
index d743bbaea0..0000000000
--- a/saleor/checkout/tests/conftest.py
+++ /dev/null
@@ -1,73 +0,0 @@
-import datetime
-from decimal import Decimal
-
-import pytest
-from django.utils import timezone
-
-from ...checkout.models import CheckoutLine
-from ...plugins.manager import get_plugins_manager
-from ..fetch import fetch_checkout_info, fetch_checkout_lines
-
-
-def priced_checkout_factory():
- def factory(checkout):
- manager = get_plugins_manager(allow_replica=False)
- lines, _ = fetch_checkout_lines(checkout)
- checkout_info = fetch_checkout_info(checkout, lines, manager)
-
- tax = Decimal("1.23")
-
- lines_to_update = []
- for line_info in lines:
- line = line_info.line
- lines_to_update.append(line)
-
- line.total_price = manager.calculate_checkout_line_total(
- checkout_info, lines, line_info, None
- )
- line.total_price_gross_amount *= tax
-
- CheckoutLine.objects.bulk_update(
- lines_to_update,
- [
- "total_price_net_amount",
- "total_price_gross_amount",
- ],
- )
-
- checkout.shipping_price = manager.calculate_checkout_shipping(
- checkout_info, lines, None
- )
- checkout.subtotal = manager.calculate_checkout_subtotal(
- checkout_info, lines, None
- )
- checkout.total = manager.calculate_checkout_total(checkout_info, lines, None)
-
- checkout.shipping_price_gross_amount *= tax
- checkout.subtotal_gross_amount *= tax
- checkout.total_gross_amount *= tax
- # Cache prices until invalidated by force
- checkout.price_expiration = timezone.now() + datetime.timedelta(days=1)
- checkout.save()
-
- return checkout
-
- return factory
-
-
-def priced_checkout_with_item(priced_checkout_factory, checkout_with_item):
- return priced_checkout_factory(checkout_with_item)
-
-
-def priced_checkout_with_items(priced_checkout_factory, checkout_with_items):
- return priced_checkout_factory(checkout_with_items)
-
-
-def priced_checkout_with_voucher_percentage(
- priced_checkout_factory, checkout_with_voucher_percentage
-):
- return priced_checkout_factory(checkout_with_voucher_percentage)
diff --git a/saleor/checkout/tests/fixtures/__init__.py b/saleor/checkout/tests/fixtures/__init__.py
new file mode 100644
index 0000000000..9c7ab5317e
--- /dev/null
+++ b/saleor/checkout/tests/fixtures/__init__.py
@@ -0,0 +1,9 @@
+from .benchmark import * # noqa: F403
+from .checkout import * # noqa: F403
+from .checkout_info import * # noqa: F403
+from .checkout_line import * # noqa: F403
+from .checkout_line_info import * # noqa: F403
+from .checkout_with_discount import * # noqa: F403
+from .checkout_with_payment import * # noqa: F403
+from .checkout_with_prices import * # noqa: F403
+from .checkout_with_transaction import * # noqa: F403
diff --git a/saleor/graphql/checkout/tests/benchmark/fixtures.py b/saleor/checkout/tests/fixtures/benchmark.py
similarity index 94%
rename from saleor/graphql/checkout/tests/benchmark/fixtures.py
rename to saleor/checkout/tests/fixtures/benchmark.py
index 6cac83fa7c..3a5e327a17 100644
--- a/saleor/graphql/checkout/tests/benchmark/fixtures.py
+++ b/saleor/checkout/tests/fixtures/benchmark.py
@@ -1,7 +1,7 @@
import pytest
from prices import Money, TaxedMoney
-from .....checkout.models import Checkout
+from ....checkout.models import Checkout
CHECKOUT_COUNT_IN_BENCHMARKS = 10
diff --git a/saleor/checkout/tests/fixtures/checkout.py b/saleor/checkout/tests/fixtures/checkout.py
new file mode 100644
index 0000000000..777421109a
--- /dev/null
+++ b/saleor/checkout/tests/fixtures/checkout.py
@@ -0,0 +1,606 @@
+import datetime
+from decimal import Decimal
+
+import pytest
+from django.utils import timezone
+
+from ....plugins.manager import get_plugins_manager
+from ...fetch import fetch_checkout_info, fetch_checkout_lines
+from ...models import Checkout, CheckoutLine, CheckoutMetadata
+from ...utils import add_variant_to_checkout
+
+
+def checkout(db, channel_USD, settings):
+ checkout = Checkout.objects.create(
+ currency=channel_USD.currency_code,
+ channel=channel_USD,
+ price_expiration=timezone.now() + settings.CHECKOUT_PRICES_TTL,
+ )
+ checkout.set_country("US", commit=True)
+ CheckoutMetadata.objects.create(checkout=checkout)
+ return checkout
+
+
+def checkout_JPY(channel_JPY):
+ checkout = Checkout.objects.create(
+ currency=channel_JPY.currency_code, channel=channel_JPY
+ )
+ checkout.set_country("JP", commit=True)
+ return checkout
+
+
+def checkout_with_item(checkout, product):
+ variant = product.variants.first()
+ checkout_info = fetch_checkout_info(
+ checkout, [], get_plugins_manager(allow_replica=False)
+ )
+ add_variant_to_checkout(checkout_info, variant, 3)
+ checkout.save()
+ return checkout
+
+
+def checkout_with_item_and_tax_exemption(checkout_with_item):
+ checkout_with_item.tax_exemption = True
+ checkout_with_item.save(update_fields=["tax_exemption"])
+ return checkout_with_item
+
+
+def checkout_with_same_items_in_multiple_lines(checkout, product):
+ variant = product.variants.first()
+ checkout_info = fetch_checkout_info(
+ checkout, [], get_plugins_manager(allow_replica=False)
+ )
+ add_variant_to_checkout(checkout_info, variant, 1)
+ add_variant_to_checkout(checkout_info, variant, 1, force_new_line=True)
+ checkout.save()
+ return checkout
+
+
+def checkout_with_item_total_0(checkout, product_price_0):
+ variant = product_price_0.variants.get()
+ checkout_info = fetch_checkout_info(
+ checkout, [], get_plugins_manager(allow_replica=False)
+ )
+ add_variant_to_checkout(checkout_info, variant, 1)
+ checkout.save()
+ return checkout
+
+
+def checkout_JPY_with_item(checkout_JPY, product_in_channel_JPY):
+ variant = product_in_channel_JPY.variants.get()
+ checkout_info = fetch_checkout_info(
+ checkout_JPY, [], get_plugins_manager(allow_replica=False)
+ )
+ add_variant_to_checkout(checkout_info, variant, 3)
+ checkout_JPY.save()
+ return checkout_JPY
+
+
+def checkouts_list(channel_USD, channel_PLN):
+ checkouts_usd = Checkout.objects.bulk_create(
+ [
+ Checkout(currency=channel_USD.currency_code, channel=channel_USD),
+ Checkout(currency=channel_USD.currency_code, channel=channel_USD),
+ Checkout(currency=channel_USD.currency_code, channel=channel_USD),
+ ]
+ )
+ checkouts_pln = Checkout.objects.bulk_create(
+ [
+ Checkout(currency=channel_PLN.currency_code, channel=channel_PLN),
+ Checkout(currency=channel_PLN.currency_code, channel=channel_PLN),
+ ]
+ )
+ return [*checkouts_pln, *checkouts_usd]
+
+
+def checkouts_assigned_to_customer(channel_USD, channel_PLN, customer_user):
+ return Checkout.objects.bulk_create(
+ [
+ Checkout(
+ currency=channel_USD.currency_code,
+ channel=channel_USD,
+ user=customer_user,
+ ),
+ Checkout(
+ currency=channel_PLN.currency_code,
+ channel=channel_PLN,
+ user=customer_user,
+ ),
+ ]
+ )
+
+
+def checkout_ready_to_complete(checkout_with_item, address, shipping_method, gift_card):
+ checkout = checkout_with_item
+ checkout.shipping_address = address
+ checkout.shipping_method = shipping_method
+ checkout.billing_address = address
+ checkout.metadata_storage.store_value_in_metadata(items={"accepted": "true"})
+ checkout.metadata_storage.store_value_in_private_metadata(
+ items={"accepted": "false"}
+ )
+ checkout_with_item.gift_cards.add(gift_card)
+ checkout.save()
+ checkout.metadata_storage.save()
+ return checkout
+
+
+def checkout_with_digital_item(checkout, digital_content, address):
+ """Create a checkout with a digital line."""
+ variant = digital_content.product_variant
+ checkout_info = fetch_checkout_info(
+ checkout, [], get_plugins_manager(allow_replica=False)
+ )
+ add_variant_to_checkout(checkout_info, variant, 1)
+ checkout.discount_amount = Decimal(0)
+ checkout.billing_address = address
+ checkout.email = "[email protected]"
+ checkout.save()
+ return checkout
+
+
+def checkout_with_shipping_required(checkout_with_item, product):
+ checkout = checkout_with_item
+ variant = product.variants.get()
+ checkout_info = fetch_checkout_info(
+ checkout, [], get_plugins_manager(allow_replica=False)
+ )
+ add_variant_to_checkout(checkout_info, variant, 3)
+ checkout.save()
+ return checkout
+
+
+def checkout_with_item_and_shipping_method(checkout_with_item, shipping_method):
+ checkout = checkout_with_item
+ checkout.shipping_method = shipping_method
+ checkout.save()
+ return checkout
+
+
+def checkout_with_shipping_method(checkout_with_shipping_address, shipping_method):
+ checkout = checkout_with_shipping_address
+
+ checkout.shipping_method = shipping_method
+ checkout.save()
+
+ return checkout
+
+
+def checkout_without_shipping_required(checkout, product_without_shipping):
+ variant = product_without_shipping.variants.get()
+ checkout_info = fetch_checkout_info(
+ checkout, [], get_plugins_manager(allow_replica=False)
+ )
+ add_variant_to_checkout(checkout_info, variant, 1)
+ checkout.save()
+ return checkout
+
+
+def checkout_with_single_item(checkout, product):
+ variant = product.variants.get()
+ checkout_info = fetch_checkout_info(
+ checkout, [], get_plugins_manager(allow_replica=False)
+ )
+ add_variant_to_checkout(checkout_info, variant, 1)
+ checkout.save()
+ return checkout
+
+
+def checkout_with_variant_without_inventory_tracking(
+ checkout, variant_without_inventory_tracking, address, shipping_method
+):
+ variant = variant_without_inventory_tracking
+ checkout_info = fetch_checkout_info(
+ checkout, [], get_plugins_manager(allow_replica=False)
+ )
+ add_variant_to_checkout(checkout_info, variant, 1)
+ checkout.shipping_address = address
+ checkout.shipping_method = shipping_method
+ checkout.billing_address = address
+ checkout.metadata_storage.store_value_in_metadata(items={"accepted": "true"})
+ checkout.metadata_storage.store_value_in_private_metadata(
+ items={"accepted": "false"}
+ )
+ checkout.save()
+ checkout.metadata_storage.save()
+ return checkout
+
+
+def checkout_with_variants(
+ checkout,
+ stock,
+ product_with_default_variant,
+ product_with_single_variant,
+ product_with_two_variants,
+):
+ checkout_info = fetch_checkout_info(
+ checkout, [], get_plugins_manager(allow_replica=False)
+ )
+
+ add_variant_to_checkout(
+ checkout_info, product_with_default_variant.variants.get(), 1
+ )
+ add_variant_to_checkout(
+ checkout_info, product_with_single_variant.variants.get(), 10
+ )
+ add_variant_to_checkout(
+ checkout_info, product_with_two_variants.variants.first(), 3
+ )
+ add_variant_to_checkout(checkout_info, product_with_two_variants.variants.last(), 5)
+
+ checkout.save()
+ return checkout
+
+
+def checkout_with_shipping_address(checkout_with_variants, address):
+ checkout = checkout_with_variants
+
+ checkout.shipping_address = address.get_copy()
+ checkout.save()
+
+ return checkout
+
+
+def checkout_with_billing_address(checkout_with_shipping_method, address):
+ checkout = checkout_with_shipping_method
+
+ checkout.billing_address = address
+ checkout.save()
+
+ return checkout
+
+
+def checkout_with_variants_for_cc(
+ checkout, stocks_for_cc, product_variant_list, product_with_two_variants
+):
+ CheckoutLine.objects.bulk_create(
+ [
+ CheckoutLine(
+ checkout=checkout,
+ variant=product_variant_list[0],
+ quantity=3,
+ currency="USD",
+ ),
+ CheckoutLine(
+ checkout=checkout,
+ variant=product_variant_list[1],
+ quantity=10,
+ currency="USD",
+ ),
+ CheckoutLine(
+ checkout=checkout,
+ variant=product_with_two_variants.variants.last(),
+ quantity=5,
+ currency="USD",
+ ),
+ ]
+ )
+ return checkout
+
+
+def checkout_with_shipping_address_for_cc(checkout_with_variants_for_cc, address):
+ checkout = checkout_with_variants_for_cc
+
+ checkout.shipping_address = address.get_copy()
+ checkout.save()
+
+ return checkout
+
+
+def checkout_with_billing_address_for_cc(checkout_with_delivery_method_for_cc, address):
+ checkout = checkout_with_delivery_method_for_cc
+
+ checkout.billing_address = address
+ checkout.save()
+
+ return checkout
+
+
+def checkout_with_delivery_method_for_cc(
+ warehouses_for_cc, checkout_with_shipping_address_for_cc
+):
+ checkout = checkout_with_shipping_address_for_cc
+ checkout.collection_point = warehouses_for_cc[1]
+
+ checkout.save()
+
+ return checkout
+
+
+def checkout_with_items(checkout, product_list, product):
+ variant = product.variants.get()
+ checkout_info = fetch_checkout_info(
+ checkout, [], get_plugins_manager(allow_replica=False)
+ )
+ add_variant_to_checkout(checkout_info, variant, 1)
+ for prod in product_list:
+ variant = prod.variants.get()
+ add_variant_to_checkout(checkout_info, variant, 1)
+ checkout.save()
+ checkout.refresh_from_db()
+ return checkout
+
+
+def checkout_with_items_and_shipping(checkout_with_items, address, shipping_method):
+ checkout_with_items.shipping_address = address
+ checkout_with_items.shipping_method = shipping_method
+ checkout_with_items.billing_address = address
+ checkout_with_items.save()
+ return checkout_with_items
+
+
+def checkout_with_item_and_shipping(checkout_with_item, address, shipping_method):
+ checkout_with_item.shipping_address = address
+ checkout_with_item.shipping_method = shipping_method
+ checkout_with_item.billing_address = address
+ checkout_with_item.save()
+ return checkout_with_item
+
+
+def checkout_with_gift_card(checkout_with_item, gift_card):
+ checkout_with_item.gift_cards.add(gift_card)
+ checkout_with_item.save()
+ return checkout_with_item
+
+
+def checkout_with_preorders_only(
+ checkout,
+ stocks_for_cc,
+ preorder_variant_with_end_date,
+ preorder_variant_channel_threshold,
+):
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(
+ checkout, lines, get_plugins_manager(allow_replica=False)
+ )
+ add_variant_to_checkout(checkout_info, preorder_variant_with_end_date, 2)
+ add_variant_to_checkout(checkout_info, preorder_variant_channel_threshold, 2)
+
+ checkout.save()
+ return checkout
+
+
+def checkout_with_preorders_and_regular_variant(
+ checkout, stocks_for_cc, preorder_variant_with_end_date, product_variant_list
+):
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(
+ checkout, lines, get_plugins_manager(allow_replica=False)
+ )
+ add_variant_to_checkout(checkout_info, preorder_variant_with_end_date, 2)
+ add_variant_to_checkout(checkout_info, product_variant_list[0], 2)
+
+ checkout.save()
+ return checkout
+
+
+def checkout_with_gift_card_items(
+ checkout, non_shippable_gift_card_product, shippable_gift_card_product
+):
+ checkout_info = fetch_checkout_info(
+ checkout, [], get_plugins_manager(allow_replica=False)
+ )
+ non_shippable_variant = non_shippable_gift_card_product.variants.get()
+ shippable_variant = shippable_gift_card_product.variants.get()
+ add_variant_to_checkout(checkout_info, non_shippable_variant, 1)
+ add_variant_to_checkout(checkout_info, shippable_variant, 2)
+ checkout.save()
+ return checkout
+
+
+def checkout_with_item_and_preorder_item(
+ checkout_with_item, product, preorder_variant_channel_threshold
+):
+ checkout_info = fetch_checkout_info(
+ checkout_with_item, [], get_plugins_manager(allow_replica=False)
+ )
+ add_variant_to_checkout(checkout_info, preorder_variant_channel_threshold, 1)
+ return checkout_with_item
+
+
+def checkout_with_problems(
+ checkout_with_items,
+ product_type,
+ address,
+ shipping_method,
+ category,
+ default_tax_class,
+ channel_USD,
+ warehouse,
+):
+ checkout_with_items.shipping_address = address
+ checkout_with_items.billing_address = address
+ checkout_with_items.shipping_method = shipping_method
+ checkout_with_items.save(
+ update_fields=["shipping_address", "shipping_method", "billing_address"]
+ )
+
+ first_line = checkout_with_items.lines.first()
+ first_line.variant.track_inventory = True
+ first_line.variant.save(update_fields=["track_inventory"])
+
+ product_type = first_line.variant.product.product_type
+ product_type.is_shipping_required = True
+ product_type.save(update_fields=["is_shipping_required"])
+
+ first_line.variant.stocks.all().delete()
+
+ second_line = checkout_with_items.lines.last()
+
+ available_at = datetime.datetime.now(tz=datetime.UTC) + datetime.timedelta(days=5)
+ product = second_line.variant.product
+ product.channel_listings.update(
+ available_for_purchase_at=available_at, is_published=False
+ )
+
+ return checkout_with_items
+
+
+def user_checkout(customer_user, channel_USD):
+ checkout = Checkout.objects.create(
+ user=customer_user,
+ channel=channel_USD,
+ billing_address=customer_user.default_billing_address,
+ shipping_address=customer_user.default_shipping_address,
+ note="Test notes",
+ currency="USD",
+ )
+ CheckoutMetadata.objects.create(checkout=checkout)
+ return checkout
+
+
+def user_checkout_for_cc(customer_user, channel_USD, warehouse_for_cc):
+ checkout = Checkout.objects.create(
+ user=customer_user,
+ email=customer_user.email,
+ channel=channel_USD,
+ billing_address=customer_user.default_billing_address,
+ shipping_address=warehouse_for_cc.address,
+ collection_point=warehouse_for_cc,
+ note="Test notes",
+ currency="USD",
+ )
+ return checkout
+
+
+def user_checkout_PLN(customer_user, channel_PLN):
+ checkout = Checkout.objects.create(
+ user=customer_user,
+ channel=channel_PLN,
+ billing_address=customer_user.default_billing_address,
+ shipping_address=customer_user.default_shipping_address,
+ note="Test notes",
+ currency="PLN",
+ )
+ return checkout
+
+
+def user_checkout_with_items(user_checkout, product_list):
+ checkout_info = fetch_checkout_info(
+ user_checkout, [], get_plugins_manager(allow_replica=False)
+ )
+ for product in product_list:
+ variant = product.variants.get()
+ add_variant_to_checkout(checkout_info, variant, 1)
+ user_checkout.refresh_from_db()
+ return user_checkout
+
+
+def user_checkout_with_items_for_cc(user_checkout_for_cc, product_list):
+ checkout_info = fetch_checkout_info(
+ user_checkout_for_cc, [], get_plugins_manager(allow_replica=False)
+ )
+ for product in product_list:
+ variant = product.variants.get()
+ add_variant_to_checkout(checkout_info, variant, 1)
+ user_checkout_for_cc.refresh_from_db()
+ return user_checkout_for_cc
+
+
+def user_checkouts(request, user_checkout_with_items, user_checkout_with_items_for_cc):
+ if request.param == "regular":
+ return user_checkout_with_items
+ if request.param == "click_and_collect":
+ return user_checkout_with_items_for_cc
+ raise ValueError("Internal test error")
+
+
+def customer_checkout(customer_user, checkout_with_voucher_percentage_and_shipping):
+ checkout_with_voucher_percentage_and_shipping.user = customer_user
+ checkout_with_voucher_percentage_and_shipping.save()
+ return checkout_with_voucher_percentage_and_shipping
+
+
+def checkout_for_cc(channel_USD, customer_user):
+ checkout = Checkout.objects.create(
+ channel=channel_USD,
+ billing_address=customer_user.default_billing_address,
+ shipping_address=customer_user.default_shipping_address,
+ note="Test notes",
+ currency="USD",
+ email=customer_user.email,
+ )
+ CheckoutMetadata.objects.create(checkout=checkout)
+ return checkout
+
+
+def checkout_with_item_for_cc(checkout_for_cc, product_variant_list):
+ CheckoutLine.objects.create(
+ checkout=checkout_for_cc,
+ variant=product_variant_list[0],
+ quantity=1,
+ currency=checkout_for_cc.currency,
+ )
+ return checkout_for_cc
+
+
+def checkout_with_items_for_cc(checkout_for_cc, product_variant_list):
+ CheckoutLine.objects.bulk_create(
+ [
+ CheckoutLine(
+ checkout=checkout_for_cc,
+ variant=product_variant_list[0],
+ quantity=1,
+ currency=checkout_for_cc.currency,
+ ),
+ CheckoutLine(
+ checkout=checkout_for_cc,
+ variant=product_variant_list[1],
+ quantity=1,
+ currency=checkout_for_cc.currency,
+ ),
+ CheckoutLine(
+ checkout=checkout_for_cc,
+ variant=product_variant_list[2],
+ quantity=1,
+ currency=checkout_for_cc.currency,
+ ),
+ ]
+ )
+ checkout_for_cc.set_country("US", commit=True)
+
+ return checkout_for_cc
diff --git a/saleor/checkout/tests/fixtures/checkout_info.py b/saleor/checkout/tests/fixtures/checkout_info.py
new file mode 100644
index 0000000000..135a813aef
--- /dev/null
+++ b/saleor/checkout/tests/fixtures/checkout_info.py
@@ -0,0 +1,41 @@
+import pytest
+
+from ....plugins.manager import get_plugins_manager
+from ....shipping.models import ShippingMethodChannelListing
+from ...fetch import CheckoutInfo, fetch_checkout_info, fetch_checkout_lines
+
+
+def checkout_info(checkout_lines_info):
+ manager = get_plugins_manager(allow_replica=False)
+ checkout = checkout_lines_info[0].line.checkout
+ checkout_info = fetch_checkout_info(checkout, checkout_lines_info, manager)
+ return checkout_info
+
+
+def checkout_with_items_and_shipping_info(checkout_with_items_and_shipping):
+ checkout = checkout_with_items_and_shipping
+ channel = checkout.channel
+ shipping_address = checkout.shipping_address
+ shipping_method = checkout.shipping_method
+ shipping_channel_listing = ShippingMethodChannelListing.objects.get(
+ channel=channel,
+ shipping_method=shipping_method,
+ )
+ manager = get_plugins_manager(allow_replica=False)
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = CheckoutInfo(
+ checkout=checkout,
+ user=checkout.user,
+ channel=channel,
+ billing_address=checkout.billing_address,
+ shipping_address=shipping_address,
+ shipping_method=shipping_method,
+ shipping_channel_listings=[shipping_channel_listing],
+ tax_configuration=channel.tax_configuration,
+ discounts=[],
+ manager=manager,
+ lines=lines,
+ )
+ return checkout_info
diff --git a/saleor/checkout/tests/fixtures/checkout_line.py b/saleor/checkout/tests/fixtures/checkout_line.py
new file mode 100644
index 0000000000..9559366cbb
--- /dev/null
+++ b/saleor/checkout/tests/fixtures/checkout_line.py
@@ -0,0 +1,108 @@
+import datetime
+
+import pytest
+from django.utils import timezone
+
+from ....plugins.manager import get_plugins_manager
+from ....warehouse.models import PreorderReservation, Reservation
+from ...fetch import fetch_checkout_info
+from ...utils import add_variant_to_checkout
+
+
+def checkout_line(checkout_with_item):
+ return checkout_with_item.lines.first()
+
+
+def checkout_lines(checkout_with_items):
+ return checkout_with_items.lines.all()
+
+
+def checkout_line_with_reservation_in_many_stocks(
+ customer_user, variant_with_many_stocks, checkout
+):
+ variant = variant_with_many_stocks
+ stocks = variant.stocks.all().order_by("pk")
+ checkout_line = checkout.lines.create(
+ variant=variant,
+ quantity=3,
+ )
+
+ reserved_until = timezone.now() + datetime.timedelta(minutes=5)
+
+ Reservation.objects.bulk_create(
+ [
+ Reservation(
+ checkout_line=checkout_line,
+ stock=stocks[0],
+ quantity_reserved=2,
+ reserved_until=reserved_until,
+ ),
+ Reservation(
+ checkout_line=checkout_line,
+ stock=stocks[1],
+ quantity_reserved=1,
+ reserved_until=reserved_until,
+ ),
+ ]
+ )
+
+ return checkout_line
+
+
+def checkout_line_with_one_reservation(
+ customer_user, variant_with_many_stocks, checkout
+):
+ variant = variant_with_many_stocks
+ stocks = variant.stocks.all().order_by("pk")
+ checkout_line = checkout.lines.create(
+ variant=variant,
+ quantity=2,
+ )
+
+ reserved_until = timezone.now() + datetime.timedelta(minutes=5)
+
+ Reservation.objects.create(
+ checkout_line=checkout_line,
+ stock=stocks[0],
+ quantity_reserved=2,
+ reserved_until=reserved_until,
+ )
+
+ return checkout_line
+
+
+def checkout_line_with_preorder_item(
+ checkout, product, preorder_variant_channel_threshold
+):
+ checkout_info = fetch_checkout_info(
+ checkout, [], get_plugins_manager(allow_replica=False)
+ )
+ add_variant_to_checkout(checkout_info, preorder_variant_channel_threshold, 1)
+ return checkout.lines.last()
+
+
+def checkout_line_with_reserved_preorder_item(
+ checkout, product, preorder_variant_channel_threshold
+):
+ checkout_info = fetch_checkout_info(
+ checkout, [], get_plugins_manager(allow_replica=False)
+ )
+ add_variant_to_checkout(checkout_info, preorder_variant_channel_threshold, 2)
+ checkout_line = checkout.lines.last()
+
+ reserved_until = timezone.now() + datetime.timedelta(minutes=5)
+
+ PreorderReservation.objects.create(
+ checkout_line=checkout_line,
+ product_variant_channel_listing=checkout_line.variant.channel_listings.first(),
+ quantity_reserved=2,
+ reserved_until=reserved_until,
+ )
+
+ return checkout_line
diff --git a/saleor/checkout/tests/fixtures/checkout_line_info.py b/saleor/checkout/tests/fixtures/checkout_line_info.py
new file mode 100644
index 0000000000..f40aa42b67
--- /dev/null
+++ b/saleor/checkout/tests/fixtures/checkout_line_info.py
@@ -0,0 +1,44 @@
+import pytest
+
+from ...fetch import fetch_checkout_lines
+
+
+def checkout_lines_info(checkout_with_items, categories, published_collections):
+ lines = checkout_with_items.lines.all()
+ category1, category2 = categories
+
+ product1 = lines[0].variant.product
+ product1.category = category1
+ product1.collections.add(*published_collections[:2])
+ product1.save()
+
+ product2 = lines[1].variant.product
+ product2.category = category2
+ product2.collections.add(published_collections[0])
+ product2.save()
+
+ lines_info, _ = fetch_checkout_lines(checkout_with_items)
+ return lines_info
+
+
+def checkout_lines_with_multiple_quantity_info(
+ checkout_with_items, categories, published_collections
+):
+ checkout_with_items.lines.update(quantity=5)
+ lines = checkout_with_items.lines.all()
+ category1, category2 = categories
+
+ product1 = lines[0].variant.product
+ product1.category = category1
+ product1.collections.add(*published_collections[:2])
+ product1.save()
+
+ product2 = lines[1].variant.product
+ product2.category = category2
+ product2.collections.add(published_collections[0])
+ product2.save()
+
+ lines_info, _ = fetch_checkout_lines(checkout_with_items)
+ return lines_info
diff --git a/saleor/checkout/tests/fixtures/checkout_with_discount.py b/saleor/checkout/tests/fixtures/checkout_with_discount.py
new file mode 100644
index 0000000000..4e66127432
--- /dev/null
+++ b/saleor/checkout/tests/fixtures/checkout_with_discount.py
@@ -0,0 +1,263 @@
+from decimal import Decimal
+
+import graphene
+import pytest
+from prices import Money
+
+from ....discount import DiscountType, DiscountValueType, RewardType, RewardValueType
+from ....discount.models import CheckoutDiscount, CheckoutLineDiscount, Promotion
+from ....plugins.manager import get_plugins_manager
+from ....product.models import ProductVariantChannelListing
+from ...fetch import fetch_checkout_info, fetch_checkout_lines
+from ...models import CheckoutLine
+from ...utils import add_variant_to_checkout, add_voucher_to_checkout
+
+
+def checkout_with_item_on_sale(checkout_with_item, promotion_converted_from_sale):
+ line = checkout_with_item.lines.first()
+ channel = checkout_with_item.channel
+ discount_amount = Decimal("5.0")
+ variant = line.variant
+ variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
+ predicate = {"variantPredicate": {"ids": [variant_id]}}
+ rule = promotion_converted_from_sale.rules.first()
+ rule.catalogue_predicate = predicate
+ rule.reward_value = discount_amount
+ rule.save(update_fields=["catalogue_predicate", "reward_value"])
+ rule.channels.add(channel)
+ channel_listing = variant.channel_listings.get(channel=channel)
+ channel_listing.discounted_price_amount = (
+ channel_listing.price_amount - discount_amount
+ )
+ channel_listing.save(update_fields=["discounted_price_amount"])
+
+ CheckoutLineDiscount.objects.create(
+ line=line,
+ promotion_rule=rule,
+ type=DiscountType.SALE,
+ value_type=rule.reward_value_type,
+ value=discount_amount,
+ amount_value=discount_amount * line.quantity,
+ currency=channel.currency_code,
+ )
+
+ return checkout_with_item
+
+
+def checkout_with_item_on_promotion(checkout_with_item):
+ line = checkout_with_item.lines.first()
+ channel = checkout_with_item.channel
+ promotion = Promotion.objects.create(name="Checkout promotion")
+
+ variant = line.variant
+
+ reward_value = Decimal("5")
+ rule = promotion.rules.create(
+ catalogue_predicate={
+ "productPredicate": {
+ "ids": [graphene.Node.to_global_id("Product", variant.product.id)]
+ }
+ },
+ reward_value_type=RewardValueType.FIXED,
+ reward_value=reward_value,
+ )
+ rule.channels.add(channel)
+
+ variant_channel_listing = variant.channel_listings.get(channel=channel)
+
+ variant_channel_listing.discounted_price_amount = (
+ variant_channel_listing.price_amount - reward_value
+ )
+ variant_channel_listing.save(update_fields=["discounted_price_amount"])
+
+ variant_channel_listing.variantlistingpromotionrule.create(
+ promotion_rule=rule,
+ discount_amount=reward_value,
+ currency=channel.currency_code,
+ )
+ CheckoutLineDiscount.objects.create(
+ line=line,
+ type=DiscountType.PROMOTION,
+ value_type=DiscountValueType.FIXED,
+ value=reward_value,
+ amount_value=reward_value * line.quantity,
+ currency=channel.currency_code,
+ promotion_rule=rule,
+ )
+
+ return checkout_with_item
+
+
+def checkout_with_item_and_order_discount(
+ checkout_with_item, catalogue_promotion_without_rules
+):
+ channel = checkout_with_item.channel
+
+ reward_value = Decimal("5")
+
+ rule = catalogue_promotion_without_rules.rules.create(
+ order_predicate={
+ "discountedObjectPredicate": {"baseSubtotalPrice": {"range": {"gte": 20}}}
+ },
+ reward_value_type=RewardValueType.FIXED,
+ reward_value=reward_value,
+ reward_type=RewardType.SUBTOTAL_DISCOUNT,
+ )
+ rule.channels.add(channel)
+
+ CheckoutDiscount.objects.create(
+ checkout=checkout_with_item,
+ promotion_rule=rule,
+ type=DiscountType.ORDER_PROMOTION,
+ value_type=rule.reward_value_type,
+ value=rule.reward_value,
+ amount_value=rule.reward_value,
+ currency=channel.currency_code,
+ )
+ checkout_with_item.discount_amount = reward_value
+ checkout_with_item.save(update_fields=["discount_amount"])
+
+ return checkout_with_item
+
+
+def checkout_with_item_and_gift_promotion(checkout_with_item, gift_promotion_rule):
+ channel = checkout_with_item.channel
+ variants = gift_promotion_rule.gifts.all()
+ variant_listings = ProductVariantChannelListing.objects.filter(variant__in=variants)
+ top_price, variant_id = max(
+ variant_listings.values_list("discounted_price_amount", "variant")
+ )
+
+ line = CheckoutLine.objects.create(
+ checkout=checkout_with_item,
+ quantity=1,
+ variant_id=variant_id,
+ is_gift=True,
+ currency="USD",
+ )
+
+ CheckoutLineDiscount.objects.create(
+ line=line,
+ promotion_rule=gift_promotion_rule,
+ type=DiscountType.ORDER_PROMOTION,
+ value_type=RewardValueType.FIXED,
+ value=top_price,
+ amount_value=top_price,
+ currency=channel.currency_code,
+ )
+
+ return checkout_with_item
+
+
+def checkout_with_item_and_voucher(checkout_with_item, voucher):
+ manager = get_plugins_manager(allow_replica=False)
+ lines, _ = fetch_checkout_lines(checkout_with_item)
+ checkout_info = fetch_checkout_info(checkout_with_item, lines, manager)
+ add_voucher_to_checkout(
+ manager, checkout_info, lines, voucher, voucher.codes.first()
+ )
+ checkout_with_item.refresh_from_db()
+ return checkout_with_item
+
+
+def checkout_with_item_and_voucher_specific_products(
+ checkout_with_item, voucher_specific_product_type
+):
+ manager = get_plugins_manager(allow_replica=False)
+ lines, _ = fetch_checkout_lines(checkout_with_item)
+ checkout_info = fetch_checkout_info(checkout_with_item, lines, manager)
+ add_voucher_to_checkout(
+ manager,
+ checkout_info,
+ lines,
+ voucher_specific_product_type,
+ voucher_specific_product_type.codes.first(),
+ )
+ checkout_with_item.refresh_from_db()
+ return checkout_with_item
+
+
+def checkout_with_item_and_voucher_once_per_order(checkout_with_item, voucher):
+ voucher.apply_once_per_order = True
+ voucher.save()
+ manager = get_plugins_manager(allow_replica=False)
+ lines, _ = fetch_checkout_lines(checkout_with_item)
+ checkout_info = fetch_checkout_info(checkout_with_item, lines, manager)
+ add_voucher_to_checkout(
+ manager, checkout_info, lines, voucher, voucher.codes.first()
+ )
+ checkout_with_item.refresh_from_db()
+ return checkout_with_item
+
+
+def checkout_with_item_and_voucher_and_shipping_method(
+ checkout_with_item_and_voucher, shipping_method
+):
+ checkout = checkout_with_item_and_voucher
+ checkout.shipping_method = shipping_method
+ checkout.save()
+ return checkout
+
+
+def checkout_with_voucher(checkout, product, voucher):
+ variant = product.variants.get()
+ checkout_info = fetch_checkout_info(
+ checkout, [], get_plugins_manager(allow_replica=False)
+ )
+ add_variant_to_checkout(checkout_info, variant, 3)
+ checkout.voucher_code = voucher.code
+ checkout.discount = Money("20.00", "USD")
+ checkout.save()
+ return checkout
+
+
+def checkout_with_voucher_percentage(checkout, product, voucher_percentage):
+ variant = product.variants.get()
+ checkout_info = fetch_checkout_info(
+ checkout, [], get_plugins_manager(allow_replica=False)
+ )
+ add_variant_to_checkout(checkout_info, variant, 3)
+ checkout.voucher_code = voucher_percentage.code
+ checkout.discount = Money("3.00", "USD")
+ checkout.save()
+ return checkout
+
+
+def checkout_with_voucher_percentage_and_shipping(
+ checkout_with_voucher_percentage, shipping_method, address
+):
+ checkout = checkout_with_voucher_percentage
+ checkout.shipping_method = shipping_method
+ checkout.shipping_address = address
+ checkout.save()
+ return checkout
+
+
+def checkout_with_voucher_free_shipping(
+ checkout_with_items_and_shipping, voucher_free_shipping
+):
+ manager = get_plugins_manager(allow_replica=False)
+ lines, _ = fetch_checkout_lines(checkout_with_items_and_shipping)
+ checkout_info = fetch_checkout_info(
+ checkout_with_items_and_shipping, lines, manager
+ )
+ add_voucher_to_checkout(
+ manager,
+ checkout_info,
+ lines,
+ voucher_free_shipping,
+ voucher_free_shipping.codes.first(),
+ )
+ return checkout_with_items_and_shipping
diff --git a/saleor/graphql/checkout/tests/benchmark/conftest.py b/saleor/checkout/tests/fixtures/checkout_with_payment.py
similarity index 55%
rename from saleor/graphql/checkout/tests/benchmark/conftest.py
rename to saleor/checkout/tests/fixtures/checkout_with_payment.py
index 267935c81a..c0ab588f7c 100644
--- a/saleor/graphql/checkout/tests/benchmark/conftest.py
+++ b/saleor/checkout/tests/fixtures/checkout_with_payment.py
@@ -1,150 +1,34 @@
import pytest
-from .....checkout import calculations
-from .....checkout.fetch import fetch_checkout_info, fetch_checkout_lines
-from .....checkout.models import CheckoutLine
-from .....checkout.utils import add_variant_to_checkout, add_voucher_to_checkout
-from .....payment import ChargeStatus, TransactionKind
-from .....payment.models import Payment
-from .....plugins.manager import get_plugins_manager
+from ....payment import ChargeStatus, TransactionKind
+from ....payment.models import Payment
+from ....plugins.manager import get_plugins_manager
+from ... import calculations
+from ...fetch import fetch_checkout_info, fetch_checkout_lines
+from ...utils import add_variant_to_checkout
@pytest.fixture
-def customer_checkout(customer_user, checkout_with_voucher_percentage_and_shipping):
- checkout_with_voucher_percentage_and_shipping.user = customer_user
- checkout_with_voucher_percentage_and_shipping.save()
- return checkout_with_voucher_percentage_and_shipping
-
-
-def checkout_with_variants(
- checkout,
- stock,
- product_with_default_variant,
- product_with_single_variant,
- product_with_two_variants,
-):
- checkout_info = fetch_checkout_info(
- checkout, [], get_plugins_manager(allow_replica=False)
- )
-
- add_variant_to_checkout(
- checkout_info, product_with_default_variant.variants.get(), 1
- )
- add_variant_to_checkout(
- checkout_info, product_with_single_variant.variants.get(), 10
- )
- add_variant_to_checkout(
- checkout_info, product_with_two_variants.variants.first(), 3
- )
- add_variant_to_checkout(checkout_info, product_with_two_variants.variants.last(), 5)
-
- checkout.save()
- return checkout
-
-
-def checkout_with_variants_for_cc(checkout_for_cc, stocks_for_cc, product_variant_list):
- CheckoutLine.objects.bulk_create(
+def checkout_with_payments(checkout):
+ Payment.objects.bulk_create(
[
- CheckoutLine(
- checkout=checkout_for_cc,
- variant=product_variant_list[0],
- quantity=1,
- currency="USD",
+ Payment(
+ gateway="mirumee.payments.dummy", is_active=True, checkout=checkout
),
- CheckoutLine(
- checkout=checkout_for_cc,
- variant=product_variant_list[1],
- quantity=1,
- currency="USD",
+ Payment(
+ gateway="mirumee.payments.dummy", is_active=False, checkout=checkout
),
]
)
- return checkout_for_cc
-
-
-def checkout_with_shipping_address(checkout_with_variants, address):
- checkout = checkout_with_variants
-
- checkout.shipping_address = address.get_copy()
- checkout.save()
-
- return checkout
-
-
-def checkout_with_shipping_address_for_cc(checkout_with_variants_for_cc, address):
- checkout = checkout_with_variants_for_cc
-
- checkout.shipping_address = address.get_copy()
- checkout.save()
-
- return checkout
-
-
-def checkout_with_shipping_method(checkout_with_shipping_address, shipping_method):
- checkout = checkout_with_shipping_address
-
- checkout.shipping_method = shipping_method
- checkout.save()
-
- return checkout
-
-
-def checkout_with_delivery_method_for_cc(
- warehouses_for_cc, checkout_with_shipping_address_for_cc
-):
- checkout = checkout_with_shipping_address_for_cc
- checkout.collection_point = warehouses_for_cc[1]
-
- checkout.save()
-
- return checkout
-
-
-def checkout_with_billing_address(checkout_with_shipping_method, address):
- checkout = checkout_with_shipping_method
-
- checkout.billing_address = address
- checkout.save()
-
- return checkout
-
-
-def checkout_with_billing_address_for_cc(checkout_with_delivery_method_for_cc, address):
- checkout = checkout_with_delivery_method_for_cc
-
- checkout.billing_address = address
- checkout.save()
-
return checkout
@pytest.fixture
-def checkout_with_voucher(checkout_with_billing_address, voucher):
+def checkout_with_charged_payment(checkout_with_billing_address):
checkout = checkout_with_billing_address
manager = get_plugins_manager(allow_replica=False)
lines, _ = fetch_checkout_lines(checkout)
checkout_info = fetch_checkout_info(checkout, lines, manager)
- add_voucher_to_checkout(
- manager, checkout_info, lines, voucher, voucher.codes.first()
- )
- return checkout
-
-
-def checkout_with_charged_payment(checkout_with_voucher):
- checkout = checkout_with_voucher
- manager = get_plugins_manager(allow_replica=False)
- lines, _ = fetch_checkout_lines(checkout)
- checkout_info = fetch_checkout_info(checkout_with_voucher, lines, manager)
- manager = get_plugins_manager(allow_replica=False)
taxed_total = calculations.checkout_total(
manager=manager,
checkout_info=checkout_info,
@@ -174,21 +58,12 @@ def checkout_with_charged_payment(checkout_with_voucher):
@pytest.fixture
-def checkout_with_digital_line_with_charged_payment(
- checkout_with_billing_address, digital_content, site_settings
-):
- checkout = checkout_with_billing_address
- variant = digital_content.product_variant
-
- site_settings.automatic_fulfillment_digital_products = True
- site_settings.save(update_fields=["automatic_fulfillment_digital_products"])
-
- checkout_info = fetch_checkout_info(
- checkout, [], get_plugins_manager(allow_replica=False)
- )
- add_variant_to_checkout(checkout_info, variant, 1)
+def checkout_with_charged_payment_for_cc(checkout_with_billing_address_for_cc):
+ checkout = checkout_with_billing_address_for_cc
manager = get_plugins_manager(allow_replica=False)
lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, manager)
+ manager = get_plugins_manager(allow_replica=False)
taxed_total = calculations.checkout_total(
manager=manager,
checkout_info=checkout_info,
@@ -213,15 +88,24 @@ def checkout_with_digital_line_with_charged_payment(
gateway_response={},
is_success=True,
)
+
return checkout
@pytest.fixture
-def checkout_with_charged_payment_for_cc(checkout_with_billing_address_for_cc):
- checkout = checkout_with_billing_address_for_cc
+def checkout_preorder_with_charged_payment(
+ checkout_with_billing_address,
+ preorder_variant_channel_threshold,
+ preorder_variant_global_threshold,
+):
+ checkout = checkout_with_billing_address
manager = get_plugins_manager(allow_replica=False)
lines, _ = fetch_checkout_lines(checkout)
checkout_info = fetch_checkout_info(checkout, lines, manager)
+ add_variant_to_checkout(checkout_info, preorder_variant_channel_threshold, 1)
+ add_variant_to_checkout(checkout_info, preorder_variant_global_threshold, 1)
+
+ lines, _ = fetch_checkout_lines(checkout)
manager = get_plugins_manager(allow_replica=False)
taxed_total = calculations.checkout_total(
manager=manager,
@@ -252,20 +136,21 @@ def checkout_with_charged_payment_for_cc(checkout_with_billing_address_for_cc):
@pytest.fixture
-def checkout_preorder_with_charged_payment(
- checkout_with_billing_address,
- preorder_variant_channel_threshold,
- preorder_variant_global_threshold,
+def checkout_with_digital_line_with_charged_payment(
+ checkout_with_billing_address, digital_content, site_settings
):
checkout = checkout_with_billing_address
- manager = get_plugins_manager(allow_replica=False)
- lines, _ = fetch_checkout_lines(checkout)
- checkout_info = fetch_checkout_info(checkout, lines, manager)
- add_variant_to_checkout(checkout_info, preorder_variant_channel_threshold, 1)
- add_variant_to_checkout(checkout_info, preorder_variant_global_threshold, 1)
+ variant = digital_content.product_variant
- lines, _ = fetch_checkout_lines(checkout)
+ site_settings.automatic_fulfillment_digital_products = True
+ site_settings.save(update_fields=["automatic_fulfillment_digital_products"])
+
+ checkout_info = fetch_checkout_info(
+ checkout, [], get_plugins_manager(allow_replica=False)
+ )
+ add_variant_to_checkout(checkout_info, variant, 1)
manager = get_plugins_manager(allow_replica=False)
+ lines, _ = fetch_checkout_lines(checkout)
taxed_total = calculations.checkout_total(
manager=manager,
checkout_info=checkout_info,
@@ -290,5 +175,4 @@ def checkout_preorder_with_charged_payment(
gateway_response={},
is_success=True,
)
-
return checkout
diff --git a/saleor/checkout/tests/fixtures/checkout_with_prices.py b/saleor/checkout/tests/fixtures/checkout_with_prices.py
new file mode 100644
index 0000000000..4d7629fb1e
--- /dev/null
+++ b/saleor/checkout/tests/fixtures/checkout_with_prices.py
@@ -0,0 +1,154 @@
+import datetime
+from decimal import Decimal
+
+import pytest
+from django.utils import timezone
+
+from ....plugins.manager import get_plugins_manager
+from ... import base_calculations
+from ...fetch import fetch_checkout_info, fetch_checkout_lines
+from ...models import CheckoutLine
+
+
+def checkout_with_prices(
+ checkout_with_items,
+ address,
+ address_other_country,
+ warehouse,
+ customer_user,
+ shipping_method,
+ voucher,
+):
+ # Need to save shipping_method before fetching checkout info.
+ checkout_with_items.shipping_method = shipping_method
+ checkout_with_items.save(update_fields=["shipping_method"])
+
+ manager = get_plugins_manager(allow_replica=False)
+ lines = checkout_with_items.lines.all()
+ lines_info, _ = fetch_checkout_lines(checkout_with_items)
+ checkout_info = fetch_checkout_info(checkout_with_items, lines_info, manager)
+
+ for line, line_info in zip(lines, lines_info):
+ line.total_price_net_amount = base_calculations.calculate_base_line_total_price(
+ line_info
+ ).amount
+ line.total_price_gross_amount = line.total_price_net_amount * Decimal("1.230")
+
+ checkout_with_items.discount_amount = Decimal("5.000")
+ checkout_with_items.discount_name = "Voucher 5 USD"
+ checkout_with_items.user = customer_user
+ checkout_with_items.billing_address = address
+ checkout_with_items.shipping_address = address_other_country
+ checkout_with_items.collection_point = warehouse
+ checkout_with_items.subtotal_net_amount = Decimal("100.000")
+ checkout_with_items.subtotal_gross_amount = Decimal("123.000")
+ checkout_with_items.total_net_amount = Decimal("150.000")
+ checkout_with_items.total_gross_amount = Decimal("178.000")
+ shipping_amount = base_calculations.base_checkout_delivery_price(
+ checkout_info, lines_info
+ ).amount
+ checkout_with_items.shipping_price_net_amount = shipping_amount
+ checkout_with_items.shipping_price_gross_amount = shipping_amount * Decimal("1.08")
+ checkout_with_items.metadata_storage.metadata = {"meta_key": "meta_value"}
+ checkout_with_items.metadata_storage.private_metadata = {
+ "priv_meta_key": "priv_meta_value"
+ }
+
+ checkout_with_items.lines.bulk_update(
+ lines,
+ [
+ "total_price_net_amount",
+ "total_price_gross_amount",
+ ],
+ )
+
+ checkout_with_items.save(
+ update_fields=[
+ "discount_amount",
+ "discount_name",
+ "user",
+ "billing_address",
+ "shipping_address",
+ "collection_point",
+ "subtotal_net_amount",
+ "subtotal_gross_amount",
+ "total_net_amount",
+ "total_gross_amount",
+ "shipping_price_net_amount",
+ "shipping_price_gross_amount",
+ ]
+ )
+ checkout_with_items.metadata_storage.save(
+ update_fields=["metadata", "private_metadata"]
+ )
+
+ user = checkout_with_items.user
+ user.metadata = {"user_public_meta_key": "user_public_meta_value"}
+ user.save(update_fields=["metadata"])
+
+ return checkout_with_items
+
+
+def priced_checkout_factory():
+ def factory(checkout):
+ manager = get_plugins_manager(allow_replica=False)
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, manager)
+
+ tax = Decimal("1.23")
+
+ lines_to_update = []
+ for line_info in lines:
+ line = line_info.line
+ lines_to_update.append(line)
+
+ line.total_price = manager.calculate_checkout_line_total(
+ checkout_info, lines, line_info, None
+ )
+ line.total_price_gross_amount *= tax
+
+ CheckoutLine.objects.bulk_update(
+ lines_to_update,
+ [
+ "total_price_net_amount",
+ "total_price_gross_amount",
+ ],
+ )
+
+ checkout.shipping_price = manager.calculate_checkout_shipping(
+ checkout_info, lines, None
+ )
+ checkout.subtotal = manager.calculate_checkout_subtotal(
+ checkout_info, lines, None
+ )
+ checkout.total = manager.calculate_checkout_total(checkout_info, lines, None)
+
+ checkout.shipping_price_gross_amount *= tax
+ checkout.subtotal_gross_amount *= tax
+ checkout.total_gross_amount *= tax
+ # Cache prices until invalidated by force
+ checkout.price_expiration = timezone.now() + datetime.timedelta(days=1)
+ checkout.save()
+
+ return checkout
+
+ return factory
+
+
+def priced_checkout_with_item(priced_checkout_factory, checkout_with_item):
+ return priced_checkout_factory(checkout_with_item)
+
+
+def priced_checkout_with_items(priced_checkout_factory, checkout_with_items):
+ return priced_checkout_factory(checkout_with_items)
+
+
+def priced_checkout_with_voucher_percentage(
+ priced_checkout_factory, checkout_with_voucher_percentage
+):
+ return priced_checkout_factory(checkout_with_voucher_percentage)
diff --git a/saleor/checkout/tests/fixtures/checkout_with_transaction.py b/saleor/checkout/tests/fixtures/checkout_with_transaction.py
new file mode 100644
index 0000000000..e849518fdf
--- /dev/null
+++ b/saleor/checkout/tests/fixtures/checkout_with_transaction.py
@@ -0,0 +1,18 @@
+from decimal import Decimal
+
+import pytest
+
+from ....payment.models import TransactionItem
+
+
+def checkout_with_item_and_transaction_item(checkout_with_item):
+ TransactionItem.objects.create(
+ name="Credit card",
+ psp_reference="PSP ref",
+ available_actions=["refund"],
+ currency="USD",
+ checkout_id=checkout_with_item.pk,
+ charged_value=Decimal("10"),
+ )
+ return checkout_with_item
diff --git a/saleor/checkout/tests/test_calculations.py b/saleor/checkout/tests/test_calculations.py
index beb24085f3..92b06622d2 100644
--- a/saleor/checkout/tests/test_calculations.py
+++ b/saleor/checkout/tests/test_calculations.py
@@ -38,11 +38,6 @@ from ..calculations import (
from ..fetch import CheckoutLineInfo, fetch_checkout_info, fetch_checkout_lines
-def checkout_lines(checkout_with_items):
- return checkout_with_items.lines.all()
-
-
@pytest.fixture
def tax_data(checkout_with_items, checkout_lines):
checkout = checkout_with_items
diff --git a/saleor/discount/tests/test_utils/fixtures.py b/saleor/discount/tests/test_utils/fixtures.py
deleted file mode 100644
index b3244fb7b1..0000000000
--- a/saleor/discount/tests/test_utils/fixtures.py
+++ /dev/null
@@ -1,139 +0,0 @@
-from decimal import Decimal
-
-import graphene
-import pytest
-from prices import TaxedMoney
-
-from ....checkout.fetch import fetch_checkout_info, fetch_checkout_lines
-from ....core.taxes import zero_money
-from ....discount import RewardType, RewardValueType
-from ....order import OrderStatus
-from ....plugins.manager import get_plugins_manager
-from ....product.models import VariantChannelListingPromotionRule
-from ....warehouse.models import Stock
-
-
-def checkout_lines_info(checkout_with_items, categories, published_collections):
- lines = checkout_with_items.lines.all()
- category1, category2 = categories
-
- product1 = lines[0].variant.product
- product1.category = category1
- product1.collections.add(*published_collections[:2])
- product1.save()
-
- product2 = lines[1].variant.product
- product2.category = category2
- product2.collections.add(published_collections[0])
- product2.save()
-
- lines_info, _ = fetch_checkout_lines(checkout_with_items)
- return lines_info
-
-
-def checkout_info(checkout_lines_info):
- manager = get_plugins_manager(allow_replica=False)
- checkout = checkout_lines_info[0].line.checkout
- checkout_info = fetch_checkout_info(checkout, checkout_lines_info, manager)
- return checkout_info
-
-
-def checkout_lines_with_multiple_quantity_info(
- checkout_with_items, categories, published_collections
-):
- checkout_with_items.lines.update(quantity=5)
- lines = checkout_with_items.lines.all()
- category1, category2 = categories
-
- product1 = lines[0].variant.product
- product1.category = category1
- product1.collections.add(*published_collections[:2])
- product1.save()
-
- product2 = lines[1].variant.product
- product2.category = category2
- product2.collections.add(published_collections[0])
- product2.save()
-
- lines_info, _ = fetch_checkout_lines(checkout_with_items)
- return lines_info
-
-
-def draft_order_and_promotions(
- order_with_lines,
- order_promotion_without_rules,
- catalogue_promotion_without_rules,
- channel_USD,
-):
- # given
- order = order_with_lines
- line_1 = order.lines.get(quantity=3)
- line_2 = order.lines.get(quantity=2)
-
- # prepare catalogue promotions
- catalogue_promotion = catalogue_promotion_without_rules
- variant_1 = line_1.variant
- variant_2 = line_2.variant
- rule_catalogue = catalogue_promotion.rules.create(
- name="Catalogue rule fixed",
- catalogue_predicate={
- "variantPredicate": {
- "ids": [graphene.Node.to_global_id("ProductVariant", variant_2.id)]
- }
- },
- reward_value_type=RewardValueType.FIXED,
- reward_value=Decimal(3),
- )
- rule_catalogue.channels.add(channel_USD)
-
- listing = variant_2.channel_listings.first()
- listing.discounted_price_amount = Decimal(17)
- listing.save(update_fields=["discounted_price_amount"])
-
- currency = order.currency
- VariantChannelListingPromotionRule.objects.create(
- variant_channel_listing=listing,
- promotion_rule=rule_catalogue,
- discount_amount=Decimal(3),
- currency=currency,
- )
-
- # prepare order promotion - subtotal
- order_promotion = order_promotion_without_rules
- rule_total = order_promotion.rules.create(
- name="Fixed subtotal rule",
- order_predicate={
- "discountedObjectPredicate": {"baseSubtotalPrice": {"range": {"gte": 10}}}
- },
- reward_value_type=RewardValueType.FIXED,
- reward_value=Decimal(25),
- reward_type=RewardType.SUBTOTAL_DISCOUNT,
- )
- rule_total.channels.add(channel_USD)
-
- # prepare order promotion - gift
- rule_gift = order_promotion.rules.create(
- name="Gift subtotal rule",
- order_predicate={
- "discountedObjectPredicate": {"baseSubtotalPrice": {"range": {"gte": 10}}}
- },
- reward_type=RewardType.GIFT,
- )
- rule_gift.channels.add(channel_USD)
- rule_gift.gifts.set([variant_1, variant_2])
- Stock.objects.update(quantity=100)
-
- # reset prices
- order.total = TaxedMoney(net=zero_money(currency), gross=zero_money(currency))
- order.subtotal = TaxedMoney(net=zero_money(currency), gross=zero_money(currency))
- order.undiscounted_total = TaxedMoney(
- net=zero_money(currency), gross=zero_money(currency)
- )
- order.status = OrderStatus.DRAFT
- order.save()
-
- return order, rule_catalogue, rule_total, rule_gift
diff --git a/saleor/graphql/checkout/tests/test_checkout_filters.py b/saleor/graphql/checkout/tests/test_checkout_filters.py
index b4bcc9aeba..4f104c5099 100644
--- a/saleor/graphql/checkout/tests/test_checkout_filters.py
+++ b/saleor/graphql/checkout/tests/test_checkout_filters.py
@@ -17,26 +17,21 @@ from ...tests.utils import get_graphql_content
from ..enums import CheckoutAuthorizeStatusEnum, CheckoutChargeStatusEnum
from ..filters import CheckoutDiscountedObjectWhere
-
-def checkout_query_with_filter():
- query = """
- query ($filter: CheckoutFilterInput!, ) {
- checkouts(first: 5, filter:$filter) {
- totalCount
- edges {
- node {
- id
- }
- }
+CHECKOUT_QUERY_WITH_FILTER = """
+ query ($filter: CheckoutFilterInput!, ) {
+ checkouts(first: 5, filter:$filter) {
+ totalCount
+ edges {
+ node {
+ id
}
}
- """
- return query
+ }
+ }
+"""
def test_checkout_query_with_filter_channels_with_one_channel(
- checkout_query_with_filter,
staff_api_client,
permission_manage_checkouts,
checkouts_list,
@@ -48,7 +43,7 @@ def test_checkout_query_with_filter_channels_with_one_channel(
# when
response = staff_api_client.post_graphql(
- checkout_query_with_filter,
+ CHECKOUT_QUERY_WITH_FILTER,
variables,
permissions=(permission_manage_checkouts,),
)
@@ -60,7 +55,6 @@ def test_checkout_query_with_filter_channels_with_one_channel(
def test_checkout_query_with_filter_channels_without_channel(
- checkout_query_with_filter,
staff_api_client,
permission_manage_checkouts,
checkouts_list,
@@ -70,7 +64,7 @@ def test_checkout_query_with_filter_channels_without_channel(
# when
response = staff_api_client.post_graphql(
- checkout_query_with_filter,
+ CHECKOUT_QUERY_WITH_FILTER,
variables,
permissions=(permission_manage_checkouts,),
)
@@ -82,7 +76,6 @@ def test_checkout_query_with_filter_channels_without_channel(
def test_checkout_query_with_filter_channels_with_many_channel(
- checkout_query_with_filter,
staff_api_client,
permission_manage_checkouts,
checkouts_list,
@@ -98,7 +91,7 @@ def test_checkout_query_with_filter_channels_with_many_channel(
# when
response = staff_api_client.post_graphql(
- checkout_query_with_filter,
+ CHECKOUT_QUERY_WITH_FILTER,
variables,
permissions=(permission_manage_checkouts,),
)
@@ -110,7 +103,6 @@ def test_checkout_query_with_filter_channels_with_many_channel(
def test_checkout_query_with_filter_channels_with_empty_channel(
- checkout_query_with_filter,
staff_api_client,
permission_manage_checkouts,
checkouts_list,
@@ -122,7 +114,7 @@ def test_checkout_query_with_filter_channels_with_empty_channel(
# when
response = staff_api_client.post_graphql(
- checkout_query_with_filter,
+ CHECKOUT_QUERY_WITH_FILTER,
variables,
permissions=(permission_manage_checkouts,),
)
@@ -187,17 +179,21 @@ def test_checkout_query_with_filter_channels_with_empty_channel(
def test_checkout_query_with_filter_created(
checkouts_filter,
count,
- checkout_query_with_filter,
staff_api_client,
permission_manage_checkouts,
channel_USD,
):
+ # given
Checkout.objects.create(channel=channel_USD)
with freeze_time("2012-01-14"):
Checkout.objects.create(channel=channel_USD)
variables = {"filter": checkouts_filter}
staff_api_client.user.user_permissions.add(permission_manage_checkouts)
- response = staff_api_client.post_graphql(checkout_query_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(CHECKOUT_QUERY_WITH_FILTER, variables)
+
+ # then
content = get_graphql_content(response)
checkouts_list = content["data"]["checkouts"]["edges"]
@@ -216,12 +212,12 @@ def test_checkouts_query_with_filter_customer_fields(
checkouts_filter,
user_field,
user_value,
- checkout_query_with_filter,
staff_api_client,
permission_manage_checkouts,
customer_user,
channel_USD,
):
+ # given
setattr(customer_user, user_field, user_value)
customer_user.save()
customer_user.refresh_from_db()
@@ -235,7 +231,11 @@ def test_checkouts_query_with_filter_customer_fields(
variables = {"filter": checkouts_filter}
staff_api_client.user.user_permissions.add(permission_manage_checkouts)
- response = staff_api_client.post_graphql(checkout_query_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(CHECKOUT_QUERY_WITH_FILTER, variables)
+
+ # then
content = get_graphql_content(response)
checkout_list = content["data"]["checkouts"]["edges"]
checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
@@ -276,6 +276,7 @@ def test_query_checkout_with_sort(
address,
channel_USD,
):
+ # given
created_checkouts = []
with freeze_time("2017-01-14"):
created_checkouts.append(
@@ -317,7 +318,11 @@ def test_query_checkout_with_sort(
variables = {"sort_by": checkout_sort}
staff_api_client.user.user_permissions.add(permission_manage_checkouts)
+
+ # when
response = staff_api_client.post_graphql(QUERY_CHECKOUT_WITH_SORT, variables)
+
+ # then
content = get_graphql_content(response)
checkouts = content["data"]["checkouts"]["edges"]
@@ -341,12 +346,12 @@ def test_query_checkout_with_sort(
def test_checkouts_query_with_filter_search(
checkouts_filter,
count,
- checkout_query_with_filter,
staff_api_client,
permission_manage_checkouts,
customer_user,
channel_USD,
):
+ # given
user1 = User.objects.create(email="[email protected]")
user2 = User.objects.create(email="[email protected]")
user3 = User.objects.create(email="[email protected]")
@@ -383,19 +388,23 @@ def test_checkouts_query_with_filter_search(
payment.transactions.create(gateway_response={}, is_success=True)
variables = {"filter": checkouts_filter}
staff_api_client.user.user_permissions.add(permission_manage_checkouts)
- response = staff_api_client.post_graphql(checkout_query_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(CHECKOUT_QUERY_WITH_FILTER, variables)
+
+ # then
content = get_graphql_content(response)
assert content["data"]["checkouts"]["totalCount"] == count
def test_checkouts_query_with_filter_search_by_global_payment_id(
- checkout_query_with_filter,
staff_api_client,
permission_manage_checkouts,
customer_user,
channel_USD,
):
+ # given
checkouts = Checkout.objects.bulk_create(
[
Checkout(
@@ -417,17 +426,26 @@ def test_checkouts_query_with_filter_search_by_global_payment_id(
variables = {"filter": {"search": global_id}}
staff_api_client.user.user_permissions.add(permission_manage_checkouts)
- response = staff_api_client.post_graphql(checkout_query_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(CHECKOUT_QUERY_WITH_FILTER, variables)
+
+ # then
content = get_graphql_content(response)
assert content["data"]["checkouts"]["totalCount"] == 1
def test_checkouts_query_with_filter_search_by_token(
- checkout_query_with_filter, checkout, staff_api_client, permission_manage_checkouts
+ checkout, staff_api_client, permission_manage_checkouts
):
+ # given
variables = {"filter": {"search": checkout.pk}}
staff_api_client.user.user_permissions.add(permission_manage_checkouts)
- response = staff_api_client.post_graphql(checkout_query_with_filter, variables)
+
+ # when
+ response = staff_api_client.post_graphql(CHECKOUT_QUERY_WITH_FILTER, variables)
+
+ # then
content = get_graphql_content(response)
assert content["data"]["checkouts"]["totalCount"] == 1
@@ -507,7 +525,6 @@ def test_checkouts_query_with_filter_authorize_status(
transaction_data,
statuses,
expected_count,
- checkout_query_with_filter,
staff_api_client,
permission_manage_checkouts,
customer_user,
@@ -538,7 +555,7 @@ def test_checkouts_query_with_filter_authorize_status(
staff_api_client.user.user_permissions.add(permission_manage_checkouts)
# when
- response = staff_api_client.post_graphql(checkout_query_with_filter, variables)
+ response = staff_api_client.post_graphql(CHECKOUT_QUERY_WITH_FILTER, variables)
# then
content = get_graphql_content(response)
@@ -610,7 +627,6 @@ def test_checkouts_query_with_filter_charge_status(
transaction_data,
statuses,
expected_count,
- checkout_query_with_filter,
checkout_with_prices,
staff_api_client,
permission_manage_checkouts,
@@ -640,7 +656,7 @@ def test_checkouts_query_with_filter_charge_status(
staff_api_client.user.user_permissions.add(permission_manage_checkouts)
# when
- response = staff_api_client.post_graphql(checkout_query_with_filter, variables)
+ response = staff_api_client.post_graphql(CHECKOUT_QUERY_WITH_FILTER, variables)
# then
content = get_graphql_content(response)
diff --git a/saleor/order/tests/fixtures/draft_order.py b/saleor/order/tests/fixtures/draft_order.py
index 06974449a0..6f3a3c3ef4 100644
--- a/saleor/order/tests/fixtures/draft_order.py
+++ b/saleor/order/tests/fixtures/draft_order.py
@@ -1,16 +1,19 @@
from decimal import Decimal
from functools import partial
+import graphene
import pytest
from django.conf import settings
from prices import Money, TaxedMoney, fixed_discount
-from ....discount import DiscountType, DiscountValueType
+from ....core.taxes import zero_money
+from ....discount import DiscountType, DiscountValueType, RewardType, RewardValueType
from ....discount.models import VoucherCode
from ....discount.utils.voucher import (
create_or_update_discount_object_from_order_level_voucher,
)
-from ....warehouse.models import Allocation, PreorderAllocation
+from ....product.models import VariantChannelListingPromotionRule
+from ....warehouse.models import Allocation, PreorderAllocation, Stock
from ... import OrderOrigin, OrderStatus
from ...base_calculations import base_order_subtotal
from ...models import Order
@@ -158,3 +161,80 @@ def draft_orders_in_different_channels(
Order.objects.bulk_update(draft_order_list, ["channel"])
return draft_order_list
+
+
+def draft_order_and_promotions(
+ order_with_lines,
+ order_promotion_without_rules,
+ catalogue_promotion_without_rules,
+ channel_USD,
+):
+ # given
+ order = order_with_lines
+ line_1 = order.lines.get(quantity=3)
+ line_2 = order.lines.get(quantity=2)
+
+ # prepare catalogue promotions
+ catalogue_promotion = catalogue_promotion_without_rules
+ variant_1 = line_1.variant
+ variant_2 = line_2.variant
+ rule_catalogue = catalogue_promotion.rules.create(
+ name="Catalogue rule fixed",
+ catalogue_predicate={
+ "variantPredicate": {
+ "ids": [graphene.Node.to_global_id("ProductVariant", variant_2.id)]
+ }
+ },
+ reward_value_type=RewardValueType.FIXED,
+ reward_value=Decimal(3),
+ )
+ rule_catalogue.channels.add(channel_USD)
+
+ listing = variant_2.channel_listings.first()
+ listing.discounted_price_amount = Decimal(17)
+ listing.save(update_fields=["discounted_price_amount"])
+
+ currency = order.currency
+ VariantChannelListingPromotionRule.objects.create(
+ variant_channel_listing=listing,
+ promotion_rule=rule_catalogue,
+ discount_amount=Decimal(3),
+ currency=currency,
+ )
+
+ # prepare order promotion - subtotal
+ order_promotion = order_promotion_without_rules
+ rule_total = order_promotion.rules.create(
+ name="Fixed subtotal rule",
+ order_predicate={
+ "discountedObjectPredicate": {"baseSubtotalPrice": {"range": {"gte": 10}}}
+ },
+ reward_value_type=RewardValueType.FIXED,
+ reward_value=Decimal(25),
+ reward_type=RewardType.SUBTOTAL_DISCOUNT,
+ )
+ rule_total.channels.add(channel_USD)
+
+ # prepare order promotion - gift
+ rule_gift = order_promotion.rules.create(
+ name="Gift subtotal rule",
+ order_predicate={
+ "discountedObjectPredicate": {"baseSubtotalPrice": {"range": {"gte": 10}}}
+ },
+ reward_type=RewardType.GIFT,
+ )
+ rule_gift.channels.add(channel_USD)
+ rule_gift.gifts.set([variant_1, variant_2])
+ Stock.objects.update(quantity=100)
+
+ # reset prices
+ order.total = TaxedMoney(net=zero_money(currency), gross=zero_money(currency))
+ order.subtotal = TaxedMoney(net=zero_money(currency), gross=zero_money(currency))
+ order.undiscounted_total = TaxedMoney(
+ net=zero_money(currency), gross=zero_money(currency)
+ )
+ order.status = OrderStatus.DRAFT
+ order.save()
+
+ return order, rule_catalogue, rule_total, rule_gift
diff --git a/saleor/plugins/avatax/tests/conftest.py b/saleor/plugins/avatax/tests/conftest.py
index 594d03319e..bd7e9198f9 100644
--- a/saleor/plugins/avatax/tests/conftest.py
+++ b/saleor/plugins/avatax/tests/conftest.py
@@ -3,9 +3,6 @@ import os
import pytest
from ....account.models import Address
-from ....checkout.fetch import CheckoutInfo, fetch_checkout_lines
-from ....shipping.models import ShippingMethodChannelListing
-from ...manager import get_plugins_manager
from ...models import PluginConfiguration
from .. import AvataxConfiguration
from ..plugin import AvataxPlugin
@@ -90,43 +87,6 @@ def ship_to_pl_address(db):
)
-def checkout_with_items_and_shipping(checkout_with_items, address, shipping_method):
- checkout_with_items.shipping_address = address
- checkout_with_items.shipping_method = shipping_method
- checkout_with_items.billing_address = address
- checkout_with_items.save()
- return checkout_with_items
-
-
-def checkout_with_items_and_shipping_info(checkout_with_items_and_shipping):
- checkout = checkout_with_items_and_shipping
- channel = checkout.channel
- shipping_address = checkout.shipping_address
- shipping_method = checkout.shipping_method
- shipping_channel_listing = ShippingMethodChannelListing.objects.get(
- channel=channel,
- shipping_method=shipping_method,
- )
- manager = get_plugins_manager(allow_replica=False)
- lines, _ = fetch_checkout_lines(checkout)
- checkout_info = CheckoutInfo(
- checkout=checkout,
- user=checkout.user,
- channel=channel,
- billing_address=checkout.billing_address,
- shipping_address=shipping_address,
- shipping_method=shipping_method,
- shipping_channel_listings=[shipping_channel_listing],
- tax_configuration=channel.tax_configuration,
- discounts=[],
- manager=manager,
- lines=lines,
- )
- return checkout_info
-
-
@pytest.fixture
def avalara_response_for_checkout_with_items_and_shipping():
return {
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index 49e1cd78fc..d32245aa4f 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -14,10 +14,8 @@ from django.core.files.uploadedfile import SimpleUploadedFile
from django.db import connection
from django.template.defaultfilters import truncatechars
from django.test.utils import CaptureQueriesContext as BaseCaptureQueriesContext
-from django.utils import timezone
from freezegun import freeze_time
from PIL import Image
-from prices import Money
from ..account.models import Address, Group, StaffNotificationRecipient, User
from ..attribute import AttributeEntityType, AttributeInputType, AttributeType
@@ -28,13 +26,6 @@ from ..attribute.models import (
AttributeValueTranslation,
)
from ..attribute.utils import associate_attribute_values_to_instance
-from ..checkout import base_calculations
-from ..checkout.fetch import fetch_checkout_info, fetch_checkout_lines
-from ..checkout.models import Checkout, CheckoutLine, CheckoutMetadata
-from ..checkout.utils import (
- add_variant_to_checkout,
- add_voucher_to_checkout,
-)
from ..core import JobStatus
from ..core.models import EventDelivery, EventDeliveryAttempt, EventPayload
from ..core.payments import PaymentInterface
@@ -43,16 +34,9 @@ from ..core.utils.editorjs import clean_editor_js
from ..csv.events import ExportEvents
from ..csv.models import ExportEvent, ExportFile
from ..discount import (
- DiscountType,
- DiscountValueType,
PromotionEvents,
- RewardType,
- RewardValueType,
)
from ..discount.models import (
- CheckoutDiscount,
- CheckoutLineDiscount,
- Promotion,
PromotionEvent,
PromotionRuleTranslation,
PromotionTranslation,
@@ -64,19 +48,13 @@ from ..payment.models import Payment, TransactionEvent, TransactionItem
from ..payment.transaction_item_calculations import recalculate_transaction_amounts
from ..payment.utils import create_manual_adjustment_events
from ..permission.enums import get_permissions
-from ..plugins.manager import get_plugins_manager
from ..product.models import (
CategoryTranslation,
CollectionTranslation,
ProductTranslation,
- ProductVariantChannelListing,
ProductVariantTranslation,
)
from ..tax import TaxCalculationStrategy
-from ..warehouse.models import (
- PreorderReservation,
- Reservation,
-)
from ..webhook.event_types import WebhookEventAsyncType
from ..webhook.transport.utils import to_payment_app_id
from .utils import dummy_editorjs
@@ -164,707 +142,6 @@ def assert_max_num_queries(capture_queries):
return partial(capture_queries, exact=False)
-def checkout(db, channel_USD, settings):
- checkout = Checkout.objects.create(
- currency=channel_USD.currency_code,
- channel=channel_USD,
- price_expiration=timezone.now() + settings.CHECKOUT_PRICES_TTL,
- )
- checkout.set_country("US", commit=True)
- CheckoutMetadata.objects.create(checkout=checkout)
- return checkout
-
-
-def checkout_JPY(channel_JPY):
- checkout = Checkout.objects.create(
- currency=channel_JPY.currency_code, channel=channel_JPY
- )
- checkout.set_country("JP", commit=True)
- return checkout
-
-
-def checkout_with_item(checkout, product):
- variant = product.variants.first()
- checkout_info = fetch_checkout_info(
- checkout, [], get_plugins_manager(allow_replica=False)
- )
- add_variant_to_checkout(checkout_info, variant, 3)
- checkout.save()
- return checkout
-
-
-def checkout_with_item_on_sale(checkout_with_item, promotion_converted_from_sale):
- line = checkout_with_item.lines.first()
- channel = checkout_with_item.channel
- discount_amount = Decimal("5.0")
- variant = line.variant
- variant_id = graphene.Node.to_global_id("ProductVariant", variant.id)
- predicate = {"variantPredicate": {"ids": [variant_id]}}
- rule = promotion_converted_from_sale.rules.first()
- rule.catalogue_predicate = predicate
- rule.reward_value = discount_amount
- rule.save(update_fields=["catalogue_predicate", "reward_value"])
- rule.channels.add(channel)
- channel_listing = variant.channel_listings.get(channel=channel)
- channel_listing.discounted_price_amount = (
- channel_listing.price_amount - discount_amount
- )
- channel_listing.save(update_fields=["discounted_price_amount"])
-
- CheckoutLineDiscount.objects.create(
- line=line,
- promotion_rule=rule,
- type=DiscountType.SALE,
- value_type=rule.reward_value_type,
- value=discount_amount,
- amount_value=discount_amount * line.quantity,
- currency=channel.currency_code,
- )
-
- return checkout_with_item
-
-
-def checkout_with_item_on_promotion(checkout_with_item):
- line = checkout_with_item.lines.first()
- channel = checkout_with_item.channel
- promotion = Promotion.objects.create(name="Checkout promotion")
-
- variant = line.variant
-
- reward_value = Decimal("5")
- rule = promotion.rules.create(
- catalogue_predicate={
- "productPredicate": {
- "ids": [graphene.Node.to_global_id("Product", variant.product.id)]
- }
- },
- reward_value_type=RewardValueType.FIXED,
- reward_value=reward_value,
- )
- rule.channels.add(channel)
-
- variant_channel_listing = variant.channel_listings.get(channel=channel)
-
- variant_channel_listing.discounted_price_amount = (
- variant_channel_listing.price_amount - reward_value
- )
- variant_channel_listing.save(update_fields=["discounted_price_amount"])
-
- variant_channel_listing.variantlistingpromotionrule.create(
- promotion_rule=rule,
- discount_amount=reward_value,
- currency=channel.currency_code,
- )
- CheckoutLineDiscount.objects.create(
- line=line,
- type=DiscountType.PROMOTION,
- value_type=DiscountValueType.FIXED,
- value=reward_value,
- amount_value=reward_value * line.quantity,
- currency=channel.currency_code,
- promotion_rule=rule,
- )
-
- return checkout_with_item
-
-
-def checkout_with_item_and_order_discount(
- checkout_with_item, catalogue_promotion_without_rules
-):
- channel = checkout_with_item.channel
-
- reward_value = Decimal("5")
-
- rule = catalogue_promotion_without_rules.rules.create(
- order_predicate={
- "discountedObjectPredicate": {"baseSubtotalPrice": {"range": {"gte": 20}}}
- },
- reward_value_type=RewardValueType.FIXED,
- reward_value=reward_value,
- reward_type=RewardType.SUBTOTAL_DISCOUNT,
- )
- rule.channels.add(channel)
-
- CheckoutDiscount.objects.create(
- checkout=checkout_with_item,
- promotion_rule=rule,
- type=DiscountType.ORDER_PROMOTION,
- value_type=rule.reward_value_type,
- value=rule.reward_value,
- amount_value=rule.reward_value,
- currency=channel.currency_code,
- )
- checkout_with_item.discount_amount = reward_value
- checkout_with_item.save(update_fields=["discount_amount"])
-
- return checkout_with_item
-
-
-def checkout_with_item_and_gift_promotion(checkout_with_item, gift_promotion_rule):
- channel = checkout_with_item.channel
- variants = gift_promotion_rule.gifts.all()
- variant_listings = ProductVariantChannelListing.objects.filter(variant__in=variants)
- top_price, variant_id = max(
- variant_listings.values_list("discounted_price_amount", "variant")
- )
-
- line = CheckoutLine.objects.create(
- checkout=checkout_with_item,
- quantity=1,
- variant_id=variant_id,
- is_gift=True,
- currency="USD",
- )
-
- CheckoutLineDiscount.objects.create(
- line=line,
- promotion_rule=gift_promotion_rule,
- type=DiscountType.ORDER_PROMOTION,
- value_type=RewardValueType.FIXED,
- value=top_price,
- amount_value=top_price,
- currency=channel.currency_code,
- )
-
- return checkout_with_item
-
-
-def checkout_with_item_and_transaction_item(checkout_with_item):
- TransactionItem.objects.create(
- name="Credit card",
- psp_reference="PSP ref",
- available_actions=["refund"],
- currency="USD",
- checkout_id=checkout_with_item.pk,
- charged_value=Decimal("10"),
- )
- return checkout_with_item
-
-
-def checkout_with_item_and_tax_exemption(checkout_with_item):
- checkout_with_item.tax_exemption = True
- checkout_with_item.save(update_fields=["tax_exemption"])
- return checkout_with_item
-
-
-def checkout_with_same_items_in_multiple_lines(checkout, product):
- variant = product.variants.first()
- checkout_info = fetch_checkout_info(
- checkout, [], get_plugins_manager(allow_replica=False)
- )
- add_variant_to_checkout(checkout_info, variant, 1)
- add_variant_to_checkout(checkout_info, variant, 1, force_new_line=True)
- checkout.save()
- return checkout
-
-
-def checkout_with_item_and_voucher_specific_products(
- checkout_with_item, voucher_specific_product_type
-):
- manager = get_plugins_manager(allow_replica=False)
- lines, _ = fetch_checkout_lines(checkout_with_item)
- checkout_info = fetch_checkout_info(checkout_with_item, lines, manager)
- add_voucher_to_checkout(
- manager,
- checkout_info,
- lines,
- voucher_specific_product_type,
- voucher_specific_product_type.codes.first(),
- )
- checkout_with_item.refresh_from_db()
- return checkout_with_item
-
-
-def checkout_with_item_and_voucher_once_per_order(checkout_with_item, voucher):
- voucher.apply_once_per_order = True
- voucher.save()
- manager = get_plugins_manager(allow_replica=False)
- lines, _ = fetch_checkout_lines(checkout_with_item)
- checkout_info = fetch_checkout_info(checkout_with_item, lines, manager)
- add_voucher_to_checkout(
- manager, checkout_info, lines, voucher, voucher.codes.first()
- )
- checkout_with_item.refresh_from_db()
- return checkout_with_item
-
-
-def checkout_with_item_and_voucher(checkout_with_item, voucher):
- manager = get_plugins_manager(allow_replica=False)
- lines, _ = fetch_checkout_lines(checkout_with_item)
- checkout_info = fetch_checkout_info(checkout_with_item, lines, manager)
- add_voucher_to_checkout(
- manager, checkout_info, lines, voucher, voucher.codes.first()
- )
- checkout_with_item.refresh_from_db()
- return checkout_with_item
-
-
-def checkout_line(checkout_with_item):
- return checkout_with_item.lines.first()
-
-
-def checkout_with_item_total_0(checkout, product_price_0):
- variant = product_price_0.variants.get()
- checkout_info = fetch_checkout_info(
- checkout, [], get_plugins_manager(allow_replica=False)
- )
- add_variant_to_checkout(checkout_info, variant, 1)
- checkout.save()
- return checkout
-
-
-def checkout_JPY_with_item(checkout_JPY, product_in_channel_JPY):
- variant = product_in_channel_JPY.variants.get()
- checkout_info = fetch_checkout_info(
- checkout_JPY, [], get_plugins_manager(allow_replica=False)
- )
- add_variant_to_checkout(checkout_info, variant, 3)
- checkout_JPY.save()
- return checkout_JPY
-
-
-def checkouts_list(channel_USD, channel_PLN):
- checkouts_usd = Checkout.objects.bulk_create(
- [
- Checkout(currency=channel_USD.currency_code, channel=channel_USD),
- Checkout(currency=channel_USD.currency_code, channel=channel_USD),
- Checkout(currency=channel_USD.currency_code, channel=channel_USD),
- ]
- )
- checkouts_pln = Checkout.objects.bulk_create(
- [
- Checkout(currency=channel_PLN.currency_code, channel=channel_PLN),
- Checkout(currency=channel_PLN.currency_code, channel=channel_PLN),
- ]
- )
- return [*checkouts_pln, *checkouts_usd]
-
-
-def checkouts_assigned_to_customer(channel_USD, channel_PLN, customer_user):
- return Checkout.objects.bulk_create(
- [
- Checkout(
- currency=channel_USD.currency_code,
- channel=channel_USD,
- user=customer_user,
- ),
- Checkout(
- currency=channel_PLN.currency_code,
- channel=channel_PLN,
- user=customer_user,
- ),
- ]
- )
-
-
-def checkout_ready_to_complete(checkout_with_item, address, shipping_method, gift_card):
- checkout = checkout_with_item
- checkout.shipping_address = address
- checkout.shipping_method = shipping_method
- checkout.billing_address = address
- checkout.metadata_storage.store_value_in_metadata(items={"accepted": "true"})
- checkout.metadata_storage.store_value_in_private_metadata(
- items={"accepted": "false"}
- )
- checkout_with_item.gift_cards.add(gift_card)
- checkout.save()
- checkout.metadata_storage.save()
- return checkout
-
-
-def checkout_with_digital_item(checkout, digital_content, address):
- """Create a checkout with a digital line."""
- variant = digital_content.product_variant
- checkout_info = fetch_checkout_info(
- checkout, [], get_plugins_manager(allow_replica=False)
- )
- add_variant_to_checkout(checkout_info, variant, 1)
- checkout.discount_amount = Decimal(0)
- checkout.billing_address = address
- checkout.email = "[email protected]"
- checkout.save()
- return checkout
-
-
-def checkout_with_shipping_required(checkout_with_item, product):
- checkout = checkout_with_item
- variant = product.variants.get()
- checkout_info = fetch_checkout_info(
- checkout, [], get_plugins_manager(allow_replica=False)
- )
- add_variant_to_checkout(checkout_info, variant, 3)
- checkout.save()
- return checkout
-
-
-def checkout_with_item_and_shipping_method(checkout_with_item, shipping_method):
- checkout = checkout_with_item
- checkout.shipping_method = shipping_method
- checkout.save()
- return checkout
-
-
-def checkout_with_item_and_voucher_and_shipping_method(
- checkout_with_item_and_voucher, shipping_method
-):
- checkout = checkout_with_item_and_voucher
- checkout.shipping_method = shipping_method
- checkout.save()
- return checkout
-
-
-def checkout_without_shipping_required(checkout, product_without_shipping):
- variant = product_without_shipping.variants.get()
- checkout_info = fetch_checkout_info(
- checkout, [], get_plugins_manager(allow_replica=False)
- )
- add_variant_to_checkout(checkout_info, variant, 1)
- checkout.save()
- return checkout
-
-
-def checkout_with_single_item(checkout, product):
- variant = product.variants.get()
- checkout_info = fetch_checkout_info(
- checkout, [], get_plugins_manager(allow_replica=False)
- )
- add_variant_to_checkout(checkout_info, variant, 1)
- checkout.save()
- return checkout
-
-
-def checkout_with_variant_without_inventory_tracking(
- checkout, variant_without_inventory_tracking, address, shipping_method
-):
- variant = variant_without_inventory_tracking
- checkout_info = fetch_checkout_info(
- checkout, [], get_plugins_manager(allow_replica=False)
- )
- add_variant_to_checkout(checkout_info, variant, 1)
- checkout.shipping_address = address
- checkout.shipping_method = shipping_method
- checkout.billing_address = address
- checkout.metadata_storage.store_value_in_metadata(items={"accepted": "true"})
- checkout.metadata_storage.store_value_in_private_metadata(
- items={"accepted": "false"}
- )
- checkout.save()
- checkout.metadata_storage.save()
- return checkout
-
-
-def checkout_with_variants(
- checkout,
- stock,
- product_with_default_variant,
- product_with_single_variant,
- product_with_two_variants,
-):
- checkout_info = fetch_checkout_info(
- checkout, [], get_plugins_manager(allow_replica=False)
- )
-
- add_variant_to_checkout(
- checkout_info, product_with_default_variant.variants.get(), 1
- )
- add_variant_to_checkout(
- checkout_info, product_with_single_variant.variants.get(), 10
- )
- add_variant_to_checkout(
- checkout_info, product_with_two_variants.variants.first(), 3
- )
- add_variant_to_checkout(checkout_info, product_with_two_variants.variants.last(), 5)
-
- checkout.save()
- return checkout
-
-
-def checkout_with_shipping_address(checkout_with_variants, address):
- checkout = checkout_with_variants
-
- checkout.shipping_address = address.get_copy()
- checkout.save()
-
- return checkout
-
-
-def checkout_with_variants_for_cc(
- checkout, stocks_for_cc, product_variant_list, product_with_two_variants
-):
- CheckoutLine.objects.bulk_create(
- [
- CheckoutLine(
- checkout=checkout,
- variant=product_variant_list[0],
- quantity=3,
- currency="USD",
- ),
- CheckoutLine(
- checkout=checkout,
- variant=product_variant_list[1],
- quantity=10,
- currency="USD",
- ),
- CheckoutLine(
- checkout=checkout,
- variant=product_with_two_variants.variants.last(),
- quantity=5,
- currency="USD",
- ),
- ]
- )
- return checkout
-
-
-def checkout_with_shipping_address_for_cc(checkout_with_variants_for_cc, address):
- checkout = checkout_with_variants_for_cc
-
- checkout.shipping_address = address.get_copy()
- checkout.save()
-
- return checkout
-
-
-def checkout_with_items(checkout, product_list, product):
- variant = product.variants.get()
- checkout_info = fetch_checkout_info(
- checkout, [], get_plugins_manager(allow_replica=False)
- )
- add_variant_to_checkout(checkout_info, variant, 1)
- for prod in product_list:
- variant = prod.variants.get()
- add_variant_to_checkout(checkout_info, variant, 1)
- checkout.save()
- checkout.refresh_from_db()
- return checkout
-
-
-def checkout_with_items_and_shipping(checkout_with_items, address, shipping_method):
- checkout_with_items.shipping_address = address
- checkout_with_items.shipping_method = shipping_method
- checkout_with_items.billing_address = address
- checkout_with_items.save()
- return checkout_with_items
-
-
-def checkout_with_item_and_shipping(checkout_with_item, address, shipping_method):
- checkout_with_item.shipping_address = address
- checkout_with_item.shipping_method = shipping_method
- checkout_with_item.billing_address = address
- checkout_with_item.save()
- return checkout_with_item
-
-
-def checkout_with_voucher(checkout, product, voucher):
- variant = product.variants.get()
- checkout_info = fetch_checkout_info(
- checkout, [], get_plugins_manager(allow_replica=False)
- )
- add_variant_to_checkout(checkout_info, variant, 3)
- checkout.voucher_code = voucher.code
- checkout.discount = Money("20.00", "USD")
- checkout.save()
- return checkout
-
-
-def checkout_with_voucher_percentage(checkout, product, voucher_percentage):
- variant = product.variants.get()
- checkout_info = fetch_checkout_info(
- checkout, [], get_plugins_manager(allow_replica=False)
- )
- add_variant_to_checkout(checkout_info, variant, 3)
- checkout.voucher_code = voucher_percentage.code
- checkout.discount = Money("3.00", "USD")
- checkout.save()
- return checkout
-
-
-def checkout_with_voucher_free_shipping(
- checkout_with_items_and_shipping, voucher_free_shipping
-):
- manager = get_plugins_manager(allow_replica=False)
- lines, _ = fetch_checkout_lines(checkout_with_items_and_shipping)
- checkout_info = fetch_checkout_info(
- checkout_with_items_and_shipping, lines, manager
- )
- add_voucher_to_checkout(
- manager,
- checkout_info,
- lines,
- voucher_free_shipping,
- voucher_free_shipping.codes.first(),
- )
- return checkout_with_items_and_shipping
-
-
-def checkout_with_gift_card(checkout_with_item, gift_card):
- checkout_with_item.gift_cards.add(gift_card)
- checkout_with_item.save()
- return checkout_with_item
-
-
-def checkout_with_preorders_only(
- checkout,
- stocks_for_cc,
- preorder_variant_with_end_date,
- preorder_variant_channel_threshold,
-):
- lines, _ = fetch_checkout_lines(checkout)
- checkout_info = fetch_checkout_info(
- checkout, lines, get_plugins_manager(allow_replica=False)
- )
- add_variant_to_checkout(checkout_info, preorder_variant_with_end_date, 2)
- add_variant_to_checkout(checkout_info, preorder_variant_channel_threshold, 2)
-
- checkout.save()
- return checkout
-
-
-def checkout_with_preorders_and_regular_variant(
- checkout, stocks_for_cc, preorder_variant_with_end_date, product_variant_list
-):
- lines, _ = fetch_checkout_lines(checkout)
- checkout_info = fetch_checkout_info(
- checkout, lines, get_plugins_manager(allow_replica=False)
- )
- add_variant_to_checkout(checkout_info, preorder_variant_with_end_date, 2)
- add_variant_to_checkout(checkout_info, product_variant_list[0], 2)
-
- checkout.save()
- return checkout
-
-
-def checkout_with_gift_card_items(
- checkout, non_shippable_gift_card_product, shippable_gift_card_product
-):
- checkout_info = fetch_checkout_info(
- checkout, [], get_plugins_manager(allow_replica=False)
- )
- non_shippable_variant = non_shippable_gift_card_product.variants.get()
- shippable_variant = shippable_gift_card_product.variants.get()
- add_variant_to_checkout(checkout_info, non_shippable_variant, 1)
- add_variant_to_checkout(checkout_info, shippable_variant, 2)
- checkout.save()
- return checkout
-
-
-def checkout_with_voucher_percentage_and_shipping(
- checkout_with_voucher_percentage, shipping_method, address
-):
- checkout = checkout_with_voucher_percentage
- checkout.shipping_method = shipping_method
- checkout.shipping_address = address
- checkout.save()
- return checkout
-
-
-def checkout_with_payments(checkout):
- Payment.objects.bulk_create(
- [
- Payment(
- gateway="mirumee.payments.dummy", is_active=True, checkout=checkout
- ),
- Payment(
- gateway="mirumee.payments.dummy", is_active=False, checkout=checkout
- ),
- ]
- )
- return checkout
-
-
-def checkout_with_item_and_preorder_item(
- checkout_with_item, product, preorder_variant_channel_threshold
-):
- checkout_info = fetch_checkout_info(
- checkout_with_item, [], get_plugins_manager(allow_replica=False)
- )
- add_variant_to_checkout(checkout_info, preorder_variant_channel_threshold, 1)
- return checkout_with_item
-
-
-def checkout_with_problems(
- checkout_with_items,
- product_type,
- address,
- shipping_method,
- category,
- default_tax_class,
- channel_USD,
- warehouse,
-):
- checkout_with_items.shipping_address = address
- checkout_with_items.billing_address = address
- checkout_with_items.shipping_method = shipping_method
- checkout_with_items.save(
- update_fields=["shipping_address", "shipping_method", "billing_address"]
- )
-
- first_line = checkout_with_items.lines.first()
- first_line.variant.track_inventory = True
- first_line.variant.save(update_fields=["track_inventory"])
-
- product_type = first_line.variant.product.product_type
- product_type.is_shipping_required = True
- product_type.save(update_fields=["is_shipping_required"])
-
- first_line.variant.stocks.all().delete()
-
- second_line = checkout_with_items.lines.last()
-
- available_at = datetime.datetime.now(tz=datetime.UTC) + datetime.timedelta(days=5)
- product = second_line.variant.product
- product.channel_listings.update(
- available_for_purchase_at=available_at, is_published=False
- )
-
- return checkout_with_items
-
-
@pytest.fixture
def address(db): # pylint: disable=W0613
return Address.objects.create(
@@ -998,81 +275,6 @@ def customer_users(address, customer_user, customer_user2):
return [customer_user, customer_user2, customer_user3]
-def user_checkout(customer_user, channel_USD):
- checkout = Checkout.objects.create(
- user=customer_user,
- channel=channel_USD,
- billing_address=customer_user.default_billing_address,
- shipping_address=customer_user.default_shipping_address,
- note="Test notes",
- currency="USD",
- )
- CheckoutMetadata.objects.create(checkout=checkout)
- return checkout
-
-
-def user_checkout_for_cc(customer_user, channel_USD, warehouse_for_cc):
- checkout = Checkout.objects.create(
- user=customer_user,
- email=customer_user.email,
- channel=channel_USD,
- billing_address=customer_user.default_billing_address,
- shipping_address=warehouse_for_cc.address,
- collection_point=warehouse_for_cc,
- note="Test notes",
- currency="USD",
- )
- return checkout
-
-
-def user_checkout_PLN(customer_user, channel_PLN):
- checkout = Checkout.objects.create(
- user=customer_user,
- channel=channel_PLN,
- billing_address=customer_user.default_billing_address,
- shipping_address=customer_user.default_shipping_address,
- note="Test notes",
- currency="PLN",
- )
- return checkout
-
-
-def user_checkout_with_items(user_checkout, product_list):
- checkout_info = fetch_checkout_info(
- user_checkout, [], get_plugins_manager(allow_replica=False)
- )
- for product in product_list:
- variant = product.variants.get()
- add_variant_to_checkout(checkout_info, variant, 1)
- user_checkout.refresh_from_db()
- return user_checkout
-
-
-def user_checkout_with_items_for_cc(user_checkout_for_cc, product_list):
- checkout_info = fetch_checkout_info(
- user_checkout_for_cc, [], get_plugins_manager(allow_replica=False)
- )
- for product in product_list:
- variant = product.variants.get()
- add_variant_to_checkout(checkout_info, variant, 1)
- user_checkout_for_cc.refresh_from_db()
- return user_checkout_for_cc
-
-
-def user_checkouts(request, user_checkout_with_items, user_checkout_with_items_for_cc):
- if request.param == "regular":
- return user_checkout_with_items
- if request.param == "click_and_collect":
- return user_checkout_with_items_for_cc
- raise ValueError("Internal test error")
-
-
@pytest.fixture
def admin_user(db):
"""Return a Django admin user."""
@@ -1858,95 +1060,6 @@ def image_list():
]
-def checkout_line_with_reservation_in_many_stocks(
- customer_user, variant_with_many_stocks, checkout
-):
- variant = variant_with_many_stocks
- stocks = variant.stocks.all().order_by("pk")
- checkout_line = checkout.lines.create(
- variant=variant,
- quantity=3,
- )
-
- reserved_until = timezone.now() + datetime.timedelta(minutes=5)
-
- Reservation.objects.bulk_create(
- [
- Reservation(
- checkout_line=checkout_line,
- stock=stocks[0],
- quantity_reserved=2,
- reserved_until=reserved_until,
- ),
- Reservation(
- checkout_line=checkout_line,
- stock=stocks[1],
- quantity_reserved=1,
- reserved_until=reserved_until,
- ),
- ]
- )
-
- return checkout_line
-
-
-def checkout_line_with_one_reservation(
- customer_user, variant_with_many_stocks, checkout
-):
- variant = variant_with_many_stocks
- stocks = variant.stocks.all().order_by("pk")
- checkout_line = checkout.lines.create(
- variant=variant,
- quantity=2,
- )
-
- reserved_until = timezone.now() + datetime.timedelta(minutes=5)
-
- Reservation.objects.create(
- checkout_line=checkout_line,
- stock=stocks[0],
- quantity_reserved=2,
- reserved_until=reserved_until,
- )
-
- return checkout_line
-
-
-def checkout_line_with_preorder_item(
- checkout, product, preorder_variant_channel_threshold
-):
- checkout_info = fetch_checkout_info(
- checkout, [], get_plugins_manager(allow_replica=False)
- )
- add_variant_to_checkout(checkout_info, preorder_variant_channel_threshold, 1)
- return checkout.lines.last()
-
-
-def checkout_line_with_reserved_preorder_item(
- checkout, product, preorder_variant_channel_threshold
-):
- checkout_info = fetch_checkout_info(
- checkout, [], get_plugins_manager(allow_replica=False)
- )
- add_variant_to_checkout(checkout_info, preorder_variant_channel_threshold, 2)
- checkout_line = checkout.lines.last()
-
- reserved_until = timezone.now() + datetime.timedelta(minutes=5)
-
- PreorderReservation.objects.create(
- checkout_line=checkout_line,
- product_variant_channel_listing=checkout_line.variant.channel_listings.first(),
- quantity_reserved=2,
- reserved_until=reserved_until,
- )
-
- return checkout_line
-
-
@pytest.fixture
def payment_txn_preauth(order_with_lines, payment_dummy):
order = order_with_lines
@@ -2850,140 +1963,6 @@ def delivery_method(request, warehouse_for_cc, shipping_method):
return None
-def checkout_for_cc(channel_USD, customer_user):
- checkout = Checkout.objects.create(
- channel=channel_USD,
- billing_address=customer_user.default_billing_address,
- shipping_address=customer_user.default_shipping_address,
- note="Test notes",
- currency="USD",
- email=customer_user.email,
- )
- CheckoutMetadata.objects.create(checkout=checkout)
- return checkout
-
-
-def checkout_with_items_for_cc(checkout_for_cc, product_variant_list):
- CheckoutLine.objects.bulk_create(
- [
- CheckoutLine(
- checkout=checkout_for_cc,
- variant=product_variant_list[0],
- quantity=1,
- currency=checkout_for_cc.currency,
- ),
- CheckoutLine(
- checkout=checkout_for_cc,
- variant=product_variant_list[1],
- quantity=1,
- currency=checkout_for_cc.currency,
- ),
- CheckoutLine(
- checkout=checkout_for_cc,
- variant=product_variant_list[2],
- quantity=1,
- currency=checkout_for_cc.currency,
- ),
- ]
- )
- checkout_for_cc.set_country("US", commit=True)
-
- return checkout_for_cc
-
-
-def checkout_with_item_for_cc(checkout_for_cc, product_variant_list):
- CheckoutLine.objects.create(
- checkout=checkout_for_cc,
- variant=product_variant_list[0],
- quantity=1,
- currency=checkout_for_cc.currency,
- )
- return checkout_for_cc
-
-
-def checkout_with_prices(
- checkout_with_items,
- address,
- address_other_country,
- warehouse,
- customer_user,
- shipping_method,
- voucher,
-):
- # Need to save shipping_method before fetching checkout info.
- checkout_with_items.shipping_method = shipping_method
- checkout_with_items.save(update_fields=["shipping_method"])
-
- manager = get_plugins_manager(allow_replica=False)
- lines = checkout_with_items.lines.all()
- lines_info, _ = fetch_checkout_lines(checkout_with_items)
- checkout_info = fetch_checkout_info(checkout_with_items, lines_info, manager)
-
- for line, line_info in zip(lines, lines_info):
- line.total_price_net_amount = base_calculations.calculate_base_line_total_price(
- line_info
- ).amount
- line.total_price_gross_amount = line.total_price_net_amount * Decimal("1.230")
-
- checkout_with_items.discount_amount = Decimal("5.000")
- checkout_with_items.discount_name = "Voucher 5 USD"
- checkout_with_items.user = customer_user
- checkout_with_items.billing_address = address
- checkout_with_items.shipping_address = address_other_country
- checkout_with_items.collection_point = warehouse
- checkout_with_items.subtotal_net_amount = Decimal("100.000")
- checkout_with_items.subtotal_gross_amount = Decimal("123.000")
- checkout_with_items.total_net_amount = Decimal("150.000")
- checkout_with_items.total_gross_amount = Decimal("178.000")
- shipping_amount = base_calculations.base_checkout_delivery_price(
- checkout_info, lines_info
- ).amount
- checkout_with_items.shipping_price_net_amount = shipping_amount
- checkout_with_items.shipping_price_gross_amount = shipping_amount * Decimal("1.08")
- checkout_with_items.metadata_storage.metadata = {"meta_key": "meta_value"}
- checkout_with_items.metadata_storage.private_metadata = {
- "priv_meta_key": "priv_meta_value"
- }
-
- checkout_with_items.lines.bulk_update(
- lines,
- [
- "total_price_net_amount",
- "total_price_gross_amount",
- ],
- )
-
- checkout_with_items.save(
- update_fields=[
- "discount_amount",
- "discount_name",
- "user",
- "billing_address",
- "shipping_address",
- "collection_point",
- "subtotal_net_amount",
- "subtotal_gross_amount",
- "total_net_amount",
- "total_gross_amount",
- "shipping_price_net_amount",
- "shipping_price_gross_amount",
- ]
- )
- checkout_with_items.metadata_storage.save(
- update_fields=["metadata", "private_metadata"]
- )
-
- user = checkout_with_items.user
- user.metadata = {"user_public_meta_key": "user_public_meta_value"}
- user.save(update_fields=["metadata"])
-
- return checkout_with_items
-
-
@pytest.fixture
def user_export_file(staff_user):
job = ExportFile.objects.create(user=staff_user)