import json import pytest from election_data.local2022 import OUT_DIR, clean_name, office_of, spending_cap def test_spending_cap_formula_matches_cec() -> None: # 中選會公告:2022 桃園市長 81,679,000(2022-05-31 人口 2,262,765) assert spending_cap("68000", 2_262_765) == 81_679_000 # 縣市長固定金額 3,000 萬 assert spending_cap("10016", 105_000) == 31_470_000 def test_office_and_names() -> None: assert office_of("63000") == "市長" assert office_of("10018") == "市長" assert office_of("10002") == "縣長" assert office_of("09020") == "縣長" assert clean_name("江淵") == "江聰淵" @pytest.mark.skipif(not (OUT_DIR / "65000.json").exists(), reason="需要先執行 election-data local2022") def test_built_caps_match_published_figures() -> None: # 中央社 2022-07-15 報導的中選會公告數字 published = { "65000": 105_620_000, "66000": 89_191_000, "64000": 88_138_000, "10007": 47_468_000, "10013": 41_217_000, "10009": 39_330_000, "68000": 81_679_000, } for county, cap in published.items(): data = json.loads((OUT_DIR / f"{county}.json").read_text(encoding="utf-8")) assert data["spendingCap"] == cap, county @pytest.mark.skipif(not (OUT_DIR / "68000.json").exists(), reason="需要先執行 election-data local2022") def test_candidate_votes_sum_to_cec_totals() -> None: for path in OUT_DIR.glob("[0-9]*.json"): data = json.loads(path.read_text(encoding="utf-8")) total = sum(sum(v["votes"].values()) for v in data["byVillage"].values()) assert total == data["cecValid"], path.name numbers = {f"#{c['number']}" for c in data["candidates"]} for v in data["byVillage"].values(): assert set(v["votes"]) <= numbers