forked from vacanza/holidays
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
6817 lines (5885 loc) · 301 KB
/
tests.py
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# 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.
#
# Author: ryanss <[email protected]> (c) 2014-2017
# dr-prodigy <[email protected]> (c) 2017-2020
# Website: https://github.com/dr-prodigy/python-holidays
# License: MIT (see LICENSE file)
import os
import sys
import unittest
import warnings
from glob import glob
from itertools import product
from datetime import date, datetime, timedelta
from dateutil.relativedelta import relativedelta, MO
from flake8.api import legacy as flake8
import holidays
class TestFlake8(unittest.TestCase):
def test_flake8(self):
"""Test that we conform to PEP-8."""
self.style_guide = flake8.get_style_guide(ignore=['I', 'F401', 'W504'])
self.py_files = [y for x in os.walk(os.path.abspath('holidays')) for y in glob(os.path.join(x[0], '*.py'))]
self.report = self.style_guide.check_files(self.py_files)
self.assertEqual(self.report.get_statistics('E'), [])
class TestBasics(unittest.TestCase):
def setUp(self):
self.holidays = holidays.US()
def test_contains(self):
self.assertIn(date(2014, 1, 1), self.holidays)
self.assertNotIn(date(2014, 1, 2), self.holidays)
def test_getitem(self):
self.assertEqual(self.holidays[date(2014, 1, 1)], "New Year's Day")
self.assertEqual(self.holidays.get(date(2014, 1, 1)), "New Year's Day")
self.assertRaises(KeyError, lambda: self.holidays[date(2014, 1, 2)])
self.assertIsNone(self.holidays.get(date(2014, 1, 2)))
self.assertListEqual(
self.holidays[date(2013, 12, 31): date(2014, 1, 2)],
[date(2014, 1, 1)]
)
self.assertListEqual(
self.holidays[date(2013, 12, 24): date(2014, 1, 2)],
[date(2013, 12, 25), date(2014, 1, 1)]
)
self.assertListEqual(
self.holidays[date(2013, 12, 25): date(2014, 1, 2): 3],
[date(2013, 12, 25)]
)
self.assertListEqual(
self.holidays[date(2013, 12, 25): date(2014, 1, 2): 7],
[date(2013, 12, 25), date(2014, 1, 1)]
)
self.assertListEqual(
self.holidays[date(2014, 1, 2): date(2013, 12, 30)],
[date(2014, 1, 1)]
)
self.assertListEqual(
self.holidays[date(2014, 1, 2): date(2013, 12, 25)],
[date(2014, 1, 1)]
)
self.assertListEqual(
self.holidays[date(2014, 1, 2): date(2013, 12, 24)],
[date(2014, 1, 1), date(2013, 12, 25)]
)
self.assertListEqual(
self.holidays[date(2014, 1, 1): date(2013, 12, 24): 3],
[date(2014, 1, 1)]
)
self.assertListEqual(
self.holidays[date(2014, 1, 1): date(2013, 12, 24): 7],
[date(2014, 1, 1), date(2013, 12, 25)]
)
self.assertListEqual(
self.holidays[date(2013, 12, 31): date(2014, 1, 2): -3],
[]
)
self.assertListEqual(
self.holidays[
date(2014, 1, 1): date(2013, 12, 24): timedelta(days=3)
],
[date(2014, 1, 1)]
)
self.assertListEqual(
self.holidays[
date(2014, 1, 1): date(2013, 12, 24): timedelta(days=7)
],
[date(2014, 1, 1), date(2013, 12, 25)]
)
self.assertListEqual(
self.holidays[
date(2013, 12, 31): date(2014, 1, 2): timedelta(days=3)
],
[]
)
self.assertRaises(ValueError, lambda: self.holidays[date(2014, 1, 1):])
self.assertRaises(ValueError, lambda: self.holidays[:date(2014, 1, 1)])
self.assertRaises(
TypeError,
lambda: self.holidays[date(2014, 1, 1): date(2014, 1, 2): '']
)
self.assertRaises(
ValueError,
lambda: self.holidays[date(2014, 1, 1): date(2014, 1, 2): 0]
)
def test_get(self):
self.assertEqual(self.holidays.get('2014-01-01'), "New Year's Day")
self.assertIsNone(self.holidays.get('2014-01-02'))
self.assertFalse(self.holidays.get('2014-01-02', False))
self.assertTrue(self.holidays.get('2014-01-02', True))
def test_pop(self):
self.assertRaises(KeyError, lambda: self.holidays.pop('2014-01-02'))
self.assertFalse(self.holidays.pop('2014-01-02', False))
self.assertTrue(self.holidays.pop('2014-01-02', True))
self.assertIn(date(2014, 1, 1), self.holidays)
self.assertEqual(self.holidays.pop('2014-01-01'), "New Year's Day")
self.assertNotIn(date(2014, 1, 1), self.holidays)
self.assertIn(date(2014, 7, 4), self.holidays)
def test_pop_named(self):
self.assertIn(date(2014, 1, 1), self.holidays)
self.holidays.pop_named("New Year's Day")
self.assertNotIn(date(2014, 1, 1), self.holidays)
self.assertRaises(KeyError,
lambda: self.holidays.pop_named("New Year's Dayz"))
def test_setitem(self):
self.holidays = holidays.US(years=[2014])
self.assertEqual(len(self.holidays), 10)
self.holidays[date(2014, 1, 3)] = "Fake Holiday"
self.assertEqual(len(self.holidays), 11)
self.assertIn(date(2014, 1, 3), self.holidays)
self.assertEqual(self.holidays.get(date(2014, 1, 3)), "Fake Holiday")
def test_update(self):
h = holidays.HolidayBase()
h.update({
date(2015, 1, 1): "New Year's Day",
'2015-12-25': "Christmas Day",
})
self.assertIn('2015-01-01', h)
self.assertIn(date(2015, 12, 25), h)
def test_append(self):
h = holidays.HolidayBase()
h.update({
date(2015, 1, 1): "New Year's Day",
'2015-12-25': "Christmas Day",
})
h.append([date(2015, 4, 1), '2015-04-03'])
h.append(date(2015, 4, 6))
h.append('2015-04-07')
self.assertIn('2015-01-01', h)
self.assertIn(date(2015, 12, 25), h)
self.assertIn('2015-04-01', h)
self.assertNotIn('2015-04-02', h)
self.assertIn('2015-04-03', h)
self.assertNotIn('2015-04-04', h)
self.assertNotIn('2015-04-05', h)
self.assertIn('2015-04-06', h)
self.assertIn('2015-04-07', h)
def test_eq_ne(self):
us1 = holidays.UnitedStates()
us2 = holidays.US()
us3 = holidays.UnitedStates(years=[2014])
us4 = holidays.US(years=[2014])
ca1 = holidays.Canada()
ca2 = holidays.CA()
ca3 = holidays.Canada(years=[2014])
ca4 = holidays.CA(years=[2014])
self.assertEqual(us1, us2)
self.assertEqual(us3, us4)
self.assertEqual(ca1, ca2)
self.assertEqual(ca3, ca4)
self.assertNotEqual(us1, us3)
self.assertNotEqual(us1, ca1)
self.assertNotEqual(us3, ca3)
self.assertNotEqual(us1, us3)
def test_add(self):
ca = holidays.CA()
us = holidays.US()
mx = holidays.MX()
na = ca + (us + mx)
self.assertNotIn('2014-07-01', us)
self.assertIn('2014-07-01', ca)
self.assertNotIn('2014-07-04', ca)
self.assertIn('2014-07-04', us)
self.assertIn('2014-07-04', ca + us)
self.assertIn('2014-07-04', us + ca)
self.assertIn('2015-07-04', ca + us)
self.assertIn('2015-07-04', us + ca)
self.assertIn('2015-07-01', ca + us)
self.assertIn('2015-07-01', us + ca)
self.assertIn('2014-07-04', na)
self.assertIn('2015-07-04', na)
self.assertIn('2015-07-01', na)
self.assertIn('2000-02-05', na)
self.assertEqual((ca + us).prov, 'ON')
self.assertEqual((us + ca).prov, 'ON')
ca = holidays.CA(years=[2014], expand=False)
us = holidays.US(years=[2014, 2015], expand=True)
self.assertTrue((ca + us).expand)
self.assertEqual((ca + us).years, {2014, 2015})
self.assertEqual((us + ca).years, {2014, 2015})
na = holidays.CA()
na += holidays.US()
na += holidays.MX()
self.assertEqual(na.country, ['CA', 'US', 'MX'])
self.assertIn('2014-07-04', na)
self.assertIn('2014-07-04', na)
self.assertIn('2015-07-04', na)
self.assertIn('2015-07-04', na)
self.assertIn('2015-07-01', na)
self.assertIn('2015-07-01', na)
self.assertIn('2000-02-05', na)
self.assertEqual(na.prov, 'ON')
na = holidays.CA() + holidays.US()
na += holidays.MX()
self.assertIn('2014-07-04', na)
self.assertIn('2014-07-04', na)
self.assertIn('2015-07-04', na)
self.assertIn('2015-07-04', na)
self.assertIn('2015-07-01', na)
self.assertIn('2015-07-01', na)
self.assertIn('2000-02-05', na)
self.assertEqual(na.prov, 'ON')
self.assertRaises(TypeError, lambda: holidays.US() + {})
na = ca + (us + mx) + ca + (mx + us + holidays.CA(prov='BC'))
self.assertIn('2000-02-05', na)
self.assertIn('2014-02-10', na)
self.assertIn('2014-02-17', na)
self.assertIn('2014-07-04', na)
provs = (holidays.CA(prov='ON', years=[2014]) +
holidays.CA(prov='BC', years=[2015]))
self.assertIn("2015-02-09", provs)
self.assertIn("2015-02-16", provs)
self.assertEqual(provs.prov, ['ON', 'BC'])
a = sum(holidays.CA(prov=x) for x in holidays.CA.PROVINCES)
self.assertEqual(a.country, 'CA')
self.assertEqual(a.prov, holidays.CA.PROVINCES)
self.assertIn("2015-02-09", a)
self.assertIn("2015-02-16", a)
na = holidays.CA() + holidays.US() + holidays.MX()
self.assertIn(date(1969, 12, 25), na)
self.assertEqual(na.get(date(1969, 7, 1)), "Dominion Day")
self.assertEqual(na.get(date(1983, 7, 1)), "Canada Day")
self.assertEqual(na.get(date(1969, 12, 25)),
"Christmas Day, Navidad [Christmas]")
na = holidays.MX() + holidays.CA() + holidays.US()
self.assertEqual(na.get(date(1969, 12, 25)),
"Navidad [Christmas], Christmas Day")
def test_get_list(self):
westland = holidays.NZ(prov='WTL')
chathams = holidays.NZ(prov='CIT')
wild = westland + chathams
self.assertEqual(wild[date(1969, 12, 1)],
("Westland Anniversary Day, " +
"Chatham Islands Anniversary Day"))
self.assertEqual(wild.get_list(date(1969, 12, 1)),
["Westland Anniversary Day",
"Chatham Islands Anniversary Day"])
self.assertEqual(wild.get_list(date(1969, 1, 1)),
["New Year's Day"])
self.assertEqual(westland.get_list(date(1969, 12, 1)),
["Westland Anniversary Day"])
self.assertEqual(westland.get_list(date(1969, 1, 1)),
["New Year's Day"])
self.assertEqual(chathams.get_list(date(1969, 12, 1)),
["Chatham Islands Anniversary Day"])
self.assertEqual(chathams.get_list(date(1969, 1, 1)),
["New Year's Day"])
ca = holidays.CA()
us = holidays.US()
mx = holidays.MX()
na = ca + us + mx
self.assertIn(date(1969, 12, 25), na)
self.assertEqual(na.get_list(date(1969, 12, 25)),
["Christmas Day", "Navidad [Christmas]"])
self.assertEqual(na.get_list(date(1969, 7, 1)), ["Dominion Day"])
self.assertEqual(na.get_list(date(1969, 1, 3)), [])
def test_list_supported_countries(self):
self.assertIn("AR", holidays.list_supported_countries())
self.assertIn("ZA", holidays.list_supported_countries())
def test_radd(self):
self.assertRaises(TypeError, lambda: 1 + holidays.US())
def test_inheritance(self):
class NoColumbusHolidays(holidays.US):
def _populate(self, year):
holidays.US._populate(self, year)
self.pop(date(year, 10, 1) + relativedelta(weekday=MO(+2)))
hdays = NoColumbusHolidays()
self.assertIn(date(2014, 10, 13), self.holidays)
self.assertNotIn(date(2014, 10, 13), hdays)
self.assertIn(date(2014, 1, 1), hdays)
self.assertIn(date(2020, 10, 12), self.holidays)
self.assertNotIn(date(2020, 10, 12), hdays)
self.assertIn(date(2020, 1, 1), hdays)
class NinjaTurtlesHolidays(holidays.US):
def _populate(self, year):
holidays.US._populate(self, year)
self[date(year, 7, 13)] = "Ninja Turtle's Day"
hdays = NinjaTurtlesHolidays()
self.assertNotIn(date(2014, 7, 13), self.holidays)
self.assertIn(date(2014, 7, 13), hdays)
self.assertIn(date(2014, 1, 1), hdays)
self.assertNotIn(date(2020, 7, 13), self.holidays)
self.assertIn(date(2020, 7, 13), hdays)
self.assertIn(date(2020, 1, 1), hdays)
class NewCountry(holidays.HolidayBase):
def _populate(self, year):
self[date(year, 1, 2)] = "New New Year's"
hdays = NewCountry()
self.assertNotIn(date(2014, 1, 1), hdays)
self.assertIn(date(2014, 1, 2), hdays)
class Dec31Holiday(holidays.HolidayBase):
def _populate(self, year):
self[date(year, 12, 31)] = "New Year's Eve"
self.assertIn(date(2014, 12, 31), Dec31Holiday())
def test_get_named(self):
us = holidays.UnitedStates(years=[2020])
# check for "New Year's Day" presence in get_named("new")
self.assertIn(date(2020, 1, 1), us.get_named('new'))
class TestArgs(unittest.TestCase):
def setUp(self):
self.holidays = holidays.US()
def test_country(self):
self.assertEqual(self.holidays.country, 'US')
self.assertIn(date(2014, 7, 4), self.holidays)
self.assertNotIn(date(2014, 7, 1), self.holidays)
self.holidays = holidays.UnitedStates()
self.assertEqual(self.holidays.country, 'US')
self.assertIn(date(2014, 7, 4), self.holidays)
self.assertNotIn(date(2014, 7, 1), self.holidays)
self.assertEqual(self.holidays.country, 'US')
self.holidays = holidays.CA()
self.assertEqual(self.holidays.country, 'CA')
self.assertEqual(self.holidays.prov, 'ON')
self.assertIn(date(2014, 7, 1), self.holidays)
self.assertNotIn(date(2014, 7, 4), self.holidays)
self.holidays = holidays.CA(prov='BC')
self.assertEqual(self.holidays.country, 'CA')
self.assertEqual(self.holidays.prov, 'BC')
self.assertIn(date(2014, 7, 1), self.holidays)
self.assertNotIn(date(2014, 7, 4), self.holidays)
def test_years(self):
self.assertEqual(len(self.holidays.years), 0)
self.assertNotIn(date(2014, 1, 2), self.holidays)
self.assertEqual(len(self.holidays.years), 1)
self.assertIn(2014, self.holidays.years)
self.assertNotIn(date(2013, 1, 2), self.holidays)
self.assertNotIn(date(2014, 1, 2), self.holidays)
self.assertNotIn(date(2015, 1, 2), self.holidays)
self.assertEqual(len(self.holidays.years), 3)
self.assertIn(2013, self.holidays.years)
self.assertIn(2015, self.holidays.years)
self.holidays = holidays.US(years=range(2010, 2015 + 1))
self.assertEqual(len(self.holidays.years), 6)
self.assertNotIn(2009, self.holidays.years)
self.assertIn(2010, self.holidays.years)
self.assertIn(2015, self.holidays.years)
self.assertNotIn(2016, self.holidays.years)
self.holidays = holidays.US(years=(2013, 2015, 2015))
self.assertEqual(len(self.holidays.years), 2)
self.assertIn(2013, self.holidays.years)
self.assertNotIn(2014, self.holidays.years)
self.assertIn(2015, self.holidays.years)
self.assertIn(date(2021, 12, 31), holidays.US(years=[2022]).keys())
self.holidays = holidays.US(years=2015)
self.assertNotIn(2014, self.holidays.years)
self.assertIn(2015, self.holidays.years)
def test_expand(self):
self.holidays = holidays.US(years=(2013, 2015), expand=False)
self.assertEqual(len(self.holidays.years), 2)
self.assertIn(2013, self.holidays.years)
self.assertNotIn(2014, self.holidays.years)
self.assertIn(2015, self.holidays.years)
self.assertNotIn(date(2014, 1, 1), self.holidays)
self.assertEqual(len(self.holidays.years), 2)
self.assertNotIn(2014, self.holidays.years)
def test_observed(self):
self.holidays = holidays.US(observed=False)
self.assertIn(date(2000, 1, 1), self.holidays)
self.assertNotIn(date(1999, 12, 31), self.holidays)
self.assertIn(date(2012, 1, 1), self.holidays)
self.assertNotIn(date(2012, 1, 2), self.holidays)
self.holidays.observed = True
self.assertIn(date(2000, 1, 1), self.holidays)
self.assertIn(date(1999, 12, 31), self.holidays)
self.assertIn(date(2012, 1, 1), self.holidays)
self.assertIn(date(2012, 1, 2), self.holidays)
self.holidays.observed = False
self.assertIn(date(2000, 1, 1), self.holidays)
self.assertNotIn(date(1999, 12, 31), self.holidays)
self.assertIn(date(2012, 1, 1), self.holidays)
self.assertNotIn(date(2012, 1, 2), self.holidays)
self.holidays = holidays.US(years=[2022], observed=False)
self.assertNotIn(date(2021, 12, 31), self.holidays.keys())
self.holidays = holidays.CA(observed=False)
self.assertNotIn(date(1878, 7, 3), self.holidays)
self.holidays.observed = True
self.assertIn(date(2018, 7, 2), self.holidays)
class TestKeyTransforms(unittest.TestCase):
def setUp(self):
self.holidays = holidays.US()
def test_dates(self):
self.assertIn(date(2014, 1, 1), self.holidays)
self.assertEqual(self.holidays[date(2014, 1, 1)], "New Year's Day")
self.holidays[date(2014, 1, 3)] = "Fake Holiday"
self.assertIn(date(2014, 1, 3), self.holidays)
self.assertEqual(self.holidays.pop(date(2014, 1, 3)), "Fake Holiday")
self.assertNotIn(date(2014, 1, 3), self.holidays)
def test_datetimes(self):
self.assertIn(datetime(2014, 1, 1, 13, 45), self.holidays)
self.assertEqual(self.holidays[datetime(2014, 1, 1, 13, 45)],
"New Year's Day")
self.holidays[datetime(2014, 1, 3, 1, 1)] = "Fake Holiday"
self.assertIn(datetime(2014, 1, 3, 2, 2), self.holidays)
self.assertEqual(self.holidays.pop(datetime(2014, 1, 3, 4, 4)),
"Fake Holiday")
self.assertNotIn(datetime(2014, 1, 3, 2, 2), self.holidays)
def test_timestamp(self):
self.assertIn(1388552400, self.holidays)
self.assertEqual(self.holidays[1388552400], "New Year's Day")
self.assertIn(1388552400.01, self.holidays)
self.assertEqual(self.holidays[1388552400.01], "New Year's Day")
self.holidays[1388725200] = "Fake Holiday"
self.assertIn(1388725201, self.holidays)
self.assertEqual(self.holidays.pop(1388725202), "Fake Holiday")
self.assertNotIn(1388725201, self.holidays)
def test_strings(self):
self.assertIn("2014-01-01", self.holidays)
self.assertEqual(self.holidays["2014-01-01"], "New Year's Day")
self.assertIn("01/01/2014", self.holidays)
self.assertEqual(self.holidays["01/01/2014"], "New Year's Day")
self.holidays["01/03/2014"] = "Fake Holiday"
self.assertIn("01/03/2014", self.holidays)
self.assertEqual(self.holidays.pop("01/03/2014"), "Fake Holiday")
self.assertNotIn("01/03/2014", self.holidays)
def test_exceptions(self):
self.assertRaises(
(TypeError, ValueError), lambda: "abc" in self.holidays)
self.assertRaises(
(TypeError, ValueError), lambda: self.holidays.get("abc123"))
self.assertRaises(
(TypeError, ValueError), self.holidays.__setitem__, "abc", "Test")
self.assertRaises(
(TypeError, ValueError), lambda: {} in self.holidays)
class TestCountryHoliday(unittest.TestCase):
def setUp(self):
self.holidays = holidays.CountryHoliday('US')
def test_country(self):
self.assertEqual(self.holidays.country, 'US')
def test_country_years(self):
h = holidays.CountryHoliday('US', years=[2015, 2016])
self.assertEqual(h.years, {2015, 2016})
def test_country_state(self):
h = holidays.CountryHoliday('US', state='NY')
self.assertEqual(h.state, 'NY')
def test_country_province(self):
h = holidays.CountryHoliday('AU', prov='NT')
self.assertEqual(h.prov, 'NT')
def test_exceptions(self):
self.assertRaises((KeyError), lambda: holidays.CountryHoliday('XXXX'))
class TestAruba(unittest.TestCase):
def setUp(self):
self.holidays = holidays.AW()
def test_2017(self):
self.assertIn(date(2017, 1, 1), self.holidays)
self.assertIn(date(2017, 1, 25), self.holidays)
self.assertIn(date(2017, 3, 18), self.holidays)
self.assertIn(date(2017, 2, 27), self.holidays)
self.assertIn(date(2017, 4, 14), self.holidays)
self.assertIn(date(2017, 4, 17), self.holidays)
self.assertIn(date(2017, 4, 27), self.holidays)
self.assertIn(date(2017, 5, 1), self.holidays)
self.assertIn(date(2017, 5, 25), self.holidays)
self.assertIn(date(2017, 12, 25), self.holidays)
self.assertIn(date(2017, 12, 26), self.holidays)
def test_new_years(self):
self.assertIn(date(2017, 1, 1), self.holidays)
def test_betico_day(self):
self.assertIn(date(2017, 1, 25), self.holidays)
def test_carnaval_monday(self):
self.assertIn(date(2017, 2, 27), self.holidays)
def test_anthem_and_flag_day(self):
self.assertIn(date(2017, 3, 18), self.holidays)
def test_good_friday(self):
self.assertIn(date(2017, 4, 14), self.holidays)
def test_easter_monday(self):
self.assertIn(date(2017, 4, 17), self.holidays)
def test_labour_day(self):
self.assertIn(date(2017, 5, 1), self.holidays)
def test_queens_day_between_1891_and_1948(self):
# Between 1891 and 1948 Queens Day was celebrated on 8-31
self.holidays = holidays.AW(years=[1901])
self.assertIn(date(1901, 8, 31), self.holidays)
def test_queens_day_between_1891_and_1948_substituted_later(self):
# Between 1891 and 1948 Queens Day was celebrated on 9-1
# (one day later) when Queens Day falls on a Sunday
self.holidays = holidays.AW(years=[1947])
self.assertIn(date(1947, 9, 1), self.holidays)
def test_queens_day_between_1949_and_2013(self):
self.holidays = holidays.AW(years=[1965])
self.assertIn(date(1965, 4, 30), self.holidays)
def test_queens_day_between_1949_and_1980_substituted_later(self):
self.holidays = holidays.AW(years=[1967])
self.assertIn(date(1967, 5, 1), self.holidays)
def test_queens_day_between_1980_and_2013_substituted_earlier(self):
self.holidays = holidays.AW(years=[2006])
self.assertIn(date(2006, 4, 29), self.holidays)
def test_kings_day_after_2014(self):
self.holidays = holidays.AW(years=[2013])
self.assertNotIn(date(2013, 4, 27), self.holidays)
self.holidays = holidays.AW(years=[2017])
self.assertIn(date(2017, 4, 27), self.holidays)
def test_kings_day_after_2014_substituted_earlier(self):
self.holidays = holidays.AW(years=[2188])
self.assertIn(date(2188, 4, 26), self.holidays)
def test_ascension_day(self):
self.holidays = holidays.AW(years=2017)
self.assertIn(date(2017, 5, 25), self.holidays)
def test_christmas(self):
self.holidays = holidays.AW(years=2017)
self.assertIn(date(2017, 12, 25), self.holidays)
def test_second_christmas(self):
self.holidays = holidays.AW(years=2017)
self.assertIn(date(2017, 12, 26), self.holidays)
class TestBulgaria(unittest.TestCase):
def setUp(self):
self.holidays = holidays.Bulgaria()
def test_before_1990(self):
self.assertEqual(len(holidays.Bulgaria(years=[1989])), 0)
def test_new_years_day(self):
for year in range(1990, 2020):
self.assertIn(date(year, 1, 1), self.holidays)
def test_liberation_day(self):
for year in range(1990, 2020):
self.assertIn(date(year, 3, 3), self.holidays)
def test_labour_day(self):
for year in range(1990, 2020):
self.assertIn(date(year, 5, 1), self.holidays)
def test_saint_georges_day(self):
for year in range(1990, 2020):
self.assertIn(date(year, 5, 6), self.holidays)
def test_twenty_fourth_of_may(self):
for year in range(1990, 2020):
self.assertIn(date(year, 5, 24), self.holidays)
def test_unification_day(self):
for year in range(1990, 2020):
self.assertIn(date(year, 9, 6), self.holidays)
def test_independence_day(self):
for year in range(1990, 2020):
self.assertIn(date(year, 9, 22), self.holidays)
def test_national_awakening_day(self):
for year in range(1990, 2020):
self.assertIn(date(year, 11, 1), self.holidays)
def test_christmas(self):
for year in range(1990, 2020):
self.assertIn(date(year, 12, 24), self.holidays)
self.assertIn(date(year, 12, 25), self.holidays)
self.assertIn(date(year, 12, 26), self.holidays)
def test_easter(self):
for year, month, day in [
(2000, 4, 30), (2001, 4, 15), (2002, 5, 5), (2003, 4, 27),
(2004, 4, 11), (2005, 5, 1), (2006, 4, 23), (2007, 4, 8),
(2008, 4, 27), (2009, 4, 19), (2010, 4, 4), (2011, 4, 24),
(2012, 4, 15), (2013, 5, 5), (2014, 4, 20), (2015, 4, 12),
(2016, 5, 1), (2017, 4, 16), (2018, 4, 8), (2019, 4, 28),
(2020, 4, 19), (2021, 5, 2), (2022, 4, 24)]:
easter = date(year, month, day)
easter_saturday = easter - timedelta(days=1)
easter_friday = easter - timedelta(days=2)
for holiday in [easter_friday, easter_saturday, easter]:
self.assertIn(holiday, self.holidays)
class TestCA(unittest.TestCase):
def setUp(self):
self.holidays = holidays.CA(observed=False)
def test_new_years(self):
self.assertNotIn(date(2010, 12, 31), self.holidays)
self.assertNotIn(date(2017, 1, 2), self.holidays)
self.holidays.observed = True
self.assertIn(date(2010, 12, 31), self.holidays)
self.assertIn(date(2017, 1, 2), self.holidays)
self.holidays.observed = False
for year in range(1900, 2100):
dt = date(year, 1, 1)
self.assertIn(dt, self.holidays)
self.assertNotIn(dt + relativedelta(days=-1), self.holidays)
self.assertNotIn(dt + relativedelta(days=+1), self.holidays)
def test_islander_day(self):
pei_holidays = holidays.CA(prov="PE")
for dt in [date(2009, 2, 9), date(2010, 2, 15), date(2011, 2, 21),
date(2012, 2, 20), date(2013, 2, 18), date(2014, 2, 17),
date(2015, 2, 16), date(2016, 2, 15), date(2020, 2, 17)]:
if dt.year >= 2010:
self.assertNotEqual(self.holidays[dt], "Islander Day")
elif dt.year == 2009:
self.assertNotIn(dt, self.holidays)
self.assertIn(dt, pei_holidays)
self.assertNotIn(dt + relativedelta(days=-1), pei_holidays)
self.assertNotIn(dt + relativedelta(days=+1), pei_holidays)
def test_family_day(self):
ab_holidays = holidays.CA(prov="AB")
bc_holidays = holidays.CA(prov="BC")
mb_holidays = holidays.CA(prov="MB")
sk_holidays = holidays.CA(prov="SK")
nb_holidays = holidays.CA(prov="NB")
for dt in [date(1990, 2, 19), date(1999, 2, 15), date(2000, 2, 21),
date(2006, 2, 20)]:
self.assertNotIn(dt, self.holidays)
self.assertIn(dt, ab_holidays)
self.assertNotIn(dt, bc_holidays)
self.assertNotIn(dt, mb_holidays)
self.assertNotIn(dt, sk_holidays)
dt = date(2007, 2, 19)
self.assertNotIn(dt, self.holidays)
self.assertIn(dt, ab_holidays)
self.assertNotIn(dt, bc_holidays)
self.assertNotIn(dt, mb_holidays)
self.assertIn(dt, sk_holidays)
for dt in [date(2008, 2, 18), date(2012, 2, 20), date(2014, 2, 17),
date(2018, 2, 19)]:
self.assertIn(dt, self.holidays)
self.assertIn(dt, ab_holidays)
self.assertNotIn(dt, bc_holidays)
self.assertIn(dt, mb_holidays)
self.assertIn(dt, sk_holidays)
for dt in [date(2018, 2, 19)]:
self.assertIn(dt, nb_holidays)
for dt in [date(2019, 2, 18), date(2020, 2, 17)]:
self.assertIn(dt, self.holidays)
self.assertIn(dt, ab_holidays)
self.assertIn(dt, bc_holidays)
self.assertIn(dt, mb_holidays)
self.assertIn(dt, sk_holidays)
for dt in [date(2013, 2, 11), date(2016, 2, 8)]:
self.assertNotIn(dt, self.holidays)
self.assertNotIn(dt, ab_holidays)
self.assertIn(dt, bc_holidays)
self.assertNotIn(dt, mb_holidays)
self.assertNotIn(dt, sk_holidays)
self.assertEqual(mb_holidays[date(2014, 2, 17)], "Louis Riel Day")
def test_st_patricks_day(self):
nl_holidays = holidays.CA(prov="NL", observed=False)
for dt in [date(1900, 3, 19), date(1999, 3, 15), date(2000, 3, 20),
date(2012, 3, 19), date(2013, 3, 18), date(2014, 3, 17),
date(2015, 3, 16), date(2016, 3, 14), date(2020, 3, 16)]:
self.assertNotIn(dt, self.holidays)
self.assertIn(dt, nl_holidays)
self.assertNotIn(dt + relativedelta(days=-1), nl_holidays)
self.assertNotIn(dt + relativedelta(days=+1), nl_holidays)
def test_good_friday(self):
qc_holidays = holidays.CA(prov="QC")
for dt in [date(1900, 4, 13), date(1901, 4, 5), date(1902, 3, 28),
date(1999, 4, 2), date(2000, 4, 21), date(2010, 4, 2),
date(2018, 3, 30), date(2019, 4, 19), date(2020, 4, 10)]:
self.assertIn(dt, self.holidays)
self.assertNotIn(dt + relativedelta(days=-1), self.holidays)
self.assertNotIn(dt + relativedelta(days=+1), self.holidays)
self.assertNotIn(dt, qc_holidays)
def test_easter_monday(self):
qc_holidays = holidays.CA(prov="QC")
for dt in [date(1900, 4, 16), date(1901, 4, 8), date(1902, 3, 31),
date(1999, 4, 5), date(2000, 4, 24), date(2010, 4, 5),
date(2018, 4, 2), date(2019, 4, 22), date(2020, 4, 13)]:
self.assertNotIn(dt, self.holidays)
self.assertIn(dt, qc_holidays)
self.assertNotIn(dt + relativedelta(days=-1), qc_holidays)
self.assertNotIn(dt + relativedelta(days=+1), qc_holidays)
def test_st_georges_day(self):
nl_holidays = holidays.CA(prov="NL")
for dt in [date(1990, 4, 23), date(1999, 4, 26), date(2000, 4, 24),
date(2010, 4, 19), date(2016, 4, 25), date(2020, 4, 20)]:
self.assertNotIn(dt, self.holidays)
self.assertIn(dt, nl_holidays)
self.assertNotIn(dt + relativedelta(days=-1), nl_holidays)
self.assertNotIn(dt + relativedelta(days=+1), nl_holidays)
def test_victoria_day(self):
for dt in [date(1953, 5, 18), date(1999, 5, 24), date(2000, 5, 22),
date(2010, 5, 24), date(2015, 5, 18), date(2020, 5, 18)]:
self.assertIn(dt, self.holidays)
self.assertNotIn(dt + relativedelta(days=-1), self.holidays)
self.assertNotIn(dt + relativedelta(days=+1), self.holidays)
def test_national_aboriginal_day(self):
nt_holidays = holidays.CA(prov="NT")
self.assertNotIn(date(1995, 6, 21), nt_holidays)
for year in range(1996, 2100):
dt = date(year, 6, 21)
self.assertNotIn(dt, self.holidays)
self.assertIn(dt, nt_holidays)
self.assertNotIn(dt + relativedelta(days=-1), nt_holidays)
self.assertNotIn(dt + relativedelta(days=+1), nt_holidays)
def test_st_jean_baptiste_day(self):
qc_holidays = holidays.CA(prov="QC", observed=False)
self.assertNotIn(date(1924, 6, 24), qc_holidays)
for year in range(1925, 2100):
dt = date(year, 6, 24)
self.assertNotIn(dt, self.holidays)
self.assertIn(dt, qc_holidays)
self.assertNotIn(dt + relativedelta(days=-1), qc_holidays)
self.assertNotIn(dt + relativedelta(days=+1), qc_holidays)
self.assertNotIn(date(2001, 6, 25), qc_holidays)
qc_holidays.observed = True
self.assertIn(date(2001, 6, 25), qc_holidays)
def test_discovery_day(self):
nl_holidays = holidays.CA(prov="NL")
yt_holidays = holidays.CA(prov="YT")
for dt in [date(1997, 6, 23), date(1999, 6, 21), date(2000, 6, 26),
date(2010, 6, 21), date(2016, 6, 27), date(2020, 6, 22)]:
self.assertNotIn(dt, self.holidays)
self.assertIn(dt, nl_holidays)
self.assertNotIn(dt, yt_holidays)
for dt in [date(1912, 8, 19), date(1999, 8, 16), date(2000, 8, 21),
date(2006, 8, 21), date(2016, 8, 15), date(2020, 8, 17)]:
self.assertNotIn(dt, self.holidays)
self.assertNotIn(dt, nl_holidays)
self.assertIn(dt, yt_holidays)
def test_canada_day(self):
for year in range(1900, 2100):
dt = date(year, 7, 1)
self.assertIn(dt, self.holidays)
self.assertNotIn(dt + relativedelta(days=-1), self.holidays)
self.assertNotIn(dt + relativedelta(days=+1), self.holidays)
self.assertNotIn(date(2006, 7, 3), self.holidays)
self.assertNotIn(date(2007, 7, 2), self.holidays)
self.holidays.observed = True
self.assertIn(date(2006, 7, 3), self.holidays)
self.assertIn(date(2007, 7, 2), self.holidays)
def test_nunavut_day(self):
nu_holidays = holidays.CA(prov="NU", observed=False)
self.assertNotIn(date(1999, 7, 9), nu_holidays)
self.assertNotIn(date(2000, 7, 9), nu_holidays)
self.assertIn(date(2000, 4, 1), nu_holidays)
for year in range(2001, 2100):
dt = date(year, 7, 9)
self.assertNotIn(dt, self.holidays)
self.assertIn(dt, nu_holidays)
self.assertNotIn(dt + relativedelta(days=-1), nu_holidays)
self.assertNotIn(dt + relativedelta(days=+1), nu_holidays)
self.assertNotIn(date(2017, 7, 10), nu_holidays)
nu_holidays.observed = True
self.assertIn(date(2017, 7, 10), nu_holidays)
def test_civic_holiday(self):
bc_holidays = holidays.CA(prov="BC")
for dt in [date(1900, 8, 6), date(1955, 8, 1), date(1973, 8, 6)]:
self.assertIn(dt, self.holidays)
self.assertNotIn(dt, bc_holidays)
for dt in [date(1974, 8, 5), date(1999, 8, 2), date(2000, 8, 7),
date(2010, 8, 2), date(2015, 8, 3), date(2020, 8, 3)]:
self.assertIn(dt, self.holidays)
self.assertIn(dt, bc_holidays)
def test_labour_day(self):
self.assertNotIn(date(1893, 9, 4), self.holidays)
for dt in [date(1894, 9, 3), date(1900, 9, 3), date(1999, 9, 6),
date(2000, 9, 4), date(2014, 9, 1), date(2015, 9, 7)]:
self.assertIn(dt, self.holidays)
self.assertNotIn(dt + relativedelta(days=-1), self.holidays)
self.assertNotIn(dt + relativedelta(days=+1), self.holidays)
def test_thanksgiving(self):
ns_holidays = holidays.CA(prov="NB")
for dt in [date(1931, 10, 12), date(1990, 10, 8), date(1999, 10, 11),
date(2000, 10, 9), date(2013, 10, 14), date(2020, 10, 12)]:
self.assertIn(dt, self.holidays)
self.assertNotIn(dt + relativedelta(days=-1), self.holidays)
self.assertNotIn(dt + relativedelta(days=+1), self.holidays)
self.assertNotIn(dt, ns_holidays)
def test_remembrance_day(self):
ab_holidays = holidays.CA(prov="AB", observed=False)
nl_holidays = holidays.CA(prov="NL", observed=False)
self.assertNotIn(date(1930, 11, 11), ab_holidays)
self.assertNotIn(date(1930, 11, 11), nl_holidays)
for year in range(1931, 2100):
dt = date(year, 11, 11)
self.assertNotIn(dt, self.holidays)
self.assertIn(dt, ab_holidays)
self.assertIn(dt, nl_holidays)
self.assertNotIn(dt + relativedelta(days=-1), nl_holidays)
self.assertNotIn(dt + relativedelta(days=+1), nl_holidays)
self.assertNotIn(date(2007, 11, 12), ab_holidays)
self.assertNotIn(date(2007, 11, 12), nl_holidays)
ab_holidays.observed = True
nl_holidays.observed = True
self.assertNotIn(date(2007, 11, 12), ab_holidays)
self.assertIn(date(2007, 11, 12), nl_holidays)
def test_christmas_day(self):
for year in range(1900, 2100):
dt = date(year, 12, 25)
self.assertIn(dt, self.holidays)
self.assertNotIn(dt + relativedelta(days=-1), self.holidays)
self.assertNotIn(date(2010, 12, 24), self.holidays)
self.assertNotEqual(self.holidays[date(2011, 12, 26)],
"Christmas Day (Observed)")
self.holidays.observed = True
self.assertIn(date(2010, 12, 24), self.holidays)
self.assertEqual(self.holidays[date(2011, 12, 26)],
"Christmas Day (Observed)")
def test_boxing_day(self):
for year in range(1900, 2100):
dt = date(year, 12, 26)
self.assertIn(dt, self.holidays)
self.assertNotIn(dt + relativedelta(days=+1), self.holidays)
self.assertNotIn(date(2009, 12, 28), self.holidays)
self.assertNotIn(date(2010, 12, 27), self.holidays)
self.holidays.observed = True
self.assertIn(date(2009, 12, 28), self.holidays)
self.assertIn(date(2010, 12, 27), self.holidays)
class TestCO(unittest.TestCase):
def setUp(self):
self.holidays = holidays.CO(observed=True)
def test_2016(self):
# http://www.officeholidays.com/countries/colombia/
self.assertIn(date(2016, 1, 1), self.holidays)
self.assertIn(date(2016, 1, 11), self.holidays)
self.assertIn(date(2016, 3, 21), self.holidays)
self.assertIn(date(2016, 3, 24), self.holidays)
self.assertIn(date(2016, 3, 25), self.holidays)
self.assertIn(date(2016, 5, 1), self.holidays)
self.assertIn(date(2016, 5, 9), self.holidays)
self.assertIn(date(2016, 5, 30), self.holidays)
self.assertIn(date(2016, 6, 6), self.holidays)
self.assertIn(date(2016, 7, 4), self.holidays)
self.assertIn(date(2016, 7, 20), self.holidays)
self.assertIn(date(2016, 8, 7), self.holidays)
self.assertIn(date(2016, 8, 15), self.holidays)
self.assertIn(date(2016, 10, 17), self.holidays)
self.assertIn(date(2016, 11, 7), self.holidays)
self.assertIn(date(2016, 11, 14), self.holidays)
self.assertIn(date(2016, 12, 8), self.holidays)
self.assertIn(date(2016, 12, 25), self.holidays)
def test_others(self):
# holidays falling on weekend
self.assertNotIn(date(2017, 1, 1), self.holidays)
self.assertNotIn(date(2014, 7, 20), self.holidays)
self.assertNotIn(date(2018, 8, 12), self.holidays)
self.assertIn(date(2014, 1, 6), self.holidays)
self.assertIn(date(2012, 3, 19), self.holidays)
self.assertIn(date(2015, 6, 29), self.holidays)
self.assertIn(date(2010, 8, 16), self.holidays)
self.assertIn(date(2015, 10, 12), self.holidays)
self.assertIn(date(2010, 11, 1), self.holidays)
self.assertIn(date(2013, 11, 11), self.holidays)
self.holidays.observed = False
self.assertIn(date(2016, 5, 5), self.holidays)
self.assertIn(date(2016, 5, 26), self.holidays)
class TestMX(unittest.TestCase):
def setUp(self):
self.holidays = holidays.MX(observed=False)
def test_new_years(self):
self.assertNotIn(date(2010, 12, 31), self.holidays)
self.assertNotIn(date(2017, 1, 2), self.holidays)
self.holidays.observed = True
self.assertIn(date(2010, 12, 31), self.holidays)
self.assertIn(date(2017, 1, 2), self.holidays)
self.holidays.observed = False
for year in range(1900, 2100):
dt = date(year, 1, 1)
self.assertIn(dt, self.holidays)
self.assertNotIn(dt + relativedelta(days=-1), self.holidays)
self.assertNotIn(dt + relativedelta(days=+1), self.holidays)
def test_constitution_day(self):
for dt in [date(2005, 2, 5), date(2006, 2, 5), date(2007, 2, 5),
date(2009, 2, 5),
date(2010, 2, 5),
date(2015, 2, 5),
date(2016, 2, 5),
date(2020, 2, 5),
date(2021, 2, 5),
date(2022, 2, 5)]:
self.assertIn(dt, self.holidays)
self.assertNotIn(dt + relativedelta(days=-1), self.holidays)
self.assertNotIn(dt + relativedelta(days=+1), self.holidays)
self.holidays.observed = True
for dt in [date(2005, 2, 5), date(2006, 2, 5), date(2007, 2, 5),
date(2009, 2, 5), date(2009, 2, 2),
date(2010, 2, 5), date(2010, 2, 1),
date(2015, 2, 5), date(2015, 2, 2),