Coverage for tests/unit/errormode/test_errormode_functionality.py: 98%
62 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-04-04 03:33 -0600
« prev ^ index » next coverage.py v7.6.1, created at 2025-04-04 03:33 -0600
1from __future__ import annotations
3import warnings
5from muutils.errormode import ErrorMode
7import pytest
10def test_except():
11 with pytest.raises(ValueError):
12 ErrorMode.EXCEPT.process("test-except", except_cls=ValueError)
14 with pytest.raises(TypeError):
15 ErrorMode.EXCEPT.process("test-except", except_cls=TypeError)
17 with pytest.raises(RuntimeError):
18 ErrorMode.EXCEPT.process("test-except", except_cls=RuntimeError)
20 with pytest.raises(KeyError):
21 ErrorMode.EXCEPT.process("test-except", except_cls=KeyError)
23 with pytest.raises(KeyError):
24 ErrorMode.EXCEPT.process(
25 "test-except", except_cls=KeyError, except_from=ValueError("base exception")
26 )
29def test_warn():
30 with pytest.warns(UserWarning):
31 ErrorMode.WARN.process("test-warn", warn_cls=UserWarning)
33 with pytest.warns(Warning):
34 ErrorMode.WARN.process("test-warn", warn_cls=Warning)
36 with pytest.warns(DeprecationWarning):
37 ErrorMode.WARN.process("test-warn", warn_cls=DeprecationWarning)
40def test_ignore():
41 with warnings.catch_warnings(record=True) as w:
42 ErrorMode.IGNORE.process("test-ignore")
44 ErrorMode.IGNORE.process("test-ignore", except_cls=ValueError)
45 ErrorMode.IGNORE.process("test-ignore", except_from=TypeError("base exception"))
47 ErrorMode.IGNORE.process("test-ignore", warn_cls=UserWarning)
49 assert len(w) == 0, f"There should be no warnings: {w}"
52def test_except_custom():
53 class MyCustomError(ValueError):
54 pass
56 with pytest.raises(MyCustomError):
57 ErrorMode.EXCEPT.process("test-except", except_cls=MyCustomError)
60def test_warn_custom():
61 class MyCustomWarning(Warning):
62 pass
64 with pytest.warns(MyCustomWarning):
65 ErrorMode.WARN.process("test-warn", warn_cls=MyCustomWarning)
68def test_except_mode_chained_exception():
69 try:
70 # set up the base exception
71 try:
72 raise KeyError("base exception")
73 except Exception as base_exception:
74 # catch it, raise another exception with it as the cause
75 ErrorMode.EXCEPT.process(
76 "Test chained exception",
77 except_cls=RuntimeError,
78 except_from=base_exception,
79 )
80 # catch the outer exception
81 except RuntimeError as e:
82 assert str(e) == "Test chained exception"
83 # check that the cause is the base exception
84 assert isinstance(e.__cause__, KeyError)
85 assert repr(e.__cause__) == "KeyError('base exception')"
86 else:
87 assert False, "Expected RuntimeError with cause KeyError"
90def test_logging_global():
91 import muutils.errormode as errormode
93 log: list[str] = []
95 def log_func(msg: str):
96 log.append(msg)
98 ErrorMode.LOG.process("test-log-print")
100 errormode.GLOBAL_LOG_FUNC = log_func
102 ErrorMode.LOG.process("test-log")
103 ErrorMode.LOG.process("test-log-2")
105 assert log == ["test-log", "test-log-2"]
107 ErrorMode.LOG.process("test-log-3")
109 assert log == ["test-log", "test-log-2", "test-log-3"]
112# def test_logging_pass():
113# errmode: ErrorMode = ErrorMode.LOG
115# log: list[str] = []
116# def log_func(msg: str):
117# log.append(msg)
119# errmode.process(
120# "test-log",
121# log_func=log_func,
122# )
124# errmode.process(
125# "test-log-2",
126# log_func=log_func,
127# )
129# assert log == ["test-log", "test-log-2"]
132# def test_logging_init():
133# errmode: ErrorMode = ErrorMode.LOG
135# log: list[str] = []
136# def log_func(msg: str):
137# log.append(msg)
139# errmode.set_log_loc(log_func)
141# errmode.process("test-log")
142# errmode.process("test-log-2")
144# assert log == ["test-log", "test-log-2"]
146# errmode_2: ErrorMode = ErrorMode.LOG
147# log_2: list[str] = []
148# def log_func_2(msg: str):
149# log_2.append(msg)
151# errmode_2.set_log_loc(log_func_2)
153# errmode_2.process("test-log-3")
154# errmode_2.process("test-log-4")
156# assert log_2 == ["test-log-3", "test-log-4"]
157# assert log == ["test-log", "test-log-2"]
160# def test_logging_init_2():
161# log: list[str] = []
162# def log_func(msg: str):
163# log.append(msg)
165# errmode: ErrorMode = ErrorMode.LOG.set_log_loc(log_func)
167# errmode.process("test-log")
168# errmode.process("test-log-2")
170# assert log == ["test-log", "test-log-2"]