50 lines
2.7 KiB
Python
50 lines
2.7 KiB
Python
"""Real upstream FastAPI protocol with inference mocked; no GPU or model requests."""
|
|
import json
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from fastapi.testclient import TestClient
|
|
from mineru.cli import fast_api
|
|
|
|
|
|
class ParserProtocolTests(unittest.TestCase):
|
|
def test_official_routes_do_not_restore_retired_application_features(self):
|
|
with TestClient(fast_api.app) as client:
|
|
schema = client.get('/openapi.json').json()
|
|
self.assertIn('/file_parse', schema['paths'])
|
|
self.assertNotIn('/api/file_parse', schema['paths'])
|
|
self.assertFalse(any('organize' in path or 'mindmap' in path for path in schema['paths']))
|
|
|
|
def test_pipeline_response_matches_maxkb_markdown_middle_json_and_images_contract(self):
|
|
async def infer(**kwargs):
|
|
for name in kwargs['pdf_file_names']:
|
|
output = Path(kwargs['output_dir']) / name / kwargs['parse_method']
|
|
(output / 'images').mkdir(parents=True)
|
|
(output / (name + '.md')).write_text('# CODEX 271 protocol\n\n', encoding='utf-8')
|
|
(output / (name + '_middle.json')).write_text(json.dumps({'pdf_info': [{'page_idx': 0, 'para_blocks': []}]}), encoding='utf-8')
|
|
(output / 'images/test.jpg').write_bytes(b'isolated-test-image')
|
|
with tempfile.TemporaryDirectory() as folder, \
|
|
patch.object(fast_api, 'aio_do_parse', side_effect=infer), \
|
|
patch.object(fast_api, 'guess_suffix_by_path', return_value='pdf'), \
|
|
patch.object(fast_api, 'read_fn', return_value=b'isolated-pdf'), \
|
|
TestClient(fast_api.app) as client:
|
|
response = client.post('/file_parse', files={'files': ('CODEX_271_PROTOCOL.pdf', b'%PDF-test', 'application/pdf')},
|
|
data={'backend': 'pipeline', 'parse_method': 'auto', 'output_dir': folder,
|
|
'return_md': 'true', 'return_middle_json': 'true', 'return_images': 'true',
|
|
'response_format_zip': 'false'})
|
|
self.assertEqual(response.status_code, 200, response.text[:300])
|
|
payload = response.json()
|
|
self.assertEqual(payload['version'], '2.7.1')
|
|
self.assertEqual(payload['backend'], 'pipeline')
|
|
self.assertEqual(len(payload['results']), 1)
|
|
result = next(iter(payload['results'].values()))
|
|
self.assertIn('# CODEX 271 protocol', result['md_content'])
|
|
self.assertEqual(json.loads(result['middle_json'])['pdf_info'][0]['page_idx'], 0)
|
|
self.assertTrue(result['images']['test.jpg'].startswith('data:image/jpeg;base64,'))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main(verbosity=2)
|