Files
Chan/web/tests/test_deriv_proxy.py
jackyu66gitandCursor c22cd48f36 Web 加上资金面/情绪叠图,并收紧默认图面。
默认主/次/次次改为 45m/15m/5m、开始时间一周;SD/CD 按 hist 摆位置和箭头;去掉笔线段背驰与第四类勾选。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-12 02:25:44 +08:00

98 lines
3.0 KiB
Python

"""资金面中转:Web 只打 data_provider,字段原样回给前端。"""
from __future__ import annotations
import sys
from pathlib import Path
from unittest.mock import patch
import pytest
WEB_ROOT = Path(__file__).resolve().parents[1]
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT))
sys.path.insert(0, str(WEB_ROOT))
SNAPSHOT = {
"exchange": "bitget",
"symbol": "BTC/USDT:USDT",
"timestamp": 1789039236740,
"datetime": "2026-09-10T11:20:36.740000Z",
"funding_rate": 0.0001,
"open_interest": 36207.21,
"oi_change_pct": -0.01,
"basis": -0.03,
}
OI_HIST = {
"metric": "open_interest_history",
"symbol": "BTC/USDT:USDT",
"period": "15m",
"count": 1,
"data": [
{
"timestamp": 1789038900000,
"datetime": "2026-09-10T11:15:00Z",
"open_interest_amount": 106162.005,
"open_interest_value": 8269553076.678,
}
],
}
@pytest.fixture
def client():
from app import create_app
app = create_app()
app.config["TESTING"] = True
return app.test_client()
def test_derivatives_proxy_passthrough(client):
with patch("api.provider.fetch_derivatives", return_value=SNAPSHOT) as mock_fetch:
resp = client.get("/api/derivatives?symbol=BTC/USDT:USDT")
assert resp.status_code == 200
body = resp.get_json()
assert body["funding_rate"] == 0.0001
assert body["open_interest"] == 36207.21
assert body["oi_change_pct"] == -0.01
assert body["basis"] == -0.03
mock_fetch.assert_called_once()
assert mock_fetch.call_args[0][0] == "BTC/USDT:USDT"
def test_sentiment_metrics_proxy_passthrough(client):
with patch("api.provider.fetch_sentiment_metrics", return_value=OI_HIST) as mock_fetch:
resp = client.get(
"/api/sentiment/metrics?metric=open_interest_history&symbol=BTC/USDT:USDT&limit=1"
)
assert resp.status_code == 200
body = resp.get_json()
assert body["metric"] == "open_interest_history"
assert body["data"][0]["open_interest_amount"] == 106162.005
mock_fetch.assert_called_once()
assert mock_fetch.call_args[0][0] == "open_interest_history"
def test_sentiment_metrics_requires_metric(client):
resp = client.get("/api/sentiment/metrics?symbol=BTC/USDT:USDT")
assert resp.status_code == 400
def test_sentiment_latest_proxy_passthrough(client):
latest = {
"symbol": "BTC/USDT:USDT",
"data": {
"taker_buy_sell_ratio": {"buy_sell_ratio": 1.36},
"long_short_account_ratio": {"long_short_ratio": 1.5},
"top_long_short_position_ratio": {"long_short_ratio": 2.26},
},
}
with patch("api.provider.fetch_sentiment_latest", return_value=latest) as mock_fetch:
resp = client.get("/api/sentiment/latest?symbol=BTC/USDT:USDT")
assert resp.status_code == 200
body = resp.get_json()
assert body["data"]["taker_buy_sell_ratio"]["buy_sell_ratio"] == 1.36
mock_fetch.assert_called_once()