"""资金面中转: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()