File size: 1,334 Bytes
a8fb296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""GET /api/stats serves `governments` from governments_count (#351).

`fetch_stats_from_neon` is patched with canned aggregate rows (no database).
"""

from __future__ import annotations

import asyncio
from typing import Any

import pytest

from api.routes import stats_neon


@pytest.fixture(autouse=True)
def _clear_cache():
    stats_neon.STATS_CACHE.clear()
    yield
    stats_neon.STATS_CACHE.clear()


def _stats(monkeypatch, row: dict[str, Any] | None) -> dict[str, Any]:
    async def fake_fetch(level, state=None, county=None, city=None):
        return row

    monkeypatch.setattr(stats_neon, "fetch_stats_from_neon", fake_fetch)
    return asyncio.run(stats_neon.get_stats(state=None, county=None, city=None))


def test_governments_comes_from_governments_count(monkeypatch):
    out = _stats(
        monkeypatch,
        {"jurisdictions_count": 70, "school_districts_count": 10, "governments_count": 40},
    )
    assert out["governments"] == 40
    assert out["jurisdictions"] == 70


def test_governments_is_null_when_the_serving_table_predates_the_column(monkeypatch):
    out = _stats(monkeypatch, {"jurisdictions_count": 70, "school_districts_count": 10})
    assert out["governments"] is None


def test_governments_is_null_when_no_row(monkeypatch):
    assert _stats(monkeypatch, None)["governments"] is None