Skip to content

Commit f4b5b9e

Browse files
committed
add tests for _infer_module_name helper functions
1 parent 127eaad commit f4b5b9e

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

Diff for: dash/_pages.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,23 @@ def _module_name_is_package(module_name):
9090
)
9191

9292

93-
def _path_to_module(path):
93+
def _path_to_module_name(path):
9494
return str(path).replace(".py", "").strip(os.sep).replace(os.sep, ".")
9595

9696

9797
def _infer_module_name(page_path):
9898
relative_path = page_path.split(CONFIG.pages_folder)[-1]
99-
module = _path_to_module(relative_path)
99+
module = _path_to_module_name(relative_path)
100100
proj_root = flask.helpers.get_root_path(CONFIG.name)
101101
if CONFIG.pages_folder.startswith(proj_root):
102102
parent_path = CONFIG.pages_folder[len(proj_root) :]
103103
else:
104104
parent_path = CONFIG.pages_folder
105-
parent_module = _path_to_module(parent_path)
105+
parent_module = _path_to_module_name(parent_path)
106106

107107
module_name = f"{parent_module}.{module}"
108108
if _module_name_is_package(CONFIG.name):
109-
# Only prefix with CONFIG.name when its an imported package name
109+
# Only prefix with CONFIG.name when it's an imported package name
110110
module_name = f"{CONFIG.name}.{module_name}"
111111
return module_name
112112

Diff for: tests/unit/pages/test_pages.py

+24
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,30 @@ def test_infer_path(clear_pages_state, module_name, template, pages_folder, expe
3535
assert result == expected
3636

3737

38+
@pytest.mark.parametrize(
39+
"module_name, expected",
40+
[
41+
(__name__, False),
42+
(__package__, True),
43+
],
44+
)
45+
def test_module_name_is_package(module_name, expected):
46+
assert _pages._module_name_is_package(module_name) == expected
47+
48+
49+
@pytest.mark.parametrize(
50+
"path, expected",
51+
[
52+
("/page.py", "page"),
53+
("/pages/page.py", "pages.page"),
54+
("/pages", "pages"),
55+
("/sub_dir/pages", "sub_dir.pages"),
56+
],
57+
)
58+
def test_path_to_module_name(path, expected):
59+
assert _pages._path_to_module_name(path) == expected
60+
61+
3862
@pytest.mark.parametrize(
3963
"name, pages_folder, expected_module_name",
4064
[

0 commit comments

Comments
 (0)