From 6b8b9d3644dc829c184be414678c95494ff87aa3 Mon Sep 17 00:00:00 2001 From: Jatin3128 <140256508+Jatin3128@users.noreply.github.com> Date: Wed, 29 Jul 2026 12:38:33 +0530 Subject: [PATCH] test: isolate accounts settings mutation in overdue threshold test (#57441) * test: isolate accounts settings mutation in overdue threshold test test_overdue_billing_threshold_on_submit mutated the Accounts Settings singleton without restoring it, so a failed assertion mid-test leaked enable_overdue_billing_threshold and the bypass role into later tests that submit sales invoices. Wrap the mutations in try/finally and restore the originals. Also assert that a 0 overdue limit on the customer inherits the customer group's limit. * test: restore credit limits in overdue threshold fallback test --- .../selling/doctype/customer/test_customer.py | 67 +++++++++++++------ 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/erpnext/selling/doctype/customer/test_customer.py b/erpnext/selling/doctype/customer/test_customer.py index 721ea466938..807f653fc27 100644 --- a/erpnext/selling/doctype/customer/test_customer.py +++ b/erpnext/selling/doctype/customer/test_customer.py @@ -445,33 +445,44 @@ class TestCustomer(ERPNextTestSuite): overdue = get_customer_overdue_amount("_Test Customer", "_Test Company") settings = frappe.get_single("Accounts Settings") - settings.enable_overdue_billing_threshold = 1 - settings.role_allowed_to_bypass_overdue_billing = None - settings.save() - set_overdue_billing_threshold("_Test Customer", "_Test Company", overdue - 100) + original_enable = settings.enable_overdue_billing_threshold + original_bypass_role = settings.role_allowed_to_bypass_overdue_billing + try: + settings.enable_overdue_billing_threshold = 1 + settings.role_allowed_to_bypass_overdue_billing = None + settings.save() + set_overdue_billing_threshold("_Test Customer", "_Test Company", overdue - 100) - # overdue is over the threshold and the user has no bypass role -> blocked - si = create_sales_invoice(do_not_submit=True) - self.assertRaises(frappe.ValidationError, si.submit) + # overdue is over the threshold and the user has no bypass role -> blocked + si = create_sales_invoice(do_not_submit=True) + self.assertRaises(frappe.ValidationError, si.submit) - # a user holding the bypass role can still submit - settings.role_allowed_to_bypass_overdue_billing = "Accounts Manager" - settings.save() - si = create_sales_invoice(do_not_submit=True) - si.submit() - self.assertEqual(si.docstatus, 1) + # a user holding the bypass role can still submit + settings.role_allowed_to_bypass_overdue_billing = "Accounts Manager" + settings.save() + si = create_sales_invoice(do_not_submit=True) + si.submit() + self.assertEqual(si.docstatus, 1) - # threshold still crossed, but the feature is off -> never blocked - settings.enable_overdue_billing_threshold = 0 - settings.role_allowed_to_bypass_overdue_billing = None - settings.save() - si = create_sales_invoice(do_not_submit=True) - si.submit() - self.assertEqual(si.docstatus, 1) + # threshold still crossed, but the feature is off -> never blocked + settings.enable_overdue_billing_threshold = 0 + settings.role_allowed_to_bypass_overdue_billing = None + settings.save() + si = create_sales_invoice(do_not_submit=True) + si.submit() + self.assertEqual(si.docstatus, 1) + finally: + settings.enable_overdue_billing_threshold = original_enable + settings.role_allowed_to_bypass_overdue_billing = original_bypass_role + settings.save() def test_overdue_billing_threshold_falls_back_to_customer_group(self): customer_group = frappe.get_cached_value("Customer", "_Test Customer", "customer_group") group = frappe.get_doc("Customer Group", customer_group) + customer = frappe.get_doc("Customer", "_Test Customer") + self._restore_credit_limits_after(group) + self._restore_credit_limits_after(customer) + group.credit_limits = [] group.append("credit_limits", {"company": "_Test Company", "overdue_billing_threshold": 5000}) group.save() @@ -483,6 +494,22 @@ class TestCustomer(ERPNextTestSuite): set_overdue_billing_threshold("_Test Customer", "_Test Company", 2000) self.assertEqual(get_overdue_billing_threshold("_Test Customer", "_Test Company"), 2000) + # a 0 on the customer inherits the group's limit + set_overdue_billing_threshold("_Test Customer", "_Test Company", 0) + self.assertEqual(get_overdue_billing_threshold("_Test Customer", "_Test Company"), 5000) + + def _restore_credit_limits_after(self, doc): + original = [row.as_dict(no_default_fields=True) for row in doc.credit_limits] + + def restore(): + fresh = frappe.get_doc(doc.doctype, doc.name) + fresh.credit_limits = [] + for row in original: + fresh.append("credit_limits", row) + fresh.save() + + self.addCleanup(restore) + def test_overdue_threshold_row_without_credit_limit(self): from erpnext.accounts.doctype.sales_invoice.test_sales_invoice import create_sales_invoice