Files

63 lines
2.3 KiB
Python

#!/usr/bin/env python3
"""Verify the ISO 3166-1 alpha-3 country-code extraction.
Exit code 0 = correct, 1 = any check failed.
Pure assertions on the artifacts; no network, no external data.
"""
import csv
import json
import sys
OUT = "/root/.hermes/wizard-outputs/ledger"
csv_path = f"{OUT}/iso3_alpha3.csv"
json_path = f"{OUT}/iso3_alpha3.json"
failures = []
def check(name, cond, msg=None):
if not cond:
failures.append((name, msg or ""))
# --- CSV -------------------------------------------------------------------
with open(csv_path, encoding="utf-8", newline="") as fh:
reader = csv.DictReader(fh)
rows = list(reader)
headers = reader.fieldnames
check("csv.exists", True)
check("csv.headers", headers == ["alpha_3", "country_name"], f"got {headers}")
check("csv.rowcount", len(rows) == 249, f"got {len(rows)} rows, expected 249")
check("csv.no_empty", all(r.get("alpha_3") and r.get("country_name") for r in rows))
check("csv.no_dup_codes", len({r["alpha_3"] for r in rows}) == 249)
check("csv.sorted", [r["country_name"] for r in rows] == sorted(
r["country_name"] for r in rows))
check("csv.code_format", all(len(r["alpha_3"]) == 3 and r["alpha_3"].isupper()
and r["alpha_3"].isalpha() for r in rows))
by_name = {r["country_name"]: r["alpha_3"] for r in rows}
check("csv.abw", by_name.get("Aruba") == "ABW", by_name.get("Aruba"))
check("csv.usa", by_name.get("United States of America") == "USA", by_name.get("United States of America"))
check("csv.vnm", by_name.get("Viet Nam") == "VNM", by_name.get("Viet Nam"))
check("csv.zwe", by_name.get("Zimbabwe") == "ZWE", by_name.get("Zimbabwe"))
# --- JSON ------------------------------------------------------------------
with open(json_path, encoding="utf-8") as fh:
jrows = json.load(fh)
check("json.exists", True)
check("json.rowcount", len(jrows) == 249)
check("json.keys", all(list(r.keys()) == ["alpha_3", "country_name"] for r in jrows))
check("json.match_csv", jrows == rows)
# --- Cross-source sanity (none) --------------------------------------------
print("iso3_alpha3: rows =", len(rows))
print("alpha_3 range:", rows[0]["alpha_3"], "->", rows[-1]["alpha_3"])
if failures:
print("FAILED CHECKS:")
for name, msg in failures:
print(f" - {name}" + (f": {msg}" if msg else ""))
sys.exit(1)
print("ALL CHECKS PASSED")