袁聪的第三次提交,API接口完善

This commit is contained in:
2026-09-12 12:20:01 +08:00
parent 0a78a9df76
commit 91f5373cfb
20 changed files with 524 additions and 73 deletions
@@ -156,6 +156,43 @@ async def _seed_failed_queries(task_id: str) -> None:
))
async def _seed_latest_success_after_failure(task_id: str) -> None:
await _seed_failed_queries(task_id)
now = datetime.now(UTC).replace(tzinfo=None)
async with SessionFactory() as session, session.begin():
session.add(OffsiteQueryRecord(
task_id=task_id,
rule_code="subscription_holding_ratio",
natural_language_request="基金代码为15911,查询基金最新总份额、最新净值和申请前持有份额",
script_path="nl2sql_yc.py",
result_summary={
"status": "success",
"data": {"total": 1, "rows": [{
"nav": "1.250000",
"total_fund_shares": "10000000000.0000",
"total_quantity": "100000000.0000",
}]},
},
status="success",
error_message=None,
created_at=now,
))
rule_result = await session.scalar(select(OffsiteRuleResult).where(
OffsiteRuleResult.task_id == task_id,
OffsiteRuleResult.rule_code == "subscription_holding_ratio",
))
assert rule_result is not None
rule_result.result = "正常"
rule_result.document_value = {"申购金额元": "200000000.00"}
rule_result.database_value = {
"最新净值": "1.250000",
"基金最新总份额": "10000000000.0000",
"申请前持有份额": "100000000.0000",
}
rule_result.calculation = {"申购后持有比例": "0.026"}
rule_result.created_at = now
async def _count_audit(action_type: str) -> int:
async with SessionFactory() as session:
rows = await session.execute(select(InteractionAudit).where(
@@ -244,6 +281,31 @@ async def test_nl2sql_fields_mark_query_failed_when_query_blocked() -> None:
TRACE_ID = ""
@pytest.mark.integration
async def test_nl2sql_fields_show_only_latest_attempt_per_rule() -> None:
"""重试成功后,页面不能继续展示同一规则的历史失败状态。"""
global TRACE_ID
TRACE_ID = f"trace-nl2sql-latest-attempt-{uuid4()}"
task_id = ""
try:
task_id = await _seed_document("subscription")
await _seed_latest_success_after_failure(task_id)
_install_context(("operator",), ("offsite:read",))
response = await _get_fields(task_id)
assert response.status_code == 200
data = response.json()["data"]
assert len(data["queries"]) == 1
assert data["queries"][0]["rule_code"] == "subscription_holding_ratio"
assert data["queries"][0]["status"] == "success"
assert data["queries"][0]["row_count"] == 1
assert data["field_status"]["最新净值"] == "success"
finally:
await _cleanup(task_id)
app.dependency_overrides.clear()
TRACE_ID = ""
@pytest.mark.integration
async def test_nl2sql_fields_mark_pending_before_verification() -> None:
"""尚未触发核对时字段为空,必须标为 pending,不能伪装成查询失败。"""