Skip to content

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

Add voucher limit validation test

Krzysztof Wolskiff2825fed853
diff --git a/tests/test_discounts.py b/tests/test_discounts.py
index b1476e235e..4f67b8a72e 100644
--- a/tests/test_discounts.py
+++ b/tests/test_discounts.py
@@ -4,10 +4,34 @@ import pytest
from mock import Mock
from prices import FixedDiscount, FractionalDiscount, Price
+from django_prices.templatetags.prices_i18n import net
+
from saleor.discount.models import NotApplicable, Sale, Voucher
from saleor.product.models import Product, ProductVariant
[email protected]('limit, value, valid', [
+ (Price(5, currency='USD'), Price(10, currency='USD'), True),
+ (Price(10, currency='USD'), Price(10, currency='USD'), True),
+ (Price(10, currency='USD'), Price(5, currency='USD'), False),
+])
+def test_voucher_limit_validation(settings, limit, value, valid):
+ settings.DEFAULT_CURRENCY = 'USD'
+ voucher = Voucher(
+ code='unique', type=Voucher.SHIPPING_TYPE,
+ discount_value_type=Voucher.DISCOUNT_VALUE_FIXED,
+ discount_value=Price(10, currency='USD'),
+ limit=limit)
+ if valid:
+ voucher.validate_limit(value)
+ else:
+ with pytest.raises(NotApplicable) as e:
+ voucher.validate_limit(value)
+ msg = 'This offer is only valid for orders over %(amount)s.' % {
+ 'amount': net(limit)}
+ assert str(e.value) == msg
+
+
@pytest.mark.integration
@pytest.mark.django_db(transaction=True)
def test_variant_discounts(product_in_stock):
@@ -59,8 +83,8 @@ def test_value_voucher_checkout_discount(settings, total, discount_value,
discount_value_type=discount_type,
discount_value=discount_value,
limit=Price(limit, currency='USD') if limit is not None else None)
- checkout = Mock(cart=Mock(get_total=Mock(
- return_value=Price(total, currency='USD'))))
+ checkout = Mock(get_subtotal=Mock(return_value=Price(total,
+ currency='USD')))
discount = voucher.get_discount_for_checkout(checkout)
assert discount.amount == Price(expected_value, currency='USD')
@@ -72,8 +96,8 @@ def test_value_voucher_checkout_discount_not_applicable(settings):
discount_value_type=Voucher.DISCOUNT_VALUE_FIXED,
discount_value=10,
limit=100)
- checkout = Mock(cart=Mock(get_total=Mock(
- return_value=Price(10, currency='USD'))))
+ checkout = Mock(get_subtotal=Mock(
+ return_value=Price(10, currency='USD')))
with pytest.raises(NotApplicable) as e:
voucher.get_discount_for_checkout(checkout)
assert str(e.value) == 'This offer is only valid for orders over $100.00.'
@@ -90,6 +114,7 @@ def test_shipping_voucher_checkout_discount(
discount_type, apply_to, expected_value):
settings.DEFAULT_CURRENCY = 'USD'
checkout = Mock(
+ get_subtotal=Mock(return_value=Price(100, currency='USD')),
is_shipping_required=True, shipping_method=Mock(
price=Price(shipping_cost, currency='USD'),
country_code=shipping_country_code))
@@ -97,28 +122,34 @@ def test_shipping_voucher_checkout_discount(
code='unique', type=Voucher.SHIPPING_TYPE,
discount_value_type=discount_type,
discount_value=discount_value,
- apply_to=apply_to)
+ apply_to=apply_to,
+ limit=None)
discount = voucher.get_discount_for_checkout(checkout)
assert discount.amount == Price(expected_value, currency='USD')
@pytest.mark.parametrize(
- 'is_shipping_required, shipping_method, discount_value, discount_type, apply_to, limit, error_msg', [ # noqa
- (True, Mock(country_code='PL'), 10, Voucher.DISCOUNT_VALUE_FIXED, 'US',
- None, 'This offer is only valid in United States of America.'),
- (True, None, 10, Voucher.DISCOUNT_VALUE_FIXED, None, None,
+ 'is_shipping_required, shipping_method, discount_value, discount_type, '
+ 'apply_to, limit, subtotal, error_msg', [
+ (True, Mock(country_code='PL'), 10, Voucher.DISCOUNT_VALUE_FIXED,
+ 'US', None, Price(10, currency='USD'),
+ 'This offer is only valid in United States of America.'),
+ (True, None, 10, Voucher.DISCOUNT_VALUE_FIXED,
+ None, None, Price(10, currency='USD'),
'Please select a shipping method first.'),
- (False, None, 10, Voucher.DISCOUNT_VALUE_FIXED, None, None,
+ (False, None, 10, Voucher.DISCOUNT_VALUE_FIXED,
+ None, None, Price(10, currency='USD'),
'Your order does not require shipping.'),
(True, Mock(price=Price(10, currency='USD')), 10,
- Voucher.DISCOUNT_VALUE_FIXED, None, 5,
- 'This offer is only valid for shipping over $5.00.')]) # noqa
+ Voucher.DISCOUNT_VALUE_FIXED, None, 5, Price(2, currency='USD'),
+ 'This offer is only valid for orders over $5.00.')])
def test_shipping_voucher_checkout_discountnot_applicable(
settings, is_shipping_required, shipping_method, discount_value,
- discount_type, apply_to, limit, error_msg):
+ discount_type, apply_to, limit, subtotal, error_msg):
settings.DEFAULT_CURRENCY = 'USD'
checkout = Mock(is_shipping_required=is_shipping_required,
- shipping_method=shipping_method)
+ shipping_method=shipping_method,
+ get_subtotal=Mock(return_value=subtotal))
voucher = Voucher(
code='unique', type=Voucher.SHIPPING_TYPE,
discount_value_type=discount_type,