refactor(test): move tests to inventory dimension#54508
refactor(test): move tests to inventory dimension#54508ruthra-kumar wants to merge 1 commit intofrappe:developfrom
Conversation
📝 WalkthroughWalkthroughThis change relocates a test for inventory dimension persistence behavior from the subcontracting receipt test suite to the inventory dimension test suite. The new test Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@erpnext/stock/doctype/inventory_dimension/test_inventory_dimension.py`:
- Around line 570-593: Wrap the changes to global state in a try/finally: before
calling set_backflush_based_on("BOM") capture the current backflush setting (the
Buying Settings singleton) into a local variable, then call
set_backflush_based_on("BOM") and run the test steps; in the finally block
restore the captured backflush value via set_backflush_based_on(...) and also
ensure inventory_dimension.reqd is set back to its original value and
inventory_dimension.save() is called so the "Inv Site" dimension is always
reverted even if assertions or saves fail. Reference:
set_backflush_based_on(...), inventory_dimension.reqd, and
inventory_dimension.save().
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: e4e70de8-48ac-42f9-92d1-b3349f43c37f
📒 Files selected for processing (2)
erpnext/stock/doctype/inventory_dimension/test_inventory_dimension.pyerpnext/subcontracting/doctype/subcontracting_receipt/test_subcontracting_receipt.py
💤 Files with no reviewable changes (1)
- erpnext/subcontracting/doctype/subcontracting_receipt/test_subcontracting_receipt.py
| inventory_dimension.reqd = 1 | ||
| inventory_dimension.save() | ||
|
|
||
| set_backflush_based_on("BOM") | ||
|
|
||
| sco = get_subcontracting_order() | ||
| rm_items = get_rm_items(sco.supplied_items) | ||
| itemwise_details = make_stock_in_entry(rm_items=rm_items) | ||
| make_stock_transfer_entry( | ||
| sco_no=sco.name, | ||
| rm_items=rm_items, | ||
| itemwise_details=copy.deepcopy(itemwise_details), | ||
| ) | ||
| scr = make_subcontracting_receipt(sco.name) | ||
| scr.items[0].inv_site = "Site 1" | ||
| scr.save() | ||
|
|
||
| scr.supplied_items[0].inv_site = "Site 1" | ||
| scr.save() | ||
|
|
||
| self.assertEqual(scr.supplied_items[0].inv_site, "Site 1") | ||
|
|
||
| inventory_dimension.reqd = 0 | ||
| inventory_dimension.save() |
There was a problem hiding this comment.
Guard cleanup with try/finally and reset backflush_based_on.
Two pieces of global state leak if this test fails or even succeeds:
set_backflush_based_on("BOM")is never reverted. SinceBuying Settings.backflush_raw_materials_of_subcontract_based_onis a singleton persisted across the test run, this will silently alter behavior for any subsequent subcontracting tests in the same process.inventory_dimension.reqd = 0is only restored on the happy path. IfassertEqualon line 590 (or any priorsave()) raises,reqd=1remains on the shared "Inv Site" dimension — and other tests in this file (test_validate_negative_stock_for_inventory_dimension,test_validate_negative_stock_with_multiple_dimension) reuse the same "Inv Site" dimension via the idempotentcreate_inventory_dimension, so they will start seeing a mandatory field and fail with unrelated errors.
Wrap the mutation/restore in try/finally (and capture the previous backflush value):
🧹 Proposed cleanup
- inventory_dimension.reqd = 1
- inventory_dimension.save()
-
- set_backflush_based_on("BOM")
-
- sco = get_subcontracting_order()
- rm_items = get_rm_items(sco.supplied_items)
- itemwise_details = make_stock_in_entry(rm_items=rm_items)
- make_stock_transfer_entry(
- sco_no=sco.name,
- rm_items=rm_items,
- itemwise_details=copy.deepcopy(itemwise_details),
- )
- scr = make_subcontracting_receipt(sco.name)
- scr.items[0].inv_site = "Site 1"
- scr.save()
-
- scr.supplied_items[0].inv_site = "Site 1"
- scr.save()
-
- self.assertEqual(scr.supplied_items[0].inv_site, "Site 1")
-
- inventory_dimension.reqd = 0
- inventory_dimension.save()
+ previous_backflush = frappe.db.get_single_value(
+ "Buying Settings", "backflush_raw_materials_of_subcontract_based_on"
+ )
+ inventory_dimension.reqd = 1
+ inventory_dimension.save()
+ set_backflush_based_on("BOM")
+ try:
+ sco = get_subcontracting_order()
+ rm_items = get_rm_items(sco.supplied_items)
+ itemwise_details = make_stock_in_entry(rm_items=rm_items)
+ make_stock_transfer_entry(
+ sco_no=sco.name,
+ rm_items=rm_items,
+ itemwise_details=copy.deepcopy(itemwise_details),
+ )
+ scr = make_subcontracting_receipt(sco.name)
+ scr.items[0].inv_site = "Site 1"
+ scr.save()
+
+ scr.supplied_items[0].inv_site = "Site 1"
+ scr.save()
+
+ self.assertEqual(scr.supplied_items[0].inv_site, "Site 1")
+ finally:
+ inventory_dimension.reqd = 0
+ inventory_dimension.save()
+ if previous_backflush:
+ set_backflush_based_on(previous_backflush)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@erpnext/stock/doctype/inventory_dimension/test_inventory_dimension.py` around
lines 570 - 593, Wrap the changes to global state in a try/finally: before
calling set_backflush_based_on("BOM") capture the current backflush setting (the
Buying Settings singleton) into a local variable, then call
set_backflush_based_on("BOM") and run the test steps; in the finally block
restore the captured backflush value via set_backflush_based_on(...) and also
ensure inventory_dimension.reqd is set back to its original value and
inventory_dimension.save() is called so the "Inv Site" dimension is always
reverted even if assertions or saves fail. Reference:
set_backflush_based_on(...), inventory_dimension.reqd, and
inventory_dimension.save().
No description provided.