Skip to content

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

Add test for once per order

fowczarek2efe2bc9e5dd
diff --git a/tests/conftest.py b/tests/conftest.py
index 4e60b2c7dd..71877d7316 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -22,7 +22,7 @@ from saleor.checkout.models import Checkout
from saleor.checkout.utils import add_variant_to_checkout
from saleor.dashboard.menu.utils import update_menu
from saleor.discount import DiscountInfo, DiscountValueType, VoucherType
-from saleor.discount.models import Sale, Voucher, VoucherTranslation
+from saleor.discount.models import Sale, Voucher, VoucherCustomer, VoucherTranslation
from saleor.giftcard.models import GiftCard
from saleor.menu.models import Menu, MenuItem
from saleor.order import OrderStatus
@@ -697,6 +697,12 @@ def voucher_shipping_type():
)
+def voucher_customer(voucher, customer_user):
+ email = customer_user.email
+ return VoucherCustomer.objects.create(voucher=voucher, customer_email=email)
+
+
@pytest.fixture()
def order_line(order, variant):
net = variant.get_price()
diff --git a/tests/test_checkout.py b/tests/test_checkout.py
index 549f76b9d5..6bc585d9b0 100644
--- a/tests/test_checkout.py
+++ b/tests/test_checkout.py
@@ -866,8 +866,10 @@ def test_get_voucher_discount_for_checkout_voucher_validation(
):
get_voucher_discount_for_checkout(voucher, checkout_with_voucher)
subtotal = calculate_checkout_subtotal(checkout_with_voucher, [])
+ quantity = checkout_with_voucher.quantity
+ customer_email = checkout_with_voucher.get_customer_email()
mock_validate_voucher.assert_called_once_with(
- voucher, subtotal.gross, checkout_with_voucher.quantity
+ voucher, subtotal.gross, quantity, customer_email
)
diff --git a/tests/test_discounts.py b/tests/test_discounts.py
index 9fa270e701..cc86397bcb 100644
--- a/tests/test_discounts.py
+++ b/tests/test_discounts.py
@@ -6,11 +6,13 @@ from prices import Money
from saleor.checkout.utils import get_voucher_discount_for_checkout
from saleor.discount import DiscountInfo, DiscountValueType, VoucherType
-from saleor.discount.models import NotApplicable, Sale, Voucher
+from saleor.discount.models import NotApplicable, Sale, Voucher, VoucherCustomer
from saleor.discount.utils import (
+ add_voucher_usege_by_customer,
decrease_voucher_usage,
get_product_discount_on_sale,
increase_voucher_usage,
+ remove_voucher_usege_by_customer,
validate_voucher,
)
from saleor.product.models import Product, ProductVariant
@@ -186,6 +188,34 @@ def test_decrease_voucher_usage():
assert voucher.used == 9
+def test_add_voucher_usege_by_customer(voucher, customer_user):
+ voucherCustomerCount = VoucherCustomer.objects.all().count()
+ add_voucher_usege_by_customer(voucher, customer_user.email)
+ assert VoucherCustomer.objects.all().count() == voucherCustomerCount + 1
+ voucherCustomer = VoucherCustomer.objects.first()
+ assert voucherCustomer.voucher == voucher
+ assert voucherCustomer.customer_email == customer_user.email
+
+
+def test_add_voucher_usege_by_customer_raise_not_applicable(voucher_customer):
+ voucher = voucher_customer.voucher
+ customer_email = voucher_customer.customer_email
+ with pytest.raises(NotApplicable):
+ add_voucher_usege_by_customer(voucher, customer_email)
+
+
+def test_remove_voucher_usege_by_customer(voucher_customer):
+ voucherCustomerCount = VoucherCustomer.objects.all().count()
+ voucher = voucher_customer.voucher
+ customer_email = voucher_customer.customer_email
+ remove_voucher_usege_by_customer(voucher, customer_email)
+ assert VoucherCustomer.objects.all().count() == voucherCustomerCount - 1
+
+
+def test_remove_voucher_usege_by_customer_not_exists(voucher):
+ remove_voucher_usege_by_customer(voucher, "[email protected]")
+
+
@pytest.mark.parametrize(
"total, min_amount_spent, total_quantity, min_checkout_items_quantity,"
"discount_value_type",
@@ -212,7 +242,7 @@ def test_validate_voucher(
min_checkout_items_quantity=min_checkout_items_quantity,
)
total_price = Money(total, "USD")
- validate_voucher(voucher, total_price, total_quantity)
+ validate_voucher(voucher, total_price, total_quantity, "[email protected]")
@pytest.mark.parametrize(
@@ -232,7 +262,7 @@ def test_validate_voucher_not_applicable(
discount_value,
discount_value_type,
):
- voucher = Voucher(
+ voucher = Voucher.objects.create(
code="unique",
type=VoucherType.ENTIRE_ORDER,
discount_value_type=discount_value_type,
@@ -240,10 +270,17 @@ def test_validate_voucher_not_applicable(
min_amount_spent=get_min_amount_spent(min_amount_spent),
min_checkout_items_quantity=min_checkout_items_quantity,
)
- voucher.save()
total_price = Money(total, "USD")
with pytest.raises(NotApplicable):
- validate_voucher(voucher, total_price, total_quantity)
+ validate_voucher(voucher, total_price, total_quantity, "[email protected]")
+
+
+def test_validate_voucher_not_applicable_once_per_customer(voucher, customer_user):
+ voucher.apply_once_per_customer = True
+ voucher.save()
+ VoucherCustomer.objects.create(voucher=voucher, customer_email=customer_user.email)
+ with pytest.raises(NotApplicable):
+ validate_voucher(voucher, 0, 0, customer_user.email)
date_time_now = timezone.now()
diff --git a/tests/test_order.py b/tests/test_order.py
index e281e22620..244cc81b7b 100644
--- a/tests/test_order.py
+++ b/tests/test_order.py
@@ -713,6 +713,8 @@ def test_get_voucher_discount_for_order_voucher_validation(
order_with_lines.save()
validate_voucher_in_order(order_with_lines)
subtotal = order_with_lines.get_subtotal()
+ quantity = order_with_lines.get_total_quantity()
+ customer_email = order_with_lines.get_customer_email()
mock_validate_voucher.assert_called_once_with(
- voucher, subtotal.gross, order_with_lines.get_total_quantity()
+ voucher, subtotal.gross, quantity, customer_email
)