Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add test for getting voucher discount for checkout
fowczarekf3cc7e2a20a9
diff --git a/tests/test_checkout.py b/tests/test_checkout.pyindex 179c12eef7..cad1f0ea11 100644--- a/tests/test_checkout.py+++ b/tests/test_checkout.py@@ -848,6 +848,42 @@ def test_get_discount_for_checkout_value_voucher(assert discount == Money(discount_amount, "USD")+ "total, total_quantity, discount_value, discount_type, min_amount_spent, "+ "min_quantity_of_products",+ [+ ("99", 9, 10, DiscountValueType.FIXED, None, 10),+ ("99", 9, 10, DiscountValueType.FIXED, 100, None),+ ("99", 10, 10, DiscountValueType.PERCENTAGE, 100, 10),+ ("100", 9, 10, DiscountValueType.PERCENTAGE, 100, 10),+ ("99", 9, 10, DiscountValueType.PERCENTAGE, 100, 10),+ ],+)+def test_get_discount_for_checkout_value_voucher_not_applicable(+ total,+ total_quantity,+ discount_value,+ discount_type,+ min_amount_spent,+ min_quantity_of_products,+):+ voucher = Voucher(+ code="unique",+ type=VoucherType.ENTIRE_ORDER,+ discount_value_type=discount_type,+ discount_value=discount_value,+ min_amount_spent=(+ Money(min_amount_spent, "USD") if min_amount_spent is not None else None+ ),+ min_quantity_of_products=min_quantity_of_products,+ )+ checkout = Mock(+ get_subtotal=Mock(return_value=Money(total, "USD")), quantity=total_quantity+ )+ with pytest.raises(NotApplicable):+ get_voucher_discount_for_checkout(voucher, checkout)++@pytest.mark.parametrize("discount_value, discount_type, apply_once_per_order, discount_amount",[@@ -858,7 +894,7 @@ def test_get_discount_for_checkout_value_voucher((10, DiscountValueType.PERCENTAGE, False, 5),],)-def test_get_discount_for_checkout_apply_once_per_order(+def test_get_discount_for_checkout_specific_products_voucher(checkout_with_items,product_list,discount_value,@@ -868,7 +904,7 @@ def test_get_discount_for_checkout_apply_once_per_order():voucher = Voucher.objects.create(code="unique",- type=VoucherType.PRODUCT,+ type=VoucherType.SPECIFIC_PRODUCT,discount_value_type=discount_type,discount_value=discount_value,apply_once_per_order=apply_once_per_order,@@ -879,18 +915,45 @@ def test_get_discount_for_checkout_apply_once_per_order(assert discount == Money(discount_amount, "USD")-def test_get_discount_for_checkout_value_voucher_not_applicable():+ "total, total_quantity, discount_value, discount_type, min_amount_spent,"+ "min_quantity_of_products",+ [+ ("99", 9, 10, DiscountValueType.FIXED, None, 10),+ ("99", 9, 10, DiscountValueType.FIXED, 100, None),+ ("99", 10, 10, DiscountValueType.PERCENTAGE, 100, 10),+ ("100", 9, 10, DiscountValueType.PERCENTAGE, 100, 10),+ ("99", 9, 10, DiscountValueType.PERCENTAGE, 100, 10),+ ],+)+def test_get_discount_for_checkout_specific_products_voucher_not_applicable(+ monkeypatch,+ total,+ total_quantity,+ discount_value,+ discount_type,+ min_amount_spent,+ min_quantity_of_products,+):+ monkeypatch.setattr(+ "saleor.checkout.utils.get_prices_of_discounted_specific_product",+ lambda checkout, product: [],+ )voucher = Voucher(code="unique",- type=VoucherType.ENTIRE_ORDER,- discount_value_type=DiscountValueType.FIXED,- discount_value=10,- min_amount_spent=Money(100, "USD"),+ type=VoucherType.SPECIFIC_PRODUCT,+ discount_value_type=discount_type,+ discount_value=discount_value,+ min_amount_spent=(+ Money(min_amount_spent, "USD") if min_amount_spent is not None else None+ ),+ min_quantity_of_products=min_quantity_of_products,)- checkout = Mock(get_subtotal=Mock(return_value=Money(10, "USD")))- with pytest.raises(NotApplicable) as e:+ checkout = Mock(+ get_subtotal=Mock(return_value=Money(total, "USD")), quantity=total_quantity+ )+ with pytest.raises(NotApplicable):get_voucher_discount_for_checkout(voucher, checkout)- assert e.value.min_amount_spent == Money(100, "USD")@pytest.mark.parametrize(@@ -977,7 +1040,8 @@ def test_get_discount_for_checkout_shipping_voucher_limited_countries():@pytest.mark.parametrize("is_shipping_required, shipping_method, discount_value, discount_type,"- "countries, min_amount_spent, subtotal, error_msg",+ "countries, min_amount_spent, min_quantity_of_products, subtotal, total_quantity,"+ "error_msg",[(True,@@ -986,7 +1050,9 @@ def test_get_discount_for_checkout_shipping_voucher_limited_countries():DiscountValueType.FIXED,["US"],None,+ None,Money(10, "USD"),+ 10,"This offer is not valid in your country.",),(@@ -996,7 +1062,9 @@ def test_get_discount_for_checkout_shipping_voucher_limited_countries():DiscountValueType.FIXED,[],None,+ None,Money(10, "USD"),+ 10,"Please select a shipping method first.",),(@@ -1006,7 +1074,9 @@ def test_get_discount_for_checkout_shipping_voucher_limited_countries():DiscountValueType.FIXED,[],None,+ None,Money(10, "USD"),+ 10,"Your order does not require shipping.",),(@@ -1016,7 +1086,33 @@ def test_get_discount_for_checkout_shipping_voucher_limited_countries():DiscountValueType.FIXED,[],5,+ None,+ Money(2, "USD"),+ 10,+ "This offer is only valid for orders over $5.00.",+ ),+ (+ True,+ Mock(price=Money(10, "USD")),+ 10,+ DiscountValueType.FIXED,+ [],+ 5,+ 10,+ Money(5, "USD"),+ 9,+ "This offer is only valid for orders with a minimum of 10 quantity.",+ ),+ (+ True,+ Mock(price=Money(10, "USD")),+ 10,+ DiscountValueType.FIXED,+ [],+ 5,+ 10,Money(2, "USD"),+ 9,"This offer is only valid for orders over $5.00.",),],@@ -1028,7 +1124,9 @@ def test_get_discount_for_checkout_shipping_voucher_not_applicable(discount_type,countries,min_amount_spent,+ min_quantity_of_products,subtotal,+ total_quantity,error_msg,):checkout = Mock(@@ -1036,6 +1134,7 @@ def test_get_discount_for_checkout_shipping_voucher_not_applicable(is_shipping_required=Mock(return_value=is_shipping_required),shipping_method=shipping_method,get_shipping_price=Mock(return_value=Money(10, "USD")),+ quantity=total_quantity,)voucher = Voucher(code="unique",@@ -1045,6 +1144,7 @@ def test_get_discount_for_checkout_shipping_voucher_not_applicable(min_amount_spent=(Money(min_amount_spent, "USD") if min_amount_spent is not None else None),+ min_quantity_of_products=min_quantity_of_products,countries=countries,)with pytest.raises(NotApplicable) as e:diff --git a/tests/test_discounts.py b/tests/test_discounts.pyindex 05fe5ced51..5973c42702 100644--- a/tests/test_discounts.py+++ b/tests/test_discounts.py@@ -179,16 +179,23 @@ def test_decrease_voucher_usage():@pytest.mark.parametrize(- "total, min_amount_spent, discount_value, discount_value_type, expected_value",+ "total, min_amount_spent, total_quantity, min_quantity_of_products, "+ "discount_value, discount_value_type, expected_value",[- (20, 15, 50, DiscountValueType.PERCENTAGE, 10),- (20, None, 50, DiscountValueType.PERCENTAGE, 10),- (20, 15, 5, DiscountValueType.FIXED, 5),- (20, None, 5, DiscountValueType.FIXED, 5),+ (20, 20, 2, 2, 50, DiscountValueType.PERCENTAGE, 10),+ (20, None, 2, None, 50, DiscountValueType.PERCENTAGE, 10),+ (20, 20, 2, 2, 5, DiscountValueType.FIXED, 5),+ (20, None, 2, None, 5, DiscountValueType.FIXED, 5),],)def test_get_value_voucher_discount(- total, min_amount_spent, discount_value, discount_value_type, expected_value+ total,+ min_amount_spent,+ total_quantity,+ min_quantity_of_products,+ discount_value,+ discount_value_type,+ expected_value,):voucher = Voucher(code="unique",@@ -196,26 +203,60 @@ def test_get_value_voucher_discount(discount_value_type=discount_value_type,discount_value=discount_value,min_amount_spent=get_min_amount_spent(min_amount_spent),+ min_quantity_of_products=min_quantity_of_products,)voucher.save()total_price = Money(total, "USD")- discount = get_value_voucher_discount(voucher, total_price)+ discount = get_value_voucher_discount(voucher, total_price, total_quantity)assert discount == Money(expected_value, "USD")@pytest.mark.parametrize(- "total, min_amount_spent, shipping_price, discount_value, "- "discount_value_type, expected_value",+ "total, min_amount_spent, total_quantity, min_quantity_of_products, "+ "discount_value, discount_value_type",+ [+ (20, 50, 2, 10, 50, DiscountValueType.PERCENTAGE),+ (20, 50, 2, None, 50, DiscountValueType.PERCENTAGE),+ (20, None, 2, 10, 50, DiscountValueType.FIXED),+ ],+)+def test_get_value_voucher_discount_not_applicable(+ total,+ min_amount_spent,+ total_quantity,+ min_quantity_of_products,+ discount_value,+ discount_value_type,+):+ voucher = Voucher(+ code="unique",+ type=VoucherType.ENTIRE_ORDER,+ discount_value_type=discount_value_type,+ discount_value=discount_value,+ min_amount_spent=get_min_amount_spent(min_amount_spent),+ min_quantity_of_products=min_quantity_of_products,+ )+ voucher.save()+ total_price = Money(total, "USD")+ with pytest.raises(NotApplicable):+ get_value_voucher_discount(voucher, total_price, total_quantity)+++ "total, min_amount_spent, total_quantity, min_quantity_of_products, "+ "shipping_price, discount_value, discount_value_type, expected_value",[- (20, 15, 10, 50, DiscountValueType.PERCENTAGE, 5),- (20, None, 10, 50, DiscountValueType.PERCENTAGE, 5),- (20, 15, 10, 5, DiscountValueType.FIXED, 5),- (20, None, 10, 5, DiscountValueType.FIXED, 5),+ (20, 20, 2, 2, 10, 50, DiscountValueType.PERCENTAGE, 5),+ (20, None, 2, None, 10, 50, DiscountValueType.PERCENTAGE, 5),+ (20, 20, 2, 2, 10, 5, DiscountValueType.FIXED, 5),+ (20, None, 2, None, 10, 5, DiscountValueType.FIXED, 5),],)def test_get_shipping_voucher_discount(total,min_amount_spent,+ total_quantity,+ min_quantity_of_products,shipping_price,discount_value,discount_value_type,@@ -227,24 +268,116 @@ def test_get_shipping_voucher_discount(discount_value_type=discount_value_type,discount_value=discount_value,min_amount_spent=get_min_amount_spent(min_amount_spent),+ min_quantity_of_products=min_quantity_of_products,)voucher.save()total = Money(total, "USD")shipping_price = Money(shipping_price, "USD")- discount = get_shipping_voucher_discount(voucher, total, shipping_price)+ discount = get_shipping_voucher_discount(+ voucher, total, shipping_price, total_quantity+ )assert discount == Money(expected_value, "USD")@pytest.mark.parametrize(- "prices, discount_value_type, discount_value, voucher_type, expected_value",+ "total, min_amount_spent, total_quantity, min_quantity_of_products, "+ "shipping_price, discount_value, discount_value_type",+ [+ (20, 50, 2, 10, 10, 50, DiscountValueType.PERCENTAGE),+ (20, 50, 2, None, 10, 50, DiscountValueType.PERCENTAGE),+ (20, None, 2, 10, 10, 5, DiscountValueType.FIXED),+ ],+)+def test_get_shipping_voucher_discount_not_applicable(+ total,+ min_amount_spent,+ total_quantity,+ min_quantity_of_products,+ shipping_price,+ discount_value,+ discount_value_type,+):+ voucher = Voucher(+ code="unique",+ type=VoucherType.ENTIRE_ORDER,+ discount_value_type=discount_value_type,+ discount_value=discount_value,+ min_amount_spent=get_min_amount_spent(min_amount_spent),+ min_quantity_of_products=min_quantity_of_products,+ )+ voucher.save()+ total = Money(total, "USD")+ shipping_price = Money(shipping_price, "USD")+ with pytest.raises(NotApplicable):+ get_shipping_voucher_discount(voucher, total, shipping_price, total_quantity)+++ "prices, total, min_amount_spent, total_quantity, min_quantity_of_products, "+ "discount_value_type, discount_value, voucher_type, apply_once_per_order, "+ "expected_value",[ # noqa- ([5, 10, 15], DiscountValueType.PERCENTAGE, 10, VoucherType.PRODUCT, 3),- ([5, 10, 15], DiscountValueType.FIXED, 2, VoucherType.PRODUCT, 6),- ([5, 10, 15], DiscountValueType.FIXED, 2, VoucherType.COLLECTION, 6),+ (+ [5, 10, 15],+ 20,+ 20,+ 4,+ 4,+ DiscountValueType.PERCENTAGE,+ 10,+ VoucherType.PRODUCT,+ False,+ 3,+ ),+ (+ [5, 10, 15],+ 20,+ None,+ 4,+ None,+ DiscountValueType.PERCENTAGE,+ 20,+ VoucherType.PRODUCT,+ True,+ 1,+ ),+ (+ [5, 10, 15],+ 20,+ 20,+ 4,+ 4,+ DiscountValueType.FIXED,+ 2,+ VoucherType.SPECIFIC_PRODUCT,+ False,+ 6,+ ),+ (+ [5, 10, 15],+ 20,+ None,+ 4,+ None,+ DiscountValueType.FIXED,+ 2,+ VoucherType.COLLECTION,+ True,+ 2,+ ),],)def test_get_voucher_discount_all_products(- prices, discount_value_type, discount_value, voucher_type, expected_value+ prices,+ total,+ min_amount_spent,+ total_quantity,+ min_quantity_of_products,+ discount_value_type,+ discount_value,+ voucher_type,+ apply_once_per_order,+ expected_value,):prices = [Money(price, "USD") for price in prices]voucher = Voucher(@@ -252,12 +385,93 @@ def test_get_voucher_discount_all_products(type=voucher_type,discount_value_type=discount_value_type,discount_value=discount_value,+ apply_once_per_order=apply_once_per_order,+ min_amount_spent=get_min_amount_spent(min_amount_spent),+ min_quantity_of_products=min_quantity_of_products,)voucher.save()- discount = get_products_voucher_discount(voucher, prices)+ total = Money(total, "USD")+ discount = get_products_voucher_discount(voucher, prices, total, total_quantity)assert discount == Money(expected_value, "USD")+ "prices, total, min_amount_spent, total_quantity, min_quantity_of_products, "+ "discount_value_type, discount_value, voucher_type, apply_once_per_order, ",+ [ # noqa+ (+ [5, 10, 15],+ 20,+ 50,+ 4,+ 10,+ DiscountValueType.PERCENTAGE,+ 10,+ VoucherType.PRODUCT,+ False,+ ),+ (+ [5, 10, 15],+ 20,+ 50,+ 4,+ None,+ DiscountValueType.PERCENTAGE,+ 10,+ VoucherType.PRODUCT,+ True,+ ),+ (+ [5, 10, 15],+ 20,+ None,+ 4,+ 10,+ DiscountValueType.FIXED,+ 2,+ VoucherType.SPECIFIC_PRODUCT,+ False,+ ),+ (+ [5, 10, 15],+ 20,+ 50,+ 4,+ 10,+ DiscountValueType.FIXED,+ 2,+ VoucherType.COLLECTION,+ True,+ ),+ ],+)+def test_get_voucher_discount_all_products_not_applicable(+ prices,+ total,+ min_amount_spent,+ total_quantity,+ min_quantity_of_products,+ discount_value_type,+ discount_value,+ voucher_type,+ apply_once_per_order,+):+ prices = [Money(price, "USD") for price in prices]+ voucher = Voucher(+ code="unique",+ type=voucher_type,+ discount_value_type=discount_value_type,+ discount_value=discount_value,+ apply_once_per_order=apply_once_per_order,+ min_amount_spent=get_min_amount_spent(min_amount_spent),+ min_quantity_of_products=min_quantity_of_products,+ )+ voucher.save()+ total = Money(total, "USD")+ with pytest.raises(NotApplicable):+ get_products_voucher_discount(voucher, prices, total, total_quantity)++date_time_now = timezone.now()