Skip to content

Commit 3f75219

Browse files
committed
add tests to get cov of testing.py
1 parent 41518b5 commit 3f75219

File tree

4 files changed

+142
-16
lines changed

4 files changed

+142
-16
lines changed

src/idom/testing.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ class LogAssertionError(AssertionError):
179179

180180

181181
@contextmanager
182-
def assert_idom_did_log(
182+
def assert_idom_logged(
183183
match_message: str = "",
184184
error_type: type[Exception] | None = None,
185185
match_error: str = "",
@@ -247,9 +247,9 @@ def assert_idom_did_not_log(
247247
match_error: str = "",
248248
clear_matched_records: bool = False,
249249
) -> Iterator[None]:
250-
"""Assert the inverse of :func:`assert_idom_did_log`"""
250+
"""Assert the inverse of :func:`assert_idom_logged`"""
251251
try:
252-
with assert_idom_did_log(
252+
with assert_idom_logged(
253253
match_message, error_type, match_error, clear_matched_records
254254
):
255255
yield None
@@ -297,11 +297,15 @@ def capture_idom_logs(use_existing: bool = False) -> Iterator[list[logging.LogRe
297297
return None
298298

299299
handler = _LogRecordCaptor()
300+
original_level = ROOT_LOGGER.level
301+
302+
ROOT_LOGGER.setLevel(logging.DEBUG)
300303
ROOT_LOGGER.addHandler(handler)
301304
try:
302305
yield handler.records
303306
finally:
304307
ROOT_LOGGER.removeHandler(handler)
308+
ROOT_LOGGER.setLevel(original_level)
305309

306310

307311
class _LogRecordCaptor(logging.NullHandler):
@@ -314,9 +318,6 @@ def handle(self, record: logging.LogRecord) -> bool:
314318
return True
315319

316320

317-
_LOG_RECORD_CAPTOR_SINGLTON = _LogRecordCaptor()
318-
319-
320321
class HookCatcher:
321322
"""Utility for capturing a LifeCycleHook from a component
322323

tests/test_core/test_hooks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import idom
77
from idom.core.dispatcher import render_json_patch
88
from idom.core.hooks import LifeCycleHook
9-
from idom.testing import HookCatcher, assert_idom_did_log
9+
from idom.testing import HookCatcher, assert_idom_logged
1010
from tests.general_utils import assert_same_items
1111

1212

@@ -590,7 +590,7 @@ def bad_cleanup():
590590

591591
return idom.html.div()
592592

593-
with assert_idom_did_log(
593+
with assert_idom_logged(
594594
match_message=r"Pre-unmount effect .*? failed",
595595
error_type=ValueError,
596596
):

tests/test_core/test_layout.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from idom.testing import (
1414
HookCatcher,
1515
StaticEventHandler,
16-
assert_idom_did_log,
16+
assert_idom_logged,
1717
capture_idom_logs,
1818
)
1919
from tests.general_utils import assert_same_items
@@ -144,7 +144,7 @@ def OkChild():
144144
def BadChild():
145145
raise ValueError("error from bad child")
146146

147-
with assert_idom_did_log(
147+
with assert_idom_logged(
148148
match_error="error from bad child",
149149
clear_matched_records=True,
150150
):
@@ -188,7 +188,7 @@ def OkChild():
188188
def BadChild():
189189
raise ValueError("error from bad child")
190190

191-
with assert_idom_did_log(
191+
with assert_idom_logged(
192192
match_error="error from bad child",
193193
clear_matched_records=True,
194194
):
@@ -644,7 +644,7 @@ def ComponentReturnsDuplicateKeys():
644644
idom.html.div(key="duplicate"), idom.html.div(key="duplicate")
645645
)
646646

647-
with assert_idom_did_log(
647+
with assert_idom_logged(
648648
error_type=ValueError,
649649
match_error=r"Duplicate keys \['duplicate'\] at '/'",
650650
clear_matched_records=True,
@@ -687,7 +687,7 @@ def raise_error():
687687

688688
return idom.html.button({"onClick": raise_error})
689689

690-
with assert_idom_did_log(
690+
with assert_idom_logged(
691691
match_error="bad event handler",
692692
clear_matched_records=True,
693693
):
@@ -714,7 +714,7 @@ def Child(state):
714714
idom.hooks.use_effect(lambda: lambda: print("unmount", state))
715715
return idom.html.div(state)
716716

717-
with assert_idom_did_log(
717+
with assert_idom_logged(
718718
r"Did not render component with model state ID .*? - component already unmounted",
719719
):
720720
with idom.Layout(Parent()) as layout:
@@ -753,7 +753,7 @@ def Child():
753753
"component": Child(key="the-same-key"),
754754
}
755755

756-
with assert_idom_did_log(
756+
with assert_idom_logged(
757757
error_type=ValueError,
758758
match_error="prior element with this key wasn't a component",
759759
clear_matched_records=True,
@@ -784,7 +784,7 @@ def Child():
784784
"component": Child(key="the-same-key"),
785785
}
786786

787-
with assert_idom_did_log(
787+
with assert_idom_logged(
788788
error_type=ValueError,
789789
match_error="prior element with this key was a component",
790790
clear_matched_records=True,

tests/test_testing.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import logging
2+
3+
import pytest
4+
5+
from idom import testing
6+
from idom.log import ROOT_LOGGER as logger
7+
8+
9+
def test_assert_idom_logged_does_not_supress_errors():
10+
with pytest.raises(RuntimeError, match="expected error"):
11+
with testing.assert_idom_logged():
12+
raise RuntimeError("expected error")
13+
14+
15+
def test_assert_idom_logged_message():
16+
with testing.assert_idom_logged(match_message="my message"):
17+
logger.info("my message")
18+
19+
with testing.assert_idom_logged(match_message=r".*"):
20+
logger.info("my message")
21+
22+
23+
def test_assert_idom_logged_error():
24+
with testing.assert_idom_logged(
25+
match_message="log message",
26+
error_type=ValueError,
27+
match_error="my value error",
28+
):
29+
try:
30+
raise ValueError("my value error")
31+
except ValueError:
32+
logger.exception("log message")
33+
34+
with pytest.raises(
35+
AssertionError,
36+
match=r"Could not find a log record matching the given",
37+
):
38+
with testing.assert_idom_logged(
39+
match_message="log message",
40+
error_type=ValueError,
41+
match_error="my value error",
42+
):
43+
try:
44+
# change error type
45+
raise RuntimeError("my value error")
46+
except RuntimeError:
47+
logger.exception("log message")
48+
49+
with pytest.raises(
50+
AssertionError,
51+
match=r"Could not find a log record matching the given",
52+
):
53+
with testing.assert_idom_logged(
54+
match_message="log message",
55+
error_type=ValueError,
56+
match_error="my value error",
57+
):
58+
try:
59+
# change error message
60+
raise ValueError("something else")
61+
except ValueError:
62+
logger.exception("log message")
63+
64+
with pytest.raises(
65+
AssertionError,
66+
match=r"Could not find a log record matching the given",
67+
):
68+
with testing.assert_idom_logged(
69+
match_message="log message",
70+
error_type=ValueError,
71+
match_error="my value error",
72+
):
73+
try:
74+
# change error message
75+
raise ValueError("my error message")
76+
except ValueError:
77+
logger.exception("something else")
78+
79+
80+
def test_assert_idom_logged_assertion_error_message():
81+
with pytest.raises(
82+
AssertionError,
83+
match=r"Could not find a log record matching the given",
84+
):
85+
with testing.assert_idom_logged(
86+
# put in all possible params full assertion error message
87+
match_message=r".*",
88+
error_type=Exception,
89+
match_error=r".*",
90+
):
91+
pass
92+
93+
94+
def test_assert_idom_logged_ignores_level():
95+
original_level = logger.level
96+
logger.setLevel(logging.INFO)
97+
try:
98+
with testing.assert_idom_logged(match_message=r".*"):
99+
# this log would normally be ignored
100+
logger.debug("my message")
101+
finally:
102+
logger.setLevel(original_level)
103+
104+
105+
def test_assert_idom_did_not_log():
106+
with testing.assert_idom_did_not_log(match_message="my message"):
107+
pass
108+
109+
with testing.assert_idom_did_not_log(match_message=r"something else"):
110+
logger.info("my message")
111+
112+
with pytest.raises(
113+
AssertionError,
114+
match=r"Did find a log record matching the given",
115+
):
116+
with testing.assert_idom_did_not_log(
117+
# put in all possible params full assertion error message
118+
match_message=r".*",
119+
error_type=Exception,
120+
match_error=r".*",
121+
):
122+
try:
123+
raise Exception("something")
124+
except Exception:
125+
logger.exception("something")

0 commit comments

Comments
 (0)