109 lines
3.3 KiB
Python
109 lines
3.3 KiB
Python
import pytest
|
|
from pydantic import ValidationError
|
|
|
|
from warehouse_allocation.domain.models import AllocationRequest
|
|
from warehouse_allocation.domain.optimizer import rank_warehouses
|
|
|
|
|
|
def request_from(**overrides: object) -> AllocationRequest:
|
|
payload: dict[str, object] = {
|
|
"requestId": "optimizer-test",
|
|
"customerLocation": {"latitude": 35.7, "longitude": 51.4},
|
|
"items": [{"sku": "A", "quantity": 1}],
|
|
"transportationCosts": [
|
|
{
|
|
"warehouseId": "cheap",
|
|
"fixedCost": 100,
|
|
"distanceKm": 0,
|
|
"baseDeliveryMinutes": 120,
|
|
},
|
|
{
|
|
"warehouseId": "fast",
|
|
"fixedCost": 150,
|
|
"distanceKm": 0,
|
|
"baseDeliveryMinutes": 60,
|
|
},
|
|
],
|
|
}
|
|
payload.update(overrides)
|
|
return AllocationRequest.model_validate(payload)
|
|
|
|
|
|
def test_cost_weight_selects_cheaper_warehouse() -> None:
|
|
ranked = rank_warehouses(request_from(), {"cheap", "fast"})
|
|
|
|
assert ranked[0].warehouse_id == "cheap"
|
|
assert ranked[0].score == pytest.approx(70)
|
|
assert ranked[0].breakdown.feasible_warehouse_count == 2
|
|
|
|
|
|
def test_delivery_weight_selects_faster_warehouse() -> None:
|
|
request = request_from(weights={"cost": 0.2, "deliveryTime": 0.8})
|
|
|
|
ranked = rank_warehouses(request, {"cheap", "fast"})
|
|
|
|
assert ranked[0].warehouse_id == "fast"
|
|
|
|
|
|
def test_traffic_and_fleet_change_effective_time_and_cost() -> None:
|
|
request = request_from(
|
|
logisticsConstraints=[
|
|
{
|
|
"warehouseId": "cheap",
|
|
"trafficMultiplier": 2,
|
|
"fleetPriority": 0.5,
|
|
"handlingMinutes": 20,
|
|
}
|
|
]
|
|
)
|
|
|
|
ranked = rank_warehouses(request, {"cheap", "fast"})
|
|
|
|
assert ranked[0].warehouse_id == "fast"
|
|
cheap = next(candidate for candidate in ranked if candidate.warehouse_id == "cheap")
|
|
assert cheap.delivery_minutes == pytest.approx(520)
|
|
assert cheap.total_cost == pytest.approx(230)
|
|
|
|
|
|
def test_sla_overrun_adds_bounded_penalty() -> None:
|
|
ranked = rank_warehouses(
|
|
request_from(promisedDeliveryMinutes=30),
|
|
{"cheap", "fast"},
|
|
)
|
|
|
|
assert all(candidate.breakdown.sla_penalty == 0.25 for candidate in ranked)
|
|
|
|
|
|
def test_unavailable_and_infeasible_candidates_are_removed() -> None:
|
|
request = request_from(logisticsConstraints=[{"warehouseId": "cheap", "available": False}])
|
|
|
|
assert rank_warehouses(request, {"cheap"}) == []
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"override",
|
|
[
|
|
{"weights": {"cost": 0.4, "deliveryTime": 0.4}},
|
|
{"items": [{"sku": "A", "quantity": 1}, {"sku": "A", "quantity": 2}]},
|
|
{
|
|
"transportationCosts": [
|
|
{
|
|
"warehouseId": "duplicate",
|
|
"fixedCost": 1,
|
|
"distanceKm": 1,
|
|
"baseDeliveryMinutes": 1,
|
|
},
|
|
{
|
|
"warehouseId": "duplicate",
|
|
"fixedCost": 2,
|
|
"distanceKm": 1,
|
|
"baseDeliveryMinutes": 1,
|
|
},
|
|
]
|
|
},
|
|
],
|
|
)
|
|
def test_invalid_requests_are_rejected(override: dict[str, object]) -> None:
|
|
with pytest.raises(ValidationError):
|
|
request_from(**override)
|