holiday-cn/tests/test_fetch_holidays.py
2019-09-20 19:07:35 +08:00

44 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Test module `fetch_holidays`. """
import json
import sys
from fetch_holidays import CustomJSONEncoder, DescriptionParser, get_paper_urls, get_rules, get_paper
from .filetools import _file_path
def test_get_paper_urls():
assert get_paper_urls(2019) == [
'http://www.gov.cn/zhengce/content/2018-12/06/content_5346276.htm',
'http://www.gov.cn/zhengce/content/2019-03/22/content_5375877.htm',
]
def test_get_rules():
assert(list(get_rules(get_paper('http://www.gov.cn/zhengce/content/2019-03/22/content_5375877.htm')))
== [('劳动节', '2019年5月1日至4日放假调休共4天。4月28日星期日、5月5日星期日上班。')])
def _normalize(iterable):
return sorted(json.loads(json.dumps(list(iterable), cls=CustomJSONEncoder)),
key=lambda x: x['date'])
def _generate_tests():
with open(_file_path('description_parsing_cases.json'), 'r', encoding='utf-8', ) as f:
cases = json.load(f)
def create_test(case):
def _test():
year, description, expected = case['year'], case['description'], case['expected']
assert _normalize(DescriptionParser(
description, year).parse()) == _normalize(expected), case
return _test
for index, case in enumerate(cases, 1):
setattr(sys.modules[__name__],
f'test_description_parser_{index}', create_test(case))
_generate_tests()