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