From 63ba59e136485ca01ed65ce860d0724a4f31d59a Mon Sep 17 00:00:00 2001 From: "David (aider)" Date: Tue, 3 Sep 2024 21:06:13 +0200 Subject: [PATCH] test: add test case for coupon without maximum use --- .../doctype/coupon_code/test_coupon_code.py | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/erpnext/accounts/doctype/coupon_code/test_coupon_code.py b/erpnext/accounts/doctype/coupon_code/test_coupon_code.py index aad341e9e1d7..5abab6eba853 100644 --- a/erpnext/accounts/doctype/coupon_code/test_coupon_code.py +++ b/erpnext/accounts/doctype/coupon_code/test_coupon_code.py @@ -142,3 +142,52 @@ def test_sales_order_with_coupon_code(self): so.submit() self.assertEqual(frappe.db.get_value("Coupon Code", "SAVE30", "used"), 1) + + def test_coupon_without_max_use(self): + from erpnext.accounts.doctype.pricing_rule.test_pricing_rule import ( + make_pricing_rule, + ) + from erpnext.accounts.doctype.pricing_rule.utils import ( + update_coupon_code_count, + validate_coupon_code, + ) + + pricing_rule = make_pricing_rule( + discount_percentage=20, + selling=1, + priority=1, + apply_multiple_pricing_rules=1, + title="_Test Pricing Rule 1", + ) + pricing_rule.save() + + coupon = frappe.get_doc( + { + "doctype": "Coupon Code", + "coupon_name": "_Test Coupon Without Max Use", + "coupon_code": "TESTUNLIMITED", + "pricing_rule": pricing_rule.name, + "valid_from": frappe.utils.nowdate(), + "maximum_use": 0, + "used": 0, + } + ) + coupon.insert(ignore_permissions=True) + + # Validate initial state + self.assertEqual(coupon.used, 0) + self.assertEqual(coupon.maximum_use, 0) + + # Use coupon multiple times + for _ in range(5): + validate_coupon_code(coupon.coupon_code) + update_coupon_code_count(coupon.coupon_code, "used") + coupon.reload() + + # Check that the coupon is still valid and usage count increased + self.assertEqual(coupon.used, 5) + validate_coupon_code(coupon.coupon_code) # This should not raise an error + + # Clean up + coupon.delete() + pricing_rule.delete()