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

189 lines
8.8 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 年以前的中選會資料(流水號代碼)用名稱對照到目前的村里代碼。"""
import zipfile
from pathlib import Path
import pytest
from election_data.cec import CecArchive
from election_data.codes import county_residual_code, is_town_residual, town_residual_code
from election_data.elections import PARTY_A, PARTY_B, ElectionDef, candidate_party, load_election
from election_data.legacy import Gazetteer, legacy_places, stem
from election_data.population import VillagePopulation
pytestmark = pytest.mark.unit
ROOT = "votedata/votedata/voteData/"
AGES = tuple([0] * 101)
def village(code: str, site: str, name: str) -> VillagePopulation:
return VillagePopulation(code, site, name, 0, AGES)
#: 目前的戶政資料:新北市板橋區、臺中市(中區與豐原區)、臺南市中西區
CURRENT = [
village("65000010001", "新北市板橋區", "留侯里"),
village("65000010002", "新北市板橋區", "公舘里"),
village("65000010003", "新北市板橋區", "\U000fffb5寮里"),
village("66000010001", "臺中市中區", "大墩里"),
village("66000140001", "臺中市豐原區", "南田里"),
village("67000370001", "臺南市中西區", "赤嵌里"),
village("64000050001", "高雄市三民區", "民族里"),
village("64000380001", "高雄市那瑪夏區", "南沙魯里"),
]
#: 2018 年還在、後來合併掉的里
OLD = [village("65000010099", "新北市板橋區", "舊里")]
@pytest.fixture
def gazetteer() -> Gazetteer:
return Gazetteer.from_snapshots([OLD, CURRENT])
class TestGazetteer:
def test_stem_drops_level_and_folds_variants(self) -> None:
assert stem("板橋市", "市鎮鄉區") == stem("板橋區", "市鎮鄉區") == "板橋"
assert stem("三芝村", "村里") == "三芝"
assert stem("公館里", "村里") == stem("公舘里", "村里")
# 只去掉最後一個字:前鎮區不會變成「前」
assert stem("前鎮區", "市鎮鄉區") == "前鎮"
def test_towns_by_name_including_old_counties(self, gazetteer: Gazetteer) -> None:
assert gazetteer.town("65000", "板橋市") == "65000010"
assert gazetteer.town("66000", "豐原市") == "66000140"
# 舊臺南市的西區併進中西區;臺中市的中區還在
assert gazetteer.town("67000", "西區", "臺南市") == "67000370"
assert gazetteer.town("66000", "中區", "臺中市") == "66000010"
# 高雄縣三民鄉是現在的那瑪夏區,不是高雄市三民區
assert gazetteer.town("64000", "三民鄉", "高雄縣") == "64000380"
assert gazetteer.town("64000", "三民區", "高雄市") == "64000050"
assert gazetteer.town("65000", "不存在鄉") is None
def test_villages_current_old_and_private_use(self, gazetteer: Gazetteer) -> None:
assert gazetteer.village("65000010", "留侯里") == "65000010001"
assert gazetteer.village("65000010", "公館里") == "65000010002" # 異體字
assert gazetteer.village("65000010", "獇寮里") == "65000010003" # 私用區字元當萬用字元
assert gazetteer.village("65000010", "舊里") == "65000010099" # 2018 年以後合併的舊代碼
assert gazetteer.village("65000010", "沒有這里") is None
def csv(rows: list[list[str]]) -> str:
return "\n".join(",".join(f'"{c}"' for c in row) for row in rows) + "\n"
@pytest.fixture
def archive(tmp_path: Path) -> CecArchive:
"""2008 年格式:臺北縣 03/001、臺中縣 03/006、臺中市 03/019,鄉鎮與村里都是流水號"""
elbase = [
["00", "000", "00", "000", "0000", "全國"],
["03", "001", "00", "000", "0000", "臺北縣"],
["03", "001", "00", "001", "0000", "板橋市"],
["03", "001", "00", "001", "0001", "留侯里"],
["03", "001", "00", "001", "0002", "公館里、獇寮里"], # 數字代碼但涵蓋兩個里
["03", "001", "00", "001", "0003", "已消失里"], # 2018 年以前就合併掉
["03", "006", "00", "000", "0000", "臺中縣"],
["03", "006", "00", "001", "0000", "豐原市"],
["03", "006", "00", "001", "0004", "南田里"],
["03", "019", "00", "000", "0000", "臺中市"],
["03", "019", "00", "001", "0000", "中區"],
["03", "019", "00", "001", "0005", "大墩里"],
]
elpaty = [["2", PARTY_A], ["1", PARTY_B]]
elcand = [
["00", "000", "01", "000", "0000", "1", "甲", "2", "1", "", "", "", "", "N", "", ""],
["00", "000", "01", "000", "0000", "1", "甲副", "2", "1", "", "", "", "", "N", "", "Y"],
["00", "000", "01", "000", "0000", "2", "乙", "1", "1", "", "", "", "", "N", "", ""],
]
def ctks(cc: tuple[str, str], town: str, vill: str, a: int, b: int) -> list[list[str]]:
return [
[*cc, "01", town, vill, "0", "1", str(a), "", ""],
[*cc, "01", town, vill, "0", "2", str(b), "", ""],
]
def prof(cc: tuple[str, str], town: str, vill: str, valid: int) -> list[str]:
return [*cc, "01", town, vill, "0", str(valid), "0", str(valid), str(valid * 2)]
tp, tc, tcc = ("03", "001"), ("03", "006"), ("03", "019")
rows_ctks = [
*ctks(tp, "000", "0000", 75, 45), # 臺北縣總計:選區欄是 00 以外的寫法也要對得到候選人
*ctks(tp, "001", "0001", 30, 10),
*ctks(tp, "001", "0002", 20, 20),
*ctks(tp, "001", "0003", 10, 5),
*ctks(tp, "001", "0009", 15, 10), # elbase 沒列的里:只知道鄉鎮
*ctks(tc, "000", "0000", 10, 30),
*ctks(tc, "001", "0004", 10, 30),
*ctks(tcc, "000", "0000", 50, 50),
*ctks(tcc, "001", "0005", 20, 20), # 另有 20:20 的票只在縣市總計(村里列缺漏)
]
rows_prof = [
["00", "000", "00", "000", "0000", "0", "300", "0", "300", "600"],
[*tp, "00", "000", "0000", "0", "120", "0", "120", "240"],
prof(tp, "001", "0001", 40),
prof(tp, "001", "0002", 40),
prof(tp, "001", "0003", 15),
prof(tp, "001", "0009", 25),
[*tc, "00", "000", "0000", "0", "40", "0", "40", "80"],
prof(tc, "001", "0004", 40),
[*tcc, "00", "000", "0000", "0", "100", "0", "100", "200"],
prof(tcc, "001", "0005", 40),
prof(tcc, "001", "0006", 60), # 有投票概況、沒有得票列的里
]
path = tmp_path / "votedata.zip"
with zipfile.ZipFile(path, "w") as zf:
for kind, rows in {
"elbase": elbase,
"elpaty": elpaty,
"elcand": elcand,
"elctks": rows_ctks,
"elprof": rows_prof,
}.items():
zf.writestr(f"{ROOT}OLD/{kind}.csv", csv(rows))
return CecArchive(path)
LEGACY = ElectionDef("2008-x", "測試", "2008-03-22", "president", ("OLD",), 1.0, legacy_codes=True)
def test_places_map_old_counties_towns_and_villages(archive: CecArchive, gazetteer: Gazetteer) -> None:
places = legacy_places(archive, "OLD", gazetteer)
assert places.counties[("03", "006")] == places.counties[("03", "019")] == "66000"
assert places.villages[("03", "001", "001", "0002")] == ("65000010002", "65000010003")
assert places.villages[("03", "001", "001", "0003")] == (town_residual_code("65000010"),)
assert places.town_only == 1
def test_legacy_load_reconciles_with_county_totals(archive: CecArchive, gazetteer: Gazetteer) -> None:
data = load_election(archive, LEGACY, gazetteer=gazetteer)
by = data.by_village
assert by["65000010001"].votes == {PARTY_A: 30, PARTY_B: 10}
# 跨兩個里的列平均分配(沒有權重)
assert by["65000010002"].votes[PARTY_A] + by["65000010003"].votes[PARTY_A] == 20
assert by["65000010002"].allocated
# 對不到的里與 elbase 沒列的里記在鄉鎮層級
residual = by[town_residual_code("65000010")]
assert residual.votes == {PARTY_A: 25, PARTY_B: 15} and residual.valid == 40
# 臺中縣+臺中市併成 66000;村里列缺漏的票記在縣市層級
county = by[county_residual_code("66000")]
assert county.votes == {PARTY_A: 30, PARTY_B: 30} and county.valid == 60
assert data.cec_valid_by_county["66000"] == 140 == sum(r.valid for c, r in by.items() if c[:5] == "66000")
for cc in ("65000", "66000"):
votes = sum(sum(r.votes.values()) for c, r in by.items() if c[:5] == cc)
assert votes == data.cec_valid_by_county[cc]
assert data.cec_valid_by_county["00000"] == 300
assert is_town_residual(county_residual_code("66000"))
def test_legacy_requires_gazetteer(archive: CecArchive) -> None:
with pytest.raises(ValueError, match="gazetteer"):
load_election(archive, LEGACY)
@pytest.mark.parametrize(
("raw", "expected"),
[("民主進步黨", "民主進步黨"), ("無黨籍及未經政黨推薦", None), ("無", None), ("連署", None), ("", None)],
)
def test_candidate_party(raw: str, expected: str | None) -> None:
assert candidate_party(raw) == expected