53 lines
1.8 KiB
Python
53 lines
1.8 KiB
Python
import csv, sys
|
|
|
|
PATH = "/root/.hermes/wizard-outputs/ledger/iso3166_alpha2.csv"
|
|
|
|
def fail(msg):
|
|
print("FAIL:", msg)
|
|
sys.exit(1)
|
|
|
|
try:
|
|
with open(PATH, newline="", encoding="utf-8") as f:
|
|
reader = csv.reader(f)
|
|
rows = list(reader)
|
|
except Exception as e:
|
|
fail("cannot read file: %s" % e)
|
|
|
|
if not rows:
|
|
fail("file is empty")
|
|
|
|
header, data = rows[0], rows[1:]
|
|
if header != ["Code", "Country", "Year", "ccTLD"]:
|
|
fail("bad header: %r" % header)
|
|
if len(data) != 249:
|
|
fail("expected 249 data rows, got %d" % len(data))
|
|
if any(len(r) != 4 or any(c == "" for c in r) for r in data):
|
|
fail("a row is malformed or empty")
|
|
|
|
codes = [r[0] for r in data]
|
|
if len(set(codes)) != 249:
|
|
fail("codes not unique")
|
|
if any(not (r[2].isdigit() and len(r[2]) == 4) for r in data):
|
|
fail("a Year is not a 4-digit year")
|
|
if any(len(r[3]) < 3 or not r[3].startswith(".") or not r[3][1:].isalpha() for r in data):
|
|
fail("a ccTLD is not '.'-prefixed 2-3 alpha chars")
|
|
|
|
expect = {
|
|
"US": ("United States of America", "1974", ".us"),
|
|
"GB": ("United Kingdom of Great Britain and Northern Ireland", "1974", ".gb"),
|
|
"KR": ("Korea, Republic of", "1974", ".kr"),
|
|
"TW": ("Taiwan, Province of China", "1974", ".tw"),
|
|
"PS": ("Palestine, State of", "1999", ".ps"),
|
|
"AU": ("Australia", "1974", ".au"),
|
|
"AX": ("Åland Islands", "2004", ".ax"),
|
|
}
|
|
for code, (name, year, cctld) in expect.items():
|
|
row = next((r for r in data if r[0] == code), None)
|
|
if row is None:
|
|
fail("missing code %s" % code)
|
|
if row[1] != name or row[2] != year or row[3] != cctld:
|
|
fail("code %s row mismatch: %r" % (code, row))
|
|
|
|
print("PASS: header + 249 rows + unique codes + known-rows verified")
|
|
print("rows read: %d, distinct codes: %d" % (len(data), len(set(codes))))
|