Repository navigation
Expand file tree
/
Copy pathtest_utils.py
More file actions
139 lines (112 loc) · 4.58 KB
/
Copy pathtest_utils.py
File metadata and controls
139 lines (112 loc) · 4.58 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
# 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 sys
import unittest
import warnings
from unittest import mock
import pytest
from holidays import utils
PYTHON_VERSION = (3, 11)
class TestCountryHolidays(unittest.TestCase):
def setUp(self):
self.holidays = utils.country_holidays("US")
def test_country(self):
self.assertEqual(self.holidays.country, "US")
def test_country_single_year(self):
h = utils.country_holidays("US", years=2021)
self.assertEqual(h.years, {2021})
def test_country_years(self):
h = utils.country_holidays("US", years=(2015, 2016))
self.assertEqual(h.years, {2015, 2016})
def test_country_state(self):
h = utils.country_holidays("US", subdiv="NY")
self.assertEqual(h.subdiv, "NY")
def test_country_province(self):
h = utils.country_holidays("AU", subdiv="NT")
self.assertEqual(h.subdiv, "NT")
def test_exceptions(self):
self.assertRaises(
NotImplementedError, lambda: utils.country_holidays("XXXX")
)
self.assertRaises(
NotImplementedError,
lambda: utils.country_holidays("US", subdiv="XXXX"),
)
self.assertRaises(
NotImplementedError,
lambda: utils.country_holidays("US", subdiv="XXXX"),
)
class TestFinancialHolidays(unittest.TestCase):
def setUp(self):
self.holidays = utils.financial_holidays("NYSE")
def test_market(self):
self.assertEqual(self.holidays.market, "NYSE")
def test_market_single_year(self):
h = utils.financial_holidays("NYSE", years=2021)
self.assertEqual(h.years, {2021})
def test_market_years(self):
h = utils.financial_holidays("NYSE", years=(2015, 2016))
self.assertEqual(h.years, {2015, 2016})
def test_exceptions(self):
self.assertRaises(
NotImplementedError, lambda: utils.financial_holidays("XXXX")
)
self.assertRaises(
NotImplementedError,
lambda: utils.financial_holidays("NYSE", subdiv="XXXX"),
)
class TestAllInSameYear(unittest.TestCase):
"""Test that only holidays in the year(s) requested are returned."""
years = set(range(1950, 2051))
@pytest.mark.skipif(
sys.version_info < PYTHON_VERSION,
reason="Run once on the latest Python version only",
)
@mock.patch("pathlib.Path.rglob", return_value=())
def test_all_countries(self, unused_rglob_mock):
"""
Only holidays in the year(s) requested should be returned. This
ensures that we avoid triggering a "RuntimeError: dictionary changed
size during iteration" error.
This is logic test and not a code compatibility test, so for expediency
we only run it once on the latest Python version.
"""
warnings.simplefilter("ignore")
for country in utils.list_supported_countries():
for year in self.years:
for holiday in utils.country_holidays(country, years=year):
self.assertEqual(holiday.year, year)
self.assertEqual(
self.years,
utils.country_holidays(country, years=self.years).years,
)
@pytest.mark.skipif(
sys.version_info < PYTHON_VERSION,
reason="Run once on the latest Python version only",
)
@mock.patch("pathlib.Path.rglob", return_value=())
def test_all_financial(self, unused_rglob_mock):
"""
Only holidays in the year(s) requested should be returned. This
ensures that we avoid triggering a "RuntimeError: dictionary changed
size during iteration" error.
This is logic test and not a code compatibility test, so for expediency
we only run it once on the latest Python version.
"""
warnings.simplefilter("ignore")
for market in utils.list_supported_financial():
for year in self.years:
for holiday in utils.financial_holidays(market, years=year):
self.assertEqual(holiday.year, year)
self.assertEqual(
self.years,
utils.financial_holidays(market, years=self.years).years,
)