from pathlib import Path import csv,json,datetime as _clock,platform,copy BASE=Path(__file__).resolve().parent NORMALIZE_FIELDS={'example.csv': ['표지', '현재지점', '다음지점', '목적지', '보임', '방향확인']} LIMITS_TEXT='합성 입력만 사용하며 실제 현장·안전·법률·성능 판정을 수행하지 않습니다.' REPORT_FILE='result.csv' def compute(overrides=None): judgement_notes={} def read_judged(name): if overrides is not None and name in overrides:raw=copy.deepcopy(overrides[name]) else: with (BASE/name).open(encoding='utf-8-sig',newline='')as f:raw=list(csv.DictReader(f)) fields=NORMALIZE_FIELDS.get(name,[]) return [dict(row,**{key:row.get(key,'').strip()for key in fields})for row in raw] from pathlib import Path import csv,json,platform,datetime HERE=Path(__file__).resolve().parent source=read_judged('example.csv') route=['입구 A','갈림길 J','통로 K','승강기 E'];destination='승강기 E';out=[] for point,nxt in zip(route,route[1:]): candidates=[r for r in source if r['현재지점']==point and r['목적지']==destination];issues=[] if not candidates:issues=['해당 목적지 표지 없음'];sign='' else: r=candidates[0];sign=r['표지'] if not r['다음지점']:issues.append('연결 지점 미확인') elif r['다음지점']!=nxt:issues.append('연결 지점 다름') if r['보임']not in {'yes','no'}:issues.append('보임 미확인') elif r['보임']=='no':issues.append('가림 기록') if r['방향확인']!='yes':issues.append('방향 미확인') out.append({'지점':point,'표지':sign,'검토사항':'; '.join(issues),'정보연결확인':not issues}) actual={'결과':out,'정보보완지점':[r['지점'] for r in out if not r['정보연결확인']],'같은출발점다른목적지표지':[r['표지'] for r in source if r['현재지점']=='입구 A' and r['목적지']!=destination],'교통안전설계수행':False} if 'REPORT_HEADERS'in locals():headers=REPORT_HEADERS;output_rows=report elif '결과'in actual: output=actual['결과'];headers=list(output[0])if output else['결과'];output_rows=[[('예'if v is True else'아니오'if v is False else''if v is None else v)for v in row.values()]for row in output] else: headers=['검사항목','실제 결과'];output_rows=[[k,json.dumps(v,ensure_ascii=False)if isinstance(v,(list,dict))else v]for k,v in actual.items()] return actual,headers,output_rows,judgement_notes def at_path(value,path): for token in path: if isinstance(token,dict):value=next(x for x in value if all(x.get(k)==v for k,v in token.items())) else:value=value[token] return value def compare(value,op,expected): if op=='eq':return value==expected if op=='contains':return expected in value if op=='not_contains':return expected not in value raise ValueError('Unknown assertion operator') actual,headers,rows,notes=compute() expected=json.loads((BASE/'expected.json').read_text(encoding='utf-8')) base_checks=[{'field':k,'actual':actual.get(k),'expected':v,'pass':actual.get(k)==v,'passed':actual.get(k)==v}for k,v in expected.items()] fixture_inputs=json.loads((BASE/'boundary-inputs.json').read_text(encoding='utf-8')) fixture_expected=json.loads((BASE/'boundary-expected.json').read_text(encoding='utf-8')) boundary_results=[] for fixture in fixture_inputs: value,_,_,case_notes=compute(fixture['files']);oracle=fixture_expected[fixture['id']];assertions=[] if oracle.get('baseline_equivalent'): assertions=[{'path':[k],'actual':value.get(k),'expected':v,'pass':value.get(k)==v}for k,v in expected.items()] else: for assertion in oracle['assertions']: found=at_path(value,assertion['path']);ok=compare(found,assertion['op'],assertion['expected']) assertions.append(dict(assertion,actual=found,pass_=ok)) for assertion in assertions:assertion['pass']=assertion.pop('pass_') boundary_results.append({'id':fixture['id'],'description':fixture['description'],'actual':value,'judgement_notes':case_notes,'assertions':assertions,'pass':all(x['pass']for x in assertions)}) passed=all(x['pass']for x in base_checks)and all(x['pass']for x in boundary_results) report={'status':'PASS'if passed else'FAIL','synthetic_fixture':True,'executed_at_utc':_clock.datetime.now(_clock.timezone.utc).isoformat(),'python':platform.python_version(),'actual':actual,'judgement_notes':notes,'assertions':base_checks,'checks':base_checks,'regression_status':'PASS'if all(x['pass']for x in boundary_results)else'FAIL','regression_cases':boundary_results,'limits':LIMITS_TEXT} (BASE/'results.json').write_text(json.dumps(report,ensure_ascii=False,indent=2),encoding='utf-8') (BASE/'regression-results.json').write_text(json.dumps(boundary_results,ensure_ascii=False,indent=2),encoding='utf-8') with (BASE/REPORT_FILE).open('w',encoding='utf-8-sig',newline='')as f: writer=csv.writer(f);writer.writerow(headers);writer.writerows(rows) print(json.dumps({'status':report['status'],'baseline_fields':len(base_checks),'boundary_cases':len(boundary_results),'actual':actual},ensure_ascii=False)) if not passed:raise SystemExit(1)