Skip to content

Commit aa3a266

Browse files
fix: fix issue where formattedtime type not triggering validate correctly (#54)
1 parent 7f0bf5f commit aa3a266

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
@@ -516,6 +516,11 @@ def validate(cls, value: Union[dt.time, dt.datetime, str]) -> dt.time | None:
516516

517517
return new_time
518518

519+
@classmethod
520+
def __get_validators__(cls) -> Iterator[classmethod]:
521+
"""Gets all validators"""
522+
yield cls.validate # type: ignore
523+
519524

520525
@lru_cache()
521526
@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
@@ -365,7 +365,8 @@ def test_formattedtime(
365365
["23:00:00", "%H:%M:%S", "require",],
366366
["23:00:00Z", "%I:%M:%S", "forbid",],
367367
[dt.datetime(2025, 12, 1, 13, 0, 5, tzinfo=UTC), "%H:%M:%S", "forbid",],
368-
[dt.time(13, 0, 5, tzinfo=UTC), "%H:%M:%S", "forbid",]
368+
[dt.time(13, 0, 5, tzinfo=UTC), "%H:%M:%S", "forbid",],
369+
["12:00", "%H:%M:%S", "forbid",],
369370
]
370371
)
371372
def test_formattedtime_raises(
@@ -378,3 +379,24 @@ def test_formattedtime_raises(
378379
time_type = hct.formattedtime(time_format, timezone_treatment)
379380
with pytest.raises(ValueError):
380381
time_type.validate(time_to_validate) # pylint: disable=W0106
382+
383+
384+
class StrictTimeModel(BaseModel):
385+
time_val: hct.formattedtime(time_format="%H:%M:%S", timezone_treatment="forbid")
386+
387+
388+
@pytest.mark.parametrize(
389+
["time_to_validate", "expected_to_error"],
390+
[
391+
("12:00:00", False),
392+
("120000", True),
393+
("12:00", True),
394+
("12", True),
395+
]
396+
)
397+
def test_formattedtime_against_model(time_to_validate: str, expected_to_error: bool):
398+
if expected_to_error:
399+
with pytest.raises(ValueError):
400+
StrictTimeModel(time_val=time_to_validate)
401+
else:
402+
StrictTimeModel(time_val=time_to_validate)

0 commit comments

Comments
 (0)