Build artifacts/prezero_priority.json from the anonymized glass fill-level export (11,701 rows, 449 containers, 1,338 location/material series). The contract tier rule yields high 88, medium 248, low 113 with 1,192 overfull readings.
Add /prezero/summary, /prezero/priority, and /prezero/containers/{id} to the internal FastAPI service, with startup validation, filtering/pagination, recent reading history, and 503 isolation when the artifact is missing. Raw CSV stays gitignored; the derived 1.1 MB artifact is the source of truth. This is a priority proof, not a route: the dataset has no coordinates.
336 lines
12 KiB
Python
336 lines
12 KiB
Python
from __future__ import annotations
|
|
|
|
import copy
|
|
import json
|
|
from collections import Counter
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from pulsecast import config
|
|
from pulsecast.prezero import (
|
|
CONTAINER_KEYS,
|
|
MATERIAL_KEYS,
|
|
META_KEYS,
|
|
PRIORITIES,
|
|
READING_KEYS,
|
|
PrezeroArtifactError,
|
|
PrezeroStore,
|
|
priority_tier,
|
|
)
|
|
|
|
EXPECTED = {
|
|
"rows": 11_701,
|
|
"containers": 449,
|
|
"material_series": 1_338,
|
|
"overfull_readings": 1_192,
|
|
"high": 88,
|
|
"medium": 248,
|
|
"low": 113,
|
|
"date_from": "2025-10-17",
|
|
"date_to": "2026-09-30",
|
|
}
|
|
PUBLIC_KEYS = tuple(key for key in CONTAINER_KEYS if key != "recent_readings")
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def artifact() -> dict:
|
|
return json.loads(config.prezero_artifact().read_text(encoding="utf-8"))
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def store(artifact) -> PrezeroStore:
|
|
return PrezeroStore.from_payload(artifact)
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def summary(client) -> dict:
|
|
response = client.get("/prezero/summary")
|
|
assert response.status_code == 200, response.text
|
|
return response.json()
|
|
|
|
|
|
# --------------------------------------------------------------------------- rule
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("overfull_count", "overfull_rate", "expected"),
|
|
[
|
|
(0, 0.0, "low"),
|
|
(1, 0.01, "medium"),
|
|
(4, 0.99, "high"),
|
|
(2, 0.25, "high"),
|
|
(2, 0.2499, "medium"),
|
|
(5, 0.0, "high"),
|
|
(1, 1.0, "medium"),
|
|
],
|
|
)
|
|
def test_priority_rule(overfull_count, overfull_rate, expected):
|
|
assert priority_tier(overfull_count, overfull_rate) == expected
|
|
|
|
|
|
# --------------------------------------------------------------------------- summary
|
|
|
|
|
|
def test_summary_counts(summary):
|
|
assert set(summary) == {"meta", "summary"}
|
|
meta = summary["meta"]
|
|
for key in META_KEYS:
|
|
assert key in meta, key
|
|
assert meta["rows"] == EXPECTED["rows"]
|
|
assert meta["containers"] == EXPECTED["containers"]
|
|
assert meta["material_series"] == EXPECTED["material_series"]
|
|
assert meta["overfull_readings"] == EXPECTED["overfull_readings"]
|
|
assert meta["date_from"] == EXPECTED["date_from"]
|
|
assert meta["date_to"] == EXPECTED["date_to"]
|
|
assert meta["reference_date"] == EXPECTED["date_to"]
|
|
assert meta["source"] == "glass_fill_levels_anonymized_f.csv"
|
|
assert "overfull_count >= 5" in meta["priority_rule"]
|
|
assert any("not a route" in line for line in meta["limitations"])
|
|
|
|
counts = summary["summary"]["priority_counts"]
|
|
assert counts == {
|
|
"high": EXPECTED["high"],
|
|
"medium": EXPECTED["medium"],
|
|
"low": EXPECTED["low"],
|
|
}
|
|
assert summary["summary"]["by_material"] == {
|
|
"white": {"readings": 4_432, "overfull": 731},
|
|
"green": {"readings": 4_169, "overfull": 357},
|
|
"brown": {"readings": 3_100, "overfull": 104},
|
|
}
|
|
|
|
|
|
# --------------------------------------------------------------------------- priority
|
|
|
|
|
|
def test_priority_defaults_and_filter_totals(client, summary):
|
|
payload = client.get("/prezero/priority").json()
|
|
assert set(payload) == {"meta", "total", "count", "offset", "limit", "containers"}
|
|
assert payload["meta"] == summary["meta"]
|
|
assert payload["total"] == EXPECTED["containers"]
|
|
assert payload["limit"] == 50
|
|
assert payload["offset"] == 0
|
|
assert payload["count"] == 50
|
|
assert all(container["priority"] == "high" for container in payload["containers"])
|
|
for priority in PRIORITIES:
|
|
filtered = client.get("/prezero/priority", params={"priority": priority}).json()
|
|
assert filtered["total"] == EXPECTED[priority]
|
|
assert filtered["count"] == min(50, EXPECTED[priority])
|
|
assert all(row["priority"] == priority for row in filtered["containers"])
|
|
|
|
|
|
def test_priority_limit_offset_and_count(client, store, artifact):
|
|
payload = client.get(
|
|
"/prezero/priority", params={"priority": "high", "limit": 3}
|
|
).json()
|
|
assert payload["total"] == EXPECTED["high"]
|
|
assert payload["count"] == 3
|
|
high_ids = [
|
|
row["location_id"]
|
|
for row in artifact["containers"]
|
|
if row["priority"] == "high"
|
|
]
|
|
expected_ids = high_ids[:3]
|
|
assert [row["location_id"] for row in payload["containers"]] == expected_ids
|
|
for row in payload["containers"]:
|
|
assert set(row) == set(PUBLIC_KEYS)
|
|
assert "recent_readings" not in row
|
|
|
|
paged = client.get(
|
|
"/prezero/priority",
|
|
params={"priority": "high", "limit": 2, "offset": 2},
|
|
).json()
|
|
assert paged["total"] == EXPECTED["high"]
|
|
assert paged["count"] == 2
|
|
assert paged["offset"] == 2
|
|
assert [row["location_id"] for row in paged["containers"]] == high_ids[2:4]
|
|
|
|
beyond = client.get(
|
|
"/prezero/priority", params={"priority": "high", "limit": 10, "offset": 88}
|
|
).json()
|
|
assert beyond["total"] == EXPECTED["high"]
|
|
assert beyond["count"] == 0
|
|
assert beyond["containers"] == []
|
|
|
|
|
|
def test_priority_is_sorted_by_contract(client, store):
|
|
payload = client.get("/prezero/priority", params={"limit": 500}).json()
|
|
assert payload["total"] == EXPECTED["containers"]
|
|
assert payload["count"] == EXPECTED["containers"]
|
|
rows = payload["containers"]
|
|
assert [row["location_id"] for row in rows] == [
|
|
container["location_id"] for container in store.containers
|
|
]
|
|
rank = {priority: index for index, priority in enumerate(PRIORITIES)}
|
|
keys = [
|
|
(
|
|
rank[row["priority"]],
|
|
-row["overfull_count"],
|
|
-row["overfull_rate"],
|
|
row["days_since_last"],
|
|
row["location_id"],
|
|
)
|
|
for row in rows
|
|
]
|
|
assert keys == sorted(keys)
|
|
|
|
|
|
# --------------------------------------------------------------------------- container
|
|
|
|
|
|
def test_known_container_and_recent_readings(client, artifact):
|
|
known = next(row for row in artifact["containers"] if row["priority"] == "high")
|
|
location_id = known["location_id"]
|
|
response = client.get(f"/prezero/containers/{location_id}")
|
|
assert response.status_code == 200, response.text
|
|
payload = response.json()
|
|
|
|
assert set(payload) == {"container", "readings"}
|
|
container = payload["container"]
|
|
assert set(container) == set(PUBLIC_KEYS)
|
|
for key in PUBLIC_KEYS:
|
|
assert container[key] == known[key]
|
|
assert len(payload["readings"]) == min(10, known["readings"])
|
|
dates = [reading["date"] for reading in payload["readings"]]
|
|
assert dates == sorted(dates, reverse=True)
|
|
for reading in payload["readings"]:
|
|
assert set(reading) == set(READING_KEYS)
|
|
assert isinstance(reading["fill_level_perc"], str)
|
|
assert reading["is_overfull"] is (reading["fill_level_perc"] == "overfull")
|
|
|
|
|
|
def test_sparse_container_uses_null_numeric_fields(client, artifact):
|
|
sparse = next(row for row in artifact["containers"] if row["numeric_count"] == 0)
|
|
payload = client.get(f"/prezero/containers/{sparse['location_id']}").json()
|
|
assert payload["container"]["p95_fill"] is None
|
|
assert payload["container"]["max_fill"] is None
|
|
assert payload["container"]["mean_fill"] is None
|
|
assert payload["container"]["trend"] is None
|
|
assert payload["container"]["last_is_overfull"] is True
|
|
assert payload["container"]["last_fill"] is None
|
|
|
|
|
|
def test_unknown_container_404(client):
|
|
response = client.get("/prezero/containers/does-not-exist")
|
|
assert response.status_code == 404
|
|
assert response.json() == {
|
|
"error": {
|
|
"code": "not_found",
|
|
"message": "unknown location: does-not-exist",
|
|
}
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("params", "message"),
|
|
[
|
|
({"priority": "urgent"}, "priority must be one of high, medium, low"),
|
|
({"priority": "HIGH"}, "priority must be one of high, medium, low"),
|
|
({"limit": 0}, "limit must be between 1 and 500"),
|
|
({"limit": 501}, "limit must be between 1 and 500"),
|
|
({"offset": -1}, "offset must be >= 0"),
|
|
],
|
|
)
|
|
def test_invalid_queries_400(client, params, message):
|
|
response = client.get("/prezero/priority", params=params)
|
|
assert response.status_code == 400
|
|
assert response.json() == {"error": {"code": "bad_request", "message": message}}
|
|
|
|
|
|
def test_non_integer_query_400(client):
|
|
response = client.get("/prezero/priority", params={"limit": "many"})
|
|
assert response.status_code == 400
|
|
assert response.json()["error"]["code"] == "bad_request"
|
|
|
|
|
|
# --------------------------------------------------------------------------- artifact
|
|
|
|
|
|
def test_artifact_schema(artifact, store):
|
|
assert set(artifact) == {"meta", "summary", "containers"}
|
|
meta = artifact["meta"]
|
|
assert meta["rows"] == EXPECTED["rows"]
|
|
assert meta["containers"] == EXPECTED["containers"]
|
|
assert meta["material_series"] == EXPECTED["material_series"]
|
|
assert meta["overfull_readings"] == EXPECTED["overfull_readings"]
|
|
assert meta["date_from"] == EXPECTED["date_from"]
|
|
assert meta["date_to"] == EXPECTED["date_to"]
|
|
assert meta["reference_date"] == EXPECTED["date_to"]
|
|
|
|
containers = artifact["containers"]
|
|
assert len(containers) == EXPECTED["containers"]
|
|
assert len(store.containers) == EXPECTED["containers"]
|
|
assert len({row["location_id"] for row in containers}) == EXPECTED["containers"]
|
|
assert Counter(row["priority"] for row in containers) == {
|
|
"high": EXPECTED["high"],
|
|
"medium": EXPECTED["medium"],
|
|
"low": EXPECTED["low"],
|
|
}
|
|
|
|
for row in containers:
|
|
assert set(row) == set(CONTAINER_KEYS)
|
|
assert row["last_date"].count("-") == 2
|
|
assert len(row["recent_readings"]) == min(10, row["readings"])
|
|
assert row["readings"] == sum(
|
|
material["readings"] for material in row["materials"].values()
|
|
)
|
|
assert row["overfull_count"] == sum(
|
|
material["overfull_count"] for material in row["materials"].values()
|
|
)
|
|
for material in row["materials"].values():
|
|
assert set(material) == set(MATERIAL_KEYS)
|
|
|
|
material_totals = Counter()
|
|
material_overfull = Counter()
|
|
for row in containers:
|
|
for name, material in row["materials"].items():
|
|
material_totals[name] += material["readings"]
|
|
material_overfull[name] += material["overfull_count"]
|
|
assert material_totals == {
|
|
"white": 4_432,
|
|
"green": 4_169,
|
|
"brown": 3_100,
|
|
}
|
|
assert material_overfull == {"white": 731, "green": 357, "brown": 104}
|
|
assert sum(row["readings"] for row in containers) == EXPECTED["rows"]
|
|
assert sum(row["overfull_count"] for row in containers) == EXPECTED[
|
|
"overfull_readings"
|
|
]
|
|
|
|
|
|
def test_artifact_rejects_inconsistent_tier(artifact):
|
|
payload = copy.deepcopy(artifact)
|
|
payload["containers"][0]["priority"] = "urgent"
|
|
with pytest.raises(PrezeroArtifactError, match="priority"):
|
|
PrezeroStore.from_payload(payload)
|
|
|
|
|
|
def test_artifact_rejects_wrong_summary_count(artifact):
|
|
payload = copy.deepcopy(artifact)
|
|
payload["summary"]["priority_counts"]["high"] += 1
|
|
with pytest.raises(PrezeroArtifactError, match="priority_counts"):
|
|
PrezeroStore.from_payload(payload)
|
|
|
|
|
|
def test_missing_artifact_returns_503(tmp_path, monkeypatch):
|
|
monkeypatch.setenv(
|
|
"PULSECAST_PREZERO_ARTIFACT", str(Path(tmp_path) / "missing.json")
|
|
)
|
|
from pulsecast.server import create_app
|
|
|
|
with TestClient(create_app()) as isolated:
|
|
assert isolated.get("/healthz").status_code == 200
|
|
assert isolated.get("/readyz").status_code == 200
|
|
assert isolated.get("/meta").status_code == 200
|
|
assert isolated.get("/bootstrap").status_code == 200
|
|
for path in (
|
|
"/prezero/summary",
|
|
"/prezero/priority",
|
|
"/prezero/containers/anything",
|
|
):
|
|
response = isolated.get(path)
|
|
assert response.status_code == 503, path
|
|
assert response.json()["error"]["code"] == "unavailable"
|