395 lines
16 KiB
Python
395 lines
16 KiB
Python
"""Multi-Path Automated Unit & FSM Test Suite for Inward Receiving (UC-005).
|
|
|
|
Covers:
|
|
- TC-INW-005-01: End-to-end inward receiving flow (Gate -> Weighbridge -> GRN -> Lot).
|
|
- TC-INW-005-02: Bag tare (Bardana Katai) 3-decimal precision & shortage tolerance limits.
|
|
- TC-INW-005-03: Invariant scale rejection (tare >= gross) & duplicate vehicle check.
|
|
- TC-INW-005-04: Lot generation & initial status strictly in QUALITY_HOLD.
|
|
"""
|
|
from datetime import datetime, date, timezone
|
|
from decimal import Decimal
|
|
import unittest
|
|
|
|
from backend.app.api.receiving import ReceivingController
|
|
from backend.app.db.session import InMemoryStorage
|
|
from backend.app.models.receiving import InwardGateEntry, WeighbridgeSlip, GoodsReceiptNote, validate_gate_entry
|
|
from backend.app.models.tenant import Tenant, TenantMembership
|
|
from sandbox.weighbridge_mock import WeighbridgeMock
|
|
from sandbox.identity_mock import mock_idp
|
|
|
|
|
|
class TestInwardReceiving(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
self.store = InMemoryStorage()
|
|
self.controller = ReceivingController(store=self.store)
|
|
self.tenant_id = "tenant-alpha"
|
|
|
|
# Pre-seed active tenant and memberships in store
|
|
self.store.save_tenant(
|
|
Tenant(
|
|
tenant_id=self.tenant_id,
|
|
business_name="Alpha Agro",
|
|
legal_identifier="24ABCDE1234F1Z5",
|
|
contact_email="alpha@agro.com",
|
|
primary_mandi="Kota APMC",
|
|
status="ACTIVE"
|
|
)
|
|
)
|
|
self.store.save_membership(
|
|
TenantMembership(
|
|
tenant_id=self.tenant_id,
|
|
user_id="supervisor@alpha.com",
|
|
email="supervisor@alpha.com",
|
|
role="Security Guard",
|
|
status="ACTIVE",
|
|
membership_id="mem-03a"
|
|
)
|
|
)
|
|
self.store.save_membership(
|
|
TenantMembership(
|
|
tenant_id=self.tenant_id,
|
|
user_id="supervisor@alpha.com",
|
|
email="supervisor@alpha.com",
|
|
role="Warehouse Supervisor",
|
|
status="ACTIVE",
|
|
membership_id="mem-03b"
|
|
)
|
|
)
|
|
|
|
# Seed yard supervisor token
|
|
self.token = mock_idp.mint_jwt(
|
|
subject="supervisor@alpha.com",
|
|
email="supervisor@alpha.com",
|
|
name="Yard Supervisor",
|
|
roles=["Security Guard", "Warehouse Supervisor"],
|
|
tenant_id=self.tenant_id
|
|
)
|
|
self.auth_header = f"Bearer {self.token}"
|
|
|
|
# Pre-seed active sauda
|
|
self.sauda_id = "sauda-101"
|
|
self.store.insert("procurement_sauda", self.tenant_id, {
|
|
"id": self.sauda_id,
|
|
"tenant_id": self.tenant_id,
|
|
"sauda_number": "SDA-26-00042",
|
|
"contract_date": "2026-10-12",
|
|
"delivery_due_date": "2026-10-18",
|
|
"supplier_id": "sup-42",
|
|
"mandi_id": "mandi-kota",
|
|
"commodity_id": "SOY",
|
|
"variety": "Yellow Desi",
|
|
"grade": "FAQ",
|
|
"booked_quantity_mt": 50.0,
|
|
"trade_rate_per_qtl": 4700.0,
|
|
"cumulative_received_mt": 0.0,
|
|
"balance_mt": 50.0,
|
|
"status": "ACTIVE"
|
|
})
|
|
|
|
def test_end_to_end_inward_pipeline_happy_path(self):
|
|
"""TC-INW-005-01: Vehicle Gate Entry -> Weigh Gross -> Weigh Tare -> GRN -> Mint Lot."""
|
|
# Step 1: Gate Pass
|
|
gate_payload = {
|
|
"vehicle_number": "RJ-20-GA-4581",
|
|
"eway_bill_number": "241089201923",
|
|
"driver_name": "Ramesh Singh",
|
|
"driver_mobile": "9829012345",
|
|
"sauda_id": self.sauda_id,
|
|
"supplier_invoice_no": "INV-2026-9042",
|
|
"supplier_dispatch_weight_mt": 25.0,
|
|
"supplier_dispatch_bags": 500,
|
|
"is_seal_intact": True
|
|
}
|
|
res_gate = self.controller.create_gate_entry(self.auth_header, self.tenant_id, gate_payload)
|
|
self.assertEqual(res_gate.get("status_code"), 201)
|
|
gate_entry = res_gate["gate_entry"]
|
|
self.assertEqual(gate_entry["gate_pass_number"], "GP-26-00001")
|
|
self.assertEqual(gate_entry["status"], "GATE_IN")
|
|
|
|
# Step 2: Gross Weighment
|
|
gross_payload = {
|
|
"gate_pass_id": gate_entry["id"],
|
|
"gross_weight_mt": 38.450
|
|
}
|
|
res_gross = self.controller.create_weighment_gross(self.auth_header, self.tenant_id, gross_payload)
|
|
self.assertEqual(res_gross.get("status_code"), 201)
|
|
slip = res_gross["weighment_slip"]
|
|
self.assertEqual(slip["slip_number"], "WS-26-00001")
|
|
|
|
# Step 3: Tare Weighment & Bag Tare Worksheet (Bardana Katai)
|
|
tare_payload = {
|
|
"tare_weight_mt": 13.200,
|
|
"bag_packaging_type": "JUTE_GUNNY_NEW", # 0.650 kg/bag
|
|
"actual_bag_count": 500
|
|
}
|
|
res_tare = self.controller.record_weighment_tare(self.auth_header, self.tenant_id, slip["id"], tare_payload)
|
|
self.assertEqual(res_tare.get("status_code"), 200)
|
|
updated_slip = res_tare["weighment_slip"]
|
|
self.assertEqual(updated_slip["unloaded_weight_mt"], 25.250) # 38.45 - 13.20
|
|
self.assertEqual(updated_slip["bag_tare_deduction_mt"], 0.325) # 500 * 0.650 / 1000
|
|
self.assertEqual(updated_slip["final_net_weight_mt"], 24.925) # 25.25 - 0.325
|
|
self.assertEqual(updated_slip["shortage_weight_mt"], -0.075) # 24.925 - 25.0
|
|
self.assertFalse(updated_slip["is_shortage_exceeded"])
|
|
|
|
# Step 4: Post GRN and Mint Lot
|
|
grn_payload = {
|
|
"weighment_slip_id": slip["id"],
|
|
"godown_id": "godown-02",
|
|
"godown_stack_id": "stack-b-04"
|
|
}
|
|
res_grn = self.controller.post_grn(self.auth_header, self.tenant_id, grn_payload)
|
|
self.assertEqual(res_grn.get("status_code"), 201)
|
|
grn = res_grn["grn"]
|
|
lot = res_grn["lot"]
|
|
|
|
self.assertEqual(grn["grn_number"], "GRN-26-00001")
|
|
self.assertEqual(grn["accepted_quantity_mt"], 24.925)
|
|
self.assertEqual(lot["qc_status"], "QUALITY_HOLD") # Mandatory hold default
|
|
self.assertIn("LOT-26-10-SOY-", lot["lot_number"])
|
|
|
|
# Verify Sauda received MT updated
|
|
sauda = self.store.get("procurement_sauda", self.tenant_id, self.sauda_id)
|
|
self.assertEqual(sauda["cumulative_received_mt"], 24.925)
|
|
self.assertEqual(sauda["status"], "PARTIALLY_RECEIVED")
|
|
|
|
# Duplicate GRN attempt -> 409
|
|
res_dup_grn = self.controller.post_grn(self.auth_header, self.tenant_id, grn_payload)
|
|
self.assertEqual(res_dup_grn.get("status_code"), 409)
|
|
self.assertEqual(res_dup_grn.get("error"), "DUPLICATE_GRN")
|
|
|
|
def test_bag_tare_precision_and_shortage_tolerance_limits(self):
|
|
"""TC-INW-005-02: Bag tare calculates accurately to 3 decimals; shortage triggers tolerance."""
|
|
# 600 HDPE bags at 0.120 kg = 0.072 MT tare
|
|
tare_mt = WeighbridgeMock.calculate_bag_tare("HDPE_PLASTIC", 600)
|
|
self.assertEqual(tare_mt, Decimal("0.072"))
|
|
|
|
# Bulk produce (0 bags) = 0.000 MT
|
|
bulk_tare = WeighbridgeMock.calculate_bag_tare("BULK_LOOSE", 0)
|
|
self.assertEqual(bulk_tare, Decimal("0.000"))
|
|
|
|
# Within tolerance shortage (-0.50%)
|
|
res_ok = WeighbridgeMock.process_weighment(
|
|
gross_weight_mt=Decimal("30.000"),
|
|
tare_weight_mt=Decimal("10.000"),
|
|
packaging_type="BULK_LOOSE",
|
|
bag_count=0,
|
|
billed_weight_mt=Decimal("20.100")
|
|
)
|
|
self.assertFalse(res_ok["is_shortage_exceeded"])
|
|
|
|
# Exceeding tolerance shortage (-0.75% > 0.50%)
|
|
res_exceeded = WeighbridgeMock.process_weighment(
|
|
gross_weight_mt=Decimal("30.000"),
|
|
tare_weight_mt=Decimal("10.150"),
|
|
packaging_type="BULK_LOOSE",
|
|
bag_count=0,
|
|
billed_weight_mt=Decimal("20.000")
|
|
)
|
|
self.assertTrue(res_exceeded["is_shortage_exceeded"])
|
|
|
|
def test_scale_reading_and_vehicle_duplication_invariants(self):
|
|
"""TC-INW-005-03: Tare >= Gross rejected; duplicate vehicle in yard blocked."""
|
|
# Invariant: tare >= gross rejected
|
|
with self.assertRaises(ValueError):
|
|
WeighbridgeMock.process_weighment(
|
|
gross_weight_mt=Decimal("20.000"),
|
|
tare_weight_mt=Decimal("20.000"),
|
|
packaging_type="BULK_LOOSE",
|
|
bag_count=0,
|
|
billed_weight_mt=Decimal("20.000")
|
|
)
|
|
|
|
# Pre-seed active vehicle in yard
|
|
self.store.insert("inward_gate_entries", self.tenant_id, {
|
|
"id": "gate-prev",
|
|
"tenant_id": self.tenant_id,
|
|
"gate_pass_number": "GP-26-00099",
|
|
"vehicle_number": "RJ20GA4581",
|
|
"status": "GATE_IN"
|
|
})
|
|
|
|
# Attempt to issue second pass for same vehicle inside yard -> 409
|
|
dup_payload = {
|
|
"vehicle_number": "RJ-20-GA-4581",
|
|
"eway_bill_number": "241089201923",
|
|
"driver_name": "Ramesh",
|
|
"driver_mobile": "9829012345",
|
|
"sauda_id": self.sauda_id,
|
|
"supplier_invoice_no": "INV-01",
|
|
"supplier_dispatch_weight_mt": 25.0,
|
|
"supplier_dispatch_bags": 500
|
|
}
|
|
res = self.controller.create_gate_entry(self.auth_header, self.tenant_id, dup_payload)
|
|
self.assertEqual(res.get("status_code"), 409)
|
|
self.assertEqual(res.get("error"), "VEHICLE_ALREADY_IN_YARD")
|
|
|
|
def test_unauthenticated_and_unauthorized_receiving(self):
|
|
"""TC-INW-005-04: 401 unauthenticated and 403 unauthorized for receiving."""
|
|
res_unauth = self.controller.create_gate_entry(None, self.tenant_id, {})
|
|
self.assertEqual(res_unauth.get("status_code"), 401)
|
|
|
|
token_buyer = mock_idp.mint_jwt(
|
|
subject="buyer@alpha.com",
|
|
email="buyer@alpha.com",
|
|
name="Buyer",
|
|
roles=["Mandi Buyer"],
|
|
tenant_id=self.tenant_id
|
|
)
|
|
self.store.save_membership(
|
|
TenantMembership(
|
|
tenant_id=self.tenant_id,
|
|
user_id="buyer@alpha.com",
|
|
email="buyer@alpha.com",
|
|
role="Mandi Buyer",
|
|
status="ACTIVE"
|
|
)
|
|
)
|
|
res_bad_gate = self.controller.create_gate_entry(f"Bearer {token_buyer}", self.tenant_id, {})
|
|
self.assertEqual(res_bad_gate.get("status_code"), 403)
|
|
|
|
res_bad_grn = self.controller.post_grn(f"Bearer {token_buyer}", self.tenant_id, {})
|
|
self.assertEqual(res_bad_grn.get("status_code"), 403)
|
|
|
|
def test_list_gate_entries_and_not_found_handling(self):
|
|
"""TC-INW-005-05: List gate passes and verify 404 on missing references."""
|
|
res_list = self.controller.list_gate_entries(self.auth_header, self.tenant_id)
|
|
self.assertEqual(res_list.get("status_code"), 200)
|
|
|
|
# 404 gross weighment
|
|
res_gross = self.controller.create_weighment_gross(
|
|
self.auth_header, self.tenant_id, {"gate_pass_id": "non-existent"}
|
|
)
|
|
self.assertEqual(res_gross.get("status_code"), 404)
|
|
|
|
# 404 tare weighment
|
|
res_tare = self.controller.record_weighment_tare(
|
|
self.auth_header, self.tenant_id, "non-existent-slip", {"tare_weight_mt": 10.0}
|
|
)
|
|
self.assertEqual(res_tare.get("status_code"), 404)
|
|
|
|
def test_grn_validation_and_duplicate_handling(self):
|
|
"""TC-INW-005-06: GRN error handling for non-existent, untared, and duplicate slips."""
|
|
# Non-existent slip
|
|
res_404 = self.controller.post_grn(self.auth_header, self.tenant_id, {"weighment_slip_id": "none"})
|
|
self.assertEqual(res_404.get("status_code"), 404)
|
|
|
|
# Untared slip
|
|
self.store.insert("weighbridge_slips", self.tenant_id, {
|
|
"id": "slip-untared",
|
|
"tenant_id": self.tenant_id,
|
|
"slip_number": "WS-26-99999",
|
|
"gate_pass_id": "gate-1",
|
|
"final_net_weight_mt": None
|
|
})
|
|
res_untared = self.controller.post_grn(
|
|
self.auth_header, self.tenant_id, {"weighment_slip_id": "slip-untared"}
|
|
)
|
|
self.assertEqual(res_untared.get("status_code"), 400)
|
|
self.assertEqual(res_untared.get("error"), "TARE_NOT_COMPLETED")
|
|
|
|
# Gate pass with missing sauda
|
|
res_no_sauda = self.controller.create_gate_entry(
|
|
self.auth_header, self.tenant_id, {
|
|
"vehicle_number": "RJ20GA1111", "eway_bill_number": "123456789012",
|
|
"driver_mobile": "9829012345", "sauda_id": "non-existent"
|
|
}
|
|
)
|
|
self.assertEqual(res_no_sauda.get("status_code"), 404)
|
|
|
|
# Gate pass with closed sauda
|
|
self.store.insert("procurement_sauda", self.tenant_id, {
|
|
"id": "sauda-closed", "status": "FULFILLED"
|
|
})
|
|
res_closed_sauda = self.controller.create_gate_entry(
|
|
self.auth_header, self.tenant_id, {
|
|
"vehicle_number": "RJ20GA2222", "eway_bill_number": "123456789012",
|
|
"driver_mobile": "9829012345", "sauda_id": "sauda-closed"
|
|
}
|
|
)
|
|
self.assertEqual(res_closed_sauda.get("status_code"), 400)
|
|
|
|
def test_gate_entry_validation_errors(self):
|
|
"""TC-INW-005-07: InwardGateEntry constructor and validation edge cases."""
|
|
with self.assertRaises(ValueError):
|
|
validate_gate_entry("INVALID_VEHICLE", "123456789012", "9829012345")
|
|
with self.assertRaises(ValueError):
|
|
validate_gate_entry("RJ20GA4581", "123", "9829012345")
|
|
with self.assertRaises(ValueError):
|
|
validate_gate_entry("RJ20GA4581", "123456789012", "12345")
|
|
|
|
with self.assertRaises(ValueError):
|
|
InwardGateEntry(
|
|
tenant_id=self.tenant_id,
|
|
gate_pass_number="GP-01",
|
|
vehicle_number="RJ20GA4581",
|
|
eway_bill_number="123456789012",
|
|
driver_name="Ramesh",
|
|
driver_mobile="9829012345",
|
|
sauda_id="sauda-1",
|
|
supplier_invoice_no="INV-1",
|
|
supplier_dispatch_weight_mt=Decimal("-1.0"),
|
|
supplier_dispatch_bags=100
|
|
)
|
|
|
|
with self.assertRaises(ValueError):
|
|
InwardGateEntry(
|
|
tenant_id=self.tenant_id,
|
|
gate_pass_number="GP-01",
|
|
vehicle_number="RJ20GA4581",
|
|
eway_bill_number="123456789012",
|
|
driver_name="Ramesh",
|
|
driver_mobile="9829012345",
|
|
sauda_id="sauda-1",
|
|
supplier_invoice_no="INV-1",
|
|
supplier_dispatch_weight_mt=Decimal("10.0"),
|
|
supplier_dispatch_bags=0
|
|
)
|
|
|
|
def test_create_gate_entry_supports_bag_count_and_supplier_dispatch_bags_alias(self):
|
|
"""TC-INW-005-05: Ensure API accepts bag_count alias and resolves weighment slips by gate_pass_id."""
|
|
payload_with_bag_count = {
|
|
"vehicle_number": "MP39GH4521",
|
|
"eway_bill_number": "281098765432",
|
|
"driver_name": "Sunil Yadav",
|
|
"driver_mobile": "9826012345",
|
|
"sauda_id": self.sauda_id,
|
|
"supplier_invoice_no": "INV-SDA-2026-001",
|
|
"supplier_dispatch_weight_mt": 25.0,
|
|
"bag_count": 500,
|
|
}
|
|
res = self.controller.create_gate_entry(self.auth_header, self.tenant_id, payload_with_bag_count)
|
|
self.assertEqual(res.get("status_code"), 201)
|
|
ge = res["gate_entry"]
|
|
self.assertEqual(ge["supplier_dispatch_bags"], 500)
|
|
self.assertEqual(ge["bag_count"], 500)
|
|
|
|
# Gross weighment
|
|
res_gross = self.controller.create_weighment_gross(self.auth_header, self.tenant_id, {
|
|
"gate_pass_id": ge["id"],
|
|
"gross_weight_mt": 35.0
|
|
})
|
|
self.assertEqual(res_gross.get("status_code"), 201)
|
|
|
|
# Tare weighment using gate_pass_id as slip_id
|
|
res_tare = self.controller.record_weighment_tare(self.auth_header, self.tenant_id, ge["id"], {
|
|
"tare_weight_mt": 10.0,
|
|
"packaging_material": "JUTE",
|
|
"actual_bag_count": 500
|
|
})
|
|
self.assertEqual(res_tare.get("status_code"), 200)
|
|
|
|
# Post GRN using gate_pass_id as slip_id
|
|
res_grn = self.controller.post_grn(self.auth_header, self.tenant_id, {
|
|
"weighment_slip_id": ge["id"],
|
|
"godown_id": "WH-IND-01",
|
|
"stack_number": "STACK-A1"
|
|
})
|
|
self.assertEqual(res_grn.get("status_code"), 201)
|
|
self.assertIn("GRN-26-", res_grn["grn"]["grn_number"])
|
|
self.assertIn("LOT-26-10-SOY-", res_grn["lot"]["lot_number"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|