Skip to content

Commit adcddbf

Browse files
fix: fix issue where formattedtime type not triggering validate correctly
1 parent 2d10832 commit adcddbf

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/dve/metadata_parser/domain_types.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,11 @@ def validate(cls, value: Union[dt.time, dt.datetime, str]) -> dt.time | None:
482482

483483
return new_time
484484

485+
@classmethod
486+
def __get_validators__(cls) -> Iterator[classmethod]:
487+
"""Gets all validators"""
488+
yield cls.validate # type: ignore
489+
485490

486491
@lru_cache()
487492
@validate_arguments

tests/test_core_engine/test_backends/test_implementations/test_duckdb/test_data_contract.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_duckdb_data_contract_csv(temp_csv_file):
5050
"description": "test",
5151
"callable": "formattedtime",
5252
"constraints": {
53-
"time_format": "%Y-%m-%d",
53+
"time_format": "%H:%M:%S",
5454
"timezone_treatment": "forbid"
5555
}
5656
}

tests/test_model_generation/test_domain_types.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,8 @@ def test_formattedtime(
347347
["23:00:00", "%H:%M:%S", "require",],
348348
["23:00:00Z", "%I:%M:%S", "forbid",],
349349
[dt.datetime(2025, 12, 1, 13, 0, 5, tzinfo=UTC), "%H:%M:%S", "forbid",],
350-
[dt.time(13, 0, 5, tzinfo=UTC), "%H:%M:%S", "forbid",]
350+
[dt.time(13, 0, 5, tzinfo=UTC), "%H:%M:%S", "forbid",],
351+
["12:00", "%H:%M:%S", "forbid",],
351352
]
352353
)
353354
def test_formattedtime_raises(
@@ -360,3 +361,24 @@ def test_formattedtime_raises(
360361
time_type = hct.formattedtime(time_format, timezone_treatment)
361362
with pytest.raises(ValueError):
362363
time_type.validate(time_to_validate) # pylint: disable=W0106
364+
365+
366+
class StrictTimeModel(BaseModel):
367+
time_val: hct.formattedtime(time_format="%H:%M:%S", timezone_treatment="forbid")
368+
369+
370+
@pytest.mark.parametrize(
371+
["time_to_validate", "expected_to_error"],
372+
[
373+
("12:00:00", False),
374+
("120000", True),
375+
("12:00", True),
376+
("12", True),
377+
]
378+
)
379+
def test_formattedtime_against_model(time_to_validate: str, expected_to_error: bool):
380+
if expected_to_error:
381+
with pytest.raises(ValueError):
382+
StrictTimeModel(time_val=time_to_validate)
383+
else:
384+
StrictTimeModel(time_val=time_to_validate)

0 commit comments

Comments
 (0)