Skip to content

Commit

Permalink
Update cleanup_path() and add some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kgodlewski committed Dec 18, 2024
1 parent 3406313 commit f933b08
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/neptune_scale/api/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,29 +192,34 @@ def cleanup_path(path: str) -> str:
>>> cleanup_path('a/ /b/c')
Traceback (most recent call last):
...
ValueError: Invalid path: `a/ /b/c`. Path components must not be empty.
ValueError: Invalid path: `a/ /b/c`. Path components cannot contain leading or trailing whitespace.
>>> cleanup_path('a/b/c ')
Traceback (most recent call last):
...
ValueError: Invalid path: `a/b/c `. Path cannot contain leading or trailing whitespace.
ValueError: Invalid path: `a/b/c `. Path components cannot contain leading or trailing whitespace.
"""

if path.strip() in ("", "/"):
raise ValueError(f"Invalid path: `{path}`.")

if path.startswith("/"):
path = path[1:]
orig_parts = path.split("/")
parts = [x.strip() for x in orig_parts]

if path.endswith("/"):
for i, part in enumerate(parts):
if part != orig_parts[i]:
raise ValueError(f"Invalid path: `{path}`. Path components cannot contain leading or trailing whitespace.")

# Skip the first slash, if present
if parts[0] == "":
parts = parts[1:]

if parts[-1] == "":
raise ValueError(f"Invalid path: `{path}`. Path must not end with a slash.")

if not all(x.strip() for x in path.split("/")):
if not all(parts):
raise ValueError(f"Invalid path: `{path}`. Path components must not be empty.")

if path[0].lstrip() != path[0] or path[-1].rstrip() != path[-1]:
raise ValueError(f"Invalid path: `{path}`. Path cannot contain leading or trailing whitespace.")

return path
return "/".join(parts)


def accumulate_dict_values(value: Union[ValueType, dict[str, ValueType]], path_or_base: str) -> dict[str, Any]:
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/test_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
)

from neptune_scale import Run
from neptune_scale.api.attribute import cleanup_path


@fixture
Expand Down Expand Up @@ -71,3 +72,25 @@ def test_series(run, store):

run["sys/series"].append({"foo": 1, "bar": 2}, step=2)
store.log.assert_called_with(metrics={"sys/series/foo": 1, "sys/series/bar": 2}, step=2, timestamp=None)


@pytest.mark.parametrize(
"path", ["", " ", "/", " /", "/ ", "///", "/a ", "/a/b /", "a/b /c", "a /b/c", "a/b/", "a/b ", " /a/b"]
)
def test_cleanup_path_invalid_path(path):
with pytest.raises(ValueError) as exc:
cleanup_path(path)

exc.match("Invalid path:")


@pytest.mark.parametrize(
"path, expected",
(
("/a/b/c", "a/b/c"),
("a a/b/c", "a a/b/c"),
("/a a/b/c", "a a/b/c"),
),
)
def test_cleanup_path_valid_path(path, expected):
assert cleanup_path(path) == expected

0 comments on commit f933b08

Please sign in to comment.