93 lines
2.5 KiB
Python
93 lines
2.5 KiB
Python
"""Integration tests for the Voting App.
|
|
|
|
Tests that require actual API access and database persistence.
|
|
Skipped by default; run with: pytest tests/integration/ -m integration
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
|
|
import pytest
|
|
|
|
from src.app import create_app
|
|
|
|
|
|
@pytest.fixture
|
|
def app():
|
|
a = create_app()
|
|
a.config["TESTING"] = True
|
|
return a
|
|
|
|
|
|
@pytest.fixture
|
|
def client(app):
|
|
return app.test_client()
|
|
|
|
|
|
@pytest.mark.integration
|
|
def test_full_search_flow(client):
|
|
"""Test the complete flow: search -> select legislator -> get votes -> generate summaries."""
|
|
congress_key = os.getenv("CONGRESS_API_KEY", "")
|
|
if not congress_key:
|
|
pytest.skip("CONGRESS_API_KEY not set")
|
|
|
|
resp = client.get("/api/search?q=mitch")
|
|
data = resp.get_json()
|
|
assert len(data) >= 1
|
|
|
|
legislator_id = data[0]["id"]
|
|
resp = client.get(f"/api/legislators/{legislator_id}")
|
|
assert resp.status_code == 200
|
|
|
|
resp = client.get(f"/api/legislators/{legislator_id}/votes")
|
|
assert resp.status_code == 200
|
|
|
|
|
|
@pytest.mark.integration
|
|
def test_cache_persistence(app, client):
|
|
"""Test that cached data persists across app restarts."""
|
|
congress_key = os.getenv("CONGRESS_API_KEY", "")
|
|
if not congress_key:
|
|
pytest.skip("CONGRESS_API_KEY not set")
|
|
|
|
resp = client.get("/api/search?q=mitch")
|
|
data = resp.get_json()
|
|
assert len(data) >= 1
|
|
|
|
legislator_id = data[0]["id"]
|
|
|
|
a2 = create_app()
|
|
a2.config["TESTING"] = True
|
|
c2 = a2.test_client()
|
|
|
|
resp = c2.get(f"/api/legislators/{legislator_id}")
|
|
assert resp.status_code == 200
|
|
|
|
|
|
@pytest.mark.integration
|
|
def test_legislator_votes_integration(client):
|
|
"""Fetch real legislator votes from Congress.gov API."""
|
|
congress_key = os.getenv("CONGRESS_API_KEY", "")
|
|
if not congress_key:
|
|
pytest.skip("CONGRESS_API_KEY not set")
|
|
|
|
resp = client.get("/api/search?q=booker")
|
|
data = resp.get_json()
|
|
if not data:
|
|
pytest.skip("Legislator not found")
|
|
|
|
# Pick an in-office senator for reliable vote data (Senate XML scanning works independently of API roll calls)
|
|
legislator_id = None
|
|
for leg in data:
|
|
if leg.get("chamber") == "Senate" and leg.get("in_office"):
|
|
legislator_id = leg["id"]
|
|
break
|
|
if legislator_id is None:
|
|
pytest.skip("No in-office senator found in search results")
|
|
|
|
resp = client.get(f"/api/legislators/{legislator_id}/votes?limit=5")
|
|
assert resp.status_code == 200
|
|
vote_data = resp.get_json()
|
|
assert len(vote_data["votes"]) > 0
|