From e9d4e2ceddf9e0dff8e946f28a729242c30af5f1 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Fri, 26 Jun 2026 10:20:07 +0530 Subject: [PATCH 1/3] test: add coverage for Stock Ledger Variance report Co-Authored-By: Claude Opus 4.8 (1M context) --- .../test_stock_ledger_variance.py | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 erpnext/stock/report/stock_ledger_variance/test_stock_ledger_variance.py diff --git a/erpnext/stock/report/stock_ledger_variance/test_stock_ledger_variance.py b/erpnext/stock/report/stock_ledger_variance/test_stock_ledger_variance.py new file mode 100644 index 00000000000..e561a2fce5a --- /dev/null +++ b/erpnext/stock/report/stock_ledger_variance/test_stock_ledger_variance.py @@ -0,0 +1,83 @@ +# Copyright (c) 2026, Frappe Technologies Pvt. Ltd. and Contributors +# See license.txt + +import frappe + +from erpnext.tests.utils import ERPNextTestSuite + + +class TestStockLedgerVariance(ERPNextTestSuite): + def run_report(self, **extra): + from erpnext.stock.report.stock_ledger_variance.stock_ledger_variance import execute + + filters = {"company": "_Test Company"} + filters.update(extra) + + return execute(frappe._dict(filters))[1] + + def test_healthy_stock_has_no_variance(self): + from erpnext.stock.doctype.item.test_item import make_item + from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry + + item = make_item(properties={"is_stock_item": 1, "valuation_method": "Moving Average"}).name + + make_stock_entry( + item_code=item, + to_warehouse="_Test Warehouse - _TC", + qty=10, + rate=100, + posting_date="2026-06-01", + ) + make_stock_entry( + item_code=item, + from_warehouse="_Test Warehouse - _TC", + qty=4, + posting_date="2026-06-02", + ) + + # A clean receipt followed by a clean issue keeps the ledger consistent, + # so the corruption detector must not flag any entry for this item. + data = self.run_report(item_code=item) + self.assertFalse([row for row in data if row.get("item_code") == item]) + + qty_data = self.run_report(item_code=item, difference_in="Qty") + self.assertFalse([row for row in qty_data if row.get("item_code") == item]) + + def test_multiple_clean_movements_no_variance(self): + from erpnext.stock.doctype.item.test_item import make_item + from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry + + item = make_item(properties={"is_stock_item": 1, "valuation_method": "Moving Average"}).name + + make_stock_entry( + item_code=item, + to_warehouse="_Test Warehouse - _TC", + qty=10, + rate=100, + posting_date="2026-06-01", + ) + make_stock_entry( + item_code=item, + to_warehouse="_Test Warehouse - _TC", + qty=5, + rate=120, + posting_date="2026-06-02", + ) + make_stock_entry( + item_code=item, + to_warehouse="_Test Warehouse - _TC", + qty=8, + rate=90, + posting_date="2026-06-03", + ) + make_stock_entry( + item_code=item, + from_warehouse="_Test Warehouse - _TC", + qty=6, + posting_date="2026-06-04", + ) + + # Several receipts at different rates plus an issue still produce a + # self-consistent ledger, so no variance rows are expected. + data = self.run_report(item_code=item) + self.assertFalse([row for row in data if row.get("item_code") == item]) From 5d217295e546e82ea213dd68a7fbd54884c3e745 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Fri, 26 Jun 2026 13:28:09 +0530 Subject: [PATCH 2/3] test: reuse BootStrapTestData master data to reduce runtime Co-Authored-By: Claude Opus 4.8 (1M context) --- .../test_stock_ledger_variance.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/erpnext/stock/report/stock_ledger_variance/test_stock_ledger_variance.py b/erpnext/stock/report/stock_ledger_variance/test_stock_ledger_variance.py index e561a2fce5a..2459204ca8f 100644 --- a/erpnext/stock/report/stock_ledger_variance/test_stock_ledger_variance.py +++ b/erpnext/stock/report/stock_ledger_variance/test_stock_ledger_variance.py @@ -16,21 +16,21 @@ class TestStockLedgerVariance(ERPNextTestSuite): return execute(frappe._dict(filters))[1] def test_healthy_stock_has_no_variance(self): - from erpnext.stock.doctype.item.test_item import make_item from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry - item = make_item(properties={"is_stock_item": 1, "valuation_method": "Moving Average"}).name + item = "_Test Item" + frappe.db.set_value("Item", item, "valuation_method", "Moving Average") make_stock_entry( item_code=item, - to_warehouse="_Test Warehouse - _TC", + to_warehouse="Stores - _TC", qty=10, rate=100, posting_date="2026-06-01", ) make_stock_entry( item_code=item, - from_warehouse="_Test Warehouse - _TC", + from_warehouse="Stores - _TC", qty=4, posting_date="2026-06-02", ) @@ -44,35 +44,35 @@ class TestStockLedgerVariance(ERPNextTestSuite): self.assertFalse([row for row in qty_data if row.get("item_code") == item]) def test_multiple_clean_movements_no_variance(self): - from erpnext.stock.doctype.item.test_item import make_item from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry - item = make_item(properties={"is_stock_item": 1, "valuation_method": "Moving Average"}).name + item = "_Test Item" + frappe.db.set_value("Item", item, "valuation_method", "Moving Average") make_stock_entry( item_code=item, - to_warehouse="_Test Warehouse - _TC", + to_warehouse="Stores - _TC", qty=10, rate=100, posting_date="2026-06-01", ) make_stock_entry( item_code=item, - to_warehouse="_Test Warehouse - _TC", + to_warehouse="Stores - _TC", qty=5, rate=120, posting_date="2026-06-02", ) make_stock_entry( item_code=item, - to_warehouse="_Test Warehouse - _TC", + to_warehouse="Stores - _TC", qty=8, rate=90, posting_date="2026-06-03", ) make_stock_entry( item_code=item, - from_warehouse="_Test Warehouse - _TC", + from_warehouse="Stores - _TC", qty=6, posting_date="2026-06-04", ) From f47141a3b7fb3cc97a67d88eb79c0ff53531d07c Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Wed, 1 Jul 2026 20:15:32 +0530 Subject: [PATCH 3/3] test: flag an actual balance-qty variance in Stock Ledger Variance --- .../test_stock_ledger_variance.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/erpnext/stock/report/stock_ledger_variance/test_stock_ledger_variance.py b/erpnext/stock/report/stock_ledger_variance/test_stock_ledger_variance.py index 2459204ca8f..1787a0a75e5 100644 --- a/erpnext/stock/report/stock_ledger_variance/test_stock_ledger_variance.py +++ b/erpnext/stock/report/stock_ledger_variance/test_stock_ledger_variance.py @@ -81,3 +81,26 @@ class TestStockLedgerVariance(ERPNextTestSuite): # self-consistent ledger, so no variance rows are expected. data = self.run_report(item_code=item) self.assertFalse([row for row in data if row.get("item_code") == item]) + + def test_incorrect_balance_qty_is_flagged(self): + from erpnext.stock.doctype.stock_entry.stock_entry_utils import make_stock_entry + + item = "_Test Item 2" + warehouse = "Stores - _TC" + frappe.db.set_value("Item", item, "valuation_method", "Moving Average") + + entry = make_stock_entry( + item_code=item, to_warehouse=warehouse, qty=10, rate=100, posting_date="2026-06-01" + ) + sle = frappe.db.get_value( + "Stock Ledger Entry", + {"voucher_no": entry.name, "item_code": item, "warehouse": warehouse}, + "name", + ) + + # corrupt the stored running balance (expected 10 from the receipt, but now claims 7) + frappe.db.set_value("Stock Ledger Entry", sle, "qty_after_transaction", 7, update_modified=False) + + data = self.run_report(item_code=item, difference_in="Qty") + row = next(r for r in data if r.get("item_code") == item) + self.assertEqual(row["difference_in_qty"], -3) # 7 (stored) - 10 (expected)