Files
quality_analyse/verification/test_history_metric_row_metadata.py
T

55 lines
4.1 KiB
Python

"""Hidden metricCode types a generic value cell without becoming a display column."""
import copy,sys,unittest
from pathlib import Path
sys.path[:0]=[str(Path(__file__).resolve().parents[1]/'scripts'),str(Path(__file__).parent)]
from nao_history_report import _validate_section,_history_section_markdown,HistorySnapshotError
class HistoryMetricRowMetadataTests(unittest.TestCase):
def section(self):
return {'title':'正式指标汇总','sourceTurnId':'confirmed-source','period':{'year':2026,'month':9},'filters':{},
'columns':[{'key':'stage','label':'阶段'},{'key':'field','label':'指标'},{'key':'value','label':'正式值'}],
'rows':[{'stage':'综合','field':'合格率','metricCode':'rate','value':0.9512973521079284},
{'stage':'黑片','field':'报废率','metricCode':'scrapRate','value':0.027276306425167},
{'stage':'黑片','field':'投入重量','metricCode':'totalWeight','value':1407111.7961},
{'stage':'黑片','field':'报废重量','metricCode':'scrapWeight','value':38375.28067}]}
def test_known_metadata_is_preserved_and_types_only_value_cell(self):
section=self.section();before=copy.deepcopy(section)
_validate_section(section,{'confirmed-source'})
text=_history_section_markdown({'visuals':[{'unit':'kg','period':section['period'],'scope':{},
'originTurnId':'confirmed-source','metric':'scrap_delta'}]},section)
self.assertEqual(section,before);self.assertNotIn('metricCode',text)
self.assertIn('95.13%',text);self.assertIn('2.73%',text)
self.assertIn('1,407,111.7961 kg',text);self.assertIn('38,375.28067 kg',text)
self.assertNotIn('0.951297',text)
def test_unknown_metadata_and_wrong_types_remain_rejected(self):
for key,value in [('metricCode','not_a_metric'),('metricCode',None),('metricCode',1),('metricCode',True),
('metricCode',['rate']),('extraUnknown',1),('value',True),('value',{}),('value','0.95')]:
with self.subTest(key=key,value=value):
section=self.section();section['rows'][0][key]=value
with self.assertRaises(HistorySnapshotError):_validate_section(section,{'confirmed-source'})
def test_missing_observation_is_not_zero_and_metadata_is_never_a_public_column(self):
section=self.section();section['rows'][0]['value']=None
section['columns'].append({'key':'metricCode','label':'内部类型'})
_validate_section(section,{'confirmed-source'})
text=_history_section_markdown({'visuals':[]},section)
self.assertIn('| 综合 | 合格率 | — |',text);self.assertNotIn('内部类型',text);self.assertNotIn('metricCode',text)
def test_weight_units_ignore_ratio_chart_and_unknown_period_stays_original_unit(self):
section=self.section();rate={'originTurnId':'confirmed-source','period':section['period'],'scope':{},'metric':'composite_rate','unit':'ratio'}
mass={**rate,'metric':'scrap_delta','unit':'kg'}
text=_history_section_markdown({'visuals':[rate,mass]},section)
self.assertIn('38,375.28067 kg',text);self.assertNotIn(' ratio',text);self.assertIn('95.13%',text)
earlier=copy.deepcopy(section);earlier['period']={'year':2026,'month':8}
text=_history_section_markdown({'visuals':[rate,mass]},earlier)
self.assertIn('38,375.28067(原单位)',text);self.assertNotIn(' kg',text)
def test_chart_unit_cannot_cross_source_scope_or_explicit_stage(self):
section=self.section();section['rows']=section['rows'][2:]
chart={'originTurnId':'confirmed-source','period':section['period'],'scope':{},'metric':'scrap_delta','unit':'kg'}
for change in ({'originTurnId':'other-source'},{'scope':{'department':'其他范围'}},{'stage':'bright'}):
with self.subTest(change=change):
text=_history_section_markdown({'visuals':[{**chart,**change}]},section)
self.assertIn('38,375.28067(原单位)',text);self.assertNotIn(' kg',text)
if __name__=='__main__':unittest.main()