election-game/backend/data/pipeline/tests/people/test_people_elections.py

124 lines
4.1 KiB
Python
Raw Normal View History

選戰卡牌第四版:本週戰場、加開行程、逆風重排、逐張地圖校準;手機版修正;刪除人物資料庫與 AI 評分 玩法(docs/26): - 本週戰場:每週一個選情接近的大區,對手全力搶攻,週末淨增多的一方贏(聲勢 +1、多抽一張) - 加開行程:每週一次花資金換 1 點體力;起始資金壓到 500–1,000 萬 - 對手落後 3 個百分點以上急起直追;打對台可以搶廣告版面、讓造勢冷場;大絕招集氣加快 - 逆風 0–10 重排成一級一條規則(ASC_RULES),每一級都看得出差別 - 平衡:空戰對手、對手規則、六種類型、空戰/攻防流派、擴散不看鄰區數、區的大小係數、 對手規模依區數、只有一區的選區沒有本命區和戰場、總統大選全國動員、勢均力敵的第三位候選人 - 逐張地圖校準:apps/sim rogue-calibrate 跑過 219 張勢均力敵地圖,calibration.ts 存每張的對手係數 - 模擬工具載入劇本和伺服器一致(總統合成縣市、立委只取選區村里) 畫面: - 戰場比數、加開按鈕、急起直追提示、週末戰場輸贏;遊戲中「存檔離開」回主選單 - 手機直放、橫放的破版修正(圖鑑完成度、遊戲畫面、首頁、捏人物、服裝店、小遊戲等) 刪除(依使用者決定):人物資料庫(Wikidata、內閣、現任名單 → people.json)與 AI 評分管線、 config/people、相關測試、protocol 的 people.ts、docs/rubric-v1.md;records 用到的 names、cec_results 保留 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uirha5kfuiGbEBhsk7ijDg
2026-10-29 23:42:15 +00:00
"""中選會選舉結果的解析(歷屆參選人資料庫用)。"""
from __future__ import annotations
from collections.abc import Iterator
import pytest
from election_data.people.cec_results import ElectionSpec, candidate_totals, load_at_large, load_election
pytestmark = pytest.mark.unit
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,
)