-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_utils.py
More file actions
259 lines (205 loc) · 8.82 KB
/
test_utils.py
File metadata and controls
259 lines (205 loc) · 8.82 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import dataclasses
import importlib
import json
import logging
import unittest
import warnings
from typing import Any, cast
from unittest.mock import MagicMock, patch
from pythonjsonlogger.json import JsonFormatter
import pyoaev.utils as module
@dataclasses.dataclass
class _SampleData:
value: int
class TestUtils(unittest.TestCase):
def test_custom_json_formatter_inherits_non_deprecated_formatter(self):
self.assertTrue(issubclass(module.CustomJsonFormatter, JsonFormatter))
def test_reloading_utils_does_not_raise_jsonlogger_deprecation_warning(self):
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
importlib.reload(module)
deprecation_messages = [
str(warning.message)
for warning in caught
if issubclass(warning.category, DeprecationWarning)
]
self.assertFalse(
any(
"pythonjsonlogger.jsonlogger has been moved" in message
for message in deprecation_messages
)
)
def test_get_content_type(self):
self.assertEqual(
module.get_content_type("application/json; charset=utf-8"),
"application/json",
)
def test_copy_dict_flattens_nested_dict(self):
destination = {}
module.copy_dict(
src={"a": 1, "meta": {"x": "y", "n": 2}},
dest=destination,
)
self.assertEqual(destination, {"a": 1, "meta[x]": "y", "meta[n]": 2})
def test_remove_none_from_dict(self):
self.assertEqual(module.remove_none_from_dict({"a": 1, "b": None}), {"a": 1})
def test_encoded_id_from_existing_instance_returns_same_object(self):
encoded_id = module.EncodedId("with space")
self.assertIs(module.EncodedId(encoded_id), encoded_id)
def test_encoded_id_encodes_string_and_keeps_int(self):
self.assertEqual(module.EncodedId("a/b c"), "a%2Fb%20c")
self.assertEqual(module.EncodedId(42), "42")
def test_encoded_id_rejects_unsupported_type(self):
with self.assertRaises(TypeError):
module.EncodedId(cast(Any, ["bad"]))
def test_enhanced_json_encoder_serializes_dataclasses(self):
self.assertEqual(
json.dumps(_SampleData(value=3), cls=module.EnhancedJSONEncoder),
'{"value": 3}',
)
def test_required_optional_required_attribute_missing(self):
rules = module.RequiredOptional(required=("name",))
with self.assertRaises(AttributeError):
rules.validate_attrs(data={})
def test_required_optional_excludes_required_attribute(self):
rules = module.RequiredOptional(required=("name",))
rules.validate_attrs(data={}, excludes=["name"])
def test_required_optional_exclusive_allows_only_one_key(self):
rules = module.RequiredOptional(exclusive=("a", "b"))
with self.assertRaises(AttributeError):
rules.validate_attrs(data={"a": 1, "b": 2})
def test_required_optional_exclusive_requires_one_key(self):
rules = module.RequiredOptional(exclusive=("a", "b"))
with self.assertRaises(AttributeError):
rules.validate_attrs(data={})
def test_required_optional_exclusive_with_single_key_is_valid(self):
rules = module.RequiredOptional(exclusive=("a", "b"))
rules.validate_attrs(data={"a": 1})
def test_response_content_returns_iterator_when_requested(self):
response = MagicMock()
response.iter_content.return_value = iter([b"a", b"b"])
iterator = module.response_content(
response,
streamed=False,
action=None,
chunk_size=10,
iterator=True,
)
self.assertEqual(list(iterator), [b"a", b"b"])
def test_response_content_returns_raw_content_when_not_streamed(self):
response = MagicMock()
response.content = b"payload"
data = module.response_content(
response,
streamed=False,
action=None,
chunk_size=10,
iterator=False,
)
self.assertEqual(data, b"payload")
def test_response_content_streamed_uses_action_for_non_empty_chunks(self):
response = MagicMock()
response.iter_content.return_value = [b"one", b"", b"two"]
action = MagicMock()
returned = module.response_content(
response,
streamed=True,
action=action,
chunk_size=10,
iterator=False,
)
self.assertIsNone(returned)
action.assert_any_call(b"one")
action.assert_any_call(b"two")
self.assertEqual(action.call_count, 2)
def test_response_content_streamed_defaults_to_stdout_stream(self):
response = MagicMock()
response.iter_content.return_value = [b"visible"]
with patch("builtins.print") as mock_print:
module.response_content(
response,
streamed=True,
action=None,
chunk_size=10,
iterator=False,
)
mock_print.assert_called_once_with(b"visible")
def test_custom_json_formatter_add_fields_sets_timestamp_and_level(self):
formatter = module.CustomJsonFormatter("%(message)s")
record = logging.LogRecord(
name="test",
level=logging.INFO,
pathname=__file__,
lineno=1,
msg="hello",
args=(),
exc_info=None,
)
log_record = {}
formatter.add_fields(log_record, record, {})
self.assertIn("timestamp", log_record)
self.assertEqual(log_record["level"], "INFO")
def test_setup_logging_config_json_logging_true_uses_custom_formatter(self):
with patch("pyoaev.utils.logging.basicConfig") as mock_basic_config:
module.setup_logging_config(logging.INFO, json_logging=True)
kwargs = mock_basic_config.call_args.kwargs
self.assertEqual(kwargs["level"], logging.INFO)
self.assertIn("handlers", kwargs)
self.assertIsInstance(
kwargs["handlers"][0].formatter, module.CustomJsonFormatter
)
def test_setup_logging_config_json_logging_false_calls_basic_config(self):
with patch("pyoaev.utils.logging.basicConfig") as mock_basic_config:
module.setup_logging_config(logging.WARNING, json_logging=False)
mock_basic_config.assert_called_once_with(level=logging.WARNING)
def test_app_logger_methods_delegate_to_local_logger(self):
with patch("pyoaev.utils.setup_logging_config"):
app_logger = module.AppLogger(logging.INFO)
app_logger.local_logger = MagicMock()
app_logger.debug("d", {"x": 1})
app_logger.info("i")
app_logger.warning("w")
app_logger.error("e")
self.assertTrue(app_logger.local_logger.debug.called)
self.assertTrue(app_logger.local_logger.info.called)
self.assertTrue(app_logger.local_logger.warning.called)
self.assertTrue(app_logger.local_logger.error.called)
def test_logger_helper_returns_app_logger(self):
with patch("pyoaev.utils.setup_logging_config"):
helper = module.logger(logging.INFO)
self.assertIsInstance(helper, module.AppLogger)
def test_pingalive_ping_uses_injector_branch(self):
api = MagicMock()
logger = MagicMock()
ping_alive = module.PingAlive(
api=api, config={"id": 1}, logger=logger, ping_type="injector"
)
ping_alive.exit_event.is_set = MagicMock(side_effect=[False, True])
ping_alive.exit_event.wait = MagicMock()
ping_alive.ping()
api.injector.create.assert_called_once_with({"id": 1}, False)
ping_alive.exit_event.wait.assert_called_once_with(40)
def test_pingalive_ping_uses_collector_branch_and_logs_errors(self):
api = MagicMock()
api.collector.create.side_effect = Exception("boom")
logger = MagicMock()
ping_alive = module.PingAlive(
api=api, config={}, logger=logger, ping_type="collector"
)
ping_alive.exit_event.is_set = MagicMock(side_effect=[False, True])
ping_alive.exit_event.wait = MagicMock()
ping_alive.ping()
logger.error.assert_called_once()
ping_alive.exit_event.wait.assert_called_once_with(40)
def test_pingalive_run_and_stop(self):
ping_alive = module.PingAlive(
api=MagicMock(), config={}, logger=MagicMock(), ping_type="collector"
)
ping_alive.ping = MagicMock()
ping_alive.run()
ping_alive.stop()
ping_alive.logger.info.assert_any_call("Starting PingAlive thread")
ping_alive.logger.info.assert_any_call("Preparing PingAlive for clean shutdown")
self.assertTrue(ping_alive.exit_event.is_set())
if __name__ == "__main__":
unittest.main()