VEHwang's picture
Upload CoCogni4D-Bench (questions, media, scripts, render records, code) (part 6)
5afd2f7 verified
Raw History Blame Contribute Delete
3.18 kB
"""Schema cache invalidation and incomplete checking-chain rows."""
import json
import os
import tempfile
import unittest
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import patch
from apps.console.server.batch_commands import BatchCommands
def check_schema_cache(tmp_path):
paths = [tmp_path / name for name in ('catalog', 'packages', 'result')]
paths[0].mkdir()
for p in paths[1:]:
p.write_text('{}')
commands = BatchCommands(*paths)
with patch('apps.console.server.batch_commands.subprocess.run', return_value=SimpleNamespace(returncode=0, stdout='{"commands": {}}')) as run: # np plan 的输出带 commands,控制台往里补 convert(79adbce8)
commands.command('schema')
commands.command('schema')
assert run.call_count == 1
for i, p in enumerate(paths):
old = p.stat().st_mtime_ns
os.utime(p, ns=(old, old + 1000000))
commands.command('schema')
assert run.call_count == i + 2
for name in ('plan', 'validate'):
before = run.call_count
commands.command(name, {})
commands.command(name, {})
assert run.call_count == before + 2
def check_questions_for_error_row(tmp_path):
from apps.console.server import segment_conversion as module
segment = tmp_path / 'segment'
segment.mkdir()
good = dict(candidate_id='good', decision={'result':'PASS'}, public={'version_id':'v', 'text':{'zh':[{'text':'题面'}]}}, binding={'local_span_s':[1,2]})
error = dict(candidate_id='bad', decision={'result':'ERROR'})
(segment / 'questions.jsonl').write_text('\n'.join(map(json.dumps, [good, error])))
rows, detail, counts = module.questions_for('segment', tmp_path)
assert [r['id'] for r in rows] == ['good']
assert list(detail) == ['good']
assert counts == {'PASS':1, 'errors':1}
assert module.questions_for('missing', tmp_path) == ([], {}, {})
def check_hashed_assets_cacheable(tmp_path):
from apps.console.server.http_app import Application
from apps.console.server.registry import Registry
(tmp_path / 'assets').mkdir()
(tmp_path / 'index.html').write_text('<html></html>')
(tmp_path / 'assets' / 'app-abc123.js').write_text('x')
app = Application(Registry([]), tmp_path)
try:
assert app.dispatch('GET', '/assets/app-abc123.js').headers['Cache-Control'] == 'public, max-age=31536000, immutable'
assert app.dispatch('GET', '/').headers['Cache-Control'] == 'no-cache'
finally:
app.close()
class UiFixes(unittest.TestCase):
# 原为 pytest 写法(tmp_path 夹具),Makefile 的 unittest discover 收不到,10-22 前从没跑过
def run_in_tmp(self, check):
with tempfile.TemporaryDirectory() as tmp:
check(Path(tmp))
def test_schema_cache(self):
self.run_in_tmp(check_schema_cache)
def test_questions_for_error_row(self):
self.run_in_tmp(check_questions_for_error_row)
def test_hashed_assets_cacheable(self):
self.run_in_tmp(check_hashed_assets_cacheable)
if __name__ == "__main__":
unittest.main()