Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 4,779 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 145 146 147 148 149 150 151 152 153 | """GET /api/topics/{topic_id}/state-map (#1127): meetings per state plus the AI reading behind a topic's box map.
The DB layer is mocked: ``get_db_pool`` returns a fake pool that routes each SQL to canned rows. The rows are test
stand-ins for the shapes the marts return, not warehouse figures.
"""
from __future__ import annotations
import asyncio
from typing import Any
import asyncpg
import httpx
import pytest
from fastapi import FastAPI
from api.routes import topics
PATH = "/api/topics/{}/state-map"
MEETINGS = [
{"state_code": "TX", "state": "Texas", "meetings": 12},
{"state_code": "WI", "state": "Wisconsin", "meetings": 5},
]
LEAN = [
{
"state_code": "TX",
"lean_meetings": 6,
"public_against_meetings": 3,
"public_for_meetings": 1,
"silent_meetings": 1,
"no_side_meetings": 1,
"disagreement_meetings": 1,
}
]
class _Conn:
def __init__(self, lean: list[dict[str, Any]] | None, lean_error: bool = False):
self.lean = lean
self.lean_error = lean_error
self.calls: list[tuple[str, tuple[Any, ...]]] = []
async def fetch(self, sql: str, *params: Any):
self.calls.append((sql, params))
if "topic_meeting_lean" in sql:
if self.lean_error:
raise asyncpg.exceptions.UndefinedTableError('relation "topic_meeting_lean" does not exist')
return self.lean or []
if "topic_transcript_link" in sql:
return [m for m in MEETINGS if params[1] is None or m["state_code"] == params[1]]
return []
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, topic_id: int, params: dict[str, Any] | None = None) -> httpx.Response:
async def pool():
return _Pool(conn)
monkeypatch.setattr(topics, "get_db_pool", pool)
app = FastAPI()
app.include_router(topics.router)
async def go():
async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://t") as c:
return await c.get(PATH.format(topic_id), params=params or {})
return asyncio.run(go())
return _get
def test_merges_the_ai_reading_into_each_state(get):
r = get(_Conn(LEAN), 100006)
assert r.status_code == 200
body = r.json()
assert body["available"] is True
tx, wi = body["by_state"]
assert tx == {
"state_code": "TX",
"state": "Texas",
"meetings": 12,
"lean_meetings": 6,
"public_against_meetings": 3,
"public_for_meetings": 1,
"no_side_meetings": 1,
"silent_meetings": 1,
"disagreement_meetings": 1,
}
# A state with meetings but no reading keeps its meetings and reads 0, not missing.
assert wi["meetings"] == 5 and wi["lean_meetings"] == 0 and wi["public_against_meetings"] == 0
def test_missing_lean_table_means_meetings_only_not_an_error(get):
r = get(_Conn(None, lean_error=True), 100006)
assert r.status_code == 200
body = r.json()
assert body["available"] is False
assert [s["meetings"] for s in body["by_state"]] == [12, 5]
assert all(s["lean_meetings"] == 0 for s in body["by_state"])
def test_no_reading_for_the_topic_is_not_available(get):
r = get(_Conn([]), 100006)
assert r.json()["available"] is False
def test_state_scope_is_passed_to_both_queries(get):
conn = _Conn(LEAN)
r = get(conn, 100006, {"state": " tx "})
assert r.status_code == 200
assert [s["state_code"] for s in r.json()["by_state"]] == ["TX"]
assert all(params == (100006, "TX") for _, params in conn.calls)
def test_lean_sql_reads_the_topic_and_counts_meetings():
sql = topics._TOPIC_LEAN_BY_STATE_SQL
assert "FROM topic_meeting_lean l" in sql
assert "l.topic_id = $1" in sql
assert "officials_sided_against_public" in sql
def test_silent_meetings_are_missing_not_split(get):
"""#1200: a meeting read where the public didn't speak is counted as silent, never as split."""
lean = [{**LEAN[0], "lean_meetings": 10, "public_against_meetings": 0, "public_for_meetings": 0, "silent_meetings": 10, "no_side_meetings": 0}]
tx = get(_Conn(lean), 100005).json()["by_state"][0]
assert tx["silent_meetings"] == 10 and tx["no_side_meetings"] == 0
def test_lean_sql_counts_silent_meetings():
assert "AS silent_meetings" in topics._TOPIC_LEAN_BY_STATE_SQL
assert "did_not_speak" in topics._TOPIC_LEAN_BY_STATE_SQL
|