File size: 6,368 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
"""Tests for GET /states and GET /api/states endpoints."""

from __future__ import annotations

import asyncio
from unittest.mock import AsyncMock, MagicMock, patch

import httpx
import pytest

from api.main import app
from config import settings


def _run_async(coro):
    return asyncio.run(coro)


def test_states_routes_registered_in_openapi():
    """Verify both /states and /api/states routes are registered in OpenAPI schema."""
    paths = set(app.openapi().get("paths", {}).keys())
    assert "/states" in paths
    assert "/api/states" in paths


def test_get_states_offline_without_target_states_is_empty(monkeypatch):
    """When DB pool is None and target_states is unset, returns an empty list (no invented states)."""
    monkeypatch.setattr(settings, "target_states", None)

    async def _test():
        with patch("api.routes.bills_neon.get_pool", new_callable=AsyncMock) as mock_pool:
            mock_pool.return_value = None
            async with httpx.AsyncClient(
                transport=httpx.ASGITransport(app=app),
                base_url="http://test",
            ) as client:
                resp = await client.get("/states")
                assert resp.status_code == 200
                data = resp.json()
                assert data == {"states": [], "count": 0}

                # Test /api/states alias
                api_resp = await client.get("/api/states")
                assert api_resp.status_code == 200
                assert api_resp.json() == data

    _run_async(_test())


def test_get_states_configured_target_states(monkeypatch):
    """When settings.target_states is configured, returns those uppercased and sorted."""
    monkeypatch.setattr(settings, "target_states", ["in", "al", "wi", "al"])

    async def _test():
        with patch("api.routes.bills_neon.get_pool", new_callable=AsyncMock) as mock_pool:
            mock_pool.return_value = None
            async with httpx.AsyncClient(
                transport=httpx.ASGITransport(app=app),
                base_url="http://test",
            ) as client:
                resp = await client.get("/states")
                assert resp.status_code == 200
                data = resp.json()
                assert data["states"] == ["AL", "IN", "WI"]
                assert data["count"] == 3

    _run_async(_test())


def test_get_states_from_database():
    """When DB is active, returns distinct uppercase state codes from bills."""
    mock_conn = AsyncMock()
    mock_conn.fetch.return_value = [
        {"state_code": "wa"},
        {"state_code": "ga"},
        {"state_code": "in"},
    ]

    mock_pool = MagicMock()
    mock_pool.acquire.return_value.__aenter__.return_value = mock_conn
    mock_pool.acquire.return_value.__aexit__.return_value = None

    async def _test():
        with patch("api.routes.bills_neon.get_pool", new_callable=AsyncMock) as mock_get_pool:
            mock_get_pool.return_value = mock_pool
            async with httpx.AsyncClient(
                transport=httpx.ASGITransport(app=app),
                base_url="http://test",
            ) as client:
                resp = await client.get("/api/states")
                assert resp.status_code == 200
                data = resp.json()
                assert data["states"] == ["GA", "IN", "WA"]
                assert data["count"] == 3

    _run_async(_test())


def test_get_states_db_error_without_target_states_is_empty(monkeypatch):
    """When the DB query raises and target_states is unset, returns [] rather than invented states."""
    monkeypatch.setattr(settings, "target_states", None)
    mock_conn = AsyncMock()
    mock_conn.fetch.side_effect = RuntimeError("Postgres connection timeout")

    mock_pool = MagicMock()
    mock_pool.acquire.return_value.__aenter__.return_value = mock_conn
    mock_pool.acquire.return_value.__aexit__.return_value = None

    async def _test():
        with patch("api.routes.bills_neon.get_pool", new_callable=AsyncMock) as mock_get_pool:
            mock_get_pool.return_value = mock_pool
            async with httpx.AsyncClient(
                transport=httpx.ASGITransport(app=app),
                base_url="http://test",
            ) as client:
                resp = await client.get("/states")
                assert resp.status_code == 200
                assert resp.json() == {"states": [], "count": 0}

    _run_async(_test())


def test_get_states_db_error_uses_configured_target_states(monkeypatch):
    """When the DB query raises, configured target_states (real configuration) are returned."""
    monkeypatch.setattr(settings, "target_states", ["wi", "al"])
    mock_conn = AsyncMock()
    mock_conn.fetch.side_effect = RuntimeError("Postgres connection timeout")

    mock_pool = MagicMock()
    mock_pool.acquire.return_value.__aenter__.return_value = mock_conn
    mock_pool.acquire.return_value.__aexit__.return_value = None

    async def _test():
        with patch("api.routes.bills_neon.get_pool", new_callable=AsyncMock) as mock_get_pool:
            mock_get_pool.return_value = mock_pool
            async with httpx.AsyncClient(
                transport=httpx.ASGITransport(app=app),
                base_url="http://test",
            ) as client:
                resp = await client.get("/states")
                assert resp.status_code == 200
                assert resp.json() == {"states": ["AL", "WI"], "count": 2}

    _run_async(_test())


def test_get_states_empty_db_result_without_target_states_is_empty(monkeypatch):
    """When the query returns no rows and target_states is unset, returns []."""
    monkeypatch.setattr(settings, "target_states", None)
    mock_conn = AsyncMock()
    mock_conn.fetch.return_value = []

    mock_pool = MagicMock()
    mock_pool.acquire.return_value.__aenter__.return_value = mock_conn
    mock_pool.acquire.return_value.__aexit__.return_value = None

    async def _test():
        with patch("api.routes.bills_neon.get_pool", new_callable=AsyncMock) as mock_get_pool:
            mock_get_pool.return_value = mock_pool
            async with httpx.AsyncClient(
                transport=httpx.ASGITransport(app=app),
                base_url="http://test",
            ) as client:
                resp = await client.get("/states")
                assert resp.json() == {"states": [], "count": 0}

    _run_async(_test())