From f4b5b9e04b4b3371b3b75598c01d10904e9366da Mon Sep 17 00:00:00 2001 From: Ned Letcher Date: Fri, 27 Jan 2023 23:17:14 +1100 Subject: [PATCH] add tests for _infer_module_name helper functions --- dash/_pages.py | 8 ++++---- tests/unit/pages/test_pages.py | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/dash/_pages.py b/dash/_pages.py index badce49659..a4933dc3ec 100644 --- a/dash/_pages.py +++ b/dash/_pages.py @@ -90,23 +90,23 @@ def _module_name_is_package(module_name): ) -def _path_to_module(path): +def _path_to_module_name(path): return str(path).replace(".py", "").strip(os.sep).replace(os.sep, ".") def _infer_module_name(page_path): relative_path = page_path.split(CONFIG.pages_folder)[-1] - module = _path_to_module(relative_path) + module = _path_to_module_name(relative_path) proj_root = flask.helpers.get_root_path(CONFIG.name) if CONFIG.pages_folder.startswith(proj_root): parent_path = CONFIG.pages_folder[len(proj_root) :] else: parent_path = CONFIG.pages_folder - parent_module = _path_to_module(parent_path) + parent_module = _path_to_module_name(parent_path) module_name = f"{parent_module}.{module}" if _module_name_is_package(CONFIG.name): - # Only prefix with CONFIG.name when its an imported package name + # Only prefix with CONFIG.name when it's an imported package name module_name = f"{CONFIG.name}.{module_name}" return module_name diff --git a/tests/unit/pages/test_pages.py b/tests/unit/pages/test_pages.py index 1fa490a2d5..df943c8f9b 100644 --- a/tests/unit/pages/test_pages.py +++ b/tests/unit/pages/test_pages.py @@ -35,6 +35,30 @@ def test_infer_path(clear_pages_state, module_name, template, pages_folder, expe assert result == expected +@pytest.mark.parametrize( + "module_name, expected", + [ + (__name__, False), + (__package__, True), + ], +) +def test_module_name_is_package(module_name, expected): + assert _pages._module_name_is_package(module_name) == expected + + +@pytest.mark.parametrize( + "path, expected", + [ + ("/page.py", "page"), + ("/pages/page.py", "pages.page"), + ("/pages", "pages"), + ("/sub_dir/pages", "sub_dir.pages"), + ], +) +def test_path_to_module_name(path, expected): + assert _pages._path_to_module_name(path) == expected + + @pytest.mark.parametrize( "name, pages_folder, expected_module_name", [