forked from linkong/planet
42 lines
1.6 KiB
Python
42 lines
1.6 KiB
Python
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from app.core.earth_geography_policy import display_country, display_iso_a3
|
|
|
|
|
|
def test_missing_deployment_policy_has_an_actionable_error(monkeypatch, tmp_path):
|
|
from app.core import earth_geography_policy as policy
|
|
|
|
policy._policy_country_by_code.cache_clear()
|
|
monkeypatch.setattr(policy, "_POLICY_PATH", tmp_path / "missing-policy.json")
|
|
try:
|
|
with pytest.raises(FileNotFoundError, match="EARTH_DISPLAY_POLICY_MISSING"):
|
|
policy.display_country("China")
|
|
finally:
|
|
policy._policy_country_by_code.cache_clear()
|
|
|
|
|
|
def test_display_country_groups_china_regions_and_kosovo() -> None:
|
|
for source in ("Taiwan", "TWN", "Hong Kong", "HKG", "Macao", "MAC", "中国(台湾)"):
|
|
assert display_country(source) == "中国"
|
|
assert display_iso_a3(source) == "CHN"
|
|
for source in ("Kosovo", "XK", "SRB"):
|
|
assert display_country(source) == "塞尔维亚"
|
|
assert display_iso_a3(source) == "SRB"
|
|
|
|
|
|
def test_display_country_keeps_other_country_and_rejects_unknown() -> None:
|
|
assert display_country("Japan") == "日本"
|
|
assert display_iso_a3("Japan") == "JPN"
|
|
assert display_country("unexpected non-country") is None
|
|
|
|
|
|
def test_country_projection_agrees_with_versioned_boundary_rules() -> None:
|
|
path = Path(__file__).resolve().parents[2] / "config/earth-boundary-pov-policy.china-v1.json"
|
|
policy = json.loads(path.read_text(encoding="utf-8"))
|
|
for rule in policy["rules"]:
|
|
for code in rule.get("sourceIsoA3", []):
|
|
assert display_iso_a3(code) == rule.get("targetIsoA3", rule.get("parentIsoA3"))
|