import io import json import unittest from contextlib import redirect_stdout from unittest.mock import call, patch from apps.console.tools.fill_check import QUESTION_FIELDS, SEGMENT_FIELDS, main, rates class FillRateTests(unittest.TestCase): # 每栏只在用得上的题里算:没有房间的题不进房间那栏的分母;写死为空(rooms={})的算没填 def test_denominator_is_questions_that_use_the_field(self): room = {'1': {'method': 'instant.event', 'fill': {'event': {'method': 'event.enter_area', 'fill': {'place': {'method': 'area.room', 'value': {'room': 'kitchen'}}}}}}} plain = {'1': {'method': 'instant.clock_second', 'value': {}}} rows = [({'slots': room, 'bindings': {'rooms': {}}, 'event_readings': []}, None), ({'slots': room, 'bindings': {'rooms': {'1.event.place': 'region-2'}}, 'event_readings': [{'event_id': '1.event'}]}, {'scoring': {'status': 'unknown'}}), ({'slots': plain, 'bindings': {}}, None)] r = rates(rows, QUESTION_FIELDS) self.assertEqual(r['房间绑定(俯视图高亮)'], (1, 2)) self.assertEqual(r['事件时刻(时间轴事件刻)'], (1, 2)) self.assertEqual(r['答案判分(容差)'], (0, 3)) self.assertEqual(r['两路锚定(多机身)'], (0, 0)) def test_hard_coded_placeholders_are_not_filled(self): segs = [({'selfcheck_conclusion': 'NOT_CHECKED', 'idle_remainder_fraction': None},), ({'selfcheck_conclusion': 'pass', 'idle_remainder_fraction': .2},)] self.assertEqual(rates(segs, SEGMENT_FIELDS), {'自检结论': (1, 2), '剩余原地占比': (1, 2)}) class FetchPathTests(unittest.TestCase): def test_main_fetches_current_urls_with_encoded_filenames(self): segment = 'run__段 /#?%+' ids = ['q /#?%+', 'q2'] index = {'questions': [{'segment': segment, 'instance_id': qid} for qid in ids]} answers = {qid: {'scoring': {'status': 'known'}} for qid in ids} detail = {'render_stale': []} segments = {'segments': [{'id': segment, 'selfcheck_conclusion': 'pass', 'idle_remainder_fraction': .2}]} base = 'http://127.0.0.1:19042' urls = [base + path for path in ( '/evidence/ev/questions.json', '/evidence/ev/run__%E6%AE%B5%20%2F%23%3F%25%2B.answers.json', '/evidence/ev/q/q%20%2F%23%3F%25%2B.json', '/evidence/ev/q/q2.json', '/api/v1/segments', )] payloads = dict(zip(urls, (index, answers, detail, detail, segments))) def respond(url, timeout): return io.BytesIO(json.dumps(payloads[url]).encode('utf-8')) for suffix in ('', '/'): with self.subTest(base_suffix=suffix): output = io.StringIO() with patch('apps.console.tools.fill_check.urllib.request.urlopen', side_effect=respond) as fetch: with redirect_stdout(output): result = main([base + suffix, '--run', 'run']) self.assertEqual(result, 0) self.assertEqual(fetch.call_args_list, [call(url, timeout=20) for url in urls]) self.assertIn('渲染验收台抽 2 道,剧本验收台 1 段', output.getvalue()) self.assertIn('答案判分(容差): 2/2', output.getvalue())