33
44import pendulum
55import pytest
6- from pydantic import BaseModel , ValidationError
6+ from pydantic import BaseModel , TypeAdapter , ValidationError
77
88from pydantic_extra_types .pendulum_dt import Date , DateTime , Duration
99
@@ -51,6 +51,8 @@ def test_existing_instance(instance):
5151 assert dt .minute == instance .minute
5252 assert dt .second == instance .second
5353 assert dt .microsecond == instance .microsecond
54+ assert isinstance (dt , pendulum .DateTime )
55+ assert type (dt ) is DateTime
5456 if dt .tzinfo != instance .tzinfo :
5557 assert dt .tzinfo .utcoffset (dt ) == instance .tzinfo .utcoffset (instance )
5658
@@ -75,6 +77,8 @@ def test_pendulum_date_existing_instance(instance):
7577 assert d .day == instance .day
7678 assert d .month == instance .month
7779 assert d .year == instance .year
80+ assert isinstance (d , pendulum .Date )
81+ assert type (d ) is Date
7882
7983
8084@pytest .mark .parametrize (
@@ -93,14 +97,15 @@ def test_duration_timedelta__existing_instance(instance):
9397 model = DurationModel (delta_t = instance )
9498
9599 assert model .delta_t .total_seconds () == instance .total_seconds ()
100+ assert isinstance (model .delta_t , pendulum .Duration )
101+ assert model .delta_t
96102
97103
98104@pytest .mark .parametrize (
99105 'dt' ,
100106 [
101107 pendulum .now ().to_iso8601_string (),
102108 pendulum .now ().to_w3c_string (),
103- pendulum .now ().to_iso8601_string (),
104109 ],
105110)
106111def test_pendulum_dt_from_serialized (dt ):
@@ -110,15 +115,23 @@ def test_pendulum_dt_from_serialized(dt):
110115 dt_actual = pendulum .parse (dt )
111116 model = DtModel (dt = dt )
112117 assert model .dt == dt_actual
118+ assert type (model .dt ) is DateTime
119+ assert isinstance (model .dt , pendulum .DateTime )
113120
114121
115- def test_pendulum_date_from_serialized ():
122+ @pytest .mark .parametrize (
123+ 'd' ,
124+ [pendulum .now ().date ().isoformat (), pendulum .now ().to_w3c_string (), pendulum .now ().to_iso8601_string ()],
125+ )
126+ def test_pendulum_date_from_serialized (d ):
116127 """
117128 Verifies that building an instance from serialized, well-formed strings decode properly.
118129 """
119- date_actual = pendulum .parse ('2024-03-18' ).date ()
120- model = DateModel (d = '2024-03-18' )
130+ date_actual = pendulum .parse (d ).date ()
131+ model = DateModel (d = d )
121132 assert model .d == date_actual
133+ assert type (model .d ) is Date
134+ assert isinstance (model .d , pendulum .Date )
122135
123136
124137@pytest .mark .parametrize (
@@ -138,9 +151,11 @@ def test_pendulum_duration_from_serialized(delta_t_str):
138151 true_delta_t = pendulum .parse (delta_t_str )
139152 model = DurationModel (delta_t = delta_t_str )
140153 assert model .delta_t == true_delta_t
154+ assert type (model .delta_t ) is Duration
155+ assert isinstance (model .delta_t , pendulum .Duration )
141156
142157
143- @pytest .mark .parametrize ('dt' , [None , 'malformed' , pendulum .now ().to_iso8601_string ()[:5 ], 42 ])
158+ @pytest .mark .parametrize ('dt' , [None , 'malformed' , pendulum .now ().to_iso8601_string ()[:5 ], 42 , 'P10Y10M10D' ])
144159def test_pendulum_dt_malformed (dt ):
145160 """
146161 Verifies that the instance fails to validate if malformed dt are passed.
@@ -149,7 +164,7 @@ def test_pendulum_dt_malformed(dt):
149164 DtModel (dt = dt )
150165
151166
152- @pytest .mark .parametrize ('date' , [None , 'malformed' , pendulum .today ().to_iso8601_string ()[:5 ], 42 ])
167+ @pytest .mark .parametrize ('date' , [None , 'malformed' , pendulum .today ().to_iso8601_string ()[:5 ], 42 , 'P10Y10M10D' ])
153168def test_pendulum_date_malformed (date ):
154169 """
155170 Verifies that the instance fails to validate if malformed date are passed.
@@ -160,11 +175,32 @@ def test_pendulum_date_malformed(date):
160175
161176@pytest .mark .parametrize (
162177 'delta_t' ,
163- [None , 'malformed' , pendulum .today ().to_iso8601_string ()[:5 ], 42 , '12m' ],
178+ [None , 'malformed' , pendulum .today ().to_iso8601_string ()[:5 ], 42 , '12m' , '2021-01-01T12:00:00' ],
164179)
165180def test_pendulum_duration_malformed (delta_t ):
166181 """
167182 Verifies that the instance fails to validate if malformed durations are passed.
168183 """
169184 with pytest .raises (ValidationError ):
170185 DurationModel (delta_t = delta_t )
186+
187+
188+ @pytest .mark .parametrize (
189+ 'input_type, value, is_instance' ,
190+ [
191+ (Date , '2021-01-01' , pendulum .Date ),
192+ (Date , date (2021 , 1 , 1 ), pendulum .Date ),
193+ (Date , pendulum .date (2021 , 1 , 1 ), pendulum .Date ),
194+ (DateTime , '2021-01-01T12:00:00' , pendulum .DateTime ),
195+ (DateTime , datetime (2021 , 1 , 1 , 12 , 0 , 0 ), pendulum .DateTime ),
196+ (DateTime , pendulum .datetime (2021 , 1 , 1 , 12 , 0 , 0 ), pendulum .DateTime ),
197+ (Duration , 'P1DT25H' , pendulum .Duration ),
198+ (Duration , timedelta (days = 1 , hours = 25 ), pendulum .Duration ),
199+ (Duration , pendulum .duration (days = 1 , hours = 25 ), pendulum .Duration ),
200+ ],
201+ )
202+ def test_date_type_adapter (input_type : type , value , is_instance : type ):
203+ validated = TypeAdapter (input_type ).validate_python (value )
204+ assert type (validated ) is input_type
205+ assert isinstance (validated , input_type )
206+ assert isinstance (validated , is_instance )
0 commit comments