2
2
import pytest
3
3
from pydantic import BaseModel , ValidationError
4
4
5
- from pydantic_extra_types .pendulum_dt import Date , DateTime
5
+ from pydantic_extra_types .pendulum_dt import Date , DateTime , Duration
6
6
7
7
8
8
class DtModel (BaseModel ):
@@ -13,6 +13,10 @@ class DateModel(BaseModel):
13
13
d : Date
14
14
15
15
16
+ class DurationModel (BaseModel ):
17
+ delta_t : Duration
18
+
19
+
16
20
def test_pendulum_dt_existing_instance ():
17
21
"""
18
22
Verifies that constructing a model with an existing pendulum dt doesn't throw.
@@ -31,8 +35,23 @@ def test_pendulum_date_existing_instance():
31
35
assert model .d == today
32
36
33
37
38
+ def test_pendulum_duration_existing_instance ():
39
+ """
40
+ Verifies that constructing a model with an existing pendulum duration doesn't throw.
41
+ """
42
+ delta_t = pendulum .duration (days = 42 , hours = 13 , minutes = 37 )
43
+ model = DurationModel (delta_t = delta_t )
44
+
45
+ assert model .delta_t .total_seconds () == delta_t .total_seconds ()
46
+
47
+
34
48
@pytest .mark .parametrize (
35
- 'dt' , [pendulum .now ().to_iso8601_string (), pendulum .now ().to_w3c_string (), pendulum .now ().to_iso8601_string ()]
49
+ 'dt' ,
50
+ [
51
+ pendulum .now ().to_iso8601_string (),
52
+ pendulum .now ().to_w3c_string (),
53
+ pendulum .now ().to_iso8601_string (),
54
+ ],
36
55
)
37
56
def test_pendulum_dt_from_serialized (dt ):
38
57
"""
@@ -52,6 +71,25 @@ def test_pendulum_date_from_serialized():
52
71
assert model .d == date_actual
53
72
54
73
74
+ @pytest .mark .parametrize (
75
+ 'delta_t_str' ,
76
+ [
77
+ 'P3.14D' ,
78
+ 'PT404H' ,
79
+ 'P1DT25H' ,
80
+ 'P2W' ,
81
+ 'P10Y10M10D' ,
82
+ ],
83
+ )
84
+ def test_pendulum_duration_from_serialized (delta_t_str ):
85
+ """
86
+ Verifies that building an instance from serialized, well-formed strings decode properly.
87
+ """
88
+ true_delta_t = pendulum .parse (delta_t_str )
89
+ model = DurationModel (delta_t = delta_t_str )
90
+ assert model .delta_t == true_delta_t
91
+
92
+
55
93
@pytest .mark .parametrize ('dt' , [None , 'malformed' , pendulum .now ().to_iso8601_string ()[:5 ], 42 ])
56
94
def test_pendulum_dt_malformed (dt ):
57
95
"""
@@ -68,3 +106,15 @@ def test_pendulum_date_malformed(date):
68
106
"""
69
107
with pytest .raises (ValidationError ):
70
108
DateModel (d = date )
109
+
110
+
111
+ @pytest .mark .parametrize (
112
+ 'delta_t' ,
113
+ [None , 'malformed' , pendulum .today ().to_iso8601_string ()[:5 ], 42 , '12m' ],
114
+ )
115
+ def test_pendulum_duration_malformed (delta_t ):
116
+ """
117
+ Verifies that the instance fails to validate if malformed durations are passed.
118
+ """
119
+ with pytest .raises (ValidationError ):
120
+ DurationModel (delta_t = delta_t )
0 commit comments