From 577be4f9ad9ad88137c2c33e4522db3d2a97eab4 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Sat, 3 Jan 2026 17:30:32 +0530 Subject: [PATCH] refactor(test): make purchase receipt deterministic --- .../purchase_receipt/test_purchase_receipt.py | 30 ++++++++---------- .../serial_and_batch_bundle.py | 2 +- erpnext/tests/utils.py | 31 +++++++++++++++++++ 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py index 6315997f5ec..aa1a804d729 100644 --- a/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py +++ b/erpnext/stock/doctype/purchase_receipt/test_purchase_receipt.py @@ -35,7 +35,7 @@ class TestPurchaseReceipt(ERPNextTestSuite): cls.load_test_records("Purchase Receipt") def setUp(self): - frappe.db.set_single_value("Buying Settings", "allow_multiple_items", 1) + frappe.local.future_sle = {} def tearDown(self): frappe.db.rollback() @@ -2665,12 +2665,12 @@ class TestPurchaseReceipt(ERPNextTestSuite): gl_entries = get_gl_entries("Purchase Receipt", pr.name, skip_cancelled=True, as_dict=False) warehouse_account = get_warehouse_account_map("_Test Company") expected_gle = ( - ("Stock Received But Not Billed - _TC", 0, 10000, "Main - _TC"), - ("Freight and Forwarding Charges - _TC", 0, 2000, "Main - _TC"), - ("Expenses Included In Valuation - _TC", 0, 2000, "Main - _TC"), - (warehouse_account[pr.items[0].warehouse]["account"], 14000, 0, "Main - _TC"), + ("Stock Received But Not Billed - _TC", 0.0, 10000.0, "Main - _TC"), + ("Freight and Forwarding Charges - _TC", 0.0, 2000.0, "Main - _TC"), + ("Expenses Included In Valuation - _TC", 0.0, 2000.0, "Main - _TC"), + (warehouse_account[pr.items[0].warehouse]["account"], 14000.0, 0.0, "Main - _TC"), ) - self.assertSequenceEqual(expected_gle, gl_entries) + self.assertCountEqual(expected_gle, gl_entries) frappe.local.enable_perpetual_inventory["_Test Company"] = old_perpetual_inventory def test_purchase_receipt_with_use_serial_batch_field_for_rejected_qty(self): @@ -4177,8 +4177,6 @@ class TestPurchaseReceipt(ERPNextTestSuite): make_purchase_return, ) - frappe.flags.through_repost_item_valuation = False - sn_item_code = make_item( "Test Serial No for Validation", {"has_serial_no": 1, "serial_no_series": "SN-TSNFVAL-.#####"} ).name @@ -4211,8 +4209,6 @@ class TestPurchaseReceipt(ERPNextTestSuite): make_purchase_return, ) - frappe.flags.through_repost_item_valuation = False - batch_item_code = make_item( "Test Batch No for Validation", {"has_batch_no": 1, "batch_number_series": "BT-TSNFVAL-.#####", "create_new_batch": 1}, @@ -4856,8 +4852,14 @@ class TestPurchaseReceipt(ERPNextTestSuite): self.assertRaises(NegativeStockError, pr.cancel) @ERPNextTestSuite.change_settings( - "Buying Settings", {"set_landed_cost_based_on_purchase_invoice_rate": 1, "maintain_same_rate": 0} + "Buying Settings", + { + "set_landed_cost_based_on_purchase_invoice_rate": 1, + "maintain_same_rate": 0, + "allow_multiple_items": 1, + }, ) + @ERPNextTestSuite.change_settings("Accounts Settings", {"over_billing_allowance": 100}) def test_set_lcv_from_pi_created_against_po(self): from erpnext.buying.doctype.purchase_order.purchase_order import ( make_purchase_invoice as make_pi_against_po, @@ -4867,10 +4869,6 @@ class TestPurchaseReceipt(ERPNextTestSuite): ) from erpnext.buying.doctype.purchase_order.test_purchase_order import create_purchase_order - original_value = frappe.db.get_single_value("Accounts Settings", "over_billing_allowance") - - frappe.db.set_single_value("Accounts Settings", "over_billing_allowance", 100) - item_code = create_item("Test Item for LCV from PI against PO").name po = create_purchase_order(item_code=item_code, qty=10, rate=400) @@ -4895,8 +4893,6 @@ class TestPurchaseReceipt(ERPNextTestSuite): amt_diff = 5000 * (row.qty / 10) - row.amount self.assertEqual(row.amount_difference_with_purchase_invoice, amt_diff) - frappe.db.set_single_value("Accounts Settings", "over_billing_allowance", original_value) - def test_purchase_return_with_and_without_return_against_rejected_qty(self): from erpnext.stock.doctype.purchase_receipt.purchase_receipt import ( make_purchase_return as _make_purchase_return, diff --git a/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py b/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py index 0d438714e8a..5521d297d05 100644 --- a/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py +++ b/erpnext/stock/doctype/serial_and_batch_bundle/serial_and_batch_bundle.py @@ -444,7 +444,7 @@ class SerialandBatchBundle(Document): self.set_incoming_rate_for_inward_transaction(row, save, prev_sle=prev_sle) def validate_returned_serial_batch_no(self, return_against, row, original_inv_details): - if frappe.flags.through_repost_item_valuation: + if frappe.flags.through_repost_item_valuation and not frappe.in_test: return if row.serial_no and row.serial_no not in original_inv_details["serial_nos"]: diff --git a/erpnext/tests/utils.py b/erpnext/tests/utils.py index bb54b2c45e4..09dc1ca5d56 100644 --- a/erpnext/tests/utils.py +++ b/erpnext/tests/utils.py @@ -1186,6 +1186,15 @@ class ERPNextTestSuite(unittest.TestCase): "supplier_group": "_Test Supplier Group", "tax_category": "_Test Tax Category 1", }, + { + "doctype": "Supplier", + "supplier_name": "_Test Internal Supplier 2", + "supplier_group": "_Test Supplier Group", + "is_internal_supplier": 1, + "territory": "_Test Territory", + "represents_company": cls.companies[6].name, + "companies": [{"company": cls.companies[6].name}], + }, ] cls.suppliers = [] for x in records: @@ -2218,6 +2227,18 @@ class ERPNextTestSuite(unittest.TestCase): "batch_number_series": "BASR.#####", "create_new_batch": 1, }, + { + "doctype": "Item", + "item_code": "Test Asset Item", + "item_name": "Test Asset Item", + "is_stock_item": 0, + "item_group": "All Item Groups", + "stock_uom": "Box", + "is_fixed_asset": 1, + "auto_create_assets": 1, + "asset_category": cls.asset_category[0].name, + "asset_naming_series": "ABC.###", + }, ] cls.item = [] for x in records: @@ -2405,6 +2426,16 @@ class ERPNextTestSuite(unittest.TestCase): "doctype": "Customer", "territory": "_Test Territory", }, + { + "customer_group": "_Test Customer Group", + "customer_name": "_Test Internal Customer 2", + "customer_type": "Individual", + "doctype": "Customer", + "is_internal_customer": 1, + "territory": "_Test Territory", + "represents_company": cls.companies[6].name, + "companies": [{"company": cls.companies[6].name}], + }, ] cls.customer = [] for x in records: