File size: 5,323 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
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
"""GET /api/data-centers/decisions (#933): data center decisions, debated or passed without debate.

The DB layer is mocked: ``get_db_pool`` returns a fake pool that records SQL and params and
returns canned rows (counts for the counts query, items otherwise). The rows are test
stand-ins; which decisions are about a data center is decided in dbt (data_center_decision).
"""

from __future__ import annotations

import asyncio
from datetime import date
from typing import Any

import asyncpg
import httpx
import pytest
from fastapi import FastAPI

from api.routes import data_centers

PATH = "/api/data-centers/decisions"


class _Conn:
    def __init__(self, counts=None, items=None, error: Exception | None = None):
        self.counts = counts or []
        self.items = items or []
        self.error = error
        self.calls: list[tuple[str, tuple[Any, ...]]] = []

    async def fetch(self, sql: str, *params: Any):
        self.calls.append((sql, params))
        if self.error:
            raise self.error
        return self.counts if "count(DISTINCT" in sql else self.items


class _Acquire:
    def __init__(self, conn):
        self.conn = conn

    async def __aenter__(self):
        return self.conn

    async def __aexit__(self, *_):
        return None


class _Pool:
    def __init__(self, conn):
        self.conn = conn

    def acquire(self):
        return _Acquire(self.conn)


@pytest.fixture
def get(monkeypatch):
    def _get(conn: _Conn, params: dict[str, Any] | None = None) -> httpx.Response:
        async def pool():
            return _Pool(conn)

        monkeypatch.setattr(data_centers, "get_db_pool", pool)
        app = FastAPI()
        app.include_router(data_centers.router, prefix="/api")

        async def go():
            async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://t") as c:
                return await c.get(PATH, params=params)

        return asyncio.run(go())

    return _get


def _item(**kw) -> dict[str, Any]:
    base: dict[str, Any] = {
        "data_center_decision_id": "d1", "contested": True, "headline": "Rezoning for a data center campus",
        "outcome": "Approved", "votes_for": None, "votes_against": None, "vote_text": None,
        "meeting_date": date(2026, 3, 2), "jurisdiction_name": "Testville", "state_code": "AL",
        "video_url": "https://www.youtube.com/watch?v=vidA", "decision_url": "/decisions/d1",
    }
    base.update(kw)
    return base


def test_state_scope_counts_items_and_bound_params(get) -> None:
    conn = _Conn(
        counts=[{"contested": 1, "uncontested": 1, "meetings": 2}],
        items=[
            _item(),
            _item(data_center_decision_id="u1", contested=False, headline="Approve Data Center Ordinance",
                  vote_text="voice vote", decision_url=None),
        ],
    )
    resp = get(conn, {"state": "Alabama"})
    assert resp.status_code == 200, resp.text
    body = resp.json()
    assert body["scope"]["level"] == "state" and body["scope"]["state_code"] == "AL"
    assert body["available"] is True
    assert body["counts"] == {"contested": 1, "uncontested": 1, "meetings": 2}
    debated, consent = body["items"]
    assert debated["contested"] is True and debated["decision_url"] == "/decisions/d1"
    assert consent["contested"] is False and consent["vote_text"] == "voice vote"
    # an uncontested item's vote is never turned into numbers, and it has no detail page
    assert consent["votes_for"] is None and consent["votes_against"] is None and consent["decision_url"] is None
    counts_sql, params = conn.calls[0]
    assert "d.state_code = $1" in counts_sql and params == ("AL",)
    items_sql, item_params = conn.calls[1]
    assert item_params == ("AL", data_centers.DECISIONS_DEFAULT) and "LIMIT $2" in items_sql


def test_city_scope_filters_by_jurisdiction(get) -> None:
    conn = _Conn(counts=[{"contested": 0, "uncontested": 0, "meetings": 0}])
    body = get(conn, {"state": "GA", "city": "Fayetteville"}).json()
    assert body["scope"]["level"] == "city" and body["scope"]["city"] == "Fayetteville"
    sql, params = conn.calls[0]
    assert "d.jurisdiction_name ILIKE $2" in sql and params == ("GA", "%Fayetteville%")


def test_route_reads_the_mart_only() -> None:
    scope = data_centers.build_scope(None, None, None)
    counts_sql, params, items_sql, item_params = data_centers.build_decision_queries(scope, 5)
    assert "FROM data_center_decision d WHERE TRUE" in counts_sql and params == [] and item_params == [5]
    # the rule for "about a data center" lives in dbt, not here
    assert "~*" not in counts_sql + items_sql and "event_decision" not in counts_sql + items_sql


def test_missing_mart_is_unavailable_not_zero(get) -> None:
    resp = get(_Conn(error=asyncpg.exceptions.UndefinedTableError("no relation")), {"state": "AL"})
    assert resp.status_code == 200
    body = resp.json()
    assert body["available"] is False and body["scope"]["state_code"] == "AL"
    assert all(v is None for v in body["counts"].values())
    assert body["items"] == []


def test_limit_is_bounded(get) -> None:
    assert get(_Conn(), {"limit": data_centers.DECISIONS_MAX + 1}).status_code == 422


def test_other_errors_are_500(get) -> None:
    assert get(_Conn(error=RuntimeError("boom"))).status_code == 500