Add REST API with dashboard and configurable port (server.host/port, TM_PORT, --port)
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
"""Tests für REST-API / Dashboard (trademind serve)."""
|
||||
import textwrap
|
||||
|
||||
import pytest
|
||||
|
||||
from trademind.config import ServerConfig, load
|
||||
|
||||
|
||||
CFG = textwrap.dedent(
|
||||
"""
|
||||
trading:
|
||||
base_currency: BTC
|
||||
quote_currency: USDT
|
||||
candles: 120
|
||||
"""
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def cfg_file(tmp_path):
|
||||
p = tmp_path / "config.yaml"
|
||||
p.write_text(CFG, encoding="utf-8")
|
||||
return str(p)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(cfg_file):
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from trademind.server import make_app
|
||||
|
||||
app = make_app(cfg_file, data="mock", exchange="binance")
|
||||
with TestClient(app) as c:
|
||||
yield c
|
||||
|
||||
|
||||
def test_server_config_defaults_and_env():
|
||||
assert ServerConfig().port == 8080
|
||||
assert ServerConfig().host == "0.0.0.0"
|
||||
cfg = ServerConfig.from_dict({"port": "9443", "host": "127.0.0.1"})
|
||||
assert cfg.port == 9443
|
||||
assert cfg.host == "127.0.0.1"
|
||||
|
||||
|
||||
def test_config_load_includes_server(cfg_file):
|
||||
cfg = load(cfg_file)
|
||||
assert cfg.server.port == 8080
|
||||
assert cfg.server.host == "0.0.0.0"
|
||||
|
||||
|
||||
def test_health(client):
|
||||
r = client.get("/api/health")
|
||||
assert r.status_code == 200
|
||||
body = r.json()
|
||||
assert body["status"] == "ok"
|
||||
assert body["version"]
|
||||
|
||||
|
||||
def test_dashboard_html(client):
|
||||
r = client.get("/")
|
||||
assert r.status_code == 200
|
||||
assert "text/html" in r.headers["content-type"]
|
||||
assert "TradeMind" in r.text
|
||||
|
||||
|
||||
def test_status(client):
|
||||
r = client.get("/api/status")
|
||||
assert r.status_code == 200
|
||||
body = r.json()
|
||||
assert body["symbol"] == "BTC/USDT"
|
||||
assert body["trading"]["candles"] == 120
|
||||
assert body["last_run"] is None
|
||||
|
||||
|
||||
def test_signals_mock_data(client):
|
||||
r = client.get("/api/signals?limit=10")
|
||||
assert r.status_code == 200
|
||||
body = r.json()
|
||||
assert body["broker"] == "mock"
|
||||
sigs = body["signals"]
|
||||
assert sigs, "expected at least one signal"
|
||||
last = sigs[-1]
|
||||
assert last["action"] in (-1, 0, 1, 2)
|
||||
assert last["price"] > 0
|
||||
assert isinstance(last["score"], (int, float))
|
||||
|
||||
|
||||
def test_paper_run_and_equity(client):
|
||||
r = client.post("/api/paper")
|
||||
assert r.status_code == 200
|
||||
body = r.json()
|
||||
assert body["summary"]["final_equity"] > 0
|
||||
assert len(body["equity_curve"]) > 1
|
||||
|
||||
eq = client.get("/api/equity")
|
||||
assert eq.status_code == 200
|
||||
ebody = eq.json()
|
||||
assert ebody["summary"]["final_equity"] > 0
|
||||
assert ebody["equity_curve"]
|
||||
|
||||
st = client.get("/api/status")
|
||||
assert st.json()["last_run"]["broker"] == "mock"
|
||||
Reference in New Issue
Block a user