Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/dve/metadata_parser/domain_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,11 @@ def validate(cls, value: Union[dt.time, dt.datetime, str]) -> dt.time | None:

return new_time

@classmethod
def __get_validators__(cls) -> Iterator[classmethod]:
"""Gets all validators"""
yield cls.validate # type: ignore


@lru_cache()
@validate_arguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_duckdb_data_contract_csv(temp_csv_file):
"description": "test",
"callable": "formattedtime",
"constraints": {
"time_format": "%Y-%m-%d",
"time_format": "%H:%M:%S",
"timezone_treatment": "forbid"
}
}
Expand Down
24 changes: 23 additions & 1 deletion tests/test_model_generation/test_domain_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ def test_formattedtime(
["23:00:00", "%H:%M:%S", "require",],
["23:00:00Z", "%I:%M:%S", "forbid",],
[dt.datetime(2025, 12, 1, 13, 0, 5, tzinfo=UTC), "%H:%M:%S", "forbid",],
[dt.time(13, 0, 5, tzinfo=UTC), "%H:%M:%S", "forbid",]
[dt.time(13, 0, 5, tzinfo=UTC), "%H:%M:%S", "forbid",],
["12:00", "%H:%M:%S", "forbid",],
]
)
def test_formattedtime_raises(
Expand All @@ -360,3 +361,24 @@ def test_formattedtime_raises(
time_type = hct.formattedtime(time_format, timezone_treatment)
with pytest.raises(ValueError):
time_type.validate(time_to_validate) # pylint: disable=W0106


class StrictTimeModel(BaseModel):
time_val: hct.formattedtime(time_format="%H:%M:%S", timezone_treatment="forbid")


@pytest.mark.parametrize(
["time_to_validate", "expected_to_error"],
[
("12:00:00", False),
("120000", True),
("12:00", True),
("12", True),
]
)
def test_formattedtime_against_model(time_to_validate: str, expected_to_error: bool):
if expected_to_error:
with pytest.raises(ValueError):
StrictTimeModel(time_val=time_to_validate)
else:
StrictTimeModel(time_val=time_to_validate)