Files

47 lines
1.7 KiB
Python

import csv, re, sys
PATH = sys.argv[1] if len(sys.argv) > 1 else '/root/troupe-standup/wizard-work/ledger/gdp_nominal.csv'
with open(PATH, newline='', encoding='utf-8') as f:
rows = list(csv.reader(f))
FAIL = 0
def check(cond, msg):
global FAIL
if not cond:
print('FAIL:', msg)
FAIL = 1
check(len(rows) > 0, 'file empty')
header = rows[0]
check(header == ['Country/Territory', 'IMF(2026)', 'World Bank(2025)', 'United Nations(2024)'],
'bad header: ' + str(header))
data = rows[1:]
check(len(data) == 222, 'expected 222 data rows, got %d' % len(data))
check(data[0] == ['World', '128,003,776', '118,350,166', '111,565,144'],
'bad first row: ' + str(data[0]))
check(data[-1] == ['Tuvalu', '65', '57', '56'], 'bad last row: ' + str(data[-1]))
def find(name):
hits = [r for r in data if r[0] == name or name in r[0]]
return hits[0] if hits else None
us = find('United States')
check(us is not None and us[1] == '32,642,373', 'US row wrong: ' + str(us))
ch = find('China')
check(ch is not None and ch[1] == '21,546,403', 'China row wrong: ' + str(ch))
de = find('Germany')
check(de is not None and de[1] == '5,471,299', 'Germany row wrong: ' + str(de))
# numeric-ish cells: digits/commas optionally with (year); or an explicit "N/A" marker
numpat = re.compile(r'^[\d,]+(\s*\([\d]+\))?$')
na_vals = ('', '—', '—N/a', '—N/A', 'N/a', 'N/A', 'n/a', 'NA')
bad = [(i, r[0], c) for i, r in enumerate(data) for c in r[1:] if c not in na_vals and not numpat.fullmatch(c)]
check(not bad, 'non-numeric numeric-cells: %s' % bad[:3])
print('header:', header)
print('rows:', len(data))
print('result:', 'PASS' if FAIL == 0 else 'FAIL')
sys.exit(FAIL)