|
| 1 | +import pytest |
| 2 | + |
| 3 | +from a2a.client import A2AClientError, A2AClientHTTPError, A2AClientJSONError |
| 4 | + |
| 5 | + |
| 6 | +class TestA2AClientError: |
| 7 | + """Test cases for the base A2AClientError class.""" |
| 8 | + |
| 9 | + def test_instantiation(self): |
| 10 | + """Test that A2AClientError can be instantiated.""" |
| 11 | + error = A2AClientError('Test error message') |
| 12 | + assert isinstance(error, Exception) |
| 13 | + assert str(error) == 'Test error message' |
| 14 | + |
| 15 | + def test_inheritance(self): |
| 16 | + """Test that A2AClientError inherits from Exception.""" |
| 17 | + error = A2AClientError() |
| 18 | + assert isinstance(error, Exception) |
| 19 | + |
| 20 | + |
| 21 | +class TestA2AClientHTTPError: |
| 22 | + """Test cases for A2AClientHTTPError class.""" |
| 23 | + |
| 24 | + def test_instantiation(self): |
| 25 | + """Test that A2AClientHTTPError can be instantiated with status_code and message.""" |
| 26 | + error = A2AClientHTTPError(404, 'Not Found') |
| 27 | + assert isinstance(error, A2AClientError) |
| 28 | + assert error.status_code == 404 |
| 29 | + assert error.message == 'Not Found' |
| 30 | + |
| 31 | + def test_message_formatting(self): |
| 32 | + """Test that the error message is formatted correctly.""" |
| 33 | + error = A2AClientHTTPError(500, 'Internal Server Error') |
| 34 | + assert str(error) == 'HTTP Error 500: Internal Server Error' |
| 35 | + |
| 36 | + def test_inheritance(self): |
| 37 | + """Test that A2AClientHTTPError inherits from A2AClientError.""" |
| 38 | + error = A2AClientHTTPError(400, 'Bad Request') |
| 39 | + assert isinstance(error, A2AClientError) |
| 40 | + |
| 41 | + def test_with_empty_message(self): |
| 42 | + """Test behavior with an empty message.""" |
| 43 | + error = A2AClientHTTPError(403, '') |
| 44 | + assert error.status_code == 403 |
| 45 | + assert error.message == '' |
| 46 | + assert str(error) == 'HTTP Error 403: ' |
| 47 | + |
| 48 | + def test_with_various_status_codes(self): |
| 49 | + """Test with different HTTP status codes.""" |
| 50 | + test_cases = [ |
| 51 | + (200, 'OK'), |
| 52 | + (201, 'Created'), |
| 53 | + (400, 'Bad Request'), |
| 54 | + (401, 'Unauthorized'), |
| 55 | + (403, 'Forbidden'), |
| 56 | + (404, 'Not Found'), |
| 57 | + (500, 'Internal Server Error'), |
| 58 | + (503, 'Service Unavailable'), |
| 59 | + ] |
| 60 | + |
| 61 | + for status_code, message in test_cases: |
| 62 | + error = A2AClientHTTPError(status_code, message) |
| 63 | + assert error.status_code == status_code |
| 64 | + assert error.message == message |
| 65 | + assert str(error) == f'HTTP Error {status_code}: {message}' |
| 66 | + |
| 67 | + |
| 68 | +class TestA2AClientJSONError: |
| 69 | + """Test cases for A2AClientJSONError class.""" |
| 70 | + |
| 71 | + def test_instantiation(self): |
| 72 | + """Test that A2AClientJSONError can be instantiated with a message.""" |
| 73 | + error = A2AClientJSONError('Invalid JSON format') |
| 74 | + assert isinstance(error, A2AClientError) |
| 75 | + assert error.message == 'Invalid JSON format' |
| 76 | + |
| 77 | + def test_message_formatting(self): |
| 78 | + """Test that the error message is formatted correctly.""" |
| 79 | + error = A2AClientJSONError('Missing required field') |
| 80 | + assert str(error) == 'JSON Error: Missing required field' |
| 81 | + |
| 82 | + def test_inheritance(self): |
| 83 | + """Test that A2AClientJSONError inherits from A2AClientError.""" |
| 84 | + error = A2AClientJSONError('Parsing error') |
| 85 | + assert isinstance(error, A2AClientError) |
| 86 | + |
| 87 | + def test_with_empty_message(self): |
| 88 | + """Test behavior with an empty message.""" |
| 89 | + error = A2AClientJSONError('') |
| 90 | + assert error.message == '' |
| 91 | + assert str(error) == 'JSON Error: ' |
| 92 | + |
| 93 | + def test_with_various_messages(self): |
| 94 | + """Test with different error messages.""" |
| 95 | + test_messages = [ |
| 96 | + 'Malformed JSON', |
| 97 | + 'Missing required fields', |
| 98 | + 'Invalid data type', |
| 99 | + 'Unexpected JSON structure', |
| 100 | + 'Empty JSON object', |
| 101 | + ] |
| 102 | + |
| 103 | + for message in test_messages: |
| 104 | + error = A2AClientJSONError(message) |
| 105 | + assert error.message == message |
| 106 | + assert str(error) == f'JSON Error: {message}' |
| 107 | + |
| 108 | + |
| 109 | +class TestExceptionHierarchy: |
| 110 | + """Test the exception hierarchy and relationships.""" |
| 111 | + |
| 112 | + def test_exception_hierarchy(self): |
| 113 | + """Test that the exception hierarchy is correct.""" |
| 114 | + assert issubclass(A2AClientError, Exception) |
| 115 | + assert issubclass(A2AClientHTTPError, A2AClientError) |
| 116 | + assert issubclass(A2AClientJSONError, A2AClientError) |
| 117 | + |
| 118 | + def test_catch_specific_exception(self): |
| 119 | + """Test that specific exceptions can be caught.""" |
| 120 | + try: |
| 121 | + raise A2AClientHTTPError(404, 'Not Found') |
| 122 | + except A2AClientHTTPError as e: |
| 123 | + assert e.status_code == 404 |
| 124 | + assert e.message == 'Not Found' |
| 125 | + |
| 126 | + def test_catch_base_exception(self): |
| 127 | + """Test that derived exceptions can be caught as base exception.""" |
| 128 | + exceptions = [ |
| 129 | + A2AClientHTTPError(404, 'Not Found'), |
| 130 | + A2AClientJSONError('Invalid JSON'), |
| 131 | + ] |
| 132 | + |
| 133 | + for raised_error in exceptions: |
| 134 | + try: |
| 135 | + raise raised_error |
| 136 | + except A2AClientError as e: |
| 137 | + assert isinstance(e, A2AClientError) |
| 138 | + |
| 139 | + |
| 140 | +class TestExceptionRaising: |
| 141 | + """Test cases for raising and handling the exceptions.""" |
| 142 | + |
| 143 | + def test_raising_http_error(self): |
| 144 | + """Test raising an HTTP error and checking its properties.""" |
| 145 | + with pytest.raises(A2AClientHTTPError) as excinfo: |
| 146 | + raise A2AClientHTTPError(429, 'Too Many Requests') |
| 147 | + |
| 148 | + error = excinfo.value |
| 149 | + assert error.status_code == 429 |
| 150 | + assert error.message == 'Too Many Requests' |
| 151 | + assert str(error) == 'HTTP Error 429: Too Many Requests' |
| 152 | + |
| 153 | + def test_raising_json_error(self): |
| 154 | + """Test raising a JSON error and checking its properties.""" |
| 155 | + with pytest.raises(A2AClientJSONError) as excinfo: |
| 156 | + raise A2AClientJSONError('Invalid format') |
| 157 | + |
| 158 | + error = excinfo.value |
| 159 | + assert error.message == 'Invalid format' |
| 160 | + assert str(error) == 'JSON Error: Invalid format' |
| 161 | + |
| 162 | + def test_raising_base_error(self): |
| 163 | + """Test raising the base error.""" |
| 164 | + with pytest.raises(A2AClientError) as excinfo: |
| 165 | + raise A2AClientError('Generic client error') |
| 166 | + |
| 167 | + assert str(excinfo.value) == 'Generic client error' |
| 168 | + |
| 169 | + |
| 170 | +# Additional parametrized tests for more comprehensive coverage |
| 171 | + |
| 172 | + |
| 173 | +@pytest.mark.parametrize( |
| 174 | + 'status_code,message,expected', |
| 175 | + [ |
| 176 | + (400, 'Bad Request', 'HTTP Error 400: Bad Request'), |
| 177 | + (404, 'Not Found', 'HTTP Error 404: Not Found'), |
| 178 | + (500, 'Server Error', 'HTTP Error 500: Server Error'), |
| 179 | + ], |
| 180 | +) |
| 181 | +def test_http_error_parametrized(status_code, message, expected): |
| 182 | + """Parametrized test for HTTP errors with different status codes.""" |
| 183 | + error = A2AClientHTTPError(status_code, message) |
| 184 | + assert error.status_code == status_code |
| 185 | + assert error.message == message |
| 186 | + assert str(error) == expected |
| 187 | + |
| 188 | + |
| 189 | +@pytest.mark.parametrize( |
| 190 | + 'message,expected', |
| 191 | + [ |
| 192 | + ('Missing field', 'JSON Error: Missing field'), |
| 193 | + ('Invalid type', 'JSON Error: Invalid type'), |
| 194 | + ('Parsing failed', 'JSON Error: Parsing failed'), |
| 195 | + ], |
| 196 | +) |
| 197 | +def test_json_error_parametrized(message, expected): |
| 198 | + """Parametrized test for JSON errors with different messages.""" |
| 199 | + error = A2AClientJSONError(message) |
| 200 | + assert error.message == message |
| 201 | + assert str(error) == expected |
0 commit comments