from pathlib import Path import csv,json,platform,datetime,sys HERE=Path(__file__).resolve().parent def clean(value):return str(value or '').strip() def read_csv(root,name): with (root/'input'/f'{name}.csv').open(encoding='utf-8-sig',newline='') as f:return list(csv.DictReader(f)) REPORT_HEADERS=['지점', '원래 기록상태', '정규화 상태', '판정', '사유'] def compute(root=HERE): source=read_csv(root,'original-example');partial=[];seen=[];report_rows=[];review=[] allowed_observed={'확인','물방울 관찰'} for r in source: status=clean(r.get('기록 상태'));observed=status in allowed_observed if observed:seen.append(r['지점']);reason='정의된 관찰 상태' else: partial.append(r['지점']);reason='미확인 범위 포함' if '미확인' in status else ('기록 상태 없음' if not status else '허용 목록에 없는 상태') review.append({'area':r['지점'],'state':status,'reason':reason}) report_rows.append([r['지점'],r.get('기록 상태',''),status,'관찰 범위 기록됨' if observed else '미확인·검토',reason]) claimed=clean(read_csv(root,'claim')[0].get('all_areas_completed'))=='yes' actual={'areas':len(source),'observed_within_scope':seen,'partially_unseen':partial,'recorded_photo_numbers':[r['사진번호'] for r in source],'all_areas_completed':not partial,'bad_complete_claim_rejected':claimed and bool(partial)} return actual,report_rows,{'state_policy':{'observed':sorted(allowed_observed),'other':'미확인·검토;unknown or blank is never counted as observed'},'review_rows':review} def run(root=HERE): actual,rows,details=compute(root) expected=json.loads((root/'expected/report.json').read_text(encoding='utf-8')) checks=[{'field':k,'pass':actual.get(k)==v,'actual':actual.get(k),'expected':v} for k,v in expected.items()] passed=all(c['pass'] for c in checks) and set(actual)==set(expected) report={'lab_id':root.name,'synthetic_fixture':True,'environment':{'python':platform.python_version(),'platform':platform.system(),'processor':'Python standard library'},'run_utc':datetime.datetime.now(datetime.timezone.utc).isoformat(),'status':'PASS' if passed else 'FAIL','assertion_count':len(checks),'actual':actual,'assertions':checks,'details':details,'limits':'Synthetic record checks only;not a real building inspection,safety/legal verdict,Excel app test,physical receipt or authenticity verification.'} (root/'results.json').write_text(json.dumps(report,ensure_ascii=False,indent=2),encoding='utf-8') (root/'actual.json').write_text(json.dumps(actual,ensure_ascii=False,indent=2),encoding='utf-8') with (root/'results.csv').open('w',encoding='utf-8-sig',newline='') as f: w=csv.writer(f);w.writerow(REPORT_HEADERS);w.writerows(rows) print(json.dumps(report,ensure_ascii=False,indent=2)) return report if __name__=='__main__': report=run() if report['status']!='PASS':raise SystemExit(1)