onaplogging: Docstrings, refactor, type hinting
[logging-analytics.git] / pylog / tests / test_marker_formatter.py
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
6 #
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.
10
11 import sys
12 import unittest
13 from collections import namedtuple
14
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
19
20 import pytest
21
22 from onaplogging.marker import BaseMarker
23 from onaplogging.markerFormatter import MarkerFormatter
24
25
26 class TestMarkerFormatter(unittest.TestCase):
27
28     Record = namedtuple("Record", "marker")
29
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")
34
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}")
39
40             marker_formatter = MarkerFormatter(style="$")
41             self.assertEqual(marker_formatter.style, "$")
42             self.assertEqual(marker_formatter.marker_tag, "${marker}")
43
44             with pytest.raises(ValueError):
45                 MarkerFormatter(style="*")
46
47     def test_marker_formatter_format(self):
48         record = self.Record(BaseMarker("test"))
49
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")
58
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")