Repository navigation
Expand file tree
/
Copy pathcommon.py
More file actions
410 lines (340 loc) · 14.1 KB
/
Copy pathcommon.py
File metadata and controls
410 lines (340 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# python-holidays
# ---------------
# A fast, efficient Python library for generating country, province and state
# specific sets of holidays on the fly. It aims to make determining whether a
# specific date is a holiday as fast and flexible as possible.
#
# Authors: dr-prodigy <dr.prodigy.github@gmail.com> (c) 2017-2023
# ryanss <ryanssdev@icloud.com> (c) 2014-2017
# Website: https://github.com/dr-prodigy/python-holidays
# License: MIT (see LICENSE file)
import os
import unittest
from typing import Generator
from dateutil.parser import parse
from holidays import HolidayBase
from holidays.constants import SUN
class TestCase(unittest.TestCase):
"""Base class for python-holidays test cases."""
@classmethod
def setUpClass(cls, test_class=None, years=None, years_non_observed=None):
super().setUpClass()
if test_class is None:
return None
cls.test_class = test_class
if (
hasattr(test_class, "default_language")
# Can be either 2 (e.g., en, fr, uk) or 5 (e.g., en_US, en_GB).
and len(test_class.default_language) not in {2, 5}
):
raise ValueError(
f"`{test_class.__name__}.default_language` value is invalid."
)
if hasattr(test_class, "default_language"):
cls.set_language(test_class, test_class.default_language)
if years:
cls.holidays = test_class(years=years)
if years_non_observed:
cls.holidays_non_observed = test_class(
observed=False, years=years_non_observed
)
def setUp(self):
super().setUp()
if hasattr(self.test_class, "default_language"):
self.set_language(self.test_class.default_language)
if not hasattr(self, "holidays"):
self.holidays = self.test_class()
if not hasattr(self, "holidays_non_observed"):
self.holidays_non_observed = self.test_class(observed=False)
def set_language(self, language):
os.environ["LANGUAGE"] = language
def _parse_arguments(
self, args, expand_items=True, instance_name="holidays"
):
item_args = args
instance = None
if args and issubclass(args[0].__class__, HolidayBase):
instance = args[0]
item_args = args[1:]
else:
try:
instance = getattr(self, instance_name)
self.assertTrue(
issubclass(instance.__class__, HolidayBase),
f"The `self.{instance_name}` must be a "
"`HolidayBase` subclass.",
)
except AttributeError:
raise ValueError(
"Either pass a holidays object (`HolidayBase` subclass) "
"as a first argument or initialize your `TestCase` class "
"properly with `setUpClass()` method."
)
items = []
if expand_items:
for item_arg in item_args:
if type(item_arg) in {list, set, tuple}:
items.extend(item_arg)
elif isinstance(item_arg, (Generator, range)):
items.extend(tuple(item_arg))
else:
items.append(item_arg)
else:
items.extend(item_args)
if instance_name == "holidays":
self.assertTrue(instance.observed)
else:
self.assertFalse(instance.observed)
return instance, items
def _verify_type(self, holidays):
self.assertTrue(
issubclass(holidays.__class__, HolidayBase),
"`holidays` object must be a subclass of `HolidayBase`",
)
def assertCountryAliases(self, cls, alpha_2, alpha_3):
"""Assert country aliases match."""
self.assertTrue(
issubclass(cls, HolidayBase),
"Country holidays object must be a subclass of `HolidayBase`",
)
type_error_message = (
"Country alias object must be a subclass of the country class."
)
for alias in (alpha_2, alpha_3):
self.assertIsNotNone(alias, type_error_message)
self.assertTrue(issubclass(alias, cls), type_error_message)
length_error_message = (
"This method accepts exactly 3 arguments "
"in this specific order: country base class, country alpha-2 "
"alias, and country alpha-3 alias. For example: "
"`self.assertCountryAliases(UnitedStates, US, USA)`"
)
if len(alpha_2.__name__) != 2:
raise ValueError(
f"{length_error_message}. Alias `{alpha_2.__name__}` doesn't "
"look like alpha-2 country code."
)
if len(alpha_3.__name__) != 3:
raise ValueError(
f"{length_error_message}. Alias `{alpha_3.__name__}` doesn't "
"look like alpha-3 country code."
)
# Holiday.
def _assertHoliday(self, instance_name, *args):
"""Helper: assert each date is a holiday."""
holidays, dates = self._parse_arguments(
args,
instance_name=instance_name,
)
self._verify_type(holidays)
for dt in dates:
self.assertIn(dt, holidays, dt)
def assertHoliday(self, *args):
"""Assert each date is a holiday."""
self._assertHoliday("holidays", *args)
def assertNonObservedHoliday(self, *args):
"""Assert each date is a non-observed holiday."""
self._assertHoliday("holidays_non_observed", *args)
# Holiday dates.
def _assertHolidayDates(self, instance_name, *args):
"""Helper: assert holiday dates exactly match expected dates."""
holidays, dates = self._parse_arguments(
args,
instance_name=instance_name,
)
self._verify_type(holidays)
# Check one by one for descriptive error messages.
for dt in dates:
self.assertIn(dt, holidays, dt)
self.assertEqual(
len(dates),
len(holidays.keys()),
set(dates).difference(holidays.keys()),
)
def assertHolidayDates(self, *args):
"""Assert holiday dates exactly match expected dates."""
self._assertHolidayDates("holidays", *args)
def assertNonObservedHolidayDates(self, *args):
"""Assert holiday dates exactly match expected dates."""
self._assertHolidayDates("holidays_non_observed", *args)
# Holiday name.
def _assertHolidayName(self, name, instance_name, *args):
"""Helper: assert a holiday with a specific name exists."""
holidays, _ = self._parse_arguments(
args,
instance_name=instance_name,
)
self.assertEqual(
len(holidays.years),
len(holidays.get_named(name, lookup="exact")),
name,
)
def assertHolidayName(self, name, *args):
"""Assert a holiday with a specific name exists."""
self._assertHolidayName(name, "holidays", *args)
def assertNonObservedHolidayName(self, name, *args):
"""Assert a non-observed holiday with a specific name exists."""
self._assertHolidayName(name, "holidays_non_observed", *args)
def _assertHolidayNameInYears(self, name, instance_name, *args):
"""
Helper: assert a holiday with a specific name exists in specified
years.
"""
holidays, years = self._parse_arguments(
args,
instance_name=instance_name,
)
holiday_years = {
dt.year for dt in holidays.get_named(name, lookup="exact")
}
self.assertTrue(set(years).issubset(holiday_years), name)
def assertHolidayNameInYears(self, name, *args):
"""Assert a holiday with a specific name exists in specified years."""
self._assertHolidayNameInYears(name, "holidays", *args)
def assertNonObservedHolidayNameInYears(self, name, *args):
"""Assert a non-observed holiday with a specific name exists in
specified years."""
self._assertHolidayNameInYears(name, "holidays_non_observed", *args)
# Holidays.
def _assertHolidays(self, instance_name, *args):
"""Helper: assert holidays exactly match expected holidays."""
holidays, expected_holidays = self._parse_arguments(
args,
expand_items=False,
instance_name=instance_name,
)
self._verify_type(holidays)
# Check one by one for descriptive error messages.
for dt, name in expected_holidays:
self.assertIn(dt, holidays)
self.assertEqual(name, holidays.get(dt), dt)
self.assertEqual(
len(holidays),
len(expected_holidays),
set(
(dt.strftime("%Y-%m-%d"), name)
for dt, name in holidays.items()
).difference((dt, name) for dt, name in expected_holidays),
)
def assertHolidays(self, *args):
"""Assert holidays exactly match expected holidays."""
self._assertHolidays("holidays", *args)
def assertNonObservedHolidays(self, *args):
"""Assert non-observed holidays exactly match expected holidays."""
self._assertHolidays("holidays_non_observed", *args)
# Holidays name.
def _assertHolidaysName(self, name, instance_name, *args):
"""Assert each holiday name matches an expected one."""
holidays, dates = self._parse_arguments(
args,
instance_name=instance_name,
)
for dt in dates:
self.assertIn(name, holidays.get_list(dt))
def assertHolidaysName(self, name, *args):
"""Assert each holiday name matches an expected one."""
self._assertHolidaysName(name, "holidays", *args)
def assertNonObservedHolidaysName(self, name, *args):
"""Assert each non-observed holiday name matches an expected one."""
self._assertHolidaysName(name, "holidays_non_observed", *args)
# No holiday.
def _assertNoHoliday(self, instance_name, *args):
"""Helper: assert each date is not a holiday."""
holidays, dates = self._parse_arguments(
args,
instance_name=instance_name,
)
for dt in dates:
self.assertNotIn(dt, holidays, dt)
def assertNoHoliday(self, *args):
"""Assert each date is not a holiday."""
self._assertNoHoliday("holidays", *args)
def assertNoNonObservedHoliday(self, *args):
"""Assert each date is not a non-observed holiday."""
self._assertNoHoliday("holidays_non_observed", *args)
# No holiday name.
def _assertNoHolidayName(self, name, instance_name, *args):
"""Helper: assert a holiday with a specific name doesn't exist."""
holidays, _ = self._parse_arguments(
args,
instance_name=instance_name,
)
self.assertFalse(holidays.get_named(name, lookup="exact"), name)
def assertNoHolidayName(self, name, *args):
"""Assert a holiday with a specific name doesn't exist."""
self._assertNoHolidayName(name, "holidays", *args)
def assertNoNonObservedHolidayName(self, name, *args):
"""Assert a non-observed holiday with a specific name doesn't exist."""
self._assertNoHolidayName(name, "holidays_non_observed", *args)
def _assertNoHolidayNameInYears(self, name, instance_name, *args):
"""Helper: assert a holiday with a specific name doesn't exist in
specified years."""
holidays, years = self._parse_arguments(
args,
instance_name=instance_name,
)
holiday_years = {
dt.year for dt in holidays.get_named(name, lookup="exact")
}
self.assertEqual(0, len(holiday_years.intersection(years)), name)
def assertNoHolidayNameInYears(self, name, *args):
"""Assert a holiday with a specific name doesn't exist in
specified years."""
self._assertNoHolidayNameInYears(name, "holidays", *args)
def assertNoNonObservedHolidayNameInYears(self, name, *args):
"""Assert a non-observed holiday with a specific name doesn't exist in
specified years."""
self._assertNoHolidayNameInYears(name, "holidays_non_observed", *args)
# No holidays.
def _assertNoHolidays(self, instance_name, *args):
"""Helper: assert holidays dict is empty."""
holidays, _ = self._parse_arguments(
args,
instance_name=instance_name,
)
self._verify_type(holidays)
self.assertFalse(holidays)
self.assertEqual(0, len(holidays))
def assertNoHolidays(self, *args):
"""Assert holidays dict is empty."""
self._assertNoHolidays("holidays", *args)
def assertNoNonObservedHolidays(self, *args):
"""Assert non-observed holidays dict is empty."""
self._assertNoHolidays("holidays_non_observed", *args)
class SundayHolidays(TestCase):
"""Common class to test countries with Sundays as a holidays."""
def assertSundays(self, cls):
holidays = cls(years=1989, include_sundays=True)
self.assertHoliday(
holidays,
"1989-12-31",
)
self.assertEqual(53, len([s for s in holidays if s.weekday() == SUN]))
holidays = cls(years=2032, include_sundays=True)
self.assertHoliday(
holidays,
"2032-01-04",
)
self.assertEqual(52, len([s for s in holidays if s.weekday() == SUN]))
self.assertNoHolidays(cls(include_sundays=True))
for sunday in (
"1989-12-31",
"2017-02-05",
"2017-02-12",
"2032-02-29",
):
self.assertEqual(parse(sunday).weekday(), SUN)
self.assertHoliday(holidays, sunday)
for non_sunday in (
"2001-05-16",
"2001-05-18",
"2016-12-27",
"2016-12-28",
"2017-02-06",
"2017-02-07",
"2017-02-08",
"2017-02-09",
"2017-02-10",
):
self.assertNotEqual(parse(non_sunday).weekday(), SUN)
self.assertNoHoliday(holidays, non_sunday)