Download web_development/backend/tests/test_api.py from lucky-loster/quant_test: direct link, hf CLI and curl.
- Browser
- Download file 4 kB
-
https://huggingface.co/lucky-loster/quant_test/resolve/main/web_development/backend/tests/test_api.py
- Command line
-
hf download hf://lucky-loster/quant_test/web_development/backend/tests/test_api.py
-
curl -L -o test_api.py https://huggingface.co/lucky-loster/quant_test/resolve/main/web_development/backend/tests/test_api.py
4 kB
| import pytest | |
| from httpx import ASGITransport, AsyncClient | |
| from app.main import app | |
| async def client(): | |
| transport = ASGITransport(app=app) | |
| async with AsyncClient(transport=transport, base_url="http://test") as ac: | |
| yield ac | |
| async def test_root(client): | |
| resp = await client.get("/") | |
| assert resp.status_code == 200 | |
| data = resp.json() | |
| assert data["name"] == "Personal Trading System V1" | |
| 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] | |
| 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) | |
| 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 | |
| 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) | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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"] == "螺纹钢" | |
| 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 | |
| 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 | |
| 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") | |
| async def test_get_trades(client): | |
| resp = await client.get("/api/trades") | |
| assert resp.status_code == 200 | |