from pathlib import Path import json,csv,datetime,sys ROOT=Path(__file__).resolve().parent def read(name): with (ROOT/name).open(encoding='utf-8-sig',newline='')as f:return list(csv.DictReader(f)) def norm(value):return value.strip() LIMITS='교육용 제품·로트·위치 문자열과 읽기용 CSV 표현을 검사합니다. 판단용 코드·로트·라벨·위치의 양끝 공백을 제외하되 원시 CSV와 자유 설명문은 덮어쓰지 않습니다. 빈값과 공백뿐인 값은 미확인이며 실제 색상·시공 호환성·품질이나 대체 사용 승인을 판정하지 않습니다.' REPORT_HEADERS=['샘플번호','색이름','대응표 대조'] def index_bases(rows): index={} for raw in rows: key=norm(raw['위치']) if key:index.setdefault(key,[]).append(raw) return index def judge(raw,index): ident=norm(raw['샘플번호']);product=norm(raw['제품코드']);lot=norm(raw['로트']);location=norm(raw['사용위치']);label=norm(raw['라벨확인']) candidates=index.get(location,[])if location else[] base=candidates[0]if len(candidates)==1 else None errors=[];different_product=False if not ident:errors.append('샘플번호 미확인') if label!='확인':errors.append('라벨 확인 보완') if not product:errors.append('제품코드 미확인') elif base: base_product=norm(base['제품코드']) if not base_product:errors.append('설치기준 제품코드 미확인') elif product!=base_product:errors.append('제품코드 불일치');different_product=True if not lot:errors.append('로트 미확인') elif base: base_lot=norm(base['로트']) if not base_lot:errors.append('설치기준 로트 미확인') elif lot!=base_lot:errors.append('로트 차이 확인') if not location:errors.append('사용위치 미확인') elif not candidates:errors.append('사용위치 미등록') elif len(candidates)>1:errors.append('설치기준 위치 중복 검토') return {'id':ident,'errors':errors,'matched':not errors,'different_product':different_product} rows=read('example.csv');installed=index_bases(read('input_installed.csv')) matched=[];review=[];report=[];product_mismatch=[] for raw in rows: decision=judge(raw,installed) (matched if decision['matched']else review).append(decision['id']) if decision['different_product']:product_mismatch.append(decision['id']) report.append([raw['샘플번호'],raw['색이름'],' / '.join(decision['errors'])or'표의 제품·로트·위치 일치']) with (ROOT/'results.csv').open('w',encoding='utf-8-sig',newline='')as f: writer=csv.writer(f);writer.writerow(REPORT_HEADERS);writer.writerows(report) with (ROOT/'results.csv').open(encoding='utf-8-sig',newline='')as f:written=list(csv.reader(f)) boundary_index=index_bases(read('boundary_installed.csv'));boundaries=[] for raw in read('boundary_samples.csv'): decision=judge(raw,boundary_index);boundaries.append({'case':raw['case_id'],'errors':decision['errors'],'matched':decision['matched']}) actual={'samples':len(rows),'table_matched':matched,'review_samples':review,'same_colour_name_count':sum(norm(r['색이름'])=='베이지'for r in rows),'product_mismatch':product_mismatch,'csv_report_headers':written[0],'csv_report_rows':written[1:],'whitespace_boundaries':boundaries} 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()] passed=all(c['passed']for c in checks)and set(actual)==set(expected) result={'synthetic_fixture':True,'executed_at_utc':datetime.datetime.now(datetime.timezone.utc).isoformat(),'runtime':sys.version.split()[0],'status':'PASS'if passed else'FAIL','assertion_count':len(checks),'actual':actual,'assertions':checks,'limits':LIMITS} (ROOT/'results.json').write_text(json.dumps(result,ensure_ascii=False,indent=2),encoding='utf-8') print(json.dumps({'status':result['status'],'checks':len(checks),'actual':actual},ensure_ascii=False)) if not passed:raise SystemExit(1)