22import pytest
33from pydantic import BaseModel , ValidationError
44
5- from pydantic_extra_types .pendulum_dt import DateTime
5+ from pydantic_extra_types .pendulum_dt import Date , DateTime
66
77
8- class Model (BaseModel ):
8+ class DtModel (BaseModel ):
99 dt : DateTime
1010
1111
12+ class DateModel (BaseModel ):
13+ d : Date
14+
15+
1216def test_pendulum_dt_existing_instance ():
1317 """
1418 Verifies that constructing a model with an existing pendulum dt doesn't throw.
1519 """
1620 now = pendulum .now ()
17- model = Model (dt = now )
21+ model = DtModel (dt = now )
1822 assert model .dt == now
1923
2024
25+ def test_pendulum_date_existing_instance ():
26+ """
27+ Verifies that constructing a model with an existing pendulum date doesn't throw.
28+ """
29+ today = pendulum .today ().date ()
30+ model = DateModel (d = today )
31+ assert model .d == today
32+
33+
2134@pytest .mark .parametrize (
2235 'dt' , [pendulum .now ().to_iso8601_string (), pendulum .now ().to_w3c_string (), pendulum .now ().to_iso8601_string ()]
2336)
@@ -26,14 +39,32 @@ def test_pendulum_dt_from_serialized(dt):
2639 Verifies that building an instance from serialized, well-formed strings decode properly.
2740 """
2841 dt_actual = pendulum .parse (dt )
29- model = Model (dt = dt )
42+ model = DtModel (dt = dt )
3043 assert model .dt == dt_actual
3144
3245
46+ def test_pendulum_date_from_serialized ():
47+ """
48+ Verifies that building an instance from serialized, well-formed strings decode properly.
49+ """
50+ date_actual = pendulum .parse ('2024-03-18' ).date ()
51+ model = DateModel (d = '2024-03-18' )
52+ assert model .d == date_actual
53+
54+
3355@pytest .mark .parametrize ('dt' , [None , 'malformed' , pendulum .now ().to_iso8601_string ()[:5 ], 42 ])
3456def test_pendulum_dt_malformed (dt ):
3557 """
3658 Verifies that the instance fails to validate if malformed dt are passed.
3759 """
3860 with pytest .raises (ValidationError ):
39- Model (dt = dt )
61+ DtModel (dt = dt )
62+
63+
64+ @pytest .mark .parametrize ('date' , [None , 'malformed' , pendulum .today ().to_iso8601_string ()[:5 ], 42 ])
65+ def test_pendulum_date_malformed (date ):
66+ """
67+ Verifies that the instance fails to validate if malformed date are passed.
68+ """
69+ with pytest .raises (ValidationError ):
70+ DateModel (d = date )
0 commit comments