voting-app/tests/unit/test_vote_client.py
2026-05-15 15:16:18 -05:00

166 lines
5.1 KiB
Python

"""Test VoteClient helper functions."""
from __future__ import annotations
from datetime import date
import pytest
from src.models import Vote
from src.vote_client import VoteClient, get_current_congress, get_session
@pytest.fixture
def vote_client() -> VoteClient:
return VoteClient("https://api.congress.gov", "test-key")
# --- Congress/session calculation ---
def test_get_session_odd_year():
assert get_session(2025) == 1
assert get_session(2023) == 1
assert get_session(1789) == 1
def test_get_session_even_year():
assert get_session(2026) == 2
assert get_session(2024) == 2
assert get_session(1790) == 2
def test_get_current_congress_2025():
assert get_current_congress() == 1 + (2025 - 1789) // 2
def test_get_current_congress_2026():
assert get_current_congress() == 1 + (2026 - 1789) // 2
# --- Vote normalization ---
def test_normalize_vote_yea():
assert VoteClient._normalize_vote_cast("Yea") == "Yea"
assert VoteClient._normalize_vote_cast("Aye") == "Yea"
assert VoteClient._normalize_vote_cast("aye") == "Yea"
assert VoteClient._normalize_vote_cast("Y") == "Yea"
assert VoteClient._normalize_vote_cast("y") == "Yea"
assert VoteClient._normalize_vote_cast("Yes") == "Yea"
assert VoteClient._normalize_vote_cast("yes") == "Yea"
def test_normalize_vote_nay():
assert VoteClient._normalize_vote_cast("Nay") == "Nay"
assert VoteClient._normalize_vote_cast("No") == "Nay"
assert VoteClient._normalize_vote_cast("no") == "Nay"
assert VoteClient._normalize_vote_cast("N") == "Nay"
assert VoteClient._normalize_vote_cast("n") == "Nay"
def test_normalize_vote_present():
assert VoteClient._normalize_vote_cast("Present") == "Present"
assert VoteClient._normalize_vote_cast("present") == "Present"
assert VoteClient._normalize_vote_cast("P") == "Present"
assert VoteClient._normalize_vote_cast("p") == "Present"
assert VoteClient._normalize_vote_cast("Answered Present") == "Present"
def test_normalize_vote_not_voting():
assert VoteClient._normalize_vote_cast("Not Voting") == "Not Voting"
assert VoteClient._normalize_vote_cast("not voting") == "Not Voting"
assert VoteClient._normalize_vote_cast("Absent") == "Not Voting"
assert VoteClient._normalize_vote_cast("absent") == "Not Voting"
def test_normalize_vote_unknown():
assert VoteClient._normalize_vote_cast("Unknown") == "Unknown"
assert VoteClient._normalize_vote_cast(" Yea ") == "Yea"
# --- Date parsing ---
def test_parse_date_iso():
assert VoteClient._parse_date("2025-06-15") == date(2025, 6, 15)
assert VoteClient._parse_date("2025-06-15T10:30:00") == date(2025, 6, 15)
def test_parse_date_empty():
assert VoteClient._parse_date("") is None
assert VoteClient._parse_date("invalid") is None
def test_parse_senate_date():
assert VoteClient._parse_senate_date("July 1, 2025, 11:56 AM") == date(2025, 7, 1)
assert VoteClient._parse_senate_date("January 3, 2025") == date(2025, 1, 3)
assert VoteClient._parse_senate_date("December 31, 2024, 12:00 PM") == date(2024, 12, 31)
def test_parse_senate_date_empty():
assert VoteClient._parse_senate_date("") is None
assert VoteClient._parse_senate_date("not a date") is None
# --- State normalization ---
def test_normalize_state_full():
assert VoteClient._normalize_state("California") == "CA"
assert VoteClient._normalize_state("Kentucky") == "KY"
assert VoteClient._normalize_state("New York") == "NY"
def test_normalize_state_abbr():
assert VoteClient._normalize_state("CA") == "CA"
assert VoteClient._normalize_state("KY") == "KY"
assert VoteClient._normalize_state("NY") == "NY"
def test_normalize_state_unknown():
assert VoteClient._normalize_state("Unknown") == "UNKNOWN"
# --- Bill ID resolution ---
def test_resolve_bill_id_hr():
roll_call = {"legislationType": "HR", "legislationNumber": "1", "congress": "119"}
assert VoteClient("https://api.congress.gov", "")._resolve_bill_id(roll_call) == "119/HR/1"
def test_resolve_bill_id_hjres():
roll_call = {"legislationType": "HJRES", "legislationNumber": "50", "congress": "119"}
assert VoteClient("https://api.congress.gov", "")._resolve_bill_id(roll_call) == "119/HJRES/50"
def test_resolve_bill_id_hres():
roll_call = {"legislationType": "HRES", "legislationNumber": "10", "congress": "119"}
assert VoteClient("https://api.congress.gov", "")._resolve_bill_id(roll_call) == "119/HRES/10"
def test_resolve_bill_id_missing_fields():
assert VoteClient("https://api.congress.gov", "")._resolve_bill_id({}) == ""
assert VoteClient("https://api.congress.gov", "")._resolve_bill_id({"legislationType": "HR"}) == ""
# --- Vote object creation ---
def test_vote_creation():
vote = Vote(
legislator_id="M000355",
roll_call_id="123",
vote_type="Yea",
bill_id="119/hr/1",
vote_date=date(2025, 6, 15),
bill_title="HR 1",
)
assert vote.legislator_id == "M000355"
assert vote.roll_call_id == "123"
assert vote.vote_type == "Yea"
assert vote.bill_id == "119/hr/1"
assert vote.vote_date == date(2025, 6, 15)
assert vote.bill_title == "HR 1"