1212
1313# 3rd party
1414import pytz
15+ import pytest
1516
1617# this package
1718from domdf_python_tools import dates
1819
1920test_date = datetime .datetime (1996 , 10 , 13 , 2 , 20 ).replace (tzinfo = pytz .utc )
2021today = datetime .datetime .utcnow ().replace (tzinfo = pytz .utc ) # make sure UTC
2122
23+
2224# TODO: travis matrix to test without pytz installed
2325# TODO: test get_timezone
2426
@@ -29,7 +31,7 @@ def test_utc_offset():
2931 assert dates .get_utc_offset ("Europe/London" , test_date ) == datetime .timedelta (0 , 3600 )
3032 assert dates .get_utc_offset ("Africa/Algiers" , test_date ) == datetime .timedelta (0 , 3600 )
3133 # TODO: Finish
32-
34+
3335 # Check that the correct UTC offsets are given for common timezones for today
3436 assert dates .get_utc_offset ("US/Pacific" , today ) == datetime .timedelta (- 1 , 61200 )
3537 assert dates .get_utc_offset ("Europe/London" , today ) == datetime .timedelta (0 , 3600 )
@@ -39,36 +41,39 @@ def test_utc_offset():
3941 assert dates .get_utc_offset ("US/Pacific" ) == datetime .timedelta (- 1 , 61200 )
4042 assert dates .get_utc_offset ("Europe/London" ) == datetime .timedelta (0 , 3600 )
4143 assert dates .get_utc_offset ("Africa/Algiers" ) == datetime .timedelta (0 , 3600 )
42- # TODO: Finish
44+
45+
46+ # TODO: Finish
4347
4448
4549def test_converting_timezone ():
4650 # No matter what timezone we convert to the timestamp should be the same
4751 for tz in pytz .all_timezones :
48- assert test_date .astimezone (dates .get_timezone (tz , test_date )).timestamp () == test_date .timestamp () == 845173200.0
49-
52+ assert test_date .astimezone (
53+ dates .get_timezone (tz , test_date )).timestamp () == test_date .timestamp () == 845173200.0
54+
5055 if dates .get_utc_offset (tz , test_date ): # otherwise the timezone stayed as UTC
5156 assert test_date .astimezone (dates .get_timezone (tz , test_date )).hour != test_date .hour
5257
5358 # And again with today's date
5459 assert today .astimezone (dates .get_timezone (tz , today )).timestamp () == today .timestamp ()
5560 if dates .get_utc_offset (tz , today ): # otherwise the timezone stayed as UTC
5661 assert today .astimezone (dates .get_timezone (tz , today )).hour != today .hour
57-
58-
62+
63+
5964def test_utc_timestamp_to_datetime ():
6065 # Going from a datetime object to timezone and back should give us the same object
6166 for tz in pytz .all_timezones :
6267 tzinfo = dates .get_timezone (tz , test_date )
6368 dt = test_date .astimezone (tzinfo )
6469 assert dates .utc_timestamp_to_datetime (dt .timestamp (), tzinfo ) == dt
65-
70+
6671 # And again with today's date
6772 tzinfo = dates .get_timezone (tz , today )
6873 dt = today .astimezone (tzinfo )
6974 assert dates .utc_timestamp_to_datetime (dt .timestamp (), tzinfo ) == dt
7075
71-
76+
7277def test_set_timezone ():
7378 # Setting the timezone should change the timestamp
7479 for tz in pytz .all_timezones :
@@ -79,7 +84,7 @@ def test_set_timezone():
7984
8085 # Difference between "today" and the new timezone should be the timezone difference
8186 assert \
82- dates .set_timezone (today , dates .get_timezone (tz , today )).timestamp () + \
87+ dates .set_timezone (today , dates .get_timezone (tz , today )).timestamp () + \
8388 dates .get_utc_offset (tz , today ).total_seconds () \
8489 == today .timestamp ()
8590
@@ -104,6 +109,102 @@ def test_set_timezone():
104109 #
105110 # )
106111 assert \
107- dates .set_timezone (test_date , dates .get_timezone (tz , test_date )).timestamp () + \
112+ dates .set_timezone (test_date , dates .get_timezone (tz , test_date )).timestamp () + \
108113 dates .get_utc_offset (tz , test_date ).total_seconds () \
109114 == test_date .timestamp ()
115+
116+
117+ months = [
118+ "January" ,
119+ "February" ,
120+ "March" ,
121+ "April" ,
122+ "May" ,
123+ "June" ,
124+ "July" ,
125+ "August" ,
126+ "September" ,
127+ "October" ,
128+ "November" ,
129+ "December" ,
130+ ]
131+
132+
133+ def test_parse_month ():
134+ for month_idx , month in enumerate (months ):
135+
136+ month_idx += 1 # to make 1-indexed
137+
138+ for i in range (3 , len (month )):
139+ assert dates .parse_month (month .lower ()[:i ]) == month
140+ assert dates .parse_month (month .upper ()[:i ]) == month
141+ assert dates .parse_month (month .capitalize ()[:i ]) == month
142+
143+ assert dates .parse_month (month_idx ) == month
144+
145+ for value in ["abc" , 0 , "0" , - 1 , "-1" , 13 , "13" ]:
146+ with pytest .raises (ValueError ):
147+ dates .parse_month (value )
148+
149+
150+ def test_get_month_number ():
151+ for month_idx , month in enumerate (months ):
152+
153+ month_idx += 1 # to make 1-indexed
154+
155+ for i in range (3 , len (month )):
156+ assert dates .get_month_number (month .lower ()[:i ]) == month_idx
157+ assert dates .get_month_number (month .upper ()[:i ]) == month_idx
158+ assert dates .get_month_number (month .capitalize ()[:i ]) == month_idx
159+
160+ assert dates .get_month_number (month ) == month_idx
161+
162+ for month_idx in range (1 , 13 ):
163+ assert dates .get_month_number (month_idx ) == month_idx
164+
165+ for value in ["abc" , 0 , "0" , - 1 , "-1" , 13 , "13" ]:
166+ with pytest .raises (ValueError ):
167+ dates .get_month_number (value )
168+
169+
170+ def test_check_date ():
171+ for month_idx , month in enumerate (months ):
172+
173+ month_idx += 1 # to make 1-indexed
174+
175+ if month_idx in {9 , 4 , 6 , 11 }:
176+ max_day = 30
177+ elif month_idx == 2 :
178+ max_day = 28
179+ else :
180+ max_day = 31
181+
182+ for day in range (- 5 , 36 ):
183+ if month_idx == 2 and day == 29 :
184+ for i in range (3 , len (month )):
185+ assert dates .check_date (month .lower ()[:i ], 29 )
186+ assert dates .check_date (month .upper ()[:i ], 29 )
187+ assert dates .check_date (month .capitalize ()[:i ], 29 )
188+
189+ assert not dates .check_date (month .lower ()[:i ], 29 , False )
190+ assert not dates .check_date (month .upper ()[:i ], 29 , False )
191+ assert not dates .check_date (month .capitalize ()[:i ], 29 , False )
192+
193+ assert dates .check_date (month , 29 )
194+ assert not dates .check_date (month , 29 , False )
195+
196+ elif 0 < day <= max_day :
197+ for i in range (3 , len (month )):
198+ assert dates .check_date (month .lower ()[:i ], day )
199+ assert dates .check_date (month .upper ()[:i ], day )
200+ assert dates .check_date (month .capitalize ()[:i ], day )
201+
202+ assert dates .check_date (month , day )
203+
204+ else :
205+ for i in range (3 , len (month )):
206+ assert not dates .check_date (month .lower ()[:i ], day )
207+ assert not dates .check_date (month .upper ()[:i ], day )
208+ assert not dates .check_date (month .capitalize ()[:i ], day )
209+
210+ assert not dates .check_date (month , day )
0 commit comments