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='건봄의 합성 상태표와 중복된 급수 기록을 대조합니다. 식물의 실제 수분 요구량·건강·권장 급수량을 판정하지 않습니다. 500mL는 가상 기록값입니다.' REPORT_HEADERS=['화분번호', '기록에 따른 다음 확인'] def compute(root=ROOT): data=read(root,'example.csv');candidate=[];hold=[];missing=[];report=[] for r in data: rid=j(r,'화분번호');identified=j(r,'식물식별');soil=j(r,'흙표면');weight=j(r,'화분무게느낌');guide=j(r,'관리안내확인') if not rid or identified!='확인' or guide!='확인' or soil not in {'마름','촉촉함'} or weight not in {'가벼움','무거움'}:decision='정보 보완';missing.append(rid) elif soil=='마름' and weight=='가벼움':decision='관리계획에 따른 급수 검토 후보';candidate.append(rid) else:decision='일괄 급수 보류';hold.append(rid) report.append([rid,decision]) events=read(root,'input_water_events.csv');seen={};duplicates=[] for event in events: key=event['급수기록번호'] if key in seen: if seen[key]!=event:raise ValueError('충돌 기록: '+key) duplicates.append(key) else:seen[key]=event actual={'pots':len(data),'check_candidates':candidate,'routine_watering_held':hold,'needs_context':missing,'duplicate_event_ids':duplicates,'raw_record_ml':sum(int(e['기록량_ml']) for e in events),'distinct_event_ml':sum(int(e['기록량_ml']) for e in seen.values())} return actual,report,{'policy':'Unknown soil/weight/plant context is information-needed,not an established hold or watering recommendation.'} 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)