Skip to content

Commit

Permalink
add tests for path_prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
salihuDickson committed Oct 21, 2024
1 parent 335b275 commit 0d21270
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
5 changes: 4 additions & 1 deletion crategen/models/tes_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ class TESOutput(BaseModel):
url: AnyUrl
path: str
type: Optional[TESFileType] = TESFileType.FILE
path_prefix: Optional[str] = None

@root_validator()
def validate_is_path_prefix_required(cls, values):
Expand All @@ -224,9 +225,11 @@ def validate_is_path_prefix_required(cls, values):

if bool(re.search(pattern, path)) and not bool(path_prefix):
raise ValueError(
"The 'path_prefix' property is required when the 'path' property contains wildcards"
"The 'path_prefix' property is required when the 'path' property contains a wildcard"
)

return values

@validator("path")
def validate_path(cls, value):
"""Ensure that 'path' is an absolute path and handle wildcards."""
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/test_tes_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,39 @@ def test_path_invalid(self):

assert "The 'path' property must be an absolute path" in str(exc_info.value)

@pytest.mark.parametrize(
"path,path_prefix",
[
("/path/to/file.txt", None),
("/path/to/directory", None),
("/path/to/files*.txt", "/path/to"),
("/path/to/data???.csv", "/path/to"),
],
)
def test_validate_is_path_prefix_necessary_valid(self, path, path_prefix):
"""Test that validator accepts valid paths and path prefixes."""
tes_output = TESOutput(
url="https://example.com", path=path, path_prefix=path_prefix
)
assert bool(tes_output)

@pytest.mark.parametrize(
"path,path_prefix",
[
("/path/to/files*.txt", None),
("/path/to/data???.csv", ""),
],
)
def test_validate_is_path_prefix_necessary_invalid(self, path, path_prefix):
"""Test that validator rejects invalid paths and path prefixes."""
with pytest.raises(ValueError) as exc_info:
TESOutput(url="https://example.com", path=path, path_prefix=path_prefix)

assert (
"The 'path_prefix' property is required when the 'path' property contains a wildcard"
in str(exc_info.value)
)


class TestTESTaskLog:
"""Test suite for TESTaskLog model validators."""
Expand Down

0 comments on commit 0d21270

Please sign in to comment.