|
1
|
|
|
import unittest |
|
2
|
|
|
from datetime import datetime |
|
3
|
|
|
|
|
4
|
|
|
from pyprint.NullPrinter import NullPrinter |
|
5
|
|
|
from pyprint.Printer import Printer |
|
6
|
|
|
from pyprint.StringPrinter import StringPrinter |
|
7
|
|
|
|
|
8
|
|
|
from coalib.misc import Constants |
|
9
|
|
|
from coalib.output.printers.LogPrinter import LogPrinter |
|
10
|
|
|
from coalib.processes.communication.LogMessage import LOG_LEVEL, LogMessage |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
class LogPrinterTest(unittest.TestCase): |
|
14
|
|
|
timestamp = datetime.today() |
|
15
|
|
|
log_message = LogMessage(LOG_LEVEL.ERROR, |
|
16
|
|
|
Constants.COMPLEX_TEST_STRING, |
|
17
|
|
|
timestamp=timestamp) |
|
18
|
|
|
|
|
19
|
|
|
def test_interface(self): |
|
20
|
|
|
uut = LogPrinter(Printer()) |
|
21
|
|
|
self.assertRaises(NotImplementedError, |
|
22
|
|
|
uut.log_message, |
|
23
|
|
|
self.log_message) |
|
24
|
|
|
|
|
25
|
|
|
def test_get_printer(self): |
|
26
|
|
|
self.assertIs(LogPrinter(None).printer, None) |
|
27
|
|
|
printer = Printer() |
|
28
|
|
|
self.assertIs(LogPrinter(printer).printer, printer) |
|
29
|
|
|
|
|
30
|
|
|
def test_logging(self): |
|
31
|
|
|
uut = LogPrinter(StringPrinter(), timestamp_format="") |
|
32
|
|
|
uut.log_message(self.log_message, end="") |
|
33
|
|
|
self.assertEqual(uut.printer.string, str(self.log_message)) |
|
34
|
|
|
|
|
35
|
|
|
uut = LogPrinter(StringPrinter(), log_level=LOG_LEVEL.DEBUG) |
|
36
|
|
|
uut.log_message(self.log_message, end="") |
|
37
|
|
|
self.assertEqual( |
|
38
|
|
|
uut.printer.string, |
|
39
|
|
|
"[ERROR][" + self.timestamp.strftime("%X") + "] " + |
|
40
|
|
|
Constants.COMPLEX_TEST_STRING) |
|
41
|
|
|
|
|
42
|
|
|
uut.printer.clear() |
|
43
|
|
|
uut.log(LOG_LEVEL.ERROR, |
|
44
|
|
|
Constants.COMPLEX_TEST_STRING, |
|
45
|
|
|
timestamp=self.timestamp, |
|
46
|
|
|
end="") |
|
47
|
|
|
self.assertEqual( |
|
48
|
|
|
uut.printer.string, |
|
49
|
|
|
"[ERROR][" + self.timestamp.strftime("%X") + "] " + |
|
50
|
|
|
Constants.COMPLEX_TEST_STRING) |
|
51
|
|
|
|
|
52
|
|
|
uut.printer.clear() |
|
53
|
|
|
uut.debug(Constants.COMPLEX_TEST_STRING, |
|
54
|
|
|
"d", |
|
55
|
|
|
timestamp=self.timestamp, |
|
56
|
|
|
end="") |
|
57
|
|
|
self.assertEqual( |
|
58
|
|
|
uut.printer.string, |
|
59
|
|
|
"[DEBUG][" + self.timestamp.strftime("%X") + "] " + |
|
60
|
|
|
Constants.COMPLEX_TEST_STRING + " d") |
|
61
|
|
|
|
|
62
|
|
|
uut.printer.clear() |
|
63
|
|
|
uut.log_level = LOG_LEVEL.INFO |
|
64
|
|
|
uut.debug(Constants.COMPLEX_TEST_STRING, |
|
65
|
|
|
timestamp=self.timestamp, |
|
66
|
|
|
end="") |
|
67
|
|
|
self.assertEqual(uut.printer.string, "") |
|
68
|
|
|
|
|
69
|
|
|
uut.printer.clear() |
|
70
|
|
|
uut.info(Constants.COMPLEX_TEST_STRING, |
|
71
|
|
|
"d", |
|
72
|
|
|
timestamp=self.timestamp, |
|
73
|
|
|
end="") |
|
74
|
|
|
self.assertEqual( |
|
75
|
|
|
uut.printer.string, |
|
76
|
|
|
"[INFO][" + self.timestamp.strftime("%X") + "] " + |
|
77
|
|
|
Constants.COMPLEX_TEST_STRING + " d") |
|
78
|
|
|
|
|
79
|
|
|
uut.log_level = LOG_LEVEL.WARNING |
|
80
|
|
|
uut.printer.clear() |
|
81
|
|
|
uut.debug(Constants.COMPLEX_TEST_STRING, |
|
82
|
|
|
timestamp=self.timestamp, |
|
83
|
|
|
end="") |
|
84
|
|
|
self.assertEqual(uut.printer.string, "") |
|
85
|
|
|
|
|
86
|
|
|
uut.printer.clear() |
|
87
|
|
|
uut.warn(Constants.COMPLEX_TEST_STRING, |
|
88
|
|
|
"d", |
|
89
|
|
|
timestamp=self.timestamp, |
|
90
|
|
|
end="") |
|
91
|
|
|
self.assertEqual( |
|
92
|
|
|
uut.printer.string, |
|
93
|
|
|
"[WARNING][" + self.timestamp.strftime("%X") + "] " + |
|
94
|
|
|
Constants.COMPLEX_TEST_STRING + " d") |
|
95
|
|
|
|
|
96
|
|
|
uut.printer.clear() |
|
97
|
|
|
uut.err(Constants.COMPLEX_TEST_STRING, |
|
98
|
|
|
"d", |
|
99
|
|
|
timestamp=self.timestamp, |
|
100
|
|
|
end="") |
|
101
|
|
|
self.assertEqual( |
|
102
|
|
|
uut.printer.string, |
|
103
|
|
|
"[ERROR][" + self.timestamp.strftime("%X") + "] " + |
|
104
|
|
|
Constants.COMPLEX_TEST_STRING + " d") |
|
105
|
|
|
|
|
106
|
|
|
uut.log_level = LOG_LEVEL.DEBUG |
|
107
|
|
|
uut.printer.clear() |
|
108
|
|
|
uut.log_exception( |
|
109
|
|
|
"Something failed.", |
|
110
|
|
|
NotImplementedError(Constants.COMPLEX_TEST_STRING), |
|
111
|
|
|
timestamp=self.timestamp) |
|
112
|
|
|
self.assertTrue(uut.printer.string.startswith( |
|
113
|
|
|
"[ERROR][" + self.timestamp.strftime("%X") + |
|
114
|
|
|
"] Something failed.\n" + |
|
115
|
|
|
"[DEBUG][" + self.timestamp.strftime("%X") + |
|
116
|
|
|
"] Exception was:")) |
|
117
|
|
|
|
|
118
|
|
|
uut.log_level = LOG_LEVEL.INFO |
|
119
|
|
|
uut.printer.clear() |
|
120
|
|
|
logged = uut.log_exception( |
|
121
|
|
|
"Something failed.", |
|
122
|
|
|
NotImplementedError(Constants.COMPLEX_TEST_STRING), |
|
123
|
|
|
timestamp=self.timestamp, |
|
124
|
|
|
end="") |
|
125
|
|
|
self.assertTrue(uut.printer.string.startswith( |
|
126
|
|
|
"[ERROR][" + self.timestamp.strftime("%X") + |
|
127
|
|
|
"] Something failed.")) |
|
128
|
|
|
|
|
129
|
|
|
def test_raises(self): |
|
130
|
|
|
uut = LogPrinter(NullPrinter()) |
|
131
|
|
|
self.assertRaises(TypeError, uut.log, 5) |
|
132
|
|
|
self.assertRaises(TypeError, uut.log_exception, "message", 5) |
|
133
|
|
|
self.assertRaises(TypeError, uut.log_message, 5) |
|
134
|
|
|
|