Skip to content

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

Add test for products voucher amonut calculation with sale

fowczarek7e965249933c
diff --git a/tests/api/test_checkout_promo_codes.py b/tests/api/test_checkout_promo_codes.py
index 27197861a2..12ac636d3c 100644
--- a/tests/api/test_checkout_promo_codes.py
+++ b/tests/api/test_checkout_promo_codes.py
@@ -4,7 +4,7 @@ import graphene
from prices import TaxedMoney
from saleor.checkout.utils import add_voucher_to_checkout
-from saleor.discount import DiscountInfo
+from saleor.discount import DiscountInfo, VoucherType
from tests.api.test_checkout import (
MUTATION_CHECKOUT_LINES_DELETE,
MUTATION_CHECKOUT_SHIPPING_ADDRESS_UPDATE,
@@ -336,6 +336,26 @@ def test_checkout_add_specific_product_voucher_code_checkout_with_sale(
assert checkout.discount_amount.amount == 1.5
+def test_checkout_add_products_voucher_code_checkout_with_sale(
+ api_client, checkout_with_item, voucher_percentage, discount_info
+):
+ checkout = checkout_with_item
+ product = checkout.lines.first().variant.product
+ voucher = voucher_percentage
+ voucher.type = VoucherType.PRODUCT
+ voucher.save()
+ voucher.products.add(product)
+ assert checkout.get_subtotal() > checkout.get_subtotal(discounts=[discount_info])
+ checkout_id = graphene.Node.to_global_id("Checkout", checkout.pk)
+ variables = {"checkoutId": checkout_id, "promoCode": voucher.code}
+ data = _mutate_checkout_add_promo_code(api_client, variables)
+
+ checkout.refresh_from_db()
+ assert not data["errors"]
+ assert checkout.voucher_code == voucher.code
+ assert checkout.discount_amount.amount == 1.5
+
+
def test_checkout_add_voucher_code_not_applicable_voucher(
api_client, checkout_with_item, voucher_with_high_min_amount_spent
):
diff --git a/tests/test_checkout.py b/tests/test_checkout.py
index 595e3c861b..835756438f 100644
--- a/tests/test_checkout.py
+++ b/tests/test_checkout.py
@@ -1154,9 +1154,10 @@ def test_get_discount_for_checkout_shipping_voucher_not_applicable(
def test_get_discount_for_checkout_product_voucher_not_applicable(monkeypatch):
+ discounts = []
monkeypatch.setattr(
"saleor.checkout.utils.get_prices_of_discounted_products",
- lambda checkout, product: [],
+ lambda checkout, discounts, product: [],
)
voucher = Voucher(
code="unique",
@@ -1168,7 +1169,7 @@ def test_get_discount_for_checkout_product_voucher_not_applicable(monkeypatch):
checkout = Mock()
with pytest.raises(NotApplicable) as e:
- get_voucher_discount_for_checkout(voucher, checkout)
+ get_voucher_discount_for_checkout(voucher, checkout, discounts)
assert str(e.value) == "This offer is only valid for selected items."
diff --git a/tests/test_discounts.py b/tests/test_discounts.py
index 5973c42702..37d41aff13 100644
--- a/tests/test_discounts.py
+++ b/tests/test_discounts.py
@@ -105,9 +105,12 @@ def test_products_voucher_checkout_discount_not(
apply_once_per_order,
checkout_with_item,
):
+ discounts = []
monkeypatch.setattr(
"saleor.checkout.utils.get_prices_of_discounted_products",
- lambda lines, discounted_products: (Money(price, "USD") for price in prices),
+ lambda lines, discounts, discounted_products: (
+ Money(price, "USD") for price in prices
+ ),
)
voucher = Voucher(
code="unique",
@@ -118,7 +121,7 @@ def test_products_voucher_checkout_discount_not(
)
voucher.save()
checkout = checkout_with_item
- discount = get_voucher_discount_for_checkout(voucher, checkout)
+ discount = get_voucher_discount_for_checkout(voucher, checkout, discounts)
assert discount == Money(expected_value, "USD")