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

88 lines
3.2 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from pathlib import Path
import openpyxl
import pytest
from election_data.attributes import _coordinates, _town_rows, parse_hakka, parse_income
from election_data.population import parse_indigenous, parse_population
pytestmark = pytest.mark.unit
def population_row(
code: str = "68000010001", ages: dict[int, int] | None = None, total: int | None = None
) -> dict:
ages = ages or {}
row = {"district_code": code, "site_id": "桃園市桃園區", "village": "示例里"}
for a in range(100):
row[f"people_age_{a:03d}_m"] = str(ages.get(a, 0))
row[f"people_age_{a:03d}_f"] = "0"
row["people_age_100up_m"] = str(ages.get(100, 0))
row["people_age_100up_f"] = "0"
row["people_total"] = str(total if total is not None else sum(ages.values()))
return row
class TestPopulation:
def test_bands_and_adults(self) -> None:
[p] = parse_population([population_row(ages={5: 10, 20: 3, 29: 2, 64: 4, 65: 1, 100: 2})])
assert p.band_counts() == {"20-29": 5, "30-39": 0, "40-49": 0, "50-64": 4, "65+": 3}
assert p.adults == 12
assert p.total == 22
def test_mismatched_total_is_logged(self, caplog: pytest.LogCaptureFixture) -> None:
parse_population([population_row(ages={30: 5}, total=9)])
assert "不符" in caplog.text
def test_indigenous(self) -> None:
row = {
"district_code": "68000130001",
"indigenous_plain_total_m": "3",
"indigenous_plain_total_f": "4",
"indigenous_mountain_total_m": "10",
"indigenous_mountain_total_f": "",
}
[i] = parse_indigenous([row])
assert (i.lowland, i.highland) == (7, 10)
def test_parse_income_normalizes_and_skips_totals() -> None:
text = (
'"縣市別","村里","納稅單位(戶)","綜合所得總額","平均數","中位數"\n'
'"台北市松山區","中崙里","1464","2745453","1875","857"\n'
'"臺北市松山區","合計","9","9","9","9"\n'
'"臺北市松山區","壞資料","x","1","1","1"\n'
)
[row] = parse_income(text)
assert (row.site, row.village, row.households, row.median) == ("臺北市松山區", "中崙里", 1464, 857.0)
def test_parse_hakka() -> None:
text = "area_name,AreaCode,Ratio\n68000 ,68000040 ,0.71 \nbad,123,0.5\n"
assert parse_hakka(text) == {"68000040": 0.71}
def test_town_rows_reads_two_space_rows(tmp_path: Path) -> None:
wb = openpyxl.Workbook()
ws = wb.active
ws.append(["表1 常住人口"])
ws.append([" 桃園市", 100])
ws.append(["  桃園區", 10, 5])
ws.append(["  Taoyuan Dist"])
path = tmp_path / "t.xlsx"
wb.save(path)
assert _town_rows(path) == {"桃園區": [10, 5]}
def test_coordinates_filters_bad_points(tmp_path: Path) -> None:
xml = tmp_path / "t.xml"
xml.write_text(
"<ArrayOfOpenData_3>"
"<OpenData_3><負責人>某人</負責人><WGS84X>121.3</WGS84X><WGS84Y>24.9</WGS84Y></OpenData_3>"
"<OpenData_3><WGS84X></WGS84X><WGS84Y></WGS84Y></OpenData_3>"
"<OpenData_3><WGS84X>0</WGS84X><WGS84Y>0</WGS84Y></OpenData_3>"
"</ArrayOfOpenData_3>",
encoding="utf-8",
)
assert list(_coordinates(xml)) == [(121.3, 24.9)]