Files
Warehouse-Digital-Twin-and-…/tests/unit/test_inventory_model.py
T

126 lines
3.7 KiB
Python

"""Unit tests for inventory domain models, stock reservation, and catalog manager."""
from __future__ import annotations
from pathlib import Path
import pytest
from warehouse_twin.config.loader import ConfigLoader
from warehouse_twin.domain.inventory import (
InsufficientStockError,
InventoryCatalog,
InventoryItem,
)
def test_inventory_item_reservation_lifecycle():
"""Verify stock reservation, consumption, and release."""
item = InventoryItem(
item_id="filter_hf1",
name="Oil Filter",
category="Zone A Fast",
zone="zone_a",
unit_weight_kg=2.5,
initial_stock=100,
current_stock=100,
minimum_stock=20,
)
assert item.available_stock == 100
assert item.needs_replenishment() is False
# Reserve 30 units
item.reserve(30)
assert item.reserved_stock == 30
assert item.current_stock == 100
assert item.available_stock == 70
# Consume 20 of the reserved units
item.consume(20)
assert item.reserved_stock == 10
assert item.current_stock == 80
assert item.available_stock == 70
# Release remaining 10 reserved units
item.release_reservation(10)
assert item.reserved_stock == 0
assert item.available_stock == 80
def test_insufficient_stock_errors():
"""Verify InsufficientStockError when exceeding available or current quantities."""
item = InventoryItem(
item_id="item_scarce",
name="Scarce Part",
category="Specialized",
zone="zone_b",
unit_weight_kg=10.0,
initial_stock=5,
current_stock=5,
minimum_stock=2,
)
# Exceed reservation
with pytest.raises(InsufficientStockError) as exc_info:
item.reserve(10)
assert "Only 5 available" in str(exc_info.value)
# Exceed consumption
with pytest.raises(InsufficientStockError):
item.consume(15)
def test_replenishment_trigger_and_restocking():
"""Verify needs_replenishment detection and stock restocking."""
item = InventoryItem(
item_id="belt_bd10",
name="V-Belt",
category="Zone A Fast",
zone="zone_a",
unit_weight_kg=1.0,
initial_stock=50,
current_stock=50,
minimum_stock=15,
replenishment_batch=30,
)
assert item.needs_replenishment() is False
# Consume down to threshold
item.reserve(35)
item.consume(35)
assert item.current_stock == 15
assert item.needs_replenishment() is True
# Replenish with batch
item.replenish(item.replenishment_batch)
assert item.current_stock == 45
assert item.needs_replenishment() is False
def test_inventory_catalog_loading(project_config_dir: Path):
"""Verify InventoryCatalog loads real project inventory configuration."""
loader = ConfigLoader(project_config_dir)
bundle = loader.load_all()
catalog = InventoryCatalog.from_config_dict(bundle.inventory)
assert catalog.total_sku_count() == 13
# Check Zone A items
zone_a_items = catalog.get_items_by_zone("zone_a")
assert len(zone_a_items) == 5
assert any(i.item_id == "filter_hydraulic_hf1" for i in zone_a_items)
assert any(i.item_id == "belt_drive_bd10" for i in zone_a_items)
# Check Zone B items
zone_b_items = catalog.get_items_by_zone("zone_b")
assert len(zone_b_items) == 4
assert any(i.item_id == "transmission_combine_ct8" for i in zone_b_items)
assert any(i.item_id == "structural_chassis_frame_sf3" for i in zone_b_items)
# Check Zone C items
zone_c_items = catalog.get_items_by_zone("zone_c")
assert len(zone_c_items) == 4
assert any(i.item_id == "raw_steel_coil_sc5" for i in zone_c_items)
assert any(i.item_id == "electronic_control_module_ecm9" for i in zone_c_items)