File size: 12,382 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"""GET /api/topics/{topic}/caption-facts (#1125): the real counts behind a topic caption.

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 topic_caption_facts as tcf

PATH = "/api/topics/{}/caption-facts"


class _Conn:
    def __init__(self, routes: dict[str, Any], fail: tuple[str, ...] = ()):
        self.routes = routes
        self.fail = fail
        self.calls: list[tuple[str, tuple[Any, ...]]] = []

    async def fetch(self, sql: str, *params: Any):
        self.calls.append((sql, params))
        for marker in self.fail:
            if marker in sql:
                raise asyncpg.exceptions.UndefinedTableError(f'relation "{marker}" does not exist')
        for marker, rows in self.routes.items():
            if marker in sql:
                return rows(params) if callable(rows) else rows
        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: str, params: dict[str, Any] | None = None) -> httpx.Response:
        async def pool():
            return _Pool(conn)

        monkeypatch.setattr(tcf, "get_db_pool", pool)
        app = FastAPI()
        app.include_router(tcf.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), params=params or {})

        return asyncio.run(go())

    return _get


POPS = [
    {"state_code": "CA", "state": "California", "population": 39_000_000},
    {"state_code": "NC", "state": "North Carolina", "population": 10_000_000},
    {"state_code": "FL", "state": "Florida", "population": 21_000_000},
]
TOPIC_STATES = [
    {"state_code": "CA", "state": None, "meetings": 570},
    {"state_code": "NC", "state": None, "meetings": 317},
    {"state_code": "FL", "state": None, "meetings": 40},
]


def topic_conn(**extra) -> _Conn:
    routes = {
        "FROM civicsearch_topic": [{"topic_id": 100003, "query_id": "short-term-rentals"}],
        "FROM browse_transcript_count": [{"n": 4186}],
        "FROM browse_entity_state_transcript_count": TOPIC_STATES,
        "FROM browse_entity_place_transcript_count": [
            {"name": "Orlando", "meetings": 13},
            {"name": "Orlando Utilities", "meetings": 20},
            {"name": "Titusville", "meetings": 9},
        ],
        "FROM jurisdictions": POPS,
    }
    routes.update(extra.pop("routes", {}))
    return _Conn(routes, **extra)


def test_national_topic_counts_and_ratio(get):
    conn = topic_conn(fail=("topic_meeting_lean",))
    r = get(conn, "short-term-rentals")
    assert r.status_code == 200
    d = r.json()
    assert d["topic"] == "short-term-rentals"
    assert d["scope"]["level"] == "national"
    assert d["meetings"] == 4186
    assert [s["state_code"] for s in d["states"]] == ["CA", "NC", "FL"]
    assert d["states"][0]["state"] == "California"
    assert d["states"][0]["population"] == 39_000_000
    assert d["top_state"] == "CA"
    assert d["ratio_to_next"] == pytest.approx(1.8, abs=0.01)
    # Not data centers: no speakers; topic_meeting_lean missing -> every AI reading is null, not 0.
    assert d["speakers"] is None
    assert d["public_against_meetings"] is None and d["disagreement_meetings"] is None
    assert any("topic_meeting_lean" in sql for sql, _ in conn.calls)


def test_state_scope_reads_the_state_from_the_national_list(get):
    r = get(topic_conn(), "short-term-rentals", {"state": "fl"})
    d = r.json()
    assert d["scope"] == {"level": "state", "state_code": "FL", "state": "Florida", "city": None}
    assert d["meetings"] == 40
    # The state list is never narrowed: the state can be ranked.
    assert len(d["states"]) == 3


def test_city_scope_prefers_the_exact_place(get):
    r = get(topic_conn(), "short-term-rentals", {"state": "FL", "city": "orlando"})
    d = r.json()
    assert d["scope"]["level"] == "city"
    assert d["place"] == "Orlando"
    assert d["meetings"] == 13
    assert [p["name"] for p in d["places"]] == ["Orlando", "Orlando Utilities", "Titusville"]


def test_city_without_state_is_ignored(get):
    d = get(topic_conn(), "short-term-rentals", {"city": "Orlando"}).json()
    assert d["scope"]["level"] == "national"
    assert d["places"] is None


def test_topic_meeting_lean_lights_up_when_present(get):
    lean = [{"lean_meetings": 80, "public_against_meetings": 30, "public_for_meetings": 5,              "disagreement_meetings": 12, "disagreement_states": 4}]
    conn = topic_conn(routes={"FROM topic_meeting_lean": lean})
    d = get(conn, "short-term-rentals").json()
    assert d["lean_meetings"] == 80
    assert d["public_for_meetings"] == 5
    assert d["disagreement_meetings"] == 12
    # #1200: no split or silent count in this row: 0, not derived.
    assert d["public_silent_meetings"] == 0
    assert d["public_split_meetings"] == 0
    params = next(p for s, p in conn.calls if "topic_meeting_lean" in s)
    assert params[0] == 100003


def test_alias_resolves_flock_to_the_plate_reader_topic(get):
    conn = topic_conn()
    get(conn, "flock")
    sql, params = conn.calls[0]
    assert "civicsearch_topic" in sql and params == ("license-plate-readers",)


def test_unknown_topic_is_404(get):
    conn = _Conn({"FROM civicsearch_topic": []})
    assert get(conn, "nope").status_code == 404


def test_required_query_failure_is_503(get):
    conn = topic_conn(fail=("browse_transcript_count",))
    assert get(conn, "short-term-rentals").status_code == 503


def dc_conn(**extra) -> _Conn:
    routes = {
        "FROM civicsearch_topic": [{"topic_id": 100001, "query_id": "data-centers"}],
        "count(DISTINCT m.video_id)::int AS n FROM data_center_meeting_mention": [{"n": 1859}],
        "GROUP BY m.state_code": [
            {"state_code": "NC", "state": "North Carolina", "meetings": 122},
            {"state_code": "GA", "state": "Georgia", "meetings": 118},
        ],
        "FROM data_center_meeting_position": [
            {"for_": 3, "against": 18, "question": 1, "neutral": 7, "meetings_reviewed": 188, "states": 4}
        ],
        "FROM data_center_meeting_lean": [
            {"lean_meetings": 1828, "public_against_meetings": 283, "public_against_states": 32, "public_for_meetings": 0,
             "disagreement_meetings": 23, "disagreement_states": 10}
        ],
        "FROM jurisdictions": POPS,
    }
    routes.update(extra.pop("routes", {}))
    return _Conn(routes, **extra)


def test_data_centers_reads_speakers_and_lean(get):
    d = get(dc_conn(), "data-centers").json()
    assert d["meetings"] == 1859
    assert d["speakers"] == {"for": 3, "against": 18, "question": 1, "neutral": 7, "states": 4}
    assert d["public_against_meetings"] == 283
    assert d["public_against_states"] == 32
    assert d["public_for_meetings"] == 0  # #1200: read from public_lean for data centers too
    assert d["disagreement_meetings"] == 23
    assert d["disagreement_states"] == 10
    assert d["top_state"] == "NC"


def test_data_centers_missing_marts_are_null(get):
    conn = dc_conn(fail=("data_center_meeting_position", "data_center_meeting_lean", "jurisdictions"))
    d = get(conn, "data-centers", {"state": "GA"}).json()
    assert d["meetings"] == 118
    assert d["speakers"] is None
    assert d["lean_meetings"] is None and d["disagreement_meetings"] is None
    assert d["states"][0]["population"] is None


def test_lean_sql_scopes():
    sql, params = tcf.build_lean_sql(100001, "GA", "%Fayette%")
    assert "data_center_meeting_lean" in sql and "topic_id" not in sql
    assert params == ["GA", "%Fayette%"]
    sql, params = tcf.build_lean_sql(100005, None, None)
    assert "topic_meeting_lean" in sql and params == [100005]


def _lean_by_scope(params):
    """Data-center lean rows that depend on the scope params: (state, city) -> canned counts (test stand-ins)."""
    if not params:
        return [{"lean_meetings": 1828, "public_against_meetings": 283, "public_against_states": 32,
                 "disagreement_meetings": 23, "disagreement_states": 10}]
    if len(params) == 1:
        return [{"lean_meetings": 71, "public_against_meetings": 6, "public_against_states": 1,
                 "disagreement_meetings": 0, "disagreement_states": 0}]
    return [{"lean_meetings": 2, "public_against_meetings": 0, "public_against_states": 0,
             "disagreement_meetings": 0, "disagreement_states": 0}]


def test_city_scope_returns_state_then_national_readings(get):
    """#1182: a city too thin for its own reading gets its state's and the country's, nearest first."""
    conn = dc_conn(routes={
        "FROM data_center_meeting_lean": _lean_by_scope,
        "GROUP BY m.jurisdiction_name": [{"name": "Tuscaloosa", "meetings": 2}],
        "AND m.jurisdiction_name ILIKE": [{"n": 2}],
        "FROM jurisdictions": [{"state_code": "AL", "state": "Alabama", "population": 5_000_000}],
    })
    d = get(conn, "data-centers", {"state": "AL", "city": "Tuscaloosa"}).json()
    assert d["public_against_meetings"] == 0
    assert [(w["level"], w["state_code"], w["state"]) for w in d["wider"]] == [
        ("state", "AL", "Alabama"),
        ("national", None, None),
    ]
    assert d["wider"][0]["public_against_meetings"] == 6
    assert d["wider"][1]["public_against_meetings"] == 283
    assert d["wider"][1]["public_against_states"] == 32


