Files
Lottery-Tracker/tests/test_api.py
2026-02-18 08:24:54 -05:00

216 lines
6.3 KiB
Python

"""Integration tests for Flask API endpoints."""
from __future__ import annotations
from unittest.mock import patch
import pytest
# ---------------------------------------------------------------------------
# /api/health
# ---------------------------------------------------------------------------
def test_health(client):
resp = client.get("/api/health")
assert resp.status_code == 200
data = resp.get_json()
assert data["status"] == "ok"
# ---------------------------------------------------------------------------
# /api/jackpots
# ---------------------------------------------------------------------------
@patch("app.get_all_jackpots")
def test_jackpots(mock_get, client):
mock_get.return_value = {
"us": {"powerball": 500_000_000, "megaMillions": 300_000_000},
"canadian": {"lottoMax": 70_000_000, "lotto649": 20_000_000},
}
resp = client.get("/api/jackpots")
assert resp.status_code == 200
data = resp.get_json()
assert data["us"]["powerball"] == 500_000_000
# ---------------------------------------------------------------------------
# /api/calculate
# ---------------------------------------------------------------------------
def test_calculate_us(client):
resp = client.post(
"/api/calculate",
json={"jackpot": 100_000_000, "type": "us"},
)
assert resp.status_code == 200
data = resp.get_json()
assert data["country"] == "US"
assert data["originalJackpot"] == 100_000_000
def test_calculate_canadian(client):
resp = client.post(
"/api/calculate",
json={"jackpot": 50_000_000, "type": "canadian"},
)
assert resp.status_code == 200
data = resp.get_json()
assert data["country"] == "Canada"
def test_calculate_with_state(client):
resp = client.post(
"/api/calculate",
json={"jackpot": 100_000_000, "type": "us", "state": "CA"},
)
assert resp.status_code == 200
data = resp.get_json()
assert data["stateTaxRate"] == 0.133 # California
def test_calculate_missing_jackpot(client):
resp = client.post("/api/calculate", json={"type": "us"})
assert resp.status_code == 400
def test_calculate_bad_type(client):
resp = client.post("/api/calculate", json={"jackpot": 100, "type": "mars"})
assert resp.status_code == 400
def test_calculate_no_body(client):
resp = client.post("/api/calculate")
assert resp.status_code == 400
# ---------------------------------------------------------------------------
# /api/states
# ---------------------------------------------------------------------------
def test_states_list(client):
resp = client.get("/api/states")
assert resp.status_code == 200
data = resp.get_json()
assert len(data) == 51 # 50 states + DC
codes = [s["code"] for s in data]
assert "CA" in codes
assert "TX" in codes
def test_state_by_code(client):
resp = client.get("/api/states/NY")
assert resp.status_code == 200
data = resp.get_json()
assert data["name"] == "New York"
assert data["rate"] == 0.109
def test_state_not_found(client):
resp = client.get("/api/states/ZZ")
assert resp.status_code == 404
# ---------------------------------------------------------------------------
# /api/compare
# ---------------------------------------------------------------------------
@patch("app.get_all_jackpots")
def test_compare(mock_get, client):
mock_get.return_value = {
"us": {"powerball": 500_000_000, "megaMillions": 300_000_000},
"canadian": {"lottoMax": 70_000_000, "lotto649": 20_000_000},
}
resp = client.get("/api/compare")
assert resp.status_code == 200
data = resp.get_json()
assert len(data) == 4
names = [d["name"] for d in data]
assert "Powerball" in names
# ---------------------------------------------------------------------------
# /api/calculate/breakeven
# ---------------------------------------------------------------------------
def test_breakeven(client):
resp = client.post(
"/api/calculate/breakeven",
json={"lottery": "powerball"},
)
assert resp.status_code == 200
data = resp.get_json()
assert data["breakEvenJackpot"] > 0
assert data["lottery"] == "Powerball"
def test_breakeven_unknown_lottery(client):
resp = client.post(
"/api/calculate/breakeven",
json={"lottery": "nosuchlottery"},
)
assert resp.status_code == 400
# ---------------------------------------------------------------------------
# /api/calculate/annuity
# ---------------------------------------------------------------------------
def test_annuity(client):
resp = client.post(
"/api/calculate/annuity",
json={"jackpot": 500_000_000, "type": "us", "years": 30},
)
assert resp.status_code == 200
data = resp.get_json()
assert len(data["schedule"]) == 30
def test_annuity_canadian(client):
resp = client.post(
"/api/calculate/annuity",
json={"jackpot": 100_000_000, "type": "canadian"},
)
assert resp.status_code == 200
data = resp.get_json()
assert data["country"] == "canadian"
assert data["schedule"][0]["tax"] == 0.0
# ---------------------------------------------------------------------------
# /api/calculate/group
# ---------------------------------------------------------------------------
def test_group_play(client):
resp = client.post(
"/api/calculate/group",
json={"jackpot": 100_000_000, "members": 4, "type": "us"},
)
assert resp.status_code == 200
data = resp.get_json()
assert data["members"] == 4
assert len(data["memberResults"]) == 4
def test_group_custom_shares(client):
resp = client.post(
"/api/calculate/group",
json={"jackpot": 100_000_000, "members": 2, "shares": [0.7, 0.3], "type": "canadian"},
)
assert resp.status_code == 200
data = resp.get_json()
assert data["memberResults"][0]["share"] == pytest.approx(0.7)
# ---------------------------------------------------------------------------
# /api/odds
# ---------------------------------------------------------------------------
def test_odds(client):
resp = client.get("/api/odds")
assert resp.status_code == 200
data = resp.get_json()
assert len(data) == 4
names = [d["name"] for d in data]
assert "Powerball" in names
assert "Mega Millions" in names