Files
ict-warehouse-allocation/tests/integration/test_api.py
T
2026-07-23 07:40:44 +03:30

109 lines
3.9 KiB
Python

import httpx
from warehouse_allocation.api_examples import DEMO_SKU, DEMO_WAREHOUSE_IDS
from warehouse_allocation.infrastructure.inventory import InMemoryInventoryRepository
async def seed(repository: InMemoryInventoryRepository) -> None:
await repository.set_levels("tehran-west", {"SKU-RED-42": 10})
await repository.set_levels("karaj", {"SKU-RED-42": 10})
async def test_allocation_contract_and_selection(
client: httpx.AsyncClient,
inventory: InMemoryInventoryRepository,
allocation_payload: dict[str, object],
) -> None:
await seed(inventory)
response = await client.post("/v1/allocations", json=allocation_payload)
assert response.status_code == 200, response.text
assert response.headers["x-process-time-ms"]
body = response.json()
assert body["requestId"] == "order-1001"
assert body["warehouseId"] == "tehran-west"
assert body["estimatedDeliveryTime"] == 75
assert body["totalCost"] == 116.11
assert 0 <= body["routeOptimizationScore"] <= 100
assert body["scoreBreakdown"]["feasibleWarehouseCount"] == 2
async def test_reservation_decrements_inventory(
client: httpx.AsyncClient,
inventory: InMemoryInventoryRepository,
allocation_payload: dict[str, object],
) -> None:
await inventory.set_levels("tehran-west", {"SKU-RED-42": 2})
await inventory.set_levels("karaj", {"SKU-RED-42": 0})
allocation_payload["reserveInventory"] = True
first = await client.post("/v1/allocations", json=allocation_payload)
second = await client.post("/v1/allocations", json=allocation_payload)
assert first.status_code == 200, first.text
assert first.json()["inventoryReserved"] is True
assert second.status_code == 409
assert second.json()["error"]["code"] == "NO_FEASIBLE_WAREHOUSE"
async def test_no_single_warehouse_can_split_fulfillment(
client: httpx.AsyncClient,
inventory: InMemoryInventoryRepository,
allocation_payload: dict[str, object],
) -> None:
await inventory.set_levels("tehran-west", {"SKU-RED-42": 1})
await inventory.set_levels("karaj", {"SKU-RED-42": 1})
response = await client.post("/v1/allocations", json=allocation_payload)
assert response.status_code == 409
async def test_inventory_upsert_and_health_endpoints(client: httpx.AsyncClient) -> None:
upsert = await client.put(
"/v1/inventory",
json={"warehouseId": "w1", "levels": [{"sku": "A", "quantity": 5}]},
)
live = await client.get("/health/live")
ready = await client.get("/health/ready")
metrics = await client.get("/metrics")
assert upsert.status_code == 204, upsert.text
assert live.json()["status"] == "ok"
assert ready.json()["inventory"] == "ok"
assert "warehouse_allocation_requests_total" in metrics.text
async def test_validation_error_is_structured(
client: httpx.AsyncClient, allocation_payload: dict[str, object]
) -> None:
allocation_payload["customerLocation"] = {"latitude": 200, "longitude": 51}
response = await client.post("/v1/allocations", json=allocation_payload)
assert response.status_code == 422
assert response.json()["detail"]
async def test_openapi_demo_example_is_runnable_and_non_reserving(
client: httpx.AsyncClient,
inventory: InMemoryInventoryRepository,
) -> None:
for warehouse_id in DEMO_WAREHOUSE_IDS:
await inventory.set_levels(warehouse_id, {DEMO_SKU: 10})
openapi = (await client.get("/openapi.json")).json()
media_type = openapi["paths"]["/v1/allocations"]["post"]["requestBody"]["content"][
"application/json"
]
example = media_type["examples"]["demoQuote"]["value"]
first = await client.post("/v1/allocations", json=example)
second = await client.post("/v1/allocations", json=example)
assert first.status_code == 200
assert second.status_code == 200
assert first.json()["inventoryReserved"] is False
assert first.json()["warehouseId"] in DEMO_WAREHOUSE_IDS