File size: 3,995 Bytes
590a501
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import pytest
from httpx import ASGITransport, AsyncClient

from app.main import app


@pytest.fixture
async def client():
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as ac:
        yield ac


@pytest.mark.asyncio
async def test_root(client):
    resp = await client.get("/")
    assert resp.status_code == 200
    data = resp.json()
    assert data["name"] == "Personal Trading System V1"


@pytest.mark.asyncio
async def test_get_quotes(client):
    resp = await client.get("/api/market/quotes")
    assert resp.status_code == 200
    quotes = resp.json()
    assert len(quotes) > 10
    assert "symbol" in quotes[0]
    assert "name" in quotes[0]
    assert "exchange" in quotes[0]
    assert "category" in quotes[0]
    assert "change_pct" in quotes[0]


@pytest.mark.asyncio
async def test_get_quotes_by_category(client):
    resp = await client.get("/api/market/quotes?category=ferrous")
    assert resp.status_code == 200
    quotes = resp.json()
    assert len(quotes) > 0
    assert all(q["category"] == "ferrous" for q in quotes)


@pytest.mark.asyncio
async def test_get_contracts(client):
    resp = await client.get("/api/market/contracts")
    assert resp.status_code == 200
    contracts = resp.json()
    assert len(contracts) > 50


@pytest.mark.asyncio
async def test_get_contracts_by_category(client):
    resp = await client.get("/api/market/contracts?category=precious_metal")
    assert resp.status_code == 200
    contracts = resp.json()
    assert len(contracts) >= 2
    assert all(c["category"] == "precious_metal" for c in contracts)


@pytest.mark.asyncio
async def test_get_exchanges(client):
    resp = await client.get("/api/market/exchanges")
    assert resp.status_code == 200
    data = resp.json()
    assert "SHFE" in data
    assert "DCE" in data
    assert "CZCE" in data
    assert "CFFEX" in data


@pytest.mark.asyncio
async def test_get_categories(client):
    resp = await client.get("/api/market/categories")
    assert resp.status_code == 200
    data = resp.json()
    assert "financial" in data
    assert "precious_metal" in data
    assert "energy" in data
    assert "agriculture" in data


@pytest.mark.asyncio
async def test_get_klines(client):
    resp = await client.get("/api/market/klines/%E8%9E%BA%E7%BA%B9%E9%92%A2?limit=50")
    assert resp.status_code == 200
    klines = resp.json()
    assert len(klines) <= 50


@pytest.mark.asyncio
async def test_get_account(client):
    resp = await client.get("/api/account")
    assert resp.status_code == 200
    data = resp.json()
    assert "total_balance" in data
    assert data["total_balance"] == 1_000_000.0


@pytest.mark.asyncio
async def test_place_order(client):
    resp = await client.post("/api/orders", json={
        "symbol": "螺纹钢",
        "side": "BUY",
        "order_type": "MARKET",
        "quantity": 1,
    })
    assert resp.status_code == 200
    data = resp.json()
    assert data["status"] == "FILLED"
    assert data["symbol"] == "螺纹钢"


@pytest.mark.asyncio
async def test_get_available_strategies(client):
    resp = await client.get("/api/strategies/available")
    assert resp.status_code == 200
    strategies = resp.json()
    assert len(strategies) >= 3
    types = [s["type"] for s in strategies]
    assert "ma_crossover" in types
    assert "bollinger_bands" in types
    assert "dual_thrust" in types


@pytest.mark.asyncio
async def test_risk_metrics(client):
    resp = await client.get("/api/risk")
    assert resp.status_code == 200
    data = resp.json()
    assert "total_balance" in data
    assert "risk_usage_percent" in data


@pytest.mark.asyncio
async def test_market_mode(client):
    resp = await client.get("/api/market/mode")
    assert resp.status_code == 200
    assert resp.json()["mode"] in ("simulated", "realtime")


@pytest.mark.asyncio
async def test_get_trades(client):
    resp = await client.get("/api/trades")
    assert resp.status_code == 200