Skip to content

Commit ba1ef06

Browse files
committed
test: add unit tests for relative_import
1 parent 5cdb606 commit ba1ef06

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import collections.abc
2+
3+
import pytest
4+
5+
from _plotly_utils.importers import relative_import
6+
7+
8+
def _make():
9+
# Use the stdlib ``collections`` package as a stable lazy-import target.
10+
return relative_import(
11+
"collections",
12+
rel_modules=[".abc"],
13+
rel_classes=[".abc.Mapping"],
14+
)
15+
16+
17+
def test_all_lists_module_and_class_leaf_names():
18+
all_, _getattr, _dir = _make()
19+
assert sorted(all_) == ["Mapping", "abc"]
20+
21+
22+
def test_getattr_lazily_imports_submodule():
23+
_all, getattr_, _dir = _make()
24+
assert getattr_("abc") is collections.abc
25+
26+
27+
def test_getattr_lazily_imports_class():
28+
_all, getattr_, _dir = _make()
29+
assert getattr_("Mapping") is collections.abc.Mapping
30+
31+
32+
def test_getattr_unknown_name_raises_attribute_error():
33+
_all, getattr_, _dir = _make()
34+
with pytest.raises(AttributeError):
35+
getattr_("does_not_exist")
36+
37+
38+
def test_dir_returns_all():
39+
all_, _getattr, dir_ = _make()
40+
assert sorted(dir_()) == sorted(all_)

0 commit comments

Comments
 (0)