1
|
|
|
import unittest |
2
|
|
|
from unittest import mock |
3
|
|
|
import logging |
4
|
|
|
|
5
|
|
|
from pyprint.NullPrinter import NullPrinter |
6
|
|
|
from pyprint.Printer import Printer |
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
|
|
|
log_message = LogMessage(LOG_LEVEL.ERROR, |
15
|
|
|
Constants.COMPLEX_TEST_STRING) |
16
|
|
|
|
17
|
|
|
def test_get_printer(self): |
18
|
|
|
self.assertIs(LogPrinter(None).printer, None) |
19
|
|
|
printer = Printer() |
20
|
|
|
self.assertIs(LogPrinter(printer).printer, printer) |
21
|
|
|
|
22
|
|
|
def test_logging(self): |
23
|
|
|
uut = LogPrinter(timestamp_format="") |
24
|
|
|
uut.logger = mock.MagicMock() |
25
|
|
|
uut.log_message(self.log_message) |
26
|
|
|
|
27
|
|
|
msg = Constants.COMPLEX_TEST_STRING |
28
|
|
|
uut.logger.log.assert_called_with(logging.ERROR, msg) |
29
|
|
|
|
30
|
|
|
uut = LogPrinter(log_level=LOG_LEVEL.DEBUG) |
31
|
|
|
uut.logger = mock.MagicMock() |
32
|
|
|
|
33
|
|
|
uut.log(LOG_LEVEL.ERROR, Constants.COMPLEX_TEST_STRING) |
34
|
|
|
uut.logger.log.assert_called_with(logging.ERROR, msg) |
35
|
|
|
|
36
|
|
|
uut.debug(Constants.COMPLEX_TEST_STRING, "d") |
37
|
|
|
uut.logger.log.assert_called_with(logging.DEBUG, msg + " d") |
38
|
|
|
|
39
|
|
|
uut.log_level = LOG_LEVEL.DEBUG |
40
|
|
|
uut.log_exception("Something failed.", NotImplementedError(msg)) |
41
|
|
|
uut.logger.log.assert_any_call(logging.ERROR, "Something failed.") |
42
|
|
|
uut.logger.log.assert_called_with( |
43
|
|
|
logging.DEBUG, |
44
|
|
|
"Exception was:\n{exception}: {msg}".format( |
45
|
|
|
exception="NotImplementedError", |
46
|
|
|
msg=msg)) |
47
|
|
|
|
48
|
|
|
def test_raises(self): |
49
|
|
|
uut = LogPrinter(NullPrinter()) |
50
|
|
|
self.assertRaises(TypeError, uut.log, 5) |
51
|
|
|
self.assertRaises(TypeError, uut.log_exception, "message", 5) |
52
|
|
|
self.assertRaises(TypeError, uut.log_message, 5) |
53
|
|
|
|
54
|
|
|
def test_log_level(self): |
55
|
|
|
uut = LogPrinter() |
56
|
|
|
self.assertEqual(uut.log_level, logging.DEBUG) |
57
|
|
|
uut.log_level = logging.INFO |
58
|
|
|
self.assertEqual(uut.log_level, logging.INFO) |
59
|
|
|
|
60
|
|
|
def test_get_state(self): |
61
|
|
|
uut = LogPrinter() |
62
|
|
|
self.assertNotIn('logger', uut.__getstate__()) |
63
|
|
|
|
64
|
|
|
def test_set_state(self): |
65
|
|
|
uut = LogPrinter() |
66
|
|
|
state = uut.__getstate__() |
67
|
|
|
uut.__setstate__(state) |
68
|
|
|
self.assertIs(uut.logger, logging.getLogger()) |
69
|
|
|
|
70
|
|
|
def test_no_printer(self): |
71
|
|
|
uut = LogPrinter() |
72
|
|
|
self.assertIs(uut.logger, logging.getLogger()) |
73
|
|
|
|