-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_server.py
More file actions
275 lines (219 loc) · 9.04 KB
/
file_server.py
File metadata and controls
275 lines (219 loc) · 9.04 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
import pytest
import ujson
from pathlib import Path
from unittest.mock import Mock, AsyncMock, patch, MagicMock
from typing import Any
from lf_toolkit.io.file_server import FileHandler, FileServer
from lf_toolkit.io.handler import Handler
# Configure pytest-asyncio
pytest_plugins = ('pytest_asyncio',)
class TestFileHandler:
"""Tests for FileHandler class"""
@pytest.fixture
def handler(self):
"""Create a FileHandler instance for testing"""
return FileHandler()
@pytest.mark.asyncio
async def test_dispatch_success(self, handler):
"""Test successful command dispatch"""
# Mock the handle method
handler.handle = AsyncMock(return_value={"status": "success", "data": "test"})
request = ujson.dumps({
"command": "test_command",
"response": "",
"answer": "",
"params": {"key": "value"}
})
response_str = await handler.dispatch(request)
response = ujson.loads(response_str)
assert response["command"] == "test_command"
assert response["result"] == {"status": "success", "data": "test"}
assert "error" not in response
handler.handle.assert_called_once_with("test_command", {
"command": "test_command",
"response": "",
"answer": "",
"params": {"key": "value"}
})
@pytest.mark.asyncio
async def test_dispatch_missing_command(self, handler):
"""Test dispatch with missing command field"""
handler.handle = AsyncMock(return_value="ok")
request = ujson.dumps({
"response": "",
"answer": "",
"params": {"key": "value"}
})
response_str = await handler.dispatch(request)
response = ujson.loads(response_str)
assert response["command"] is None
assert response["result"] == "ok"
handler.handle.assert_called_once_with(None, {
"response": "",
"answer": "",
"params": {"key": "value"}
})
@pytest.mark.asyncio
async def test_dispatch_exception_handling(self, handler):
"""Test error handling when handler raises exception"""
handler.handle = AsyncMock(side_effect=ValueError("Invalid command"))
request = ujson.dumps({
"command": "bad_command",
"response": "",
"answer": "",
"params": {}
})
response_str = await handler.dispatch(request)
response = ujson.loads(response_str)
assert response["command"] == "bad_command"
assert "error" in response
assert response["error"] == "Invalid command"
assert "result" not in response
@pytest.mark.asyncio
async def test_dispatch_invalid_json(self, handler):
"""Test dispatch with invalid JSON input"""
with pytest.raises(ujson.JSONDecodeError):
await handler.dispatch("invalid json{")
@pytest.mark.asyncio
async def test_dispatch_empty_request(self, handler):
"""Test dispatch with empty request object"""
handler.handle = AsyncMock(return_value=None)
request = ujson.dumps({})
response_str = await handler.dispatch(request)
response = ujson.loads(response_str)
assert response["command"] is None
assert response["result"] is None
class TestFileServer:
"""Tests for FileServer class"""
@pytest.fixture
def temp_files(self, tmp_path):
"""Create temporary request and response files"""
request_file = tmp_path / "request.json"
response_file = tmp_path / "response.json"
return str(request_file), str(response_file)
@pytest.fixture
def mock_handler(self):
"""Create a mock handler"""
handler = Mock(spec=Handler)
handler.handle = AsyncMock()
return handler
def test_init_with_default_handler(self, temp_files):
"""Test FileServer initialization with default handler"""
request_file, response_file = temp_files
server = FileServer(request_file, response_file)
assert server._request_file_path == request_file
assert server._response_file_path == response_file
assert isinstance(server._handler, FileHandler)
def test_init_with_custom_handler(self, temp_files, mock_handler):
"""Test FileServer initialization with custom handler"""
request_file, response_file = temp_files
server = FileServer(request_file, response_file, handler=mock_handler)
assert server._request_file_path == request_file
assert server._response_file_path == response_file
assert server._handler is mock_handler
@pytest.mark.asyncio
async def test_run_success(self, temp_files, tmp_path):
"""Test successful file server run"""
request_file, response_file = temp_files
# Write request to file
request_data = {
"command": "test",
"response": "",
"answer": "",
"params": {"data": "hello"}
}
Path(request_file).write_text(ujson.dumps(request_data))
# Create server with mock handler
handler = FileHandler()
handler.handle = AsyncMock(return_value={"output": "success"})
server = FileServer(request_file, response_file, handler=handler)
# Run server
await server.run()
# Verify response file
response_content = Path(response_file).read_text()
response = ujson.loads(response_content)
assert response["command"] == "test"
assert response["result"] == {"output": "success"}
@pytest.mark.asyncio
async def test_run_with_handler_error(self, temp_files, tmp_path):
"""Test file server run when handler raises error"""
request_file, response_file = temp_files
# Write request to file
request_data = {
"command": "failing_command",
"response": "",
"answer": "",
"params": {}
}
Path(request_file).write_text(ujson.dumps(request_data))
# Create server with failing handler
handler = FileHandler()
handler.handle = AsyncMock(side_effect=RuntimeError("Processing failed"))
server = FileServer(request_file, response_file, handler=handler)
# Run server
await server.run()
# Verify error response
response_content = Path(response_file).read_text()
response = ujson.loads(response_content)
assert response["command"] == "failing_command"
assert "error" in response
assert response["error"] == "Processing failed"
@pytest.mark.asyncio
async def test_run_missing_request_file(self, temp_files):
"""Test run with non-existent request file"""
request_file, response_file = temp_files
server = FileServer(request_file, response_file)
with pytest.raises(FileNotFoundError):
await server.run()
@pytest.mark.asyncio
async def test_run_creates_response_file(self, temp_files, tmp_path):
"""Test that run creates response file if it doesn't exist"""
request_file, response_file = temp_files
# Write request file
Path(request_file).write_text(ujson.dumps({
"command": "test",
"response": "",
"answer": "",
"params": {}
}))
# Ensure response file doesn't exist
assert not Path(response_file).exists()
handler = FileHandler()
handler.handle = AsyncMock(return_value="done")
server = FileServer(request_file, response_file, handler=handler)
await server.run()
# Verify response file was created
assert Path(response_file).exists()
@pytest.mark.asyncio
async def test_run_with_empty_request_file(self, temp_files, tmp_path):
"""Test run with empty request file"""
request_file, response_file = temp_files
# Create empty request file
Path(request_file).write_text("")
server = FileServer(request_file, response_file)
with pytest.raises(ujson.JSONDecodeError):
await server.run()
@pytest.mark.asyncio
async def test_run_with_complex_nested_data(self, temp_files, tmp_path):
"""Test run with complex nested JSON data"""
request_file, response_file = temp_files
request_data = {
"command": "eval",
"response": "",
"answer": "",
"params": {
"array": [1, 2, 3],
"object": {"key": "value"},
"null": None,
"bool": True
}
}
Path(request_file).write_text(ujson.dumps(request_data))
handler = FileHandler()
handler.handle = AsyncMock(return_value={"processed": True})
server = FileServer(request_file, response_file, handler=handler)
await server.run()
response_content = Path(response_file).read_text()
response = ujson.loads(response_content)
assert response["command"] == "eval"
assert response["result"] == {"processed": True}