Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions tests/test_core/test_utils/test_importers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import collections.abc

import pytest

from _plotly_utils.importers import relative_import


def _make():
# Use the stdlib ``collections`` package as a stable lazy-import target.
return relative_import(
"collections",
rel_modules=[".abc"],
rel_classes=[".abc.Mapping"],
)


def test_all_lists_module_and_class_leaf_names():
all_, _getattr, _dir = _make()
assert sorted(all_) == ["Mapping", "abc"]


def test_getattr_lazily_imports_submodule():
_all, getattr_, _dir = _make()
assert getattr_("abc") is collections.abc


def test_getattr_lazily_imports_class():
_all, getattr_, _dir = _make()
assert getattr_("Mapping") is collections.abc.Mapping


def test_getattr_unknown_name_raises_attribute_error():
_all, getattr_, _dir = _make()
with pytest.raises(AttributeError):
getattr_("does_not_exist")


def test_dir_returns_all():
all_, _getattr, dir_ = _make()
assert sorted(dir_()) == sorted(all_)