feat: 账号卡片余额趋势折线图 + 时间窗口切换(all/1h/1d/1w/1mo/1yr) - api: history 接口支持 window 参数与窗口内均匀降采样;新增批量 /api/history - ui: 阈值进度条替换为 SVG 迷你折线图(状态色 + 阈值虚线),卡片级窗口切换器 - test: 窗口过滤、降采样、批量历史用例,共 54 个
This commit is contained in:
@@ -144,6 +144,56 @@ class TestAccounts:
|
||||
hist = client.get(f"/api/accounts/{aid}/history", headers=h).json()
|
||||
assert [x["balance"] for x in hist] == [1.5, 2.5]
|
||||
|
||||
def test_batch_history(self, client, test_db):
|
||||
from app import db
|
||||
|
||||
h = _auth(client)
|
||||
pid = self._make_platform(client, h)
|
||||
a1 = client.post("/api/accounts", json={"platform_id": pid, "name": "x", "api_key": "k"}, headers=h).json()["id"]
|
||||
a2 = client.post("/api/accounts", json={"platform_id": pid, "name": "y", "api_key": "k2"}, headers=h).json()["id"]
|
||||
with db.get_conn() as conn:
|
||||
conn.execute("INSERT INTO balance_history (account_id, balance) VALUES (?, ?)", (a1, 1.0))
|
||||
conn.execute("INSERT INTO balance_history (account_id, balance) VALUES (?, ?)", (a1, 2.0))
|
||||
conn.execute("INSERT INTO balance_history (account_id, balance) VALUES (?, ?)", (a2, 9.0))
|
||||
data = client.get("/api/history?limit=30", headers=h).json()
|
||||
assert [x["balance"] for x in data[str(a1)]] == [1.0, 2.0]
|
||||
assert [x["balance"] for x in data[str(a2)]] == [9.0]
|
||||
assert len(data) == 2
|
||||
|
||||
def test_history_window_filter(self, client, test_db):
|
||||
from app import db
|
||||
|
||||
h = _auth(client)
|
||||
pid = self._make_platform(client, h)
|
||||
aid = client.post("/api/accounts", json={"platform_id": pid, "name": "x", "api_key": "k"}, headers=h).json()["id"]
|
||||
with db.get_conn() as conn:
|
||||
conn.execute(
|
||||
"INSERT INTO balance_history (account_id, balance, checked_at) VALUES (?, ?, datetime('now','localtime','-30 days'))",
|
||||
(aid, 100.0),
|
||||
)
|
||||
conn.execute("INSERT INTO balance_history (account_id, balance) VALUES (?, ?)", (aid, 1.0))
|
||||
all_rows = client.get(f"/api/accounts/{aid}/history", headers=h).json()
|
||||
assert len(all_rows) == 2
|
||||
day_rows = client.get(f"/api/accounts/{aid}/history?window=1d", headers=h).json()
|
||||
assert len(day_rows) == 1 and day_rows[0]["balance"] == 1.0
|
||||
yr_rows = client.get(f"/api/accounts/{aid}/history?window=1yr", headers=h).json()
|
||||
assert len(yr_rows) == 2
|
||||
|
||||
def test_history_downsample(self, client, test_db):
|
||||
from app import db
|
||||
|
||||
h = _auth(client)
|
||||
pid = self._make_platform(client, h)
|
||||
aid = client.post("/api/accounts", json={"platform_id": pid, "name": "x", "api_key": "k"}, headers=h).json()["id"]
|
||||
with db.get_conn() as conn:
|
||||
for i in range(50):
|
||||
conn.execute("INSERT INTO balance_history (account_id, balance) VALUES (?, ?)", (aid, i))
|
||||
rows = client.get(f"/api/accounts/{aid}/history?limit=10", headers=h).json()
|
||||
assert len(rows) == 10
|
||||
assert rows[0]["balance"] == 0 and rows[-1]["balance"] == 49 # 首尾保留
|
||||
# 非法窗口参数被拒
|
||||
assert client.get(f"/api/accounts/{aid}/history?window=2h", headers=h).status_code == 422
|
||||
|
||||
|
||||
class TestSettings:
|
||||
def test_update_settings(self, client, tmp_path):
|
||||
|
||||
Reference in New Issue
Block a user