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

142 lines
6.2 KiB
Python

"""合併、選舉紀錄比對、更正的規則。"""
from __future__ import annotations
from dataclasses import replace
import pytest
from election_data.people.cec_results import CecResult
from election_data.people.enrich import CEC_SOURCE_ID, attach_name_only, attach_strong, index_results
from election_data.people.manual import Override
from election_data.people.merge import apply_overrides, merge_claims
from election_data.people.model import PersonClaim, PersonDraft, PositionRec, Sourced
pytestmark = pytest.mark.unit
def claim(name: str, role: str, county: str | None = None, **kw: object) -> PersonClaim:
category = "official" if role == "official" else "politician"
pos = PositionRec(role, "org", None, None, (role,))
return PersonClaim(
name=name, role=role, category=category, name_source=role, county=county, positions=(pos,), **kw
) # type: ignore[arg-type]
def result(
name: str, election: str, district: str, county: str | None, birth: str | None = None
) -> CecResult:
kind = "president" if district == "全國" else "mayor"
return CecResult(election, kind, district, county, name, "某黨", "M", birth, "碩士", 100, 0.5, True)
def test_candidate_merges_with_legislator_in_same_county() -> None:
persons, notes = merge_claims(
[
claim("張嘉郡", "legislator", "雲林縣", district="雲林縣第1選舉區"),
claim("張嘉郡", "candidate_2026", "雲林縣"),
]
)
assert len(persons) == 1
assert persons[0].roles == {"legislator", "candidate_2026"}
assert any(n.kind == "merged" for n in notes)
def test_same_name_in_different_counties_is_not_merged() -> None:
persons, notes = merge_claims(
[claim("王某", "mayor", "臺北市"), claim("王某", "candidate_2026", "高雄市")]
)
assert len(persons) == 2
assert [n.kind for n in notes] == ["not_merged"]
def test_two_current_legislators_with_same_name_stay_separate() -> None:
persons, _ = merge_claims([claim("林某", "legislator"), claim("林某", "legislator")])
assert len(persons) == 2
def test_departed_legislator_attaches_only_to_existing_person() -> None:
departed = replace(claim("洪某", "legislator"), current=False, gender=Sourced("M", ("ly",)))
persons, notes = merge_claims(
[claim("洪某", "official"), departed, replace(claim("黃某", "legislator"), current=False)]
)
assert len(persons) == 1
assert persons[0].gender and persons[0].gender.value == "M"
assert persons[0].roles == {"official"}
assert {n.kind for n in notes} == {"departed_attached", "departed_dropped"}
def test_party_conflict_is_reported_and_priority_kept() -> None:
persons, notes = merge_claims(
[
claim("甲", "mayor", "臺北市", party=Sourced("A黨", ("moi",))),
claim("甲", "party_chair", party=Sourced("B黨", ("yaml",))),
]
)
assert persons[0].party == Sourced("A黨", ("moi",))
assert any(n.kind == "conflict" for n in notes)
def test_apply_overrides() -> None:
persons, _ = merge_claims([claim("鍾某", "mayor", "苗栗縣", party=Sourced(None, ("moi",)))])
out, notes = apply_overrides(persons, [Override("鍾某", "party", "中國國民黨", "恢復黨籍", ("news",))])
assert out[0].party == Sourced("中國國民黨", ("news",))
missing, notes2 = apply_overrides(persons, [Override("不存在", "party", None, "", ("x",))])
assert missing == persons and notes2[0].kind == "override_missing"
def draft(name: str, counties: set[str] | None = None, birth: Sourced | None = None) -> PersonDraft:
return PersonDraft(
name=name,
category="politician",
roles=frozenset({"mayor"}),
name_sources=("x",),
counties=frozenset(counties or set()),
birth_date=birth,
)
def test_strong_match_by_county_adds_record_and_official_birth() -> None:
index = index_results([result("蔣某", "2022 直轄市長選舉", "臺北市", "臺北市", "1978-12-26")])
persons, notes, done = attach_strong([draft("蔣某", {"臺北市"})], index)
p = persons[0]
assert p.elections[0].election == "2022 直轄市長選舉" and p.elections[0].source_ids == (CEC_SOURCE_ID,)
assert p.birth_date == Sourced("1978-12-26", (CEC_SOURCE_ID,))
assert done[0] == {"2022 直轄市長選舉"} and notes[0].kind == "strong"
def test_strong_match_ambiguous_when_two_same_name_in_county() -> None:
rs = [
result("許某", "2024 立法委員選舉(區域)", "臺北市第1選舉區", "臺北市"),
result("許某", "2024 立法委員選舉(區域)", "臺北市第5選舉區", "臺北市"),
]
persons, notes, _ = attach_strong([draft("許某", {"臺北市"})], index_results(rs))
assert persons[0].elections == () and notes[0].kind == "ambiguous"
def test_strong_match_rejected_on_birth_conflict() -> None:
rs = [result("蔣某", "2022 直轄市長選舉", "臺北市", "臺北市", "1978-12-26")]
person = draft("蔣某", {"臺北市"}, Sourced("1980-01-01", ("cec-other",)))
persons, notes, _ = attach_strong([person], index_results(rs))
assert persons[0].elections == () and notes[0].kind == "conflict"
def test_name_only_requires_matching_birth() -> None:
rs = [result("賴某", "2024 總統副總統選舉", "全國", None, "1959-10-06")]
index = index_results(rs)
same, notes = attach_name_only([draft("賴某", birth=Sourced("1959-10-06", ("wikidata",)))], index, {})
assert same[0].elections and notes[0].kind == "name_only"
assert same[0].birth_date == Sourced("1959-10-06", (CEC_SOURCE_ID,)) # 官方資料取代 Wikidata
other, notes = attach_name_only([draft("賴某", birth=Sourced("1960-01-01", ("wikidata",)))], index, {})
assert other[0].elections == () and notes[0].kind == "different_birth"
unknown, notes = attach_name_only([draft("賴某")], index, {})
assert unknown[0].elections == () and notes[0].kind == "unverified"
def test_name_only_skips_elections_already_matched() -> None:
rs = [result("蔣某", "2022 直轄市長選舉", "臺北市", "臺北市", "1978-12-26")]
persons, notes = attach_name_only([draft("蔣某")], index_results(rs), {0: {"2022 直轄市長選舉"}})
assert persons[0].elections == () and notes == []