|
| 1 | +import unittest |
| 2 | +from unittest.mock import Mock |
| 3 | +import requests |
| 4 | + |
| 5 | +from codeocean.error import Error |
| 6 | + |
| 7 | + |
| 8 | +class TestError(unittest.TestCase): |
| 9 | + """Test cases for the Error exception class.""" |
| 10 | + |
| 11 | + def test_error_is_exception_subclass(self): |
| 12 | + """Test that Error is a subclass of Exception.""" |
| 13 | + self.assertTrue(issubclass(Error, Exception)) |
| 14 | + |
| 15 | + def test_error_with_json_dict(self): |
| 16 | + """Test Error creation with JSON dict response containing message.""" |
| 17 | + # Create mock HTTPError and response |
| 18 | + mock_response = Mock() |
| 19 | + mock_response.status_code = 400 |
| 20 | + mock_response.json.return_value = {"message": "Custom error message", "datasets": [{"id": "123", "name": "tv"}]} |
| 21 | + |
| 22 | + mock_http_error = Mock(spec=requests.HTTPError) |
| 23 | + mock_http_error.response = mock_response |
| 24 | + |
| 25 | + # Create Error instance |
| 26 | + error = Error(mock_http_error) |
| 27 | + |
| 28 | + # Verify attributes |
| 29 | + self.assertEqual(error.status_code, 400) |
| 30 | + self.assertEqual(error.message, "Custom error message") |
| 31 | + self.assertEqual(error.data, {"message": "Custom error message", "datasets": [{"id": "123", "name": "tv"}]}) |
| 32 | + |
| 33 | + def test_error_with_json_dict_no_message(self): |
| 34 | + """Test Error creation with JSON dict response without message field.""" |
| 35 | + # Create mock HTTPError and response |
| 36 | + mock_response = Mock() |
| 37 | + mock_response.status_code = 500 |
| 38 | + mock_response.json.return_value = {"error": "some other field"} |
| 39 | + |
| 40 | + mock_http_error = Mock(spec=requests.HTTPError) |
| 41 | + mock_http_error.response = mock_response |
| 42 | + |
| 43 | + # Create Error instance |
| 44 | + error = Error(mock_http_error) |
| 45 | + |
| 46 | + # Verify attributes |
| 47 | + self.assertEqual(error.status_code, 500) |
| 48 | + self.assertEqual(error.message, "An error occurred.") |
| 49 | + self.assertEqual(error.data, {"error": "some other field"}) |
| 50 | + |
| 51 | + def test_error_with_json_list(self): |
| 52 | + """Test Error creation with JSON list response.""" |
| 53 | + # Create mock HTTPError and response |
| 54 | + mock_response = Mock() |
| 55 | + mock_response.status_code = 403 |
| 56 | + mock_response.json.return_value = [{"field": "error1"}, {"field": "error2"}] |
| 57 | + |
| 58 | + mock_http_error = Mock(spec=requests.HTTPError) |
| 59 | + mock_http_error.response = mock_response |
| 60 | + |
| 61 | + # Create Error instance |
| 62 | + error = Error(mock_http_error) |
| 63 | + |
| 64 | + # Verify attributes |
| 65 | + self.assertEqual(error.status_code, 403) |
| 66 | + self.assertEqual(error.message, "An error occurred.") |
| 67 | + self.assertEqual(error.data, [{"field": "error1"}, {"field": "error2"}]) |
| 68 | + |
| 69 | + def test_error_with_non_json_response(self): |
| 70 | + """Test Error creation when response is not JSON.""" |
| 71 | + # Create mock HTTPError and response |
| 72 | + mock_response = Mock() |
| 73 | + mock_response.status_code = 404 |
| 74 | + mock_response.json.side_effect = Exception("Not JSON") |
| 75 | + mock_response.text = "Page not found" |
| 76 | + |
| 77 | + mock_http_error = Mock(spec=requests.HTTPError) |
| 78 | + mock_http_error.response = mock_response |
| 79 | + |
| 80 | + # Create Error instance |
| 81 | + error = Error(mock_http_error) |
| 82 | + |
| 83 | + # Verify attributes |
| 84 | + self.assertEqual(error.status_code, 404) |
| 85 | + self.assertEqual(error.message, "Page not found") |
| 86 | + self.assertIsNone(error.data) |
| 87 | + |
| 88 | + def test_error_str_method_with_data(self): |
| 89 | + """Test Error __str__ method when data is present.""" |
| 90 | + # Create mock HTTPError and response |
| 91 | + mock_response = Mock() |
| 92 | + mock_response.status_code = 400 |
| 93 | + mock_response.json.return_value = {"message": "Validation failed", "errors": ["field1", "field2"]} |
| 94 | + |
| 95 | + mock_http_error = Mock(spec=requests.HTTPError) |
| 96 | + mock_http_error.response = mock_response |
| 97 | + mock_http_error.__str__ = Mock(return_value="400 Client Error: Bad Request for url: http://example.com") |
| 98 | + |
| 99 | + # Create Error instance |
| 100 | + error = Error(mock_http_error) |
| 101 | + |
| 102 | + # Test __str__ method |
| 103 | + error_str = str(error) |
| 104 | + |
| 105 | + # Verify the string contains expected components |
| 106 | + self.assertEqual(error_str, """400 Client Error: Bad Request for url: http://example.com |
| 107 | +
|
| 108 | +Message: Validation failed |
| 109 | +
|
| 110 | +Data: |
| 111 | +{ |
| 112 | + "message": "Validation failed", |
| 113 | + "errors": [ |
| 114 | + "field1", |
| 115 | + "field2" |
| 116 | + ] |
| 117 | +}""") |
| 118 | + |
| 119 | + def test_error_str_method_without_data(self): |
| 120 | + """Test Error __str__ method when data is None.""" |
| 121 | + # Create mock HTTPError and response |
| 122 | + mock_response = Mock() |
| 123 | + mock_response.status_code = 404 |
| 124 | + mock_response.json.side_effect = Exception("Not JSON") |
| 125 | + mock_response.text = "Page not found" |
| 126 | + |
| 127 | + mock_http_error = Mock(spec=requests.HTTPError) |
| 128 | + mock_http_error.response = mock_response |
| 129 | + mock_http_error.__str__ = Mock(return_value="404 Client Error: Not Found for url: http://example.com") |
| 130 | + |
| 131 | + # Create Error instance |
| 132 | + error = Error(mock_http_error) |
| 133 | + |
| 134 | + # Test __str__ method |
| 135 | + error_str = str(error) |
| 136 | + |
| 137 | + # Verify the string contains expected components |
| 138 | + self.assertEqual(error_str, """404 Client Error: Not Found for url: http://example.com |
| 139 | +
|
| 140 | +Message: Page not found""") |
0 commit comments