-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathtest_logger_context_utils.py
379 lines (305 loc) · 12.9 KB
/
test_logger_context_utils.py
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
from typing import Any, Dict
import pytest
from loguru import logger
from requests import PreparedRequest, Request, Response
from fides.api.util.logger_context_utils import (
Contextualizable,
ErrorGroup,
LoggerContextKeys,
log_context,
request_details,
)
class TestLogContextDecorator:
def test_log_context_without_contextualizable_params(self, loguru_caplog):
@log_context
def func():
logger.info("returning")
return
func()
assert loguru_caplog.records[0].extra == {}
def test_log_context_with_contextualizable_params(self, loguru_caplog):
class LoggableClass(Contextualizable):
def get_log_context(self) -> Dict[LoggerContextKeys, Any]:
return {LoggerContextKeys.privacy_request_id: "123"}
@log_context
def func(param: LoggableClass):
logger.info("returning")
return param
func(LoggableClass())
assert loguru_caplog.records[0].extra == {
LoggerContextKeys.privacy_request_id.value: "123"
}
def test_log_context_with_additional_context(self, loguru_caplog):
@log_context(one_more_thing="456")
def func():
logger.info("returning")
return
func()
assert loguru_caplog.records[0].extra == {
"one_more_thing": "456",
}
def test_log_context_with_contextualizable_params_and_additional_context(
self, loguru_caplog
):
class LoggableClass(Contextualizable):
def get_log_context(self) -> Dict[LoggerContextKeys, Any]:
return {LoggerContextKeys.privacy_request_id: "123"}
@log_context(one_more_thing="456")
def func(param: LoggableClass):
logger.info("returning")
return param
func(LoggableClass())
assert loguru_caplog.records[0].extra == {
LoggerContextKeys.privacy_request_id.value: "123",
"one_more_thing": "456",
}
def test_log_context_with_captured_args(self, loguru_caplog):
"""Test that arguments are captured and mapped to context keys correctly"""
@log_context(capture_args={"task_id": LoggerContextKeys.task_id})
def func(task_id: str):
logger.info("processing task")
return task_id
func(task_id="abc123")
assert loguru_caplog.records[0].extra == {
LoggerContextKeys.task_id.value: "abc123"
}
def test_log_context_with_captured_args_and_contextualizable(self, loguru_caplog):
"""Test that both captured args and Contextualizable objects work together"""
class LoggableClass(Contextualizable):
def get_log_context(self) -> Dict[LoggerContextKeys, Any]:
return {LoggerContextKeys.privacy_request_id: "123"}
@log_context(capture_args={"task_id": LoggerContextKeys.task_id})
def func(param: LoggableClass, task_id: str):
logger.info("processing")
return param
func(LoggableClass(), task_id="abc123")
assert loguru_caplog.records[0].extra == {
LoggerContextKeys.privacy_request_id.value: "123",
LoggerContextKeys.task_id.value: "abc123",
}
def test_log_context_with_captured_args_and_additional_context(self, loguru_caplog):
"""Test that captured args work with additional context"""
@log_context(
capture_args={"task_id": LoggerContextKeys.task_id}, tenant="example"
)
def func(task_id: str):
logger.info("processing")
return task_id
func(task_id="abc123")
assert loguru_caplog.records[0].extra == {
LoggerContextKeys.task_id.value: "abc123",
"tenant": "example",
}
def test_log_context_with_missing_captured_arg(self, loguru_caplog):
"""Test that missing captured args don't cause issues"""
@log_context(capture_args={"task_id": LoggerContextKeys.task_id})
def func(different_param: str):
logger.info("processing")
return different_param
func(different_param="abc123")
assert loguru_caplog.records[0].extra == {}
def test_log_context_with_multiple_captured_args(self, loguru_caplog):
"""Test capturing multiple arguments"""
@log_context(
capture_args={
"task_id": LoggerContextKeys.task_id,
"request_id": LoggerContextKeys.privacy_request_id,
}
)
def func(task_id: str, request_id: str):
logger.info("processing")
return task_id, request_id
func(task_id="abc123", request_id="req456")
assert loguru_caplog.records[0].extra == {
LoggerContextKeys.task_id.value: "abc123",
LoggerContextKeys.privacy_request_id.value: "req456",
}
def test_log_context_with_positional_captured_args(self, loguru_caplog):
"""Test that captured args work with positional arguments"""
@log_context(capture_args={"task_id": LoggerContextKeys.task_id})
def func(other_param: str, task_id: str):
logger.info("processing")
return other_param, task_id
func("something", "abc123")
assert loguru_caplog.records[0].extra == {
LoggerContextKeys.task_id.value: "abc123"
}
def test_log_context_with_multiple_positional_captured_args(self, loguru_caplog):
"""Test that multiple captured args work with positional arguments"""
@log_context(
capture_args={
"task_id": LoggerContextKeys.task_id,
"request_id": LoggerContextKeys.privacy_request_id,
}
)
def func(other_param: str, task_id: str, request_id: str):
logger.info("processing")
return other_param, task_id, request_id
# Pass all arguments as positional arguments
func("something", "abc123", "req456")
assert loguru_caplog.records[0].extra == {
LoggerContextKeys.task_id.value: "abc123",
LoggerContextKeys.privacy_request_id.value: "req456",
}
def test_log_context_with_mixed_positional_and_keyword_only_args(
self, loguru_caplog
):
"""Test that captured args work with functions that have a mix of positional and keyword-only arguments"""
@log_context(
capture_args={
"task_id": LoggerContextKeys.task_id,
"request_id": LoggerContextKeys.privacy_request_id,
}
)
def func(task_id: str, *, request_id: str):
logger.info("processing")
return task_id, request_id
# Pass task_id as positional and request_id as keyword (required)
func("abc123", request_id="req456")
assert loguru_caplog.records[0].extra == {
LoggerContextKeys.task_id.value: "abc123",
LoggerContextKeys.privacy_request_id.value: "req456",
}
def test_log_context_with_keyword_only_args(self, loguru_caplog):
"""Test that captured args work with functions that have only keyword-only arguments"""
@log_context(
capture_args={
"task_id": LoggerContextKeys.task_id,
"request_id": LoggerContextKeys.privacy_request_id,
}
)
def func(*, task_id: str, request_id: str):
logger.info("processing")
return task_id, request_id
# All arguments must be passed as keywords
func(task_id="abc123", request_id="req456")
assert loguru_caplog.records[0].extra == {
LoggerContextKeys.task_id.value: "abc123",
LoggerContextKeys.privacy_request_id.value: "req456",
}
def test_log_context_with_default_parameters(self, loguru_caplog):
"""Test that captured args work with functions that have default parameters"""
@log_context(
capture_args={
"task_id": LoggerContextKeys.task_id,
"request_id": LoggerContextKeys.privacy_request_id,
}
)
def func(task_id: str = "default_task", request_id: str = "default_request"):
logger.info("processing")
return task_id, request_id
# Call with no arguments - should use defaults
func()
assert loguru_caplog.records[0].extra == {
LoggerContextKeys.task_id.value: "default_task",
LoggerContextKeys.privacy_request_id.value: "default_request",
}
def test_log_context_with_overridden_default_parameters(self, loguru_caplog):
"""Test that captured args work with functions where default parameters are overridden"""
@log_context(
capture_args={
"task_id": LoggerContextKeys.task_id,
"request_id": LoggerContextKeys.privacy_request_id,
}
)
def func(task_id: str = "default_task", request_id: str = "default_request"):
logger.info("processing")
return task_id, request_id
# Override only one default parameter
func(task_id="abc123")
assert loguru_caplog.records[0].extra == {
LoggerContextKeys.task_id.value: "abc123",
LoggerContextKeys.privacy_request_id.value: "default_request",
}
class TestDetailFunctions:
@pytest.fixture
def prepared_request(self) -> PreparedRequest:
return Request(
method="POST",
url="https://test/users",
headers={"Content-type": "application/json"},
params={"a": "b"},
data={"name": "test"},
).prepare()
def test_request_details(self, prepared_request):
response = Response()
response.status_code = 200
response._content = "test response".encode()
assert request_details(prepared_request, response) == {
"method": "POST",
"url": "https://test/users?a=b",
"body": "name=test",
"response": "test response",
"status_code": 200,
}
@pytest.mark.usefixtures("test_config_dev_mode_disabled")
def test_request_details_dev_mode_disabled(self, prepared_request):
response = Response()
response.status_code = 200
response._content = "test response".encode()
assert request_details(prepared_request, response) == {
"method": "POST",
"url": "https://test/users?a=b",
"status_code": 200,
}
@pytest.mark.parametrize(
"ignore_error, status_code, error_group",
[
(True, 401, ErrorGroup.authentication_error.value),
(True, 403, ErrorGroup.authentication_error.value),
(True, 400, ErrorGroup.client_error.value),
(True, 500, ErrorGroup.server_error.value),
(False, 401, ErrorGroup.authentication_error.value),
(False, 403, ErrorGroup.authentication_error.value),
(False, 400, ErrorGroup.client_error.value),
(False, 500, ErrorGroup.server_error.value),
],
)
def test_request_details_with_errors(
self, ignore_error, status_code, error_group, prepared_request
):
response = Response()
response.status_code = status_code
response._content = "test response".encode()
expected_detail = {
"method": "POST",
"url": "https://test/users?a=b",
"body": "name=test",
"response": "test response",
"status_code": status_code,
}
if not ignore_error:
expected_detail["error_group"] = error_group
assert (
request_details(prepared_request, response, ignore_error) == expected_detail
)
@pytest.mark.parametrize(
"ignore_error, status_code, error_group",
[
(True, 401, ErrorGroup.authentication_error.value),
(True, 403, ErrorGroup.authentication_error.value),
(True, 400, ErrorGroup.client_error.value),
(True, 500, ErrorGroup.server_error.value),
(False, 401, ErrorGroup.authentication_error.value),
(False, 403, ErrorGroup.authentication_error.value),
(False, 400, ErrorGroup.client_error.value),
(False, 500, ErrorGroup.server_error.value),
],
)
@pytest.mark.usefixtures("test_config_dev_mode_disabled")
def test_request_details_with_errors_dev_mode_disabled(
self, ignore_error, status_code, error_group, prepared_request
):
response = Response()
response.status_code = status_code
response._content = "test response".encode()
expected_detail = {
"method": "POST",
"url": "https://test/users?a=b",
"status_code": status_code,
}
if not ignore_error:
expected_detail["error_group"] = error_group
assert (
request_details(prepared_request, response, ignore_error) == expected_detail
)