From ba1ef06bf31a65f379fe6612d6eb154f8a61ec90 Mon Sep 17 00:00:00 2001 From: eeshsaxena Date: Mon, 6 Jul 2026 09:49:53 +0530 Subject: [PATCH] test: add unit tests for relative_import --- tests/test_core/test_utils/test_importers.py | 40 ++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/test_core/test_utils/test_importers.py diff --git a/tests/test_core/test_utils/test_importers.py b/tests/test_core/test_utils/test_importers.py new file mode 100644 index 00000000000..7c46a7c972b --- /dev/null +++ b/tests/test_core/test_utils/test_importers.py @@ -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_)