feat(api): /paper/* 路由(create建account+equity/trades查询,JWT)
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
"""模拟盘 API 路由测试(spec §10)。"""
|
||||
import os
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from sanguo_api.app import create_app
|
||||
from sanguo_api.auth import create_token, set_jwt_config
|
||||
from sanguo_api.routes_paper import set_db_path
|
||||
|
||||
|
||||
def _client(tmp_path):
|
||||
set_jwt_config(secret="t", expire_minutes=60)
|
||||
db = os.path.join(str(tmp_path), "p.db")
|
||||
app = create_app(db_path=db)
|
||||
set_db_path(db)
|
||||
return TestClient(app), create_token("admin")
|
||||
|
||||
|
||||
def _auth(token):
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
def test_create_paper(tmp_path):
|
||||
c, token = _client(tmp_path)
|
||||
resp = c.post(
|
||||
"/api/v1/paper/create",
|
||||
json={
|
||||
"symbols": ["600000"],
|
||||
"strategies": [{"name": "DoubleMa", "symbol": "600000",
|
||||
"match_session": "next_open"}],
|
||||
"start": "2024-01-01", "end": "2024-06-30",
|
||||
"initial_capital": 1_000_000,
|
||||
},
|
||||
headers=_auth(token),
|
||||
)
|
||||
assert resp.status_code == 200
|
||||
assert "account_id" in resp.json()
|
||||
|
||||
|
||||
def test_get_paper_and_empty_trades(tmp_path):
|
||||
c, token = _client(tmp_path)
|
||||
aid = c.post(
|
||||
"/api/v1/paper/create",
|
||||
json={"symbols": ["600000"],
|
||||
"strategies": [{"name": "S", "symbol": "600000"}],
|
||||
"start": "2024-01-01", "end": "2024-06-30"},
|
||||
headers=_auth(token),
|
||||
).json()["account_id"]
|
||||
assert c.get(f"/api/v1/paper/{aid}", headers=_auth(token)).status_code == 200
|
||||
assert c.get(f"/api/v1/paper/{aid}/trades", headers=_auth(token)).json() == []
|
||||
assert c.get(f"/api/v1/paper/{aid}/equity", headers=_auth(token)).json() == []
|
||||
|
||||
|
||||
def test_get_paper_404(tmp_path):
|
||||
c, token = _client(tmp_path)
|
||||
assert c.get("/api/v1/paper/999", headers=_auth(token)).status_code == 404
|
||||
|
||||
|
||||
def test_unauthorized_401(tmp_path):
|
||||
c, _ = _client(tmp_path)
|
||||
assert c.get("/api/v1/paper/1").status_code == 401
|
||||
Reference in New Issue
Block a user