-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_log_handler.py
More file actions
91 lines (80 loc) · 2.73 KB
/
Copy pathtest_log_handler.py
File metadata and controls
91 lines (80 loc) · 2.73 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
"""Tests methods in log_handler module and logging functions in init module"""
import importlib
import unittest
from logging import LogRecord
from unittest.mock import MagicMock, mock_open, patch
import aind_data_transfer_service
from aind_data_transfer_service import CustomJsonFormatter
from aind_data_transfer_service.log_handler import (
EventType,
log_submit_job_request,
)
class TestLogHandler(unittest.TestCase):
"""Tests methods in log_handler module"""
@patch("logging.info")
def test_log_submit_job_request(self, mock_log: MagicMock):
"""Tests log_submit_job_request"""
content = {
"upload_jobs": [
{"acq_datetime": "2026-10-10T00:01:02", "subject_id": "123456"}
]
}
log_submit_job_request(
content=content, event_type=EventType.STAGE_START
)
mock_log.assert_called_once_with(
"Handling request",
extra={
"event_type": EventType.STAGE_START,
"subject_id": "123456",
"acquisition_name": "123456_2026-10-10_00-01-02",
},
)
class TestCustomJsonFormatter(unittest.TestCase):
"""Tests methods CustomJsonFormatter from init module"""
@patch("time.time")
def test_format_time(self, mock_time: MagicMock):
"""Tests formatTime"""
mock_time.return_value = 1768856252.7829409
datetime_str = CustomJsonFormatter().formatTime(
record=LogRecord(
name="foo",
level=20,
pathname="foo.bar",
lineno=10,
msg=None,
args=None,
exc_info=None,
)
)
expected_datetime_str = "2026-01-19T20:57:32.782941Z"
self.assertEqual(expected_datetime_str, datetime_str)
@patch("logging.config.dictConfig")
@patch("builtins.open", new_callable=mock_open)
@patch("os.path.isfile")
@patch("logging.info")
def test_logging_config(
self,
mock_log_info: MagicMock,
mock_isfile: MagicMock,
mock_file: MagicMock,
mock_logging_config: MagicMock,
):
"""Tests that logging is configured on package init"""
example_yaml = """
key: value
list_items:
- item1
- item2
"""
mock_isfile.return_value = True
mock_file.return_value.__enter__.return_value.read.return_value = (
example_yaml
)
importlib.reload(aind_data_transfer_service)
mock_logging_config.assert_called_once_with(
{"key": "value", "list_items": ["item1", "item2"]}
)
mock_log_info.assert_called_once()
if __name__ == "__main__":
unittest.main()