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

43 lines
1.4 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.

import pytest
from election_data.codes import cec_village_code, county_code, normalize_name, roc_to_iso_date, town_code
pytestmark = pytest.mark.unit
def test_cec_village_code_matches_villcode() -> None:
# 文件中的例子:63/000/010/0002 → 63000010002
assert cec_village_code("63", "000", "010", "0002") == "63000010002"
def test_cec_village_code_rejects_cross_village_and_aggregates() -> None:
assert cec_village_code("68", "000", "010", "A001") is None
assert cec_village_code("68", "000", "010", "0000") is None
assert cec_village_code("68", "000", "000", "0000") is None
def test_cec_village_code_rejects_four_digit_village() -> None:
with pytest.raises(ValueError):
cec_village_code("68", "000", "010", "1001")
def test_county_and_town_codes() -> None:
assert county_code("68", "000") == "68000"
assert town_code("68", "000", "010") == "68000010"
@pytest.mark.parametrize(
("raw", "expected"),
[("台北市", "臺北市"), (" 桃園區 ", "桃園區"), ("AB里", "AB里")],
)
def test_normalize_name(raw: str, expected: str) -> None:
assert normalize_name(raw) == expected
@pytest.mark.parametrize(
("raw", "expected"),
[("0481006", "1959-10-06"), ("481006", "1959-10-06"), ("113/01/13", "2024-01-13"), ("abc", None)],
)
def test_roc_to_iso_date(raw: str, expected: str | None) -> None:
assert roc_to_iso_date(raw) == expected