19
19
test_date = datetime .datetime (1996 , 10 , 13 , 2 , 20 ).replace (tzinfo = pytz .utc )
20
20
today = datetime .datetime .utcnow ().replace (tzinfo = pytz .utc ) # make sure UTC
21
21
22
+
22
23
# TODO: travis matrix to test without pytz installed
23
24
# TODO: test get_timezone
24
25
@@ -78,13 +79,21 @@ def test_set_timezone():
78
79
79
80
if dates .get_utc_offset (tz , today ): # otherwise the timezone stayed as UTC
80
81
# ensure timestamp did change
81
- assert dates .set_timezone (today , dates .get_timezone (tz , today )).timestamp () != today .timestamp ()
82
+ target_tz = dates .get_timezone (tz , today )
83
+ assert target_tz is not None
84
+
85
+ assert dates .set_timezone (today , target_tz ).timestamp () != today .timestamp ()
82
86
83
87
# Difference between "today" and the new timezone should be the timezone difference
84
- assert (
85
- dates .set_timezone (today , dates .get_timezone (tz , today )).timestamp ()
86
- + dates .get_utc_offset (tz , today ).total_seconds ()
87
- ) == today .timestamp ()
88
+ target_tz = dates .get_timezone (tz , today )
89
+ assert target_tz is not None
90
+
91
+ utc_offset = dates .get_utc_offset (tz , today )
92
+ assert utc_offset is not None
93
+
94
+ as_seconds = dates .set_timezone (today , target_tz ).timestamp () + utc_offset .total_seconds ()
95
+
96
+ assert as_seconds == today .timestamp ()
88
97
89
98
if tz in {
90
99
"America/Punta_Arenas" ,
@@ -110,10 +119,14 @@ def test_set_timezone():
110
119
# get_utc_offset(tz, test_date).total_seconds()
111
120
#
112
121
# )
113
- assert (
114
- dates .set_timezone (test_date , dates .get_timezone (tz , test_date )).timestamp ()
115
- + dates .get_utc_offset (tz , test_date ).total_seconds ()
116
- ) == test_date .timestamp ()
122
+ target_tz = dates .get_timezone (tz , test_date )
123
+ assert target_tz is not None
124
+
125
+ offset = dates .get_utc_offset (tz , test_date )
126
+ assert offset is not None
127
+
128
+ as_seconds = dates .set_timezone (test_date , target_tz ).timestamp () + offset .total_seconds ()
129
+ assert as_seconds == test_date .timestamp ()
117
130
118
131
119
132
months = [
@@ -146,31 +159,39 @@ def test_parse_month():
146
159
147
160
for value in ["abc" , 0 , '0' , - 1 , "-1" , 13 , "13" ]:
148
161
with pytest .raises (ValueError , match = "Unrecognised month value" ):
149
- dates .parse_month (value )
162
+ dates .parse_month (value ) # type: ignore
150
163
151
164
152
- def test_get_month_number ():
153
- for month_idx , month in enumerate (months ):
165
+ @pytest .mark .parametrize ("month_idx, month" , enumerate (months ))
166
+ def test_get_month_number_from_name (month_idx , month ):
167
+ month_idx += 1 # to make 1-indexed
154
168
155
- month_idx += 1 # to make 1-indexed
169
+ for i in range (3 , len (month )):
170
+ assert dates .get_month_number (month .lower ()[:i ]) == month_idx
171
+ assert dates .get_month_number (month .upper ()[:i ]) == month_idx
172
+ assert dates .get_month_number (month .capitalize ()[:i ]) == month_idx
156
173
157
- for i in range (3 , len (month )):
158
- assert dates .get_month_number (month .lower ()[:i ]) == month_idx
159
- assert dates .get_month_number (month .upper ()[:i ]) == month_idx
160
- assert dates .get_month_number (month .capitalize ()[:i ]) == month_idx
174
+ assert dates .get_month_number (month ) == month_idx
161
175
162
- assert dates .get_month_number (month ) == month_idx
163
176
177
+ @pytest .mark .parametrize ("month_idx" , range (1 , 13 ))
178
+ def test_get_month_number_from_no (month_idx ):
164
179
for month_idx in range (1 , 13 ):
165
180
assert dates .get_month_number (month_idx ) == month_idx
166
181
167
- for value in [0 , - 1 , 13 ]:
168
- with pytest .raises (ValueError , match = "The given month is not recognised." ):
169
- dates .get_month_number (value )
170
182
171
- for value in ["abc" , '0' , "-1" "13" ]:
172
- with pytest .raises (ValueError , match = "Unrecognised month value" ):
173
- dates .get_month_number (value )
183
+ @pytest .mark .parametrize ("value, match" , [
184
+ (0 , "The given month is not recognised." ),
185
+ (- 1 , "The given month is not recognised." ),
186
+ (13 , "The given month is not recognised." ),
187
+ ("abc" , "Unrecognised month value" ),
188
+ ('0' , "Unrecognised month value" ),
189
+ ("-1" , "Unrecognised month value" ),
190
+ ("13" , "Unrecognised month value" ),
191
+ ])
192
+ def test_get_month_number_errors (value , match ):
193
+ with pytest .raises (ValueError , match = match ):
194
+ dates .get_month_number (value )
174
195
175
196
176
197
def test_check_date ():
0 commit comments