from pathlib import Path import csv,json,datetime,sys ROOT=Path(__file__).resolve().parent def clean(value):return str(value or '').strip() def known(value):return clean(value).lower() not in {'','미확인','미상','unknown'} def j(row,key):return clean(row.get(key)) def read(root,name): with (root/name).open(encoding='utf-8-sig',newline='') as f:return list(csv.DictReader(f)) LIMITS='보관·인계의 가상 기록을 대조합니다. 실제 수령인을 확인하거나 물품 분실·보관 책임을 판정하지 않습니다. 개인정보는 입력하지 않았습니다.' REPORT_HEADERS=['참조번호', '보관구역', '인계 대조 결과'] def compute(root=ROOT): data=read(root,'example.csv');zones={j(r,'구역'):j(r,'용도') for r in read(root,'input_zones.csv') if j(r,'구역')};events={j(r,'인계참조'):r for r in read(root,'input_handoffs.csv') if j(r,'인계참조') and j(r,'참조번호')};bad=[];unconfirmed=[];confirmed=[];report=[];reviews=[] for r in data: rid=j(r,'참조번호');zone=j(r,'보관구역');state=j(r,'기록상태');ref=j(r,'인계참조');e=[] if not rid:e.append('참조번호 미확인') if not zone or zones.get(zone)!='승인된 임시보관':bad.append(rid);e.append('보관 구역 확인 필요') if state=='인계기록': event=events.get(ref) if ref else None if not rid or not ref or not event or j(event,'참조번호')!=rid or not known(j(event,'확인시각')) or not known(j(event,'확인역할')):unconfirmed.append(rid);e.append('인계 근거 없음') else:confirmed.append(rid) elif state!='보관중':e.append('기록 상태 미확인') if e:reviews.append({'id':rid,'reasons':e}) report.append([rid,zone,' / '.join(e) or ('인계자료 연결됨' if state=='인계기록' else '보관 기록')]) actual={'records':len(data),'non_storage_zone':bad,'confirmed_handoffs':confirmed,'unconfirmed_handoffs':unconfirmed,'recorded_held':[j(r,'참조번호') for r in data if j(r,'기록상태')=='보관중'],'unverified_items_counted_as_collected':False} return actual,report,{'review':reviews,'policy':'Both handoff references and parcel identifiers must be known;matching blank references never confirms handoff.'} def run(root=ROOT): actual,rows,details=compute(root);expected=json.loads((root/'expected.json').read_text(encoding='utf-8')) checks=[{'field':k,'expected':v,'actual':actual.get(k),'passed':actual.get(k)==v} for k,v in expected.items()] result={'synthetic_fixture':True,'executed_at_utc':datetime.datetime.now(datetime.timezone.utc).isoformat(),'runtime':sys.version.split()[0],'status':'PASS' if all(c['passed'] for c in checks) else 'FAIL','assertion_count':len(checks),'actual':actual,'assertions':checks,'details':details,'limits':LIMITS} (root/'results.json').write_text(json.dumps(result,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(result,ensure_ascii=False,indent=2));return result if __name__=='__main__': r=run() if r['status']!='PASS':raise SystemExit(1)