Pylog test suite
[logging-analytics.git] / pylog / tests / test_color_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 unittest
12 import sys
13 from logging import LogRecord
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 import pytest
20
21 from onaplogging.colorFormatter import (
22     ATTRIBUTES,
23     BaseColorFormatter,
24     colored,
25     COLORS,
26     HIGHLIGHTS,
27     FMT_STR,
28     RESET,
29 )
30 from onaplogging.utils import is_above_python_3_2
31
32
33 class TestColorFormatter(unittest.TestCase):
34
35     TEST_TEXT = "test"
36
37     def test_colored_os_name_nt(self):
38
39         with patch("onaplogging.colorFormatter.os.name", "nt"):
40
41             text = colored(self.TEST_TEXT)
42             assert text == self.TEST_TEXT
43
44             text = colored(self.TEST_TEXT, color="black")
45             assert text == self.TEST_TEXT
46
47             text = colored(self.TEST_TEXT, on_color="black")
48             assert text == self.TEST_TEXT
49
50             text = colored(self.TEST_TEXT, attrs="bold")
51             assert text == self.TEST_TEXT
52
53     def test_colored_os_name_ce(self):
54
55         with patch("onaplogging.colorFormatter.os.name", "ce"):
56
57             text = colored(self.TEST_TEXT)
58             assert text == self.TEST_TEXT
59
60             text = colored(self.TEST_TEXT, color="black")
61             assert text == self.TEST_TEXT
62
63             text = colored(self.TEST_TEXT, on_color="black")
64             assert text == self.TEST_TEXT
65
66             text = colored(self.TEST_TEXT, attrs="bold")
67             assert text == self.TEST_TEXT
68
69     def test_colored_os_name_posix(self):
70
71         with patch("onaplogging.colorFormatter.os.name", "posix"):
72             text = colored(self.TEST_TEXT)
73             assert text == self.TEST_TEXT + RESET
74
75             text = colored(self.TEST_TEXT, color="black")
76             assert text == FMT_STR % (COLORS["black"], self.TEST_TEXT) + RESET
77
78             text = colored(self.TEST_TEXT, color="invalid")
79             assert text == FMT_STR % (0, self.TEST_TEXT) + RESET
80
81             text = colored(self.TEST_TEXT, on_color="red")
82             assert text == FMT_STR % (HIGHLIGHTS["red"], self.TEST_TEXT) + RESET
83
84             text = colored(self.TEST_TEXT, on_color="invalid")
85             assert text == FMT_STR % (0, self.TEST_TEXT) + RESET
86
87             text = colored(self.TEST_TEXT, attrs="bold")
88             assert text == FMT_STR % (ATTRIBUTES["bold"], self.TEST_TEXT) + RESET
89
90             text = colored(self.TEST_TEXT, attrs=["bold", "blink"])
91             assert (
92                 text
93                 == FMT_STR % (ATTRIBUTES["blink"], FMT_STR % (ATTRIBUTES["bold"], self.TEST_TEXT))
94                 + RESET
95             )
96
97             text = colored(self.TEST_TEXT, attrs="invalid")
98             assert text == FMT_STR % (0, self.TEST_TEXT) + RESET
99
100     def test_base_color_formatter(self):
101
102         if is_above_python_3_2():
103             with pytest.raises(ValueError):
104                 BaseColorFormatter(style="!")
105
106         TEST_MESSAGE = "TestMessage"
107         record = LogRecord(
108             name="TestName",
109             level=0,
110             pathname="TestPathName",
111             lineno=1,
112             msg=TEST_MESSAGE,
113             args=None,
114             exc_info=None,
115         )
116
117         base_formatter = BaseColorFormatter()
118         assert base_formatter.format(record) == TEST_MESSAGE + RESET
119
120         base_formatter = BaseColorFormatter(fmt="TEST %(message)s")
121         assert base_formatter.format(record) == "TEST " + TEST_MESSAGE + RESET
122
123         colorfmt = {record.levelname: {"color": "black", "highlight": "red", "attribute": "bold"}}
124         base_formatter = BaseColorFormatter(colorfmt=colorfmt)
125         assert (
126             base_formatter.format(record)
127             == FMT_STR
128             % (
129                 ATTRIBUTES["bold"],
130                 FMT_STR % (HIGHLIGHTS["red"], FMT_STR % (COLORS["black"], "TestMessage")),
131             )
132             + RESET
133         )