Skip to content

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

Add test for specific product voucher amonut calculation with sale

fowczarek471affd8ce43
diff --git a/tests/api/test_checkout_promo_codes.py b/tests/api/test_checkout_promo_codes.py
index cc9789fc76..27197861a2 100644
--- a/tests/api/test_checkout_promo_codes.py
+++ b/tests/api/test_checkout_promo_codes.py
@@ -314,10 +314,26 @@ def test_checkout_add_voucher_code_checkout_with_sale(
variables = {"checkoutId": checkout_id, "promoCode": voucher_percentage.code}
data = _mutate_checkout_add_promo_code(api_client, variables)
+ checkout_with_item.refresh_from_db()
assert not data["errors"]
- assert data["checkout"]["id"] == checkout_id
- assert data["checkout"]["voucherCode"] == voucher_percentage.code
- assert data["checkout"]["totalPrice"]["gross"]["amount"] == 13.5
+ assert checkout_with_item.voucher_code == voucher_percentage.code
+ assert checkout_with_item.discount_amount.amount == 1.5
+
+
+def test_checkout_add_specific_product_voucher_code_checkout_with_sale(
+ api_client, checkout_with_item, voucher_specific_product_type, discount_info
+):
+ voucher = voucher_specific_product_type
+ checkout = checkout_with_item
+ 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(
diff --git a/tests/conftest.py b/tests/conftest.py
index 4c99590fbf..4e60b2c7dd 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -667,13 +667,6 @@ def voucher(db): # pylint: disable=W0613
return Voucher.objects.create(code="mirumee", discount_value=20)
-def voucher_specific_product_type(db):
- return Voucher.objects.create(
- code="mirumee", discount_value=10, type=VoucherType.SPECIFIC_PRODUCT
- )
-
-
@pytest.fixture
def voucher_percentage(db):
return Voucher.objects.create(
@@ -683,6 +676,13 @@ def voucher_percentage(db):
)
+def voucher_specific_product_type(voucher_percentage):
+ voucher_percentage.type = VoucherType.SPECIFIC_PRODUCT
+ voucher_percentage.save()
+ return voucher_percentage
+
+
@pytest.fixture
def voucher_with_high_min_amount_spent():
return Voucher.objects.create(
diff --git a/tests/test_checkout.py b/tests/test_checkout.py
index cad1f0ea11..595e3c861b 100644
--- a/tests/test_checkout.py
+++ b/tests/test_checkout.py
@@ -935,9 +935,10 @@ def test_get_discount_for_checkout_specific_products_voucher_not_applicable(
min_amount_spent,
min_quantity_of_products,
):
+ discounts = []
monkeypatch.setattr(
"saleor.checkout.utils.get_prices_of_discounted_specific_product",
- lambda checkout, product: [],
+ lambda checkout, discounts, product: [],
)
voucher = Voucher(
code="unique",
@@ -953,7 +954,7 @@ def test_get_discount_for_checkout_specific_products_voucher_not_applicable(
get_subtotal=Mock(return_value=Money(total, "USD")), quantity=total_quantity
)
with pytest.raises(NotApplicable):
- get_voucher_discount_for_checkout(voucher, checkout)
+ get_voucher_discount_for_checkout(voucher, checkout, discounts)
@pytest.mark.parametrize(