Files
QMDSearch/tests/test_document_api.py
T
kplam 51dc8dc4f6 Initial commit: QMDSearch 分层信息检索服务
- FastAPI + Qdrant + Redis + Ollama 技术栈
- L1→L2→L3→chunk 四层分层检索(dense + sparse RRF 融合)
- 文档三级总结与 2.5 级回退
- query 解析路由与分类
- /admin 管理页面
2026-07-29 21:24:40 +08:00

75 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""文档入库 API 测试(任务管理器为 FakeManager,不真实联网)"""
from collections.abc import Iterator
import pytest
from fastapi.testclient import TestClient
from app.api.v1 import document as document_module
from app.main import app
from app.models.document import DocumentInput
from app.services.qdrant import QdrantService
class FakeManager:
"""假入库任务管理器:记录 submit 调用并返回固定 task_id"""
def __init__(self, task_id: str = "task-1") -> None:
self.task_id = task_id
self.submitted: list[DocumentInput] = []
async def submit(self, doc: DocumentInput) -> str:
self.submitted.append(doc)
return self.task_id
@pytest.fixture
def client(monkeypatch: pytest.MonkeyPatch) -> Iterator[TestClient]:
"""TestClientlifespan 中的 Qdrant 集合初始化替换为空操作"""
async def _noop_ensure_collections(self: QdrantService) -> None:
return None
monkeypatch.setattr(QdrantService, "ensure_collections", _noop_ensure_collections)
with TestClient(app) as test_client:
yield test_client
def test_ingest_submit_accepted(client: TestClient, monkeypatch: pytest.MonkeyPatch) -> None:
"""正常提交:HTTP 202code=0data 含 task_id 与 status=pending,文档已登记给管理器"""
fake = FakeManager(task_id="task-abc")
monkeypatch.setattr(document_module, "_get_task_manager", lambda: fake)
resp = client.post("/api/v1/documents", json={"text": "正文内容", "title": "标题"})
assert resp.status_code == 202
body = resp.json()
assert body["code"] == 0
data = body["data"]
assert data["task_id"] == "task-abc"
assert data["status"] == "pending"
assert [d.title for d in fake.submitted] == ["标题"]
def test_ingest_empty_text(client: TestClient) -> None:
"""空 text:路由内校验,返回 code=1001"""
resp = client.post("/api/v1/documents", json={"text": ""})
body = resp.json()
assert body["code"] == 1001
assert body["message"] == "文档内容不能为空"
assert body["data"] is None
def test_ingest_no_sync_ingestion_error(client: TestClient, monkeypatch: pytest.MonkeyPatch) -> None:
"""任务模式下同步路径不再返回 2000:提交即 202,入库失败体现在任务状态中"""
fake = FakeManager()
monkeypatch.setattr(document_module, "_get_task_manager", lambda: fake)
resp = client.post("/api/v1/documents", json={"text": "正文内容"})
assert resp.status_code == 202
body = resp.json()
assert body["code"] == 0
assert body["data"]["status"] == "pending"