-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_logger.py
98 lines (76 loc) · 3.72 KB
/
test_logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from io import StringIO
from unittest import TestCase
from unittest import main as unittest_main
import sys
from experiments.experiment_specs import VerbosityLevel
from training import logger
class TestLogger(TestCase):
""" Tests for the logger module.
Call with 'python -m test.test_logger' from project root '~'.
Call with 'python -m test_logger' from inside '~/test'. """
def test_no_medium_print_for_level_silent(self):
""" Should not print anything, because the verbosity_level is silent. """
with StringIO() as interception:
old_stdout = sys.stdout
sys.stdout = interception
logger.log_from_medium(VerbosityLevel.SILENT, "Some message")
sys.stdout = old_stdout
self.assertEqual("", interception.getvalue())
def test_medium_print_for_level_medium(self):
""" Should print the message, because the verbosity_level is medium. """
with StringIO() as interception:
old_stdout = sys.stdout
sys.stdout = interception
logger.log_from_medium(VerbosityLevel.MEDIUM, "Some message")
sys.stdout = old_stdout
self.assertEqual("Some message\n", interception.getvalue())
def test_medium_print_for_level_detailed(self):
""" Should print the message, because the verbosity_level is detailed. """
with StringIO() as interception:
old_stdout = sys.stdout
sys.stdout = interception
logger.log_from_medium(VerbosityLevel.DETAILED, "Some message")
sys.stdout = old_stdout
self.assertEqual("Some message\n", interception.getvalue())
def test_no_detailed_print_for_level_silent(self):
""" Should not print anything, because the verbosity_level is silent. """
with StringIO() as interception:
old_stdout = sys.stdout
sys.stdout = interception
logger.log_detailed_only(VerbosityLevel.SILENT, "Some message")
sys.stdout = old_stdout
self.assertEqual("", interception.getvalue())
def test_no_detailed_print_for_level_medium(self):
""" Should not print anything, because the verbosity_level is medium. """
with StringIO() as interception:
old_stdout = sys.stdout
sys.stdout = interception
logger.log_detailed_only(VerbosityLevel.MEDIUM, "Some message")
sys.stdout = old_stdout
self.assertEqual("", interception.getvalue())
def test_no_detailed_print_for_level_detailed(self):
""" Should print message, because the verbosity_level is detailed. """
with StringIO() as interception:
old_stdout = sys.stdout
sys.stdout = interception
logger.log_detailed_only(VerbosityLevel.DETAILED, "Some message")
sys.stdout = old_stdout
self.assertEqual("Some message\n", interception.getvalue())
def test_print_message_and_append_new_line(self):
""" Should print message and append a new line. """
with StringIO() as interception:
old_stdout = sys.stdout
sys.stdout = interception
logger.print_message("Some message", True)
sys.stdout = old_stdout
self.assertEqual("Some message\n", interception.getvalue())
def test_print_message_and_append_no_new_line(self):
""" Should print message and do not append a new line. """
with StringIO() as interception:
old_stdout = sys.stdout
sys.stdout = interception
logger.print_message("Some message", False)
sys.stdout = old_stdout
self.assertEqual("Some message", interception.getvalue())
if __name__ == '__main__':
unittest_main()