This repository was archived by the owner on Jul 12, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtest_mcp.py
More file actions
343 lines (283 loc) · 12.3 KB
/
Copy pathtest_mcp.py
File metadata and controls
343 lines (283 loc) · 12.3 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"""Tests for the quantcoder.mcp module."""
import pytest
from unittest.mock import AsyncMock, MagicMock, patch
from quantcoder.mcp.quantconnect_mcp import (
QuantConnectMCPClient,
QuantConnectMCPServer,
)
class TestQuantConnectMCPClient:
"""Tests for QuantConnectMCPClient class."""
@pytest.fixture
def client(self):
"""Create MCP client for testing."""
return QuantConnectMCPClient(
api_key="test-api-key",
user_id="test-user-id"
)
def test_init(self, client):
"""Test client initialization."""
assert client.api_key == "test-api-key"
assert client.user_id == "test-user-id"
assert client.base_url == "https://www.quantconnect.com/api/v2"
def test_encode_credentials(self, client):
"""Test credential encoding."""
encoded = client._encode_credentials()
import base64
decoded = base64.b64decode(encoded).decode()
assert decoded == "test-user-id:test-api-key"
@pytest.mark.asyncio
async def test_validate_code_success(self, client):
"""Test successful code validation."""
with patch.object(client, '_create_project', new_callable=AsyncMock) as mock_create:
with patch.object(client, '_upload_files', new_callable=AsyncMock) as mock_upload:
with patch.object(client, '_compile', new_callable=AsyncMock) as mock_compile:
mock_create.return_value = "project-123"
mock_compile.return_value = {
"success": True,
"compileId": "compile-456",
"errors": [],
"warnings": []
}
result = await client.validate_code("def main(): pass")
assert result["valid"] is True
assert result["project_id"] == "project-123"
mock_create.assert_called_once()
mock_upload.assert_called_once()
@pytest.mark.asyncio
async def test_validate_code_with_files(self, client):
"""Test code validation with additional files."""
with patch.object(client, '_create_project', new_callable=AsyncMock) as mock_create:
with patch.object(client, '_upload_files', new_callable=AsyncMock) as mock_upload:
with patch.object(client, '_compile', new_callable=AsyncMock) as mock_compile:
mock_create.return_value = "project-123"
mock_compile.return_value = {"success": True}
result = await client.validate_code(
code="def main(): pass",
files={"Alpha.py": "class Alpha: pass"}
)
mock_upload.assert_called_with(
"project-123",
"def main(): pass",
{"Alpha.py": "class Alpha: pass"}
)
@pytest.mark.asyncio
async def test_validate_code_error(self, client):
"""Test code validation with error."""
with patch.object(client, '_create_project', new_callable=AsyncMock) as mock_create:
mock_create.side_effect = Exception("API Error")
result = await client.validate_code("def main(): pass")
assert result["valid"] is False
assert "API Error" in result["errors"][0]
@pytest.mark.asyncio
async def test_backtest_validation_fails(self, client):
"""Test backtest when validation fails."""
with patch.object(client, 'validate_code', new_callable=AsyncMock) as mock_validate:
mock_validate.return_value = {
"valid": False,
"errors": ["Syntax error"]
}
result = await client.backtest(
code="invalid code",
start_date="2020-01-01",
end_date="2020-12-31"
)
assert result["success"] is False
assert "validation failed" in result["error"].lower()
@pytest.mark.asyncio
async def test_backtest_success(self, client):
"""Test successful backtest."""
with patch.object(client, 'validate_code', new_callable=AsyncMock) as mock_validate:
with patch.object(client, '_call_api', new_callable=AsyncMock) as mock_api:
with patch.object(client, '_wait_for_backtest', new_callable=AsyncMock) as mock_wait:
mock_validate.return_value = {
"valid": True,
"project_id": "proj-123",
"compile_id": "compile-456"
}
mock_api.return_value = {"backtestId": "backtest-789"}
mock_wait.return_value = {
"result": {
"Statistics": {"Sharpe Ratio": "1.5"},
"RuntimeStatistics": {},
"Charts": {}
}
}
result = await client.backtest(
code="def main(): pass",
start_date="2020-01-01",
end_date="2020-12-31"
)
assert result["success"] is True
assert result["backtest_id"] == "backtest-789"
@pytest.mark.asyncio
async def test_get_api_docs_with_topic(self, client):
"""Test getting API docs for known topic."""
with patch('aiohttp.ClientSession') as mock_session:
mock_response = MagicMock()
mock_response.status = 200
mock_context = AsyncMock()
mock_context.__aenter__.return_value = mock_response
mock_session.return_value.__aenter__.return_value.get.return_value = mock_context
result = await client.get_api_docs("indicators")
assert "indicators" in result.lower() or "quantconnect" in result.lower()
@pytest.mark.asyncio
async def test_get_api_docs_unknown_topic(self, client):
"""Test getting API docs for unknown topic."""
with patch('aiohttp.ClientSession') as mock_session:
mock_response = MagicMock()
mock_response.status = 200
mock_context = AsyncMock()
mock_context.__aenter__.return_value = mock_response
mock_session.return_value.__aenter__.return_value.get.return_value = mock_context
result = await client.get_api_docs("unknown topic xyz")
assert "quantconnect" in result.lower()
@pytest.mark.asyncio
async def test_deploy_live(self, client):
"""Test live deployment."""
with patch.object(client, '_call_api', new_callable=AsyncMock) as mock_api:
mock_api.return_value = {
"success": True,
"liveAlgorithmId": "live-123"
}
result = await client.deploy_live(
project_id="proj-123",
compile_id="compile-456",
node_id="node-789"
)
assert result["success"] is True
assert result["live_id"] == "live-123"
@pytest.mark.asyncio
async def test_deploy_live_error(self, client):
"""Test live deployment error."""
with patch.object(client, '_call_api', new_callable=AsyncMock) as mock_api:
mock_api.side_effect = Exception("Deployment failed")
result = await client.deploy_live(
project_id="proj-123",
compile_id="compile-456",
node_id="node-789"
)
assert result["success"] is False
assert "Deployment failed" in result["error"]
class TestQuantConnectMCPServer:
"""Tests for QuantConnectMCPServer class."""
@pytest.fixture
def server(self):
"""Create MCP server for testing."""
return QuantConnectMCPServer(
api_key="test-api-key",
user_id="test-user-id"
)
def test_init(self, server):
"""Test server initialization."""
assert server.client is not None
assert isinstance(server.client, QuantConnectMCPClient)
@pytest.mark.asyncio
async def test_start(self, server):
"""Test server start."""
await server.start()
assert server.is_running() is True
assert len(server.get_tools()) == 4
assert "validate_code" in server.get_tools()
assert "backtest" in server.get_tools()
assert "get_api_docs" in server.get_tools()
assert "deploy_live" in server.get_tools()
@pytest.mark.asyncio
async def test_stop(self, server):
"""Test server stop."""
await server.start()
assert server.is_running() is True
await server.stop()
assert server.is_running() is False
@pytest.mark.asyncio
async def test_get_tools_before_start(self, server):
"""Test getting tools before server starts."""
tools = server.get_tools()
assert tools == {}
@pytest.mark.asyncio
async def test_handle_tool_call_validate(self, server):
"""Test handling validate_code tool call."""
with patch.object(
server.client,
'validate_code',
new_callable=AsyncMock
) as mock_validate:
mock_validate.return_value = {"valid": True}
result = await server.handle_tool_call(
"validate_code",
{"code": "def main(): pass"}
)
assert result == {"valid": True}
mock_validate.assert_called_with(code="def main(): pass")
@pytest.mark.asyncio
async def test_handle_tool_call_backtest(self, server):
"""Test handling backtest tool call."""
with patch.object(
server.client,
'backtest',
new_callable=AsyncMock
) as mock_backtest:
mock_backtest.return_value = {"success": True}
result = await server.handle_tool_call(
"backtest",
{
"code": "def main(): pass",
"start_date": "2020-01-01",
"end_date": "2020-12-31"
}
)
assert result == {"success": True}
@pytest.mark.asyncio
async def test_handle_tool_call_docs(self, server):
"""Test handling get_api_docs tool call."""
with patch.object(
server.client,
'get_api_docs',
new_callable=AsyncMock
) as mock_docs:
mock_docs.return_value = "Documentation text"
result = await server.handle_tool_call(
"get_api_docs",
{"topic": "indicators"}
)
assert result == "Documentation text"
@pytest.mark.asyncio
async def test_handle_tool_call_deploy(self, server):
"""Test handling deploy_live tool call."""
with patch.object(
server.client,
'deploy_live',
new_callable=AsyncMock
) as mock_deploy:
mock_deploy.return_value = {"success": True}
result = await server.handle_tool_call(
"deploy_live",
{
"project_id": "123",
"compile_id": "456",
"node_id": "789"
}
)
assert result == {"success": True}
@pytest.mark.asyncio
async def test_handle_tool_call_unknown(self, server):
"""Test handling unknown tool call."""
with pytest.raises(ValueError) as exc_info:
await server.handle_tool_call("unknown_tool", {})
assert "Unknown tool" in str(exc_info.value)
@pytest.mark.asyncio
async def test_tool_schemas(self, server):
"""Test tool schemas are properly defined."""
await server.start()
tools = server.get_tools()
# Check validate_code schema
validate_schema = tools["validate_code"]
assert "code" in validate_schema["parameters"]
assert "code" in validate_schema["required"]
# Check backtest schema
backtest_schema = tools["backtest"]
assert "start_date" in backtest_schema["parameters"]
assert "end_date" in backtest_schema["parameters"]
# Check deploy_live schema
deploy_schema = tools["deploy_live"]
assert "project_id" in deploy_schema["required"]
assert "compile_id" in deploy_schema["required"]