1 # Copyright (c) 2020 Deutsche Telekom.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 if sys.version_info[0] < 3:
15 from mock import MagicMock, patch
16 if sys.version_info[0] >= 3:
17 from unittest.mock import MagicMock, patch
21 from onaplogging.marker import BaseMarker
22 from onaplogging.markerLogAdaptor import MarkerLogAdaptor
25 class TestMarkerLogAdaptor(unittest.TestCase):
27 def test_process(self):
28 log_adaptor = MarkerLogAdaptor(MagicMock(), extra=None)
29 msg, kwargs = log_adaptor.process("test", {})
30 self.assertEqual(msg, "test")
31 self.assertDictEqual(kwargs, {"extra": None})
33 log_adaptor = MarkerLogAdaptor(MagicMock(), extra={"A": "B"})
34 msg, kwargs = log_adaptor.process("test", {})
35 self.assertEqual(msg, "test")
36 self.assertDictEqual(kwargs, {"extra": {"A": "B"}})
38 # Commented out due to that: https://bugs.python.org/issue20239
39 # Comment out if Jenkis build runs using Python > 3.6
40 # def test_markers(self):
41 # log_adaptor = MarkerLogAdaptor(MagicMock(), extra=None)
43 # with patch("onaplogging.markerLogAdaptor.LoggerAdapter.info") as mock_info:
44 # log_adaptor.infoMarker(BaseMarker("info_marker"), "test_message")
45 # mock_info.assert_called_once()
47 # with patch("onaplogging.markerLogAdaptor.LoggerAdapter.debug") as mock_debug:
48 # log_adaptor.debugMarker(BaseMarker("info_marker"), "test_message")
49 # mock_debug.assert_called_once()
51 # with patch("onaplogging.markerLogAdaptor.LoggerAdapter.warning") as mock_warning:
52 # log_adaptor.warningMarker(BaseMarker("info_marker"), "test_message")
53 # mock_warning.assert_called_once()
55 # with patch("onaplogging.markerLogAdaptor.LoggerAdapter.error") as mock_error:
56 # log_adaptor.errorMarker(BaseMarker("info_marker"), "test_message")
57 # mock_error.assert_called_once()
59 # with patch("onaplogging.markerLogAdaptor.LoggerAdapter.exception") as mock_exception:
60 # log_adaptor.exceptionMarker(BaseMarker("info_marker"), "test_message")
61 # mock_exception.assert_called_once()
63 # with patch("onaplogging.markerLogAdaptor.LoggerAdapter.critical") as mock_critical:
64 # log_adaptor.criticalMarker(BaseMarker("info_marker"), "test_message")
65 # mock_critical.assert_called_once()
67 # with patch("onaplogging.markerLogAdaptor.LoggerAdapter.log") as mock_log:
68 # log_adaptor.logMarker(BaseMarker("info_marker"), "info", "test_message")
69 # mock_log.assert_called_once()
71 # with pytest.raises(TypeError):
72 # log_adaptor.infoMarker("info_marker_str", "test_message")
74 # with pytest.raises(Exception):
75 # log_adaptor = MarkerLogAdaptor(MagicMock(), extra={"marker": "exception"})
76 # log_adaptor.infoMarker(BaseMarker("info_marker"), "test_message")