From 0d21270d66454130f2d42217f077d76878b44dbd Mon Sep 17 00:00:00 2001 From: salihuDickson Date: Mon, 21 Oct 2024 22:33:32 +0100 Subject: [PATCH] add tests for path_prefix --- crategen/models/tes_models.py | 5 ++++- tests/unit/test_tes_models.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/crategen/models/tes_models.py b/crategen/models/tes_models.py index efe9e7a..4409b82 100644 --- a/crategen/models/tes_models.py +++ b/crategen/models/tes_models.py @@ -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): @@ -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.""" diff --git a/tests/unit/test_tes_models.py b/tests/unit/test_tes_models.py index 1bc0b03..903c75c 100644 --- a/tests/unit/test_tes_models.py +++ b/tests/unit/test_tes_models.py @@ -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."""