Skip to content

Commit

Permalink
test: add test case for auto-reservation from PR
Browse files Browse the repository at this point in the history
  • Loading branch information
s-aga-r committed Oct 23, 2023
1 parent a432290 commit adf313a
Showing 1 changed file with 85 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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
Expand All @@ -28,10 +29,6 @@ def setUp(self) -> None:
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 (
Expand Down Expand Up @@ -568,6 +565,90 @@ def test_stock_reservation_from_pick_list(self):
# 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 = [
Expand Down

0 comments on commit adf313a

Please sign in to comment.