Skip to content

Commit a166b8c

Browse files
committed
Add tests for MetaWarning class
Signed-off-by: John Pennycook <john.pennycook@intel.com>
1 parent 0c83d52 commit a166b8c

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

tests/cli/test_meta_warning.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Copyright (C) 2019-2024 Intel Corporation
2+
# SPDX-License-Identifier: BSD-3-Clause
3+
4+
import logging
5+
import re
6+
import unittest
7+
8+
from codebasin.cli import MetaWarning
9+
10+
11+
class TestMetaWarning(unittest.TestCase):
12+
"""
13+
Test MetaWarning class.
14+
"""
15+
16+
def test_constructor(self):
17+
"""Check constructor arguments"""
18+
mw = MetaWarning("regex", "msg")
19+
self.assertTrue(mw.regex, re.compile("regex"))
20+
self.assertTrue(mw.msg, "msg")
21+
self.assertEqual(mw._count, 0)
22+
23+
def test_inspect(self):
24+
"""Check inspect matches records correctly"""
25+
mw = MetaWarning("test[0-9]", "Testing")
26+
27+
record = logging.makeLogRecord(
28+
{
29+
"msg": "test1",
30+
"levelname": "WARNING",
31+
},
32+
)
33+
self.assertTrue(mw.inspect(record))
34+
self.assertEqual(mw._count, 1)
35+
36+
record = logging.makeLogRecord(
37+
{
38+
"msg": "testA",
39+
"levelname": "WARNING",
40+
},
41+
)
42+
self.assertFalse(mw.inspect(record))
43+
self.assertEqual(mw._count, 1)
44+
45+
def test_warn(self):
46+
"""Check warn produces expected logging messages"""
47+
logging.disable(logging.NOTSET)
48+
logger = logging.getLogger("codebasin")
49+
50+
mw = MetaWarning("test[0-9]", "Testing {}")
51+
with self.assertNoLogs(logger):
52+
mw.warn(logger)
53+
54+
record = logging.makeLogRecord(
55+
{
56+
"msg": "test1",
57+
"levelno": logging.WARNING,
58+
},
59+
)
60+
mw.inspect(record)
61+
with self.assertLogs(logger, level="WARNING") as cm:
62+
mw.warn(logger)
63+
self.assertEqual(cm.output, ["WARNING:codebasin:Testing 1"])
64+
logging.disable()
65+
66+
67+
if __name__ == "__main__":
68+
unittest.main()

0 commit comments

Comments
 (0)