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.
13 from collections import namedtuple
15 if sys.version_info[0] < 3:
16 from mock import patch
17 if sys.version_info[0] >= 3:
18 from unittest.mock import patch
22 from onaplogging.marker import BaseMarker
23 from onaplogging.markerFormatter import MarkerFormatter
26 class TestMarkerFormatter(unittest.TestCase):
28 Record = namedtuple("Record", "marker")
30 def test_marker_formatter_init(self):
31 marker_formatter = MarkerFormatter()
32 self.assertEqual(marker_formatter.style, "%")
33 self.assertEqual(marker_formatter.marker_tag, "%(marker)s")
35 if sys.version_info[0] >= 3:
36 marker_formatter = MarkerFormatter(style="{")
37 self.assertEqual(marker_formatter.style, "{")
38 self.assertEqual(marker_formatter.marker_tag, "{marker}")
40 marker_formatter = MarkerFormatter(style="$")
41 self.assertEqual(marker_formatter.style, "$")
42 self.assertEqual(marker_formatter.marker_tag, "${marker}")
44 with pytest.raises(ValueError):
45 MarkerFormatter(style="*")
47 def test_marker_formatter_format(self):
48 record = self.Record(BaseMarker("test"))
50 with patch("onaplogging.markerFormatter.BaseColorFormatter.format") as mock_format:
51 marker_formatter = MarkerFormatter()
52 self.assertEqual(marker_formatter._fmt, "%(message)s")
53 self.assertEqual(marker_formatter.marker_tag, "%(marker)s")
54 marker_formatter.format(record)
55 mock_format.assert_called_once()
56 self.assertEqual(marker_formatter._fmt, "%(message)s")
57 self.assertEqual(marker_formatter.marker_tag, "%(marker)s")
59 with patch("onaplogging.markerFormatter.BaseColorFormatter.format") as mock_format:
60 marker_formatter = MarkerFormatter(fmt="%(message)s %(marker)s")
61 self.assertEqual(marker_formatter._fmt, "%(message)s %(marker)s")
62 self.assertEqual(marker_formatter.marker_tag, "%(marker)s")
63 marker_formatter.format(record)
64 mock_format.assert_called_once()
65 self.assertEqual(marker_formatter._fmt, "%(message)s %(marker)s")
66 self.assertEqual(marker_formatter.marker_tag, "%(marker)s")
67 self.assertEqual(marker_formatter.temp_fmt, "%(message)s %(marker)s")