election-game/backend/data/pipeline/tests/test_historic.py

193 lines
7.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""經典之戰:2014 縣市長與 1996–2016 總統的人口基準、命令列與產出。"""
import json
from pathlib import Path
import pytest
from election_data import cli
from election_data.codes import cec_village_code
from election_data.config import BUILD_DIR
from election_data.local2022 import LOCAL_RACES
from election_data.population import parse_county_population, parse_year_end_population
from election_data.races2024 import NATIONAL_RACES
SCENARIOS = BUILD_DIR / "scenarios"
@pytest.mark.unit
def test_2014_cross_village_code_is_not_a_village() -> None:
# 2014 年的跨村里投開票所寫成 9999,不能當成村里代碼
assert cec_village_code("66", "000", "060", "9999") is None
@pytest.mark.unit
def test_parse_monthly_county_table() -> None:
rows = [
["戶數、人口數、性別比例及人口密度統計表(續)", None, None],
[None, "中 華 民 國 103 年 5 月 底", None],
["區 域 別", "戶 數", "人  口  數"],
["總 計", 10, 100],
["臺灣省", 9, 60],
["臺北市", 3, 40],
["桃園縣", 2, 35], # 升格前的名稱對到 68000
["金門縣", 1, 25],
["東沙群島", 0, 0],
]
assert parse_county_population(rows, "10305") == {"00000": 100, "63000": 40, "68000": 35, "09020": 25}
with pytest.raises(ValueError, match="年月"):
parse_county_population(rows, "10306")
@pytest.mark.unit
def test_monthly_table_merges_old_counties_and_checks_total() -> None:
rows = [
[None, "96年9月底", None],
["區域別", "戶數", "人口數"],
["總  計", 0, 30],
["臺中縣 ", 0, 20],
["臺中市 ", 0, 10],
]
assert parse_county_population(rows, "09609") == {"00000": 30, "66000": 30}
rows[2][2] = 31
with pytest.raises(ValueError, match="不符"):
parse_county_population(rows, "09609")
@pytest.mark.unit
def test_parse_year_end_table() -> None:
rows = [
["表一 各縣市人口數按性別及五歲年齡組分", None, None],
["中 華 民 國 84 年 底", None, None],
["區 域 別", "性別", "總 計"],
[None, "計", 50],
["總  計", "男", 26],
[None, "女", 24],
[None, "計", 30],
["臺 北 縣", "男", 16],
[None, "女", 14],
[None, "計", 20],
["高 雄 市", "男", 10],
[None, "女", 10],
]
assert parse_year_end_population(rows, 84) == {"00000": 50, "65000": 30, "64000": 20}
def _mock_pack(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> list[tuple]:
called: list[tuple] = []
def fake(county: str, exclude: tuple = (), name: str | None = None, older: bool = False, **extra) -> Path:
called.append((county, set(exclude), name, extra))
return tmp_path
monkeypatch.setattr(cli, "build_pack", fake)
return called
@pytest.mark.unit
def test_cli_historic_window(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
called = _mock_pack(monkeypatch, tmp_path)
args = ["pack", "--county", "00000", "--historic", "--since", "2012-01-01", "--before", "2016-01-16"]
assert cli.main([*args, "--past-only"]) == 0
county, exclude, name, extra = called[0]
assert name == "00000-before-2016-01-16"
assert extra == {"historic": True, "since": "2012-01-01", "until": "2016-01-16"}
assert exclude == set() # 之後的選舉根本不讀,不用排除
assert cli.main(args) == 0
_, exclude, _, extra = called[1]
assert "until" not in extra
assert {"2016-president", "2024-president"} <= exclude and "2014-mayor" not in exclude
@pytest.mark.unit
def test_cli_past_only_needs_before(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
_mock_pack(monkeypatch, tmp_path)
with pytest.raises(SystemExit):
cli.main(["pack", "--county", "00000", "--historic", "--past-only"])
@pytest.mark.unit
def test_race_definitions() -> None:
assert LOCAL_RACES[2014].cap_month == "10305"
# 投票月前第六個月的月底;1996 年那個月沒有月報,載入時改用年底(population.load_county_population)
assert {y: NATIONAL_RACES[y].cap_month for y in (1996, 2000, 2004, 2008, 2012, 2016)} == {
1996: "08409",
2000: "08809",
2004: "09209",
2008: "09609",
2012: "10007",
2016: "10407",
}
assert all(NATIONAL_RACES[y].folder is None for y in (1996, 2000, 2004, 2008, 2012, 2016))
@pytest.mark.skipif(
not (SCENARIOS / "2014-local" / "63000.json").exists(), reason="需要先執行 local --year 2014"
)
def test_2014_local_caps_and_totals() -> None:
# 中選會 2014 年公告的六都競選經費上限
published = {
"65000": 105_384_000,
"63000": 87_690_000,
"66000": 87_900_000,
"67000": 76_361_000,
"64000": 88_884_000,
"68000": 78_670_000,
}
files = sorted((SCENARIOS / "2014-local").glob("[0-9]*.json"))
assert len(files) == 22
for path in files:
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
assert all(c["finance"] is None for c in data["candidates"])
if data["countyCode"] in published:
assert data["spendingCap"] == published[data["countyCode"]], path.name
@pytest.mark.skipif(not (SCENARIOS / "1996-president" / "00000.json").exists(), reason="需要先執行 races")
def test_historic_presidents() -> None:
first = json.loads((SCENARIOS / "1996-president" / "00000.json").read_text(encoding="utf-8"))
assert [(c["name"], c["runningMate"]) for c in first["candidates"]] == [
("陳履安", "王清峰"),
("李登輝", "連戰"),
("彭明敏", "謝長廷"),
("林洋港", "郝柏村"),
]
assert first["candidates"][0]["party"] is None # 舊資料的「連署」是無黨籍
for year in (1996, 2000, 2004, 2008, 2012, 2016):
data = json.loads((SCENARIOS / f"{year}-president" / "00000.json").read_text(encoding="utf-8"))
counties = {code[:5] for code in data["byVillage"]}
assert len(counties) == 22, year # 舊縣市都併進現在的 22 縣市
total = sum(sum(v["votes"].values()) for v in data["byVillage"].values())
# 2004 年中選會檔案有約 50 個里的得票列與投票概況對不上,加總多 1,851 票(萬分之 1.4)
tolerance = data["cecValid"] * 2e-4 if year == 2004 else 0
assert abs(total - data["cecValid"]) <= tolerance, year
assert all(c["runningMate"] for c in data["candidates"]), year
@pytest.mark.unit
def test_blocs_only_change_lean_inputs() -> None:
from election_data.elections import (
INDEPENDENT,
PARTY_A,
PARTY_B,
ElectionData,
ElectionDef,
VillageResult,
)
from election_data.pack import _observations
election = ElectionDef("x", "測試", "2000-03-18", "president", (), 0.25, blocs=((INDEPENDENT, PARTY_B),))
votes = {PARTY_A: 40, PARTY_B: 20, INDEPENDENT: 35}
data = ElectionData(
election,
{"63000010001": VillageResult(100, 96, 95, votes)},
{"63000": 95},
{"63000": {PARTY_A: 40, PARTY_B: 20, INDEPENDENT: 35}},
)
[obs] = _observations("63000010001", [data])
assert obs.votes == {PARTY_A: 40, PARTY_B: 55}
assert obs.contested == {PARTY_A, PARTY_B}
assert data.by_village["63000010001"].votes[INDEPENDENT] == 35 # history.json 仍是原本的分法