forked from a2aproject/a2a-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_errors.py
More file actions
420 lines (345 loc) · 15.3 KB
/
test_errors.py
File metadata and controls
420 lines (345 loc) · 15.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
from typing import NoReturn
from unittest.mock import MagicMock
import pytest
from a2a.client import A2AClientError, A2AClientHTTPError, A2AClientJSONError
from a2a.client.errors import (
A2AClientInvalidArgsError,
A2AClientInvalidStateError,
A2AClientJSONRPCError,
A2AClientTimeoutError,
)
class TestA2AClientError:
"""Test cases for the base A2AClientError class."""
def test_instantiation(self) -> None:
"""Test that A2AClientError can be instantiated."""
error = A2AClientError('Test error message')
assert isinstance(error, Exception)
assert str(error) == 'Test error message'
def test_inheritance(self) -> None:
"""Test that A2AClientError inherits from Exception."""
error = A2AClientError()
assert isinstance(error, Exception)
class TestA2AClientHTTPError:
"""Test cases for A2AClientHTTPError class."""
def test_instantiation(self) -> None:
"""Test that A2AClientHTTPError can be instantiated with status_code and message."""
error = A2AClientHTTPError(404, 'Not Found')
assert isinstance(error, A2AClientError)
assert error.status_code == 404
assert error.message == 'Not Found'
def test_message_formatting(self) -> None:
"""Test that the error message is formatted correctly."""
error = A2AClientHTTPError(500, 'Internal Server Error')
assert str(error) == 'HTTP Error 500: Internal Server Error'
def test_repr(self) -> None:
"""Test that __repr__ shows structured attributes."""
error = A2AClientHTTPError(404, 'Not Found')
assert (
repr(error)
== "A2AClientHTTPError(status_code=404, message='Not Found')"
)
def test_inheritance(self) -> None:
"""Test that A2AClientHTTPError inherits from A2AClientError."""
error = A2AClientHTTPError(400, 'Bad Request')
assert isinstance(error, A2AClientError)
def test_with_empty_message(self) -> None:
"""Test behavior with an empty message."""
error = A2AClientHTTPError(403, '')
assert error.status_code == 403
assert error.message == ''
assert str(error) == 'HTTP Error 403: '
def test_with_various_status_codes(self) -> None:
"""Test with different HTTP status codes."""
test_cases = [
(200, 'OK'),
(201, 'Created'),
(400, 'Bad Request'),
(401, 'Unauthorized'),
(403, 'Forbidden'),
(404, 'Not Found'),
(500, 'Internal Server Error'),
(503, 'Service Unavailable'),
]
for status_code, message in test_cases:
error = A2AClientHTTPError(status_code, message)
assert error.status_code == status_code
assert error.message == message
assert str(error) == f'HTTP Error {status_code}: {message}'
class TestA2AClientJSONError:
"""Test cases for A2AClientJSONError class."""
def test_instantiation(self) -> None:
"""Test that A2AClientJSONError can be instantiated with a message."""
error = A2AClientJSONError('Invalid JSON format')
assert isinstance(error, A2AClientError)
assert error.message == 'Invalid JSON format'
def test_message_formatting(self) -> None:
"""Test that the error message is formatted correctly."""
error = A2AClientJSONError('Missing required field')
assert str(error) == 'JSON Error: Missing required field'
def test_repr(self) -> None:
"""Test that __repr__ shows structured attributes."""
error = A2AClientJSONError('Invalid JSON format')
assert (
repr(error) == "A2AClientJSONError(message='Invalid JSON format')"
)
def test_inheritance(self) -> None:
"""Test that A2AClientJSONError inherits from A2AClientError."""
error = A2AClientJSONError('Parsing error')
assert isinstance(error, A2AClientError)
def test_with_empty_message(self) -> None:
"""Test behavior with an empty message."""
error = A2AClientJSONError('')
assert error.message == ''
assert str(error) == 'JSON Error: '
def test_with_various_messages(self) -> None:
"""Test with different error messages."""
test_messages = [
'Malformed JSON',
'Missing required fields',
'Invalid data type',
'Unexpected JSON structure',
'Empty JSON object',
]
for message in test_messages:
error = A2AClientJSONError(message)
assert error.message == message
assert str(error) == f'JSON Error: {message}'
class TestA2AClientTimeoutError:
"""Test cases for A2AClientTimeoutError class."""
def test_instantiation(self) -> None:
"""Test that A2AClientTimeoutError can be instantiated with a message."""
error = A2AClientTimeoutError('Request timed out')
assert isinstance(error, A2AClientError)
assert error.message == 'Request timed out'
def test_message_formatting(self) -> None:
"""Test that the error message is formatted correctly."""
error = A2AClientTimeoutError('Connection timed out after 30s')
assert str(error) == 'Timeout Error: Connection timed out after 30s'
def test_repr(self) -> None:
"""Test that __repr__ shows structured attributes."""
error = A2AClientTimeoutError('Request timed out')
assert (
repr(error) == "A2AClientTimeoutError(message='Request timed out')"
)
def test_inheritance(self) -> None:
"""Test that A2AClientTimeoutError inherits from A2AClientError."""
error = A2AClientTimeoutError('timeout')
assert isinstance(error, A2AClientError)
def test_with_empty_message(self) -> None:
"""Test behavior with an empty message."""
error = A2AClientTimeoutError('')
assert error.message == ''
assert str(error) == 'Timeout Error: '
class TestA2AClientInvalidArgsError:
"""Test cases for A2AClientInvalidArgsError class."""
def test_instantiation(self) -> None:
"""Test that A2AClientInvalidArgsError can be instantiated."""
error = A2AClientInvalidArgsError('Missing required param')
assert isinstance(error, A2AClientError)
assert error.message == 'Missing required param'
def test_message_formatting(self) -> None:
"""Test that the error message is formatted correctly."""
error = A2AClientInvalidArgsError('Invalid type for param X')
assert str(error) == 'Invalid arguments error: Invalid type for param X'
def test_repr(self) -> None:
"""Test that __repr__ shows structured attributes."""
error = A2AClientInvalidArgsError('Missing required param')
assert (
repr(error)
== "A2AClientInvalidArgsError(message='Missing required param')"
)
def test_inheritance(self) -> None:
"""Test that A2AClientInvalidArgsError inherits from A2AClientError."""
error = A2AClientInvalidArgsError('bad args')
assert isinstance(error, A2AClientError)
def test_with_empty_message(self) -> None:
"""Test behavior with an empty message."""
error = A2AClientInvalidArgsError('')
assert error.message == ''
assert str(error) == 'Invalid arguments error: '
class TestA2AClientInvalidStateError:
"""Test cases for A2AClientInvalidStateError class."""
def test_instantiation(self) -> None:
"""Test that A2AClientInvalidStateError can be instantiated."""
error = A2AClientInvalidStateError('Client not initialized')
assert isinstance(error, A2AClientError)
assert error.message == 'Client not initialized'
def test_message_formatting(self) -> None:
"""Test that the error message is formatted correctly."""
error = A2AClientInvalidStateError('Already closed')
assert str(error) == 'Invalid state error: Already closed'
def test_repr(self) -> None:
"""Test that __repr__ shows structured attributes."""
error = A2AClientInvalidStateError('Client not initialized')
assert (
repr(error)
== "A2AClientInvalidStateError(message='Client not initialized')"
)
def test_inheritance(self) -> None:
"""Test that A2AClientInvalidStateError inherits from A2AClientError."""
error = A2AClientInvalidStateError('bad state')
assert isinstance(error, A2AClientError)
def test_with_empty_message(self) -> None:
"""Test behavior with an empty message."""
error = A2AClientInvalidStateError('')
assert error.message == ''
assert str(error) == 'Invalid state error: '
class TestA2AClientJSONRPCError:
"""Test cases for A2AClientJSONRPCError class."""
def _make_error_response(
self, code: int = -32600, message: str = 'Invalid Request', data=None
):
"""Helper to create a mock JSONRPCErrorResponse."""
inner_error = MagicMock()
inner_error.code = code
inner_error.message = message
inner_error.data = data
response = MagicMock()
response.error = inner_error
return response
def test_instantiation(self) -> None:
"""Test that A2AClientJSONRPCError can be instantiated."""
response = self._make_error_response()
error = A2AClientJSONRPCError(response)
assert isinstance(error, A2AClientError)
assert error.error == response.error
def test_repr(self) -> None:
"""Test that __repr__ shows the JSON-RPC error object."""
response = self._make_error_response(-32601, 'Method not found')
error = A2AClientJSONRPCError(response)
result = repr(error)
assert result.startswith('A2AClientJSONRPCError(')
assert result.endswith(')')
def test_inheritance(self) -> None:
"""Test that A2AClientJSONRPCError inherits from A2AClientError."""
response = self._make_error_response()
error = A2AClientJSONRPCError(response)
assert isinstance(error, A2AClientError)
def test_with_empty_message(self) -> None:
"""Test behavior with an empty message."""
response = self._make_error_response(message='')
error = A2AClientJSONRPCError(response)
assert error.error.message == ''
assert str(error) == f'JSON-RPC Error {response.error}'
class TestExceptionHierarchy:
"""Test the exception hierarchy and relationships."""
def test_exception_hierarchy(self) -> None:
"""Test that the exception hierarchy is correct."""
assert issubclass(A2AClientError, Exception)
assert issubclass(A2AClientHTTPError, A2AClientError)
assert issubclass(A2AClientJSONError, A2AClientError)
assert issubclass(A2AClientTimeoutError, A2AClientError)
assert issubclass(A2AClientInvalidArgsError, A2AClientError)
assert issubclass(A2AClientInvalidStateError, A2AClientError)
assert issubclass(A2AClientJSONRPCError, A2AClientError)
def test_catch_specific_exception(self) -> None:
"""Test that specific exceptions can be caught."""
try:
raise A2AClientHTTPError(404, 'Not Found')
except A2AClientHTTPError as e:
assert e.status_code == 404
assert e.message == 'Not Found'
def test_catch_base_exception(self) -> None:
"""Test that derived exceptions can be caught as base exception."""
exceptions = [
A2AClientHTTPError(404, 'Not Found'),
A2AClientJSONError('Invalid JSON'),
A2AClientTimeoutError('Timed out'),
A2AClientInvalidArgsError('Bad args'),
A2AClientInvalidStateError('Bad state'),
]
for raised_error in exceptions:
try:
raise raised_error
except A2AClientError as e:
assert isinstance(e, A2AClientError)
class TestExceptionRaising:
"""Test cases for raising and handling the exceptions."""
def test_raising_http_error(self) -> NoReturn:
"""Test raising an HTTP error and checking its properties."""
with pytest.raises(A2AClientHTTPError) as excinfo:
raise A2AClientHTTPError(429, 'Too Many Requests')
error = excinfo.value
assert error.status_code == 429
assert error.message == 'Too Many Requests'
assert str(error) == 'HTTP Error 429: Too Many Requests'
def test_raising_json_error(self) -> NoReturn:
"""Test raising a JSON error and checking its properties."""
with pytest.raises(A2AClientJSONError) as excinfo:
raise A2AClientJSONError('Invalid format')
error = excinfo.value
assert error.message == 'Invalid format'
assert str(error) == 'JSON Error: Invalid format'
def test_raising_base_error(self) -> NoReturn:
"""Test raising the base error."""
with pytest.raises(A2AClientError) as excinfo:
raise A2AClientError('Generic client error')
assert str(excinfo.value) == 'Generic client error'
def test_raising_timeout_error(self) -> NoReturn:
"""Test raising a timeout error and checking its properties."""
with pytest.raises(A2AClientTimeoutError) as excinfo:
raise A2AClientTimeoutError('Connection timed out')
error = excinfo.value
assert error.message == 'Connection timed out'
assert str(error) == 'Timeout Error: Connection timed out'
# Additional parametrized tests for more comprehensive coverage
@pytest.mark.parametrize(
'status_code,message,expected_str,expected_repr',
[
(
400,
'Bad Request',
'HTTP Error 400: Bad Request',
"A2AClientHTTPError(status_code=400, message='Bad Request')",
),
(
404,
'Not Found',
'HTTP Error 404: Not Found',
"A2AClientHTTPError(status_code=404, message='Not Found')",
),
(
500,
'Server Error',
'HTTP Error 500: Server Error',
"A2AClientHTTPError(status_code=500, message='Server Error')",
),
],
)
def test_http_error_parametrized(
status_code: int, message: str, expected_str: str, expected_repr: str
) -> None:
"""Parametrized test for HTTP errors with different status codes."""
error = A2AClientHTTPError(status_code, message)
assert error.status_code == status_code
assert error.message == message
assert str(error) == expected_str
assert repr(error) == expected_repr
@pytest.mark.parametrize(
'message,expected_str,expected_repr',
[
(
'Missing field',
'JSON Error: Missing field',
"A2AClientJSONError(message='Missing field')",
),
(
'Invalid type',
'JSON Error: Invalid type',
"A2AClientJSONError(message='Invalid type')",
),
(
'Parsing failed',
'JSON Error: Parsing failed',
"A2AClientJSONError(message='Parsing failed')",
),
],
)
def test_json_error_parametrized(
message: str, expected_str: str, expected_repr: str
) -> None:
"""Parametrized test for JSON errors with different messages."""
error = A2AClientJSONError(message)
assert error.message == message
assert str(error) == expected_str
assert repr(error) == expected_repr