38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
"""GET /task/{id} 内存失后 DB fallback (治 done task 内存清后前端轮询 404 死循环卡等待)。"""
|
|
from fastapi.testclient import TestClient
|
|
|
|
from sanguo_api.app import create_app
|
|
from sanguo_api.auth import set_jwt_config, create_token
|
|
from sanguo_backtest.result_store import BacktestResult, save_result
|
|
|
|
|
|
def _auth_client(tmp_path):
|
|
set_jwt_config("test_secret_dbfb", 60)
|
|
db = str(tmp_path / "r.db")
|
|
app = create_app(db_path=db)
|
|
c = TestClient(app)
|
|
token = create_token("admin")
|
|
return c, {"Authorization": f"Bearer {token}"}, db
|
|
|
|
|
|
def test_get_status_returns_done_when_only_in_db(tmp_path):
|
|
"""内存池无(容器重启/清理)但 DB 有 done 结果 → 返回 done,不 404。"""
|
|
c, h, db = _auth_client(tmp_path)
|
|
save_result(
|
|
BacktestResult(
|
|
task_id="cta_dbfb", type="cta", status="done", strategy="DoubleMa",
|
|
symbol="600519.SH", params={}, start="2024-01-02", end="2024-03-29",
|
|
statistics={}, equity_curve=None, trades=None,
|
|
),
|
|
db_path=db,
|
|
)
|
|
r = c.get("/api/v1/task/cta_dbfb", headers=h)
|
|
assert r.status_code == 200
|
|
assert r.json()["status"] == "done"
|
|
|
|
|
|
def test_get_status_404_when_neither_memory_nor_db(tmp_path):
|
|
c, h, _db = _auth_client(tmp_path)
|
|
r = c.get("/api/v1/task/nonexistent_xyz", headers=h)
|
|
assert r.status_code == 404
|