import json,collections,xml.etree.ElementTree as ET from decimal import Decimal from pathlib import Path P=Path('scripts/bom-import') for target in ['local','remote']: plan=json.loads((P/f'{target}-plan.json').read_text(encoding='utf-8')) db=ET.parse(P/f'{target}-snapshot.xml') materials={int(x.findtext('id')):{e.tag:e.text or '' for e in x} for x in db.findall('./materials/row')} units={x.findtext('id'):x.findtext('code') for x in db.findall('./units/row')} assert len(plan['lines'])+len(plan['issues'])+len(plan['duplicates'])==690 pairs=set() for x in plan['lines']: p,c=materials[x['parent_id']],materials[x['child_id']] assert (x['parent_id'],x['child_id']) not in pairs pairs.add((x['parent_id'],x['child_id'])) assert (p['code'],c['code'])==(x['parent_code'],x['child_code']) assert (p['unit_measure_id'],c['unit_measure_id'])==(x['parent_unit_id'],x['child_unit_id']) assert units[c['unit_measure_id']]==x['system_unit'] q=Decimal(x['system_quantity']) assert q>0 and q==q.quantize(Decimal('0.000001')) assert abs(q-Decimal(x['quantity'])*Decimal(x['factor']))<=Decimal('0.0000005') if not x['section'] and x['file'].endswith('.xlsx'): assert ''.join(p['name'].split())==''.join(x['sheet'].split()) sample={(x['parent_code'],x['child_code']):x for x in plan['lines']} assert Decimal(sample['TC0001','81003811']['system_quantity'])==25 assert Decimal(sample['TC0001','61900874']['system_quantity'])==Decimal('0.21') assert Decimal(sample['TC0014','74001501']['system_quantity'])==30 assert sample['71500051','61901777']['section']=='鱼胶单碗' assert Decimal(sample['71500050','61901036']['system_quantity'])==Decimal('0.0135') assert not plan['issues'] if target=='remote' else len(plan['issues'])==15 result=(P/f'{target}-result.txt').read_text(encoding='utf-8') assert f'verified relations={len(plan["lines"])}' in result post=ET.parse(P/f'{target}-post-snapshot.xml') actual=collections.defaultdict(list) for row in post.findall('./bom/row'):actual[(int(row.findtext('item_id')),int(row.findtext('bom_item_id')))].append(row) for x in plan['lines']: rows=actual[x['parent_id'],x['child_id']] assert len(rows)==1 and Decimal(rows[0].findtext('quantity'))==Decimal(x['system_quantity']) print(target,'source coverage, exact TC names, units, quantities, semi-finished hierarchy and committed result checks passed')