"""基本 sanity 测试 — 不依赖具体 DB 连接,只验模块能 load""" import os import sys sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) def test_imports(): """所有模块能 import 不报错""" from db import models, database from db.models import Book, BookDocument, DocumentFile from services import book_service, document_service, file_service, rustfs_service from services.wordparse import cleaner, post, render, convert print("✓ 所有模块 import OK") # Book 是 mongo helper class(非 ORM) assert hasattr(Book, 'to_mongo') assert hasattr(Book, 'from_mongo') print("✓ Book 是 mongo helper class") # mongo ↔ dict 对称 b = Book(id=1, name='test', zip_path='x', zip_sha256='abc', zip_size=100) m = b.to_mongo() assert m['_id'] == 1 and m['name'] == 'test' b2 = Book.from_mongo(m) assert b2.id == 1 and b2.name == 'test' print("✓ Book to_mongo/from_mongo 对称") # doc_id 派生 doc_id = BookDocument.make_doc_id(1, "docx/chapter1.docx") assert len(doc_id) == 16 assert doc_id == BookDocument.make_doc_id(1, "docx/chapter1.docx") print(f"✓ doc_id 幂等:{doc_id}") # doc_type 推断 from services.document_service import _infer_doc_type assert _infer_doc_type("课时作业1.docx") == "课时作业" assert _infer_doc_type("周末提升训练.docx") == "周末提升" assert _infer_doc_type("阶段质量评估(一).docx") == "阶段质量评估" assert _infer_doc_type("期末质量评估.docx") == "综合评估" assert _infer_doc_type("random.docx") == "unknown" print("✓ doc_type 推断正确") # cleaner 文本修整 from services.wordparse.cleaner import _fix_text out = _fix_text('') assert '0' in out and 'mm' not in out print(f"✓ cleaner 修单位:{out}") # rustfs key 模板 from services.rustfs_service import build_key key = build_key(123, "5b3a2f1e9c0d4b7a", "html") assert key == "book/123/docs/5b3a2f1e9c0d4b7a/output.html" print(f"✓ rustfs key 模板:{key}") if __name__ == '__main__': test_imports() print("\n✅ 全部测试通过")