6Regression Tests — logging_handler
7====================================
11Verify that ``OcDiagnosticsHandler`` correctly bridges Python's ``logging``
12module into ``oc_diagnostics._dbg()``, including level mapping, custom domain
13routing, and the non-raising contract on internal exceptions.
17- Confirm that ``OcDiagnosticsHandler.emit()`` routes a LogRecord without raising.
18- Confirm the default level map: DEBUG→L4, INFO→L2, WARNING/ERROR/CRITICAL→L1.
19- Confirm that the handler's ``domain`` argument is forwarded to ``_dbg()``.
20- Confirm that internal exceptions are swallowed (delegated to ``handleError``).
32- Must not permanently modify oc_diagnostics global state.
33- Must not start servers or require network access.
34- Must run deterministically across repeated invocations.
40import unittest.mock
as mock
46_REPO_ROOT = os.path.normpath(os.path.join(os.path.dirname(__file__),
".."))
47if _REPO_ROOT
not in sys.path:
48 sys.path.insert(0, _REPO_ROOT)
54def _make_record(level: int, msg: str =
"test message") -> logging.LogRecord:
55 """Build a minimal LogRecord for testing."""
56 return logging.LogRecord(
59 pathname=
"test_file.py",
72 """OcDiagnosticsHandler.emit() routes an INFO LogRecord through _dbg() without raising."""
74 record =
_make_record(logging.INFO,
"test handler message")
75 old_silent = _ud.SILENT_MODE
76 _ud.SILENT_MODE =
True
80 _ud.SILENT_MODE = old_silent
88 """Default level map routes Python levels to oc_diagnostics levels correctly.
90 DEBUG→L4, INFO→L2, WARNING→L1, ERROR→L1, CRITICAL→L1.
98 (logging.CRITICAL, 1),
101 for py_level, expected_diag_level
in expected:
102 with mock.patch(
"oc_diagnostics.logging_handler._dbg")
as mock_dbg:
105 assert mock_dbg.called, (
106 f
"_dbg was not called for Python level {py_level}"
108 _, kwargs = mock_dbg.call_args
109 actual_level = kwargs.get(
"level")
110 assert actual_level == expected_diag_level, (
111 f
"Python level {py_level}: expected oc_diagnostics L{expected_diag_level}, "
112 f
"got L{actual_level}"
121 """Handler's domain argument is forwarded as the first positional arg to _dbg()."""
125 with mock.patch(
"oc_diagnostics.logging_handler._dbg")
as mock_dbg:
127 assert mock_dbg.called,
"_dbg was not called"
128 args, _ = mock_dbg.call_args
129 assert args[0] ==
"MY_CUSTOM_DOMAIN", (
130 f
"Expected domain 'MY_CUSTOM_DOMAIN', got {args[0]!r}"
139 """OcDiagnosticsHandler.emit() must not propagate exceptions from _dbg()."""
144 with mock.patch.object(handler,
"handleError"):
146 "oc_diagnostics.logging_handler._dbg",
147 side_effect=RuntimeError(
"deliberate internal failure"),
test_handler_custom_domain()
test_handler_level_mapping()
logging.LogRecord _make_record(int level, str msg="test message")
test_handler_never_raises()