Debian Codegraph @

daiquiri/tests/test_daiquiri.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12 import io
13 import json
14 import logging
15 import sys
16 import types
17 import typing
18 import unittest
19 import warnings
20
21 import daiquiri
22
23 real_excepthook = sys.excepthook
24
25
26 class TestDaiquiri(unittest.TestCase):
27 def tearDown(self) -> None:
28 # Be sure to reset the warning capture
29 logging.captureWarnings(False)
30 # Reset exception hook
31 sys.excepthook = real_excepthook
32 super(TestDaiquiri, self).tearDown()
33
34 def test_setup(self) -> None:
35 daiquiri.setup()
36 daiquiri.setup(level=logging.DEBUG)
37 daiquiri.setup(program_name="foobar")
38
39 def test_excepthook(self) -> None:
40 hook_ran = False
41
42 def catcher(
43 exctype: type[BaseException],
44 value: BaseException,
45 traceback: types.TracebackType | None,
46 ) -> None:
47 nonlocal hook_ran
48 hook_ran = True
49 real_excepthook(exctype, value, traceback)
50
51 sys.excepthook = catcher
52
53 sys.excepthook(Exception, Exception("boom"), None)
54 assert hook_ran
55
56 daiquiri.setup()
57
58 hook_ran = False
59 sys.excepthook(Exception, Exception("boom"), None)
60 assert hook_ran
61
62 def test_setup_json_formatter(self) -> None:
63 stream = io.StringIO()
64 daiquiri.setup(
65 outputs=(
66 daiquiri.output.Stream(
67 stream, formatter=daiquiri.formatter.JSON_FORMATTER
68 ),
69 )
70 )
71 daiquiri.getLogger(__name__).warning("foobar")
72 expected: dict[str, typing.Any] = {"message": "foobar"}
73 self.assertEqual(expected, json.loads(stream.getvalue()))
74
75 def test_setup_json_formatter_with_extras(self) -> None:
76 stream = io.StringIO()
77 daiquiri.setup(
78 outputs=(
79 daiquiri.output.Stream(
80 stream, formatter=daiquiri.formatter.JSON_FORMATTER
81 ),
82 )
83 )
84 daiquiri.getLogger(__name__).warning("foobar", foo="bar")
85 expected: dict[str, typing.Any] = {"message": "foobar", "foo": "bar"}
86 self.assertEqual(expected, json.loads(stream.getvalue()))
87
88 def test_get_logger_set_level(self) -> None:
89 logger = daiquiri.getLogger(__name__)
90 logger.setLevel(logging.DEBUG)
91
92 def test_capture_warnings(self) -> None:
93 stream = io.StringIO()
94 daiquiri.setup(outputs=(daiquiri.output.Stream(stream),))
95 warnings.warn("omg!")
96 line = stream.getvalue()
97 self.assertIn("WARNING py.warnings: ", line)
98 self.assertIn(
99 "daiquiri/tests/test_daiquiri.py:95: "
100 'UserWarning: omg!\n warnings.warn("omg!")\n',
101 line,
102 )
103
104 def test_no_capture_warnings(self) -> None:
105 stream = io.StringIO()
106 daiquiri.setup(
107 outputs=(daiquiri.output.Stream(stream),), capture_warnings=False
108 )
109 warnings.warn("omg!")
110 self.assertEqual("", stream.getvalue())
111
112 def test_set_default_log_levels(self) -> None:
113 daiquiri.set_default_log_levels((("amqp", "debug"), ("urllib3", "warn")))
114
115 def test_parse_and_set_default_log_levels(self) -> None:
116 daiquiri.parse_and_set_default_log_levels(("urllib3=warn", "foobar=debug"))
117
118 def test_string_as_setup_outputs_arg(self) -> None:
119 daiquiri.setup(outputs=("stderr", "stdout"))
120
121 if daiquiri.handlers.syslog is not None: # type: ignore[attr-defined]
122 daiquiri.setup(outputs=("syslog",))
123
124 if daiquiri.handlers.journal is not None: # type: ignore[attr-defined]
125 daiquiri.setup(outputs=("journal",))
126
127 def test_special_kwargs(self) -> None:
128 daiquiri.getLogger(__name__).info(
129 "foobar", foo="bar", exc_info=True, stack_info=True
130 )
131
132
133 def test_extra_with_two_loggers() -> None:
134 stream = io.StringIO()
135 daiquiri.setup(outputs=(daiquiri.output.Stream(stream),))
136 log1 = daiquiri.getLogger("foobar")
137 log1.error("argh")
138 log2 = daiquiri.getLogger("foobar", key="value")
139 log2.warning("boo")
140 lines = stream.getvalue().strip().split("\n")
141 assert lines[0].endswith("ERROR foobar: argh")
142 assert lines[1].endswith("WARNING foobar [key: value]: boo")