Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 5,644 Bytes
d15f07c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | """Tests for api/routes/benchmark.py (#1393): peer selection, ranking, endpoint with a fake DB."""
from __future__ import annotations
import math
import pytest
from fastapi import HTTPException
from api.routes import benchmark
def _c(i, state, has, pop_ratio):
return {"jurisdiction_id": i, "state_code": state, "has_data": has, "pop_distance": abs(math.log(pop_ratio))}
def test_select_peers_prefers_same_state_then_data_then_closeness():
cands = [
_c("a", "GA", True, 1.0),
_c("b", "AL", False, 1.5),
_c("c", "AL", True, 1.8),
_c("d", "AL", True, 1.1),
_c("e", "MS", True, 1.0),
]
got = [c["jurisdiction_id"] for c in benchmark.select_peers(cands, "AL", 4)]
assert got == ["d", "c", "b", "a"]
def test_rank_among_ignores_null_and_missing_target():
vals = {"t": 5.0, "a": 9.0, "b": None, "c": 1.0}
assert benchmark.rank_among(vals, "t") == {"rank": 2, "of": 3}
assert benchmark.rank_among(vals, "b") is None
def test_region_of():
assert benchmark.region_of("AL") == "South"
assert benchmark.region_of("MA") == "Northeast"
assert benchmark.region_of("ZZ") is None
def test_every_metric_has_required_sql_params():
for spec in benchmark.METRICS.values():
assert "$1" in spec.values_sql and "$2" in spec.values_sql
assert spec.source
class FakeConn:
def __init__(self):
self.jur = {
"T": {"jurisdiction_id": "T", "name": "Town", "jurisdiction_type": "city", "state_code": "AL",
"state_name": "Alabama", "population": 100000},
"P1": {"jurisdiction_id": "P1", "name": "Peer One", "jurisdiction_type": "city", "state_code": "AL",
"state_name": "Alabama", "population": 90000},
"P2": {"jurisdiction_id": "P2", "name": "Peer Two", "jurisdiction_type": "city", "state_code": "GA",
"state_name": "Georgia", "population": 120000},
}
async def execute(self, *a):
return None
async def fetchrow(self, sql, jid):
return self.jur.get(jid)
async def fetch(self, sql, *args):
if "FROM jurisdictions" in sql:
if isinstance(args[0], list):
return [self.jur[i] for i in args[0] if i in self.jur]
return [self.jur["P1"], self.jur["P2"]]
ids = args[0]
vals = {"T": 1_000_000.0, "P1": 450_000.0}
return [{"jurisdiction_id": i, "value": v, "as_of": None} for i, v in vals.items() if i in ids]
class FakePool:
def __init__(self, conn):
self.conn = conn
def acquire(self):
conn = self.conn
class _Ctx:
async def __aenter__(self_):
return conn
async def __aexit__(self_, *a):
return False
return _Ctx()
@pytest.fixture
def fake_pool(monkeypatch):
async def _get():
return FakePool(FakeConn())
monkeypatch.setattr(benchmark, "get_db_pool", _get)
@pytest.mark.asyncio
async def test_endpoint_auto_peers_null_for_missing_data(fake_pool):
res = await benchmark.benchmark("federal_cdbg", jurisdiction_id="T", peers="auto", n=5, fiscal_year=None)
assert res.jurisdiction.per_capita == 10.0
by = {p.jurisdiction_id: p for p in res.peers}
assert by["P1"].per_capita == 5.0
assert by["P2"].value is None and by["P2"].per_capita is None # never 0
assert res.rank_per_capita == {"rank": 1, "of": 2}
assert res.peer_median_per_capita == 5.0
assert res.fiscal_year is None
@pytest.mark.asyncio
async def test_endpoint_explicit_peers_and_year_is_string(fake_pool):
res = await benchmark.benchmark("federal_total", jurisdiction_id="T", peers="P1", n=5, fiscal_year=2023)
assert [p.jurisdiction_id for p in res.peers] == ["P1"]
assert res.fiscal_year == "2023"
@pytest.mark.asyncio
async def test_unknown_metric_and_jurisdiction(fake_pool):
with pytest.raises(HTTPException) as e1:
await benchmark.benchmark("nope", jurisdiction_id="T", peers="auto", n=5, fiscal_year=None)
assert e1.value.status_code == 404
with pytest.raises(HTTPException) as e2:
await benchmark.benchmark("federal_cdbg", jurisdiction_id="ZZ", peers="auto", n=5, fiscal_year=None)
assert e2.value.status_code == 404
@pytest.mark.asyncio
async def test_rate_metric_has_no_per_capita_but_ranks_on_value(fake_pool):
res = await benchmark.benchmark("avg_pay_all_functions", jurisdiction_id="T", peers="P1", n=5, fiscal_year=None)
assert res.jurisdiction.per_capita is None and res.peers[0].per_capita is None
assert res.rank_per_capita is None
assert res.rank_value == {"rank": 1, "of": 2}
assert res.peer_median_value == 450_000.0
def test_payroll_metrics_read_the_served_slice_and_are_aggregate_only():
for key in ("payroll_total", "payroll_police", "avg_pay_all_functions"):
spec = benchmark.METRICS[key]
assert "jurisdiction_public_payroll_latest" in spec.values_sql
assert "Census" in spec.source
def test_opioid_metric_is_null_without_a_source_not_zero():
spec = benchmark.METRICS["opioid_settlement_spending"]
assert "FROM opioid_settlement_spending" in spec.values_sql
assert "HAVING sum(expenditure_dollars) IS NOT NULL" in spec.values_sql # unreported stays null
assert "Michigan" in spec.source
def test_cdbg_metrics_read_their_marts():
assert "FROM cdbg_allocation" in benchmark.METRICS["cdbg_allocation"].values_sql
share = benchmark.METRICS["cdbg_public_services_share"]
assert "cdbg_activity_summary" in share.values_sql and share.per_capita is False
assert share.unit == "percent"
|