袁聪的最后一次完善更新

This commit is contained in:
2026-09-14 18:13:10 +08:00
parent 54055b4328
commit 2548c39c6b
50 changed files with 2818 additions and 267 deletions
+73 -2
View File
@@ -29,6 +29,7 @@ class FakeRun:
status: str = "running"
agent_type: str = "customer_service"
session_id: str = "session-1"
request_message_id: int = 1
error_code: str | None = None
created_at: datetime = NOW
completed_at: datetime | None = None
@@ -53,15 +54,29 @@ class FakeRepository:
class FakeSession:
def __init__(self, request_message: Any = None) -> None:
self.request_message = request_message
async def __aenter__(self) -> "FakeSession":
return self
async def __aexit__(self, *exc: object) -> bool:
return False
async def get(self, model: Any, message_id: int) -> Any:
del model, message_id
return self.request_message
def patch_repository(monkeypatch: pytest.MonkeyPatch, rows: tuple[Any, Any] | None) -> None:
monkeypatch.setattr("app.service.run_query_service.SessionFactory", FakeSession)
def patch_repository(
monkeypatch: pytest.MonkeyPatch,
rows: tuple[Any, Any] | None,
request_message: Any = None,
) -> None:
monkeypatch.setattr(
"app.service.run_query_service.SessionFactory",
lambda: FakeSession(request_message),
)
monkeypatch.setattr(
"app.service.run_query_service.ConversationRepository",
lambda _session: FakeRepository(rows),
@@ -111,6 +126,62 @@ async def test_succeeded_run_exposes_result_with_string_confidence(
assert snapshot.completed_at is not None and snapshot.completed_at.endswith("Z")
async def test_financial_run_exposes_query_values_and_generated_sql(
monkeypatch: pytest.MonkeyPatch,
) -> None:
message = FakeMessage(
content="查询成功",
tool_calls={
"calls": [],
"data": {"total": 1, "rows": [{"nav": "1.250000"}]},
"sql": "SELECT n.nav AS nav FROM fin_nav n WHERE 1=1 LIMIT 50",
},
)
patch_repository(
monkeypatch,
(FakeRun(status="succeeded", agent_type="financial_nl2sql", completed_at=NOW), message),
)
snapshot = await RunQueryService().get("run-1", CONTEXT)
assert snapshot.result is not None
assert snapshot.result["data"] == {"total": 1, "rows": [{"nav": "1.250000"}]}
assert snapshot.result["sql"] == "SELECT n.nav AS nav FROM fin_nav n WHERE 1=1 LIMIT 50"
async def test_financial_run_recovers_missing_payload_from_original_question(
monkeypatch: pytest.MonkeyPatch,
) -> None:
message = FakeMessage(content="查询成功", tool_calls={"calls": []})
request_message = SimpleNamespace(content="查询15911的最新净值")
patch_repository(
monkeypatch,
(FakeRun(status="succeeded", agent_type="financial_nl2sql", completed_at=NOW), message),
request_message,
)
async def recover(
self: Any, arguments: Any, context: RequestContext
) -> dict[str, Any]:
del self
assert arguments.question == "查询15911的最新净值"
assert context.user_id == CONTEXT.user_id
return {
"status": "success",
"data": {"total": 1, "rows": [{"nav": "1.250000"}]},
"sql": "SELECT n.nav AS nav FROM fin_nav n WHERE 1=1 LIMIT 50",
}
monkeypatch.setattr(
"app.service.run_query_service.FinancialNL2SQLService.query", recover
)
snapshot = await RunQueryService().get("run-1", CONTEXT)
assert snapshot.result is not None
assert snapshot.result["data"]["rows"] == [{"nav": "1.250000"}]
assert snapshot.result["sql"].startswith("SELECT ")
async def test_failed_run_does_not_expose_result(monkeypatch: pytest.MonkeyPatch) -> None:
"""即使库里有结果消息,失败运行也不得把它当作成功结果返回。"""
patch_repository(