1
1
import pendulum
2
2
import pytest
3
3
from pydantic import BaseModel , ValidationError
4
+ from datetime import datetime , date , timedelta , UTC
4
5
5
6
from pydantic_extra_types .pendulum_dt import Date , DateTime , Duration
6
7
@@ -17,33 +18,76 @@ class DurationModel(BaseModel):
17
18
delta_t : Duration
18
19
19
20
20
- def test_pendulum_dt_existing_instance ():
21
+ @pytest .mark .parametrize (
22
+ 'instance' ,
23
+ [
24
+ pendulum .now (),
25
+ datetime .now (),
26
+ datetime .now (UTC ),
27
+ ],
28
+ )
29
+ def test_existing_instance (instance ):
21
30
"""
22
31
Verifies that constructing a model with an existing pendulum dt doesn't throw.
23
32
"""
24
- now = pendulum .now ()
25
- model = DtModel (dt = now )
26
- assert model .dt == now
33
+ model = DtModel (dt = instance )
34
+ if isinstance (instance , datetime ):
35
+ assert model .dt == pendulum .instance (instance )
36
+ if instance .tzinfo is None and isinstance (instance , datetime ):
37
+ instance = model .dt .replace (tzinfo = UTC ) # pendulum defaults to UTC
38
+ dt = model .dt
39
+ else :
40
+ assert model .dt == instance
41
+ dt = model .dt
42
+
43
+ assert dt .day == instance .day
44
+ assert dt .month == instance .month
45
+ assert dt .year == instance .year
46
+ assert dt .hour == instance .hour
47
+ assert dt .minute == instance .minute
48
+ assert dt .second == instance .second
49
+ assert dt .microsecond == instance .microsecond
50
+ if dt .tzinfo != instance .tzinfo :
51
+ assert dt .tzinfo .utcoffset (dt ) == instance .tzinfo .utcoffset (instance )
27
52
28
53
29
- def test_pendulum_date_existing_instance ():
54
+ @pytest .mark .parametrize (
55
+ 'instance' ,
56
+ [
57
+ pendulum .today (),
58
+ date .today (),
59
+ ],
60
+ )
61
+ def test_pendulum_date_existing_instance (instance ):
30
62
"""
31
63
Verifies that constructing a model with an existing pendulum date doesn't throw.
32
64
"""
33
- today = pendulum .today ().date ()
34
- model = DateModel (d = today )
35
- assert model .d == today
36
-
65
+ model = DateModel (d = instance )
66
+ if isinstance (instance , datetime ):
67
+ assert model .d == pendulum .instance (instance ).date ()
68
+ else :
69
+ assert model .d == instance
70
+ d = model .d
71
+ assert d .day == instance .day
72
+ assert d .month == instance .month
73
+ assert d .year == instance .year
37
74
38
- def test_pendulum_duration_existing_instance ():
75
+ @pytest .mark .parametrize (
76
+ 'instance' ,
77
+ [
78
+ pendulum .duration (days = 42 , hours = 13 , minutes = 37 ),
79
+ pendulum .duration (days = - 42 , hours = 13 , minutes = 37 ),
80
+ timedelta (days = 42 , hours = 13 , minutes = 37 ),
81
+ timedelta (days = - 42 , hours = 13 , minutes = 37 ),
82
+ ],
83
+ )
84
+ def test_duration_timedelta__existing_instance (instance ):
39
85
"""
40
86
Verifies that constructing a model with an existing pendulum duration doesn't throw.
41
87
"""
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 ()
88
+ model = DurationModel (delta_t = instance )
46
89
90
+ assert model .delta_t .total_seconds () == instance .total_seconds ()
47
91
48
92
@pytest .mark .parametrize (
49
93
'dt' ,
0 commit comments