refactor(test): make item deterministic

This commit is contained in:
ruthra kumar
2026-01-01 17:41:31 +05:30
parent 26d7900590
commit f758ee4adc
2 changed files with 48 additions and 8 deletions

View File

@@ -5,8 +5,8 @@
import json
import frappe
from frappe import qb
from frappe.custom.doctype.property_setter.property_setter import make_property_setter
from frappe.test_runner import make_test_objects
from frappe.utils import add_days, today
from erpnext.controllers.item_variant import (
@@ -85,11 +85,25 @@ class TestItem(ERPNextTestSuite):
item = frappe.get_doc("Item", item_code)
return item
def test_get_item_details(self):
# delete modified item price record and make as per self.globalTestRecords["Item"]
frappe.db.sql("""delete from `tabItem Price`""")
frappe.db.sql("""delete from `tabBin`""")
def make_bin(self, records):
for x in records:
x = frappe._dict(x)
bin = qb.DocType("Bin")
filters = {
"item_code": x.get("item_code"),
"warehouse": x.get("warehouse"),
"reserved_qty": x.get("reserved_qty"),
"actual_qty": x.get("actual_qty"),
"ordered_qty": x.get("ordered_qty"),
"projected_qty": x.get("projected_qty"),
}
if not frappe.db.exists("Bin", filters):
qb.from_(bin).delete().where(
bin.item_code.eq(x.item_code) & bin.warehouse.eq(x.warehouse)
).run()
frappe.get_doc(x).insert()
def test_get_item_details(self):
to_check = {
"item_code": "_Test Item",
"item_name": "_Test Item",
@@ -114,11 +128,10 @@ class TestItem(ERPNextTestSuite):
"projected_qty": 14,
}
make_test_objects("Item Price")
make_test_objects(
"Bin",
self.make_bin(
[
{
"doctype": "Bin",
"item_code": "_Test Item",
"warehouse": "_Test Warehouse - _TC",
"reserved_qty": 1,
@@ -954,6 +967,7 @@ class TestItem(ERPNextTestSuite):
msg="Different Variant UOM should not be allowed when `allow_different_uom` is disabled.",
)
@ERPNextTestSuite.change_settings("Global Defaults", {"default_company": "_Test Company"})
def test_opening_stock_for_serial_batch(self):
items = {
"Test Opening Stock for Serial No": {

View File

@@ -246,6 +246,7 @@ class ERPNextTestSuite(unittest.TestCase):
cls.make_quality_inspection_param()
cls.make_quality_inspection_template()
cls.make_employees()
cls.make_brand()
cls.update_selling_settings()
cls.update_stock_settings()
cls.update_system_settings()
@@ -2919,6 +2920,31 @@ class ERPNextTestSuite(unittest.TestCase):
)
)
@classmethod
def make_brand(cls):
records = [
{"brand": "_Test Brand", "doctype": "Brand"},
{
"brand": "_Test Brand With Item Defaults",
"doctype": "Brand",
"brand_defaults": [
{
"company": "_Test Company",
"expense_account": "_Test Account Cost for Goods Sold - _TC",
"income_account": "_Test Account Sales - _TC",
"buying_cost_center": "_Test Cost Center - _TC",
"selling_cost_center": "_Test Cost Center - _TC",
}
],
},
]
cls.brand = []
for x in records:
if not frappe.db.exists("Brand", {"brand": x.get("brand")}):
cls.brand.append(frappe.get_doc(x).insert())
else:
cls.brand.append(frappe.get_doc("Brand", {"Brand": x.get("brand")}))
@contextmanager
def set_user(self, user: str):
try: