Team Ai
Apppublic

diegobeyl/backtesting

sourceHugging Faceupdated 9mo agoView on Hugging Face
2likes
test_api_routes.py101 linesDownload Raw Back to tests
1"""Tests for API Routes"""
2import pytest
3import sys
4from pathlib import Path
5from unittest.mock import MagicMock, patch, AsyncMock
6from datetime import datetime
7
8sys.path.insert(0, str(Path(__file__).parent.parent))
9
10from fastapi.testclient import TestClient
11from api.main import app
12from api.schemas import BacktestConfig, Timeframe
13
14
15client = TestClient(app)
16
17
18class TestHealthEndpoint:
19    """Tests for /health endpoint"""
20    
21    @patch('api.routes.get_data_provider')
22    def test_health_healthy(self, mock_provider):
23        """Test healthy status"""
24        mock_provider.return_value.connect.return_value = True
25        response = client.get("/api/health")
26        assert response.status_code == 200
27        assert response.json()["status"] == "healthy"
28    
29    @patch('api.routes.get_data_provider')
30    def test_health_degraded(self, mock_provider):
31        """Test degraded status when MT5 disconnected"""
32        mock_provider.return_value.connect.return_value = False
33        response = client.get("/api/health")
34        assert response.status_code == 200
35        assert response.json()["status"] == "degraded"
36
37
38class TestSymbolsEndpoint:
39    """Tests for /symbols endpoint"""
40    
41    @patch('api.routes.get_data_provider')
42    def test_get_symbols(self, mock_provider):
43        """Test getting symbols list"""
44        mock_provider.return_value.get_symbols.return_value = ["BTCUSD", "ETHUSD"]
45        response = client.get("/api/symbols")
46        assert response.status_code == 200
47        assert "symbols" in response.json()
48
49
50class TestTimeframesEndpoint:
51    """Tests for /timeframes endpoint"""
52    
53    def test_get_timeframes(self):
54        """Test getting timeframes"""
55        response = client.get("/api/timeframes")
56        assert response.status_code == 200
57        data = response.json()
58        assert "timeframes" in data
59        values = [tf["value"] for tf in data["timeframes"]]
60        assert "D1" in values
61        assert "W1" in values
62
63
64class TestConfigEndpoints:
65    """Tests for /config endpoints"""
66    
67    def test_get_config(self):
68        """Test getting current config"""
69        response = client.get("/api/config")
70        assert response.status_code == 200
71        assert "symbol" in response.json()
72    
73    def test_patch_config(self):
74        """Test partial config update"""
75        response = client.patch("/api/config", json={"risk_percent": 2.0})
76        assert response.status_code == 200
77        assert response.json()["risk_percent"] == 2.0
78    
79    def test_reset_config(self):
80        """Test config reset"""
81        response = client.post("/api/config/reset")
82        assert response.status_code == 200
83        assert response.json()["symbol"] == "BTCUSD"
84
85
86class TestBacktestStatusEndpoint:
87    """Tests for /backtest/status endpoint"""
88    
89    def test_get_status(self):
90        """Test getting backtest status"""
91        response = client.get("/api/backtest/status")
92        assert response.status_code == 200
93        data = response.json()
94        assert "running" in data
95        assert "progress" in data
96        assert "message" in data
97
98
99if __name__ == "__main__":
100    pytest.main([__file__, "-v"])
101