"""2026 候選人名單與中選會選舉結果的解析。""" from __future__ import annotations import json from collections.abc import Iterator import pytest from election_data.people.candidates import ( COUNTY, MUNICIPAL, article_html, parse_elcand, parse_press_release, to_claim, ) from election_data.people.cec_results import ElectionSpec, candidate_totals, load_at_large, load_election pytestmark = pytest.mark.unit PRESS_BODY = ( "

中央選舉委員會今(16)日舉行委員會議…

" "

一、直轄市長候選人部分:受理登記計3人,均符合規定:

" "

(一)臺北市3人:郭璽、沈伯洋、蔣萬安。

" "

二、縣(市)長候選人部分:受理登記計4人,均符合規定:

" "

(一)新竹縣2人:鄭朝方、徐欣瑩。

" "

(二)嘉義市2人:黃宏成台灣阿成世界偉人財神總統、張啓楷。

" "

三、直轄市議員候選人部分:受理登記計610人…

" "

(一)臺北市不符合規定者計1人:某某…

" ) def _api(body: str) -> dict: content = json.dumps({"content": [{"blockType": "Text", "content": body}]}, ensure_ascii=False) return {"code": 0, "data": {"content": content, "beginTime": "20261016"}} def test_parse_press_release() -> None: body, date = article_html(_api(PRESS_BODY)) entries = parse_press_release(body, "cec") assert date == "2026-10-16" assert [(e.county, e.name, e.office) for e in entries][:2] == [ ("臺北市", "郭璽", MUNICIPAL), ("臺北市", "沈伯洋", MUNICIPAL), ] assert sum(e.office == COUNTY for e in entries) == 4 assert "某某" not in {e.name for e in entries} # 議員段落不算 def test_parse_press_release_count_mismatch_raises() -> None: with pytest.raises(ValueError, match="應有 3 人"): parse_press_release(PRESS_BODY.replace("郭璽、", ""), "cec") def test_parse_press_release_total_mismatch_raises() -> None: with pytest.raises(ValueError, match="候選人應有"): parse_press_release(PRESS_BODY.replace("受理登記計4人", "受理登記計5人"), "cec") def test_parse_elcand_and_claim() -> None: elbase = [["63", "000", "00", "000", "0000", "臺北市"]] elpaty = [["1", "中國國民黨"], ["999", "無黨籍及未經政黨推薦"]] elcand = [ [ "63", "000", "00", "000", "0000", "1", "甲某", "999", "2", "0650515", "61", "臺北市", "博士", "N", "", "", ], [ "63", "000", "00", "000", "0000", "2", "乙某", "1", "1", "0671226", "47", "臺北市", "博士", "Y", "", "", ], [ "63", "000", "00", "000", "0000", "2", "副手某", "1", "1", "0600101", "66", "臺北市", "碩士", "N", "", "Y", ], ] entries = parse_elcand(elcand, elbase, elpaty, MUNICIPAL, "elcand") assert [e.name for e in entries] == ["甲某", "乙某"] first, second = entries assert (first.party, first.party_known, first.gender, first.birth_date) == (None, True, "F", "1976-05-15") assert (second.number, second.party) == (2, "中國國民黨") claim = to_claim(second, "2026-11-12") assert claim.positions[0].title == "臺北市長候選人(號次 2)" assert claim.party and claim.party.value == "中國國民黨" assert claim.birth_date and claim.birth_date.value == "1978-12-26" def test_parse_elcand_unknown_county_raises() -> None: row = ["99", "999", "00", "000", "0000", "1", "某", "1", "1", "0650515", "61", "", "", "N", "", ""] with pytest.raises(ValueError): parse_elcand([row], [], [], COUNTY, "x") def _ctks( prv: str, city: str, area: str, town: str, vil: str, station: str, no: str, votes: int ) -> list[str]: return [prv, city, area, town, vil, station, no, str(votes), "0", ""] def test_candidate_totals_district_level_and_verification() -> None: rows = [ _ctks("63", "000", "01", "000", "0000", "0000", "1", 30), _ctks("63", "000", "01", "010", "0000", "0000", "1", 30), # 鄉鎮層級,不採用 _ctks("63", "000", "01", "010", "0001", "0001", "1", 10), _ctks("63", "000", "01", "010", "0002", "0002", "1", 20), ] assert candidate_totals(rows, national=False) == {("63", "000", "01", "1"): 30} bad = [*rows[:-1], _ctks("63", "000", "01", "010", "0002", "0002", "1", 21)] with pytest.raises(ValueError, match="不一致"): candidate_totals(bad, national=False) def test_candidate_totals_national() -> None: rows = [ _ctks("00", "000", "00", "000", "0000", "0000", "1", 15), _ctks("63", "000", "00", "000", "0000", "0000", "1", 15), _ctks("63", "000", "01", "010", "0001", "0001", "1", 5), _ctks("64", "000", "01", "010", "0001", "0002", "1", 10), ] assert candidate_totals(rows, national=True) == {("00", "000", "00", "1"): 15} class FakeArchive: def __init__(self, tables: dict[str, list[list[str]]]) -> None: self.tables = tables def rows(self, folder: str, kind: str) -> Iterator[list[str]]: yield from self.tables[kind] def test_load_election_shares_and_district_labels() -> None: archive = FakeArchive( { "elpaty": [["1", "中國國民黨"], ["16", "民主進步黨"]], "elbase": [["10", "002", "00", "000", "0000", "宜蘭縣"]], "elcand": [ [ "10", "002", "01", "000", "0000", "1", "甲", "1", "1", "0600101", "", "", "大學", "N", "", "", ], [ "10", "002", "01", "000", "0000", "2", "乙", "16", "2", "0700101", "", "", "碩士", "Y", "*", "", ], ], "elctks": [ _ctks("10", "002", "01", "000", "0000", "0000", "1", 40), _ctks("10", "002", "01", "000", "0000", "0000", "2", 60), _ctks("10", "002", "01", "010", "0001", "0001", "1", 40), _ctks("10", "002", "01", "010", "0001", "0001", "2", 60), ], } ) spec = ElectionSpec("x", "2024 立法委員選舉(區域)", "legislator_district", None) a, b = load_election(archive, spec) # type: ignore[arg-type] assert (a.district, a.county, a.votes, a.share, a.elected) == ("宜蘭縣選舉區", "宜蘭縣", 40, 0.4, False) assert (b.party, b.gender, b.birth_date, b.elected) == ("民主進步黨", "F", "1981-01-01", True) def test_load_at_large_from_elrepm() -> None: archive = FakeArchive( { "elpaty": [["1", "中國國民黨"]], "elrepm": [["1", "1", "韓國瑜", "1", "0460617", "66", "新北市", "碩士", "N", "*"]], } ) (r,) = load_at_large(archive) # type: ignore[arg-type] assert (r.name, r.party, r.gender, r.birth_date, r.votes, r.elected) == ( "韓國瑜", "中國國民黨", "M", "1957-06-17", None, True, )