def test_state_scope_returns_national_reading_and_national_none(get):
    conn = dc_conn(routes={"FROM data_center_meeting_lean": _lean_by_scope})
    d = get(conn, "data-centers", {"state": "GA"}).json()
    assert [w["level"] for w in d["wider"]] == ["national"]
    assert get(dc_conn(), "data-centers").json()["wider"] is None


def test_wider_left_out_when_no_reading_loaded(get):
    conn = topic_conn(fail=("topic_meeting_lean",))
    d = get(conn, "short-term-rentals", {"state": "FL"}).json()
    assert d["wider"] == []


def test_split_and_silent_add_up_to_the_meetings_read(get):
    """#1200: the four positions (against, for, split, silent) always add up to the meetings read."""
    lean = [{"lean_meetings": 100, "public_against_meetings": 20, "public_for_meetings": 10, "public_silent_meetings": 60,
             "public_split_meetings": 10,
             "disagreement_meetings": 0, "disagreement_states": 0}]
    d = get(topic_conn(routes={"FROM topic_meeting_lean": lean}), "guns").json()
    assert (d["public_against_meetings"], d["public_for_meetings"], d["public_split_meetings"], d["public_silent_meetings"]) == (20, 10, 10, 60)


def test_lean_sql_reads_for_and_silent_from_public_lean():
    sql, _ = tcf.build_lean_sql(100005, None, None)
    assert "AS public_for_meetings" in sql and "AS public_silent_meetings" in sql
    assert "did_not_speak" in sql and "no_public_comment_on_data_center" in sql
    assert "public_lean_p, 0) >= 0.7" in sql


def test_split_counts_mixed_and_unsure_leans_only():
    """A label outside the question's choices (#1201) is never counted as split."""
    sql, _ = tcf.build_lean_sql(100002, None, None)
    assert "l.public_lean = 'mixed'" in sql and "public_lean_p, 0) < 0.7" in sql


def test_nothing_read_is_zero_and_a_failed_read_is_null(get):
    """#1200: a lean query that ran and found nothing is 0 ("no reading yet"); one that failed stays null."""
    empty = [{"lean_meetings": 0, "public_against_meetings": 0, "public_against_states": 0, "public_for_meetings": 0,
              "public_silent_meetings": 0, "public_split_meetings": 0, "disagreement_meetings": 0, "disagreement_states": 0}]
    d = get(topic_conn(routes={"FROM topic_meeting_lean": empty}), "guns").json()
    assert d["lean_meetings"] == 0 and d["public_against_meetings"] is None
    d = get(topic_conn(fail=("topic_meeting_lean",)), "guns").json()
    assert d["lean_meetings"] is None