test: add test case for auto-reservation from PR

This commit is contained in:
s-aga-r 2023-10-23 17:52:59 +05:30
parent a432290a82
commit adf313a6d3

View File

@ -5,6 +5,7 @@ from random import randint
import frappe
from frappe.tests.utils import FrappeTestCase, change_settings
from frappe.utils import today
from erpnext.selling.doctype.sales_order.sales_order import create_pick_list, make_delivery_note
from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order
@ -28,10 +29,6 @@ class TestStockReservationEntry(FrappeTestCase):
items={self.sr_item.name: self.sr_item}, warehouse=self.warehouse, qty=100
)
def tearDown(self) -> None:
cancel_all_stock_reservation_entries()
return super().tearDown()
@change_settings("Stock Settings", {"allow_negative_stock": 0})
def test_validate_stock_reservation_settings(self) -> None:
from erpnext.stock.doctype.stock_reservation_entry.stock_reservation_entry import (
@ -568,6 +565,90 @@ class TestStockReservationEntry(FrappeTestCase):
# Test - 3: Reserved Serial/Batch Nos should be equal to Picked Serial/Batch Nos.
self.assertSetEqual(picked_sb_details, reserved_sb_details)
@change_settings(
"Stock Settings",
{
"allow_negative_stock": 0,
"enable_stock_reservation": 1,
"auto_reserve_serial_and_batch": 1,
"pick_serial_and_batch_based_on": "FIFO",
"auto_reserve_stock_for_sales_order_on_purchase": 1,
},
)
def test_stock_reservation_from_purchase_receipt(self):
from erpnext.buying.doctype.purchase_order.purchase_order import make_purchase_receipt
from erpnext.selling.doctype.sales_order.sales_order import make_material_request
from erpnext.stock.doctype.material_request.material_request import make_purchase_order
items_details = create_items()
create_material_receipt(items_details, self.warehouse, qty=10)
item_list = []
for item_code, properties in items_details.items():
item_list.append(
{
"item_code": item_code,
"warehouse": self.warehouse,
"qty": randint(11, 100),
"uom": properties.stock_uom,
"rate": randint(10, 400),
}
)
so = make_sales_order(
item_list=item_list,
warehouse=self.warehouse,
)
mr = make_material_request(so.name)
mr.schedule_date = today()
mr.save().submit()
po = make_purchase_order(mr.name)
po.supplier = "_Test Supplier"
po.save().submit()
pr = make_purchase_receipt(po.name)
pr.save().submit()
for item in pr.items:
sre, status, reserved_qty = frappe.db.get_value(
"Stock Reservation Entry",
{
"from_voucher_type": "Purchase Receipt",
"from_voucher_no": pr.name,
"from_voucher_detail_no": item.name,
},
["name", "status", "reserved_qty"],
)
# Test - 1: SRE status should be `Reserved`.
self.assertEqual(status, "Reserved")
# Test - 2: SRE Reserved Qty should be equal to PR Item Qty.
self.assertEqual(reserved_qty, item.qty)
if item.serial_and_batch_bundle:
sb_details = frappe.db.get_all(
"Serial and Batch Entry",
filters={"parent": item.serial_and_batch_bundle},
fields=["serial_no", "batch_no", "qty"],
as_list=True,
)
reserved_sb_details = frappe.db.get_all(
"Serial and Batch Entry",
filters={"parent": sre},
fields=["serial_no", "batch_no", "qty"],
as_list=True,
)
# Test - 3: Reserved Serial/Batch Nos should be equal to PR Item Serial/Batch Nos.
self.assertEqual(set(sb_details), set(reserved_sb_details))
def tearDown(self) -> None:
cancel_all_stock_reservation_entries()
return super().tearDown()
def create_items() -> dict:
items_properties = [