Skip to content

Commit 0bcb85e

Browse files
committed
add some tests for erros to increase test coverage
1 parent 7b80f35 commit 0bcb85e

3 files changed

Lines changed: 76 additions & 1 deletion

File tree

.coveragerc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[run]
2+
source = inheritance_explorer
3+
4+
[report]
5+
fail_under = 97

inheritance_explorer/_testing.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class ClassForTesting:
2+
misc_attr: int = 1
3+
24
def use_this_func(self, a: int) -> int:
35
return a
46

inheritance_explorer/tests/test_inheritance_explorer.py

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
import pytest
55

66
from inheritance_explorer._testing import ClassForTesting
7-
from inheritance_explorer.inheritance_explorer import ClassGraphTree, _ChildNode
7+
from inheritance_explorer.inheritance_explorer import (
8+
ClassGraphTree,
9+
_ChildNode,
10+
_validate_color,
11+
)
812

913

1014
@pytest.fixture()
@@ -143,3 +147,67 @@ def test_class_exclusion():
143147
)
144148
for node in cgt._node_list:
145149
assert node.child_name != "ClassForTesting2"
150+
151+
152+
def test_errors_no_source_override():
153+
# _get_source_info
154+
cgt = ClassGraphTree(ClassForTesting)
155+
with pytest.raises(
156+
RuntimeError, match="this functionality requires function tracking."
157+
):
158+
_ = cgt._get_source_info(ClassForTesting)
159+
160+
with pytest.raises(
161+
RuntimeError, match="this functionality requires function tracking."
162+
):
163+
cgt._store_node_func_source(ClassForTesting, 0)
164+
165+
cgt = ClassGraphTree(ClassForTesting, funcname="use_this_func")
166+
# monkey patch the funcname to make the callable fail
167+
cgt.funcname = "misc_attr"
168+
result = cgt._get_source_info(ClassForTesting)
169+
assert result is None
170+
171+
172+
def test_missing_source_node(cgt):
173+
with pytest.raises(KeyError, match="Could not find node for not_a_node"):
174+
cgt.get_source_code_by_node_name("not_a_node")
175+
176+
177+
def test_class_graph_special_cases(cgt):
178+
179+
# map out the structure of SimilarityContainer, track the run method
180+
assert cgt._node_list[1].parent_id == "1"
181+
with pytest.raises(
182+
ValueError, match="unexpected value, similarity_container_class"
183+
):
184+
_ = cgt.check_source_similarity(similarity_container_class="not_a_thing")
185+
186+
cgt.check_source_similarity(reference=1)
187+
188+
189+
def test_set_graphviz_args(cgt):
190+
191+
cgt.set_graphviz_args_kwargs(1, 2, 3, other_arg="hello", another=True)
192+
193+
d = cgt._graphviz_args_kwargs
194+
assert all([val in d["args"] for val in (1, 2, 3)])
195+
assert "other_arg" in d["kwargs"]
196+
assert d["kwargs"]["other_arg"] == "hello"
197+
assert "another" in d["kwargs"]
198+
assert d["kwargs"]["another"] is True
199+
200+
201+
@pytest.mark.parametrize(
202+
"input_clr,expected",
203+
[((0.0, 0.0, 0.0), "#000000"), (None, "#ffffff"), ("#000000", "#000000")],
204+
)
205+
def test_validate_color(input_clr, expected):
206+
default_clr = (1.0, 1.0, 1.0)
207+
208+
assert _validate_color(input_clr, default_rgb_tuple=default_clr) == expected
209+
210+
211+
def test_validate_color_invalid():
212+
with pytest.raises(TypeError, match="clr has unexpected type"):
213+
_validate_color(100, (1.0, 1.0, 1.0))

0 commit comments

Comments
 (0)