-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_exceptions.py
More file actions
236 lines (176 loc) · 7.58 KB
/
Copy pathtest_exceptions.py
File metadata and controls
236 lines (176 loc) · 7.58 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
"""Tests for exception classes."""
import pytest
def test_imports():
"""Test that all exceptions can be imported."""
from aptabase.exceptions import (
AptabaseError,
ConfigurationError,
NetworkError,
ValidationError,
)
assert AptabaseError is not None
assert ConfigurationError is not None
assert NetworkError is not None
assert ValidationError is not None
class TestAptabaseError:
"""Test AptabaseError base class."""
def test_create_with_message(self):
"""Test creating error with message."""
from aptabase.exceptions import AptabaseError
error = AptabaseError("Test error message")
assert str(error) == "Test error message"
def test_is_exception(self):
"""Test that AptabaseError is an Exception."""
from aptabase.exceptions import AptabaseError
error = AptabaseError("Test")
assert isinstance(error, Exception)
def test_can_be_raised(self):
"""Test that error can be raised and caught."""
from aptabase.exceptions import AptabaseError
with pytest.raises(AptabaseError, match="Test error"):
raise AptabaseError("Test error")
class TestConfigurationError:
"""Test ConfigurationError class."""
def test_create_with_message(self):
"""Test creating error with message."""
from aptabase.exceptions import ConfigurationError
error = ConfigurationError("Invalid configuration")
assert str(error) == "Invalid configuration"
def test_inherits_from_aptabase_error(self):
"""Test that ConfigurationError inherits from AptabaseError."""
from aptabase.exceptions import AptabaseError, ConfigurationError
error = ConfigurationError("Test")
assert isinstance(error, AptabaseError)
assert isinstance(error, Exception)
def test_can_be_raised(self):
"""Test that error can be raised and caught."""
from aptabase.exceptions import ConfigurationError
with pytest.raises(ConfigurationError, match="Config error"):
raise ConfigurationError("Config error")
def test_can_be_caught_as_base_error(self):
"""Test that ConfigurationError can be caught as AptabaseError."""
from aptabase.exceptions import AptabaseError, ConfigurationError
with pytest.raises(AptabaseError):
raise ConfigurationError("Test")
class TestNetworkError:
"""Test NetworkError class."""
def test_create_with_message_only(self):
"""Test creating error with message only."""
from aptabase.exceptions import NetworkError
error = NetworkError("Connection failed")
assert str(error) == "Connection failed"
assert error.status_code is None
def test_create_with_status_code(self):
"""Test creating error with status code."""
from aptabase.exceptions import NetworkError
error = NetworkError("Request failed", status_code=404)
assert str(error) == "Request failed"
assert error.status_code == 404
def test_create_with_various_status_codes(self):
"""Test creating error with various HTTP status codes."""
from aptabase.exceptions import NetworkError
test_cases = [
(400, "Bad Request"),
(401, "Unauthorized"),
(403, "Forbidden"),
(404, "Not Found"),
(500, "Internal Server Error"),
(502, "Bad Gateway"),
(503, "Service Unavailable"),
]
for status_code, message in test_cases:
error = NetworkError(message, status_code=status_code)
assert error.status_code == status_code
assert str(error) == message
def test_inherits_from_aptabase_error(self):
"""Test that NetworkError inherits from AptabaseError."""
from aptabase.exceptions import AptabaseError, NetworkError
error = NetworkError("Test")
assert isinstance(error, AptabaseError)
assert isinstance(error, Exception)
def test_can_be_raised(self):
"""Test that error can be raised and caught."""
from aptabase.exceptions import NetworkError
with pytest.raises(NetworkError, match="Network error"):
raise NetworkError("Network error", status_code=500)
def test_status_code_attribute_accessible(self):
"""Test that status_code attribute is accessible."""
from aptabase.exceptions import NetworkError
error = NetworkError("Test", status_code=200)
assert hasattr(error, "status_code")
assert error.status_code == 200
def test_can_be_caught_as_base_error(self):
"""Test that NetworkError can be caught as AptabaseError."""
from aptabase.exceptions import AptabaseError, NetworkError
with pytest.raises(AptabaseError):
raise NetworkError("Test", status_code=500)
class TestValidationError:
"""Test ValidationError class."""
def test_create_with_message(self):
"""Test creating error with message."""
from aptabase.exceptions import ValidationError
error = ValidationError("Invalid data")
assert str(error) == "Invalid data"
def test_inherits_from_aptabase_error(self):
"""Test that ValidationError inherits from AptabaseError."""
from aptabase.exceptions import AptabaseError, ValidationError
error = ValidationError("Test")
assert isinstance(error, AptabaseError)
assert isinstance(error, Exception)
def test_can_be_raised(self):
"""Test that error can be raised and caught."""
from aptabase.exceptions import ValidationError
with pytest.raises(ValidationError, match="Validation failed"):
raise ValidationError("Validation failed")
def test_can_be_caught_as_base_error(self):
"""Test that ValidationError can be caught as AptabaseError."""
from aptabase.exceptions import AptabaseError, ValidationError
with pytest.raises(AptabaseError):
raise ValidationError("Test")
class TestExceptionHierarchy:
"""Test exception hierarchy and inheritance."""
def test_all_inherit_from_aptabase_error(self):
"""Test that all custom exceptions inherit from AptabaseError."""
from aptabase.exceptions import (
AptabaseError,
ConfigurationError,
NetworkError,
ValidationError,
)
errors = [
ConfigurationError("test"),
NetworkError("test"),
ValidationError("test"),
]
for error in errors:
assert isinstance(error, AptabaseError)
def test_all_inherit_from_exception(self):
"""Test that all custom exceptions inherit from Exception."""
from aptabase.exceptions import (
ConfigurationError,
NetworkError,
ValidationError,
)
errors = [
ConfigurationError("test"),
NetworkError("test"),
ValidationError("test"),
]
for error in errors:
assert isinstance(error, Exception)
def test_catch_any_with_base_error(self):
"""Test catching any custom exception with AptabaseError."""
from aptabase.exceptions import (
AptabaseError,
ConfigurationError,
NetworkError,
ValidationError,
)
exceptions_to_test = [
ConfigurationError("config"),
NetworkError("network"),
ValidationError("validation"),
]
for exc in exceptions_to_test:
with pytest.raises(AptabaseError):
raise exc