-
-
Notifications
You must be signed in to change notification settings - Fork 216
Expand file tree
/
Copy pathtest_exceptions.py
More file actions
155 lines (123 loc) · 5.88 KB
/
test_exceptions.py
File metadata and controls
155 lines (123 loc) · 5.88 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
"""Unit tests for health_check.exceptions module."""
import datetime
import pytest
from django.utils import timezone
from health_check.exceptions import (
HealthCheckException,
ServiceReturnedUnexpectedResult,
ServiceUnavailable,
ServiceWarning,
StatusPageWarning,
)
class TestHealthCheckException:
def test_init__store_message(self):
"""Store message passed to constructor."""
exc = HealthCheckException("test message")
assert exc.message == "test message"
def test_init__timestamp_defaults_to_now(self):
"""Default timestamp to current time when not provided."""
before = timezone.now()
exc = HealthCheckException("test message")
after = timezone.now()
assert before <= exc.timestamp <= after
def test_init__store_timestamp(self):
"""Store explicit timestamp passed to constructor."""
ts = datetime.datetime(2024, 1, 1, tzinfo=datetime.timezone.utc)
exc = HealthCheckException("test message", timestamp=ts)
assert exc.timestamp == ts
def test_str__format_with_type(self):
"""Format string with message type and message."""
exc = HealthCheckException("foo")
assert str(exc) == "Unknown Error: foo"
def test_inherits_from_exception(self):
"""Inherit from Exception base class."""
exc = HealthCheckException("test")
assert isinstance(exc, Exception)
def test_can_raise_and_catch(self):
"""Can be raised and caught."""
with pytest.raises(HealthCheckException) as exc_info:
raise HealthCheckException("error message")
assert exc_info.value.message == "error message"
class TestServiceWarning:
def test_str__format_with_warning_type(self):
"""Format string with 'warning' message type."""
exc = ServiceWarning("unstable")
assert str(exc) == "Warning: unstable"
def test_inherits_from_health_check_exception(self):
"""Inherit from HealthCheckException."""
exc = ServiceWarning("test")
assert isinstance(exc, HealthCheckException)
assert isinstance(exc, Exception)
def test_can_raise_and_catch_as_exception(self):
"""Can be caught as HealthCheckException."""
with pytest.raises(HealthCheckException):
raise ServiceWarning("warning message")
def test_can_raise_and_catch_as_specific_type(self):
"""Can be caught as ServiceWarning specifically."""
with pytest.raises(ServiceWarning):
raise ServiceWarning("warning message")
class TestServiceUnavailable:
def test_str__format_with_unavailable_type(self):
"""Format string with 'unavailable' message type."""
exc = ServiceUnavailable("database down")
assert str(exc) == "Unavailable: database down"
def test_inherits_from_health_check_exception(self):
"""Inherit from HealthCheckException."""
exc = ServiceUnavailable("test")
assert isinstance(exc, HealthCheckException)
assert isinstance(exc, Exception)
def test_can_raise_and_catch_as_exception(self):
"""Can be caught as HealthCheckException."""
with pytest.raises(HealthCheckException):
raise ServiceUnavailable("unavailable message")
def test_can_raise_and_catch_as_specific_type(self):
"""Can be caught as ServiceUnavailable specifically."""
with pytest.raises(ServiceUnavailable):
raise ServiceUnavailable("unavailable message")
class TestServiceReturnedUnexpectedResult:
def test_str__format_with_unexpected_result_type(self):
"""Format string with 'unexpected result' message type."""
exc = ServiceReturnedUnexpectedResult("wrong format")
assert str(exc) == "Unexpected Result: wrong format"
def test_inherits_from_health_check_exception(self):
"""Inherit from HealthCheckException."""
exc = ServiceReturnedUnexpectedResult("test")
assert isinstance(exc, HealthCheckException)
assert isinstance(exc, Exception)
def test_can_raise_and_catch_as_exception(self):
"""Can be caught as HealthCheckException."""
with pytest.raises(HealthCheckException):
raise ServiceReturnedUnexpectedResult("unexpected result message")
def test_can_raise_and_catch_as_specific_type(self):
"""Can be caught as ServiceReturnedUnexpectedResult specifically."""
with pytest.raises(ServiceReturnedUnexpectedResult):
raise ServiceReturnedUnexpectedResult("unexpected result message")
class TestStatusPageWarning:
def test_init__store_message(self):
"""Store message passed to constructor."""
exc = StatusPageWarning("incident detected")
assert exc.message == "incident detected"
def test_init__timestamp_defaults_to_now(self):
"""Default timestamp to current time when not provided."""
before = timezone.now()
exc = StatusPageWarning("incident detected")
after = timezone.now()
assert before <= exc.timestamp <= after
def test_init__store_timestamp(self):
"""Store timestamp passed to constructor."""
ts = datetime.datetime(2024, 1, 1, tzinfo=datetime.timezone.utc)
exc = StatusPageWarning("incident detected", timestamp=ts)
assert exc.timestamp == ts
def test_str__format_with_warning_type(self):
"""Format string with 'warning' message type."""
exc = StatusPageWarning("incident detected")
assert str(exc) == "Warning: incident detected"
def test_inherits_from_service_warning(self):
"""Inherit from ServiceWarning."""
exc = StatusPageWarning("incident")
assert isinstance(exc, ServiceWarning)
assert isinstance(exc, HealthCheckException)
def test_can_raise_and_catch_as_service_warning(self):
"""Can be caught as ServiceWarning."""
with pytest.raises(ServiceWarning):
raise StatusPageWarning("incident")