Skip to content

Commit

Permalink
add tests for _infer_module_name helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ned2 committed Jan 27, 2023
1 parent 2ab35ea commit 6c5072c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
8 changes: 4 additions & 4 deletions dash/_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 24 additions & 0 deletions tests/unit/pages/test_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
[
Expand Down

0 comments on commit 6c5072c

Please # to comment.