Newer
Older
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
# encoding: utf-8
#
# ** header v3.0
# This file is a part of the CaosDB Project.
#
# Copyright (C) 2020 IndiScale GmbH
# Copyright (C) 2020 Henrik tom Wörden
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# ** end header
import logging
import os
import unittest
from tempfile import NamedTemporaryFile
from caosadvancedtools.suppressable import Suppressable
class SupTestBasic(unittest.TestCase):
def setUp(self):
self.db_file = "/tmp/test_suppress_msg_db_file.db"
self.basic = Suppressable(db_file=self.db_file)
def test_msg(self):
# testing basic setup
self.basic.info("hi", 5, "test")
digest = self.basic.hash("hi", 5)
assert self.basic.was_tagged(digest)
self.basic.info("hi", 5, "test")
def tearDown(self):
os.remove(self.db_file)
class SupTestAdvanced(SupTestBasic):
def setUp(self):
self.db_file = "/tmp/test_suppress_msg_db_file.db"
self.basic = Suppressable(db_file=self.db_file)
def test_logger(self):
"""
The logging output is directed to a file which is then checked whether
the output is as expected.
"""
logfile = NamedTemporaryFile()
logger = logging.getLogger()
logger.addHandler(logging.FileHandler(logfile.name))
logger.setLevel(logging.DEBUG)
suppressable = Suppressable(db_file=self.db_file, logger=logger)
suppressable.info("hi", 5, "test")
with open(logfile.name) as lf:
log = lf.read()
# assert that the log was written
assert "hi" in log
# there should be one line so far
assert log.count("\n") == 1
# the following is unchanged and should be suppressed
suppressable.info("hi", 5, "test")
with open(logfile.name) as lf:
log = lf.read()
# one line with one hi
assert log.count("hi") == 1
assert log.count("\n") == 1
# the following is a new message and should thus not be suppressed
suppressable.info("ha", 5, "new")
with open(logfile.name) as lf:
log = lf.read()
assert log.count("ha") == 1
assert log.count("\n") == 2
# the following has a identifier and should thus not be suppressed
suppressable.info("hi", 6, "test")
with open(logfile.name) as lf:
log = lf.read()
assert log.count("hi") == 2
assert log.count("\n") == 3
# the following should be suppressed again
suppressable.info("ha", 5, "new")
with open(logfile.name) as lf:
log = lf.read()
assert log.count("ha") == 1
assert log.count("hi") == 2
assert log.count("\n") == 3
# resetting test category; hi should be removed
suppressable.reset("test")
# hi should not be suppressed
suppressable.info("hi", 5, "test")
with open(logfile.name) as lf:
log = lf.read()
assert log.count("hi") == 3
assert log.count("\n") == 4
# the following should be suppressed still
suppressable.info("ha", 5, "new")
with open(logfile.name) as lf:
log = lf.read()
assert log.count("ha") == 1
assert log.count("hi") == 3
assert log.count("\n") == 4