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