1919test_date = datetime .datetime (1996 , 10 , 13 , 2 , 20 ).replace (tzinfo = pytz .utc )
2020today = datetime .datetime .utcnow ().replace (tzinfo = pytz .utc ) # make sure UTC
2121
22+
2223# TODO: travis matrix to test without pytz installed
2324# TODO: test get_timezone
2425
@@ -78,13 +79,21 @@ def test_set_timezone():
7879
7980 if dates .get_utc_offset (tz , today ): # otherwise the timezone stayed as UTC
8081 # 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 ()
8286
8387 # 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 ()
8897
8998 if tz in {
9099 "America/Punta_Arenas" ,
@@ -110,10 +119,14 @@ def test_set_timezone():
110119 # get_utc_offset(tz, test_date).total_seconds()
111120 #
112121 # )
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 ()
117130
118131
119132months = [
@@ -146,31 +159,39 @@ def test_parse_month():
146159
147160 for value in ["abc" , 0 , '0' , - 1 , "-1" , 13 , "13" ]:
148161 with pytest .raises (ValueError , match = "Unrecognised month value" ):
149- dates .parse_month (value )
162+ dates .parse_month (value ) # type: ignore
150163
151164
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
154168
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
156173
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
161175
162- assert dates .get_month_number (month ) == month_idx
163176
177+ @pytest .mark .parametrize ("month_idx" , range (1 , 13 ))
178+ def test_get_month_number_from_no (month_idx ):
164179 for month_idx in range (1 , 13 ):
165180 assert dates .get_month_number (month_idx ) == month_idx
166181
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 )
170182
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 )
174195
175196
176197def test_check_date ():
0 commit comments