|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import inspect |
| 4 | +import logging |
4 | 5 | from typing import Any, cast |
5 | 6 |
|
6 | 7 | import _testutils |
|
10 | 11 | ErrorDomains, |
11 | 12 | ErrorLevels, |
12 | 13 | ErrorTypes as ErrorTypes, |
| 14 | + PyErrorLog, |
13 | 15 | XMLSyntaxError, |
14 | 16 | _ListErrorLog, |
15 | 17 | _LogEntry as _LogEntry, |
| 18 | + clear_error_log, |
16 | 19 | fromstring, |
| 20 | + use_global_python_log, |
17 | 21 | ) |
18 | 22 |
|
19 | 23 | reveal_type = getattr(_testutils, "reveal_type_wrapper") |
20 | 24 |
|
21 | 25 |
|
| 26 | +### NOTES |
| 27 | +# |
| 28 | +# - Not testing manual construction of _ErrorLog; technically |
| 29 | +# feasible, but it does not make sense creating a collection |
| 30 | +# of error entries out of context |
| 31 | +# |
| 32 | +# - Not testing _DomainErrorLog as it is completely unused |
| 33 | +# |
| 34 | +# - Not testing _RotatingErrorLog, which is only used in |
| 35 | +# global lxml logging, and doesn't expose any attributes |
| 36 | +# other than those already present in _ListErrorLog |
| 37 | + |
| 38 | + |
22 | 39 | def _method_no_kwarg() -> bool: |
23 | 40 | # Param for some methods (filter_levels, receive) |
24 | 41 | # is strictly positional in lxml 4.9. |
@@ -176,9 +193,71 @@ def test_create_empty_log(self) -> None: |
176 | 193 | reveal_type(e_copy) |
177 | 194 |
|
178 | 195 |
|
179 | | -# TODO PyErrorLog, _RotatingErrorLog |
180 | | -# The unused _DomainErrorLog is ignored |
| 196 | +class TestModuleFunc: |
181 | 197 |
|
182 | | -# Not testing manual construction of _ErrorLog; technically |
183 | | -# feasible, but it does not make sense creating a collection |
184 | | -# of error entries out of context |
| 198 | + def test_sig(self) -> None: |
| 199 | + sig = inspect.signature(clear_error_log) |
| 200 | + param = list(sig.parameters.values()) |
| 201 | + assert len(param) == 0 |
| 202 | + del sig, param |
| 203 | + |
| 204 | + sig = inspect.signature(use_global_python_log) |
| 205 | + param = list(sig.parameters.values()) |
| 206 | + assert len(param) == 1 |
| 207 | + assert param[0].name == "log" |
| 208 | + assert param[0].kind == inspect.Parameter.POSITIONAL_OR_KEYWORD |
| 209 | + del sig, param |
| 210 | + |
| 211 | + with pytest.raises( |
| 212 | + TypeError, match=r"expected lxml\.etree\.PyErrorLog, got int" |
| 213 | + ): |
| 214 | + use_global_python_log(cast(Any, 1)) |
| 215 | + |
| 216 | + # exception if used after use_global_python_log |
| 217 | + clear_error_log() |
| 218 | + |
| 219 | + pylog = PyErrorLog() |
| 220 | + use_global_python_log(pylog) |
| 221 | + |
| 222 | + |
| 223 | +class TestPyErrorLog: |
| 224 | + def test_construct(self) -> None: |
| 225 | + pylog = PyErrorLog() |
| 226 | + use_global_python_log(pylog) |
| 227 | + del pylog |
| 228 | + |
| 229 | + pylog = PyErrorLog("foobar") |
| 230 | + use_global_python_log(pylog) |
| 231 | + del pylog |
| 232 | + |
| 233 | + with pytest.raises(TypeError, match="logger name must be a string"): |
| 234 | + _ = PyErrorLog(cast(Any, 1)) |
| 235 | + |
| 236 | + pylog = PyErrorLog(logger_name="foobar") |
| 237 | + use_global_python_log(pylog) |
| 238 | + del pylog |
| 239 | + |
| 240 | + logger = logging.Logger("foobar") |
| 241 | + pylog = PyErrorLog(logger=logger) |
| 242 | + use_global_python_log(pylog) |
| 243 | + del pylog |
| 244 | + |
| 245 | + with pytest.raises(AttributeError, match="has no attribute 'log'"): |
| 246 | + _ = PyErrorLog(logger=cast(Any, "foobar")) |
| 247 | + |
| 248 | + def test_properties(self) -> None: |
| 249 | + pylog = PyErrorLog() |
| 250 | + use_global_python_log(pylog) |
| 251 | + |
| 252 | + reveal_type(pylog.last_error) # None initially |
| 253 | + for mapping in pylog.level_map.items(): |
| 254 | + reveal_type(mapping[0]) |
| 255 | + reveal_type(mapping[1]) |
| 256 | + |
| 257 | + broken_xml = "<doc><a><b></a>&bar;</doc>" |
| 258 | + with pytest.raises(XMLSyntaxError): |
| 259 | + _ = fromstring(broken_xml) |
| 260 | + |
| 261 | + assert pylog.last_error is not None |
| 262 | + reveal_type(pylog.last_error) |
| 263 | + pylog.receive(pylog.last_error) |
0 commit comments