fetch_test.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. """Test module `fetch_holidays`. """
  2. import json
  3. import pytest
  4. from fetch import (
  5. CustomJSONEncoder,
  6. DescriptionParser,
  7. get_paper,
  8. get_paper_urls,
  9. get_rules,
  10. )
  11. from filetools import workspace_path
  12. def test_get_paper_urls():
  13. assert get_paper_urls(2019) == [
  14. "http://www.gov.cn/zhengce/zhengceku/2018-12/06/content_5346276.htm",
  15. "http://www.gov.cn/zhengce/zhengceku/2019-03/22/content_5375877.htm",
  16. ]
  17. def test_get_rules():
  18. assert list(
  19. get_rules(
  20. get_paper(
  21. "http://www.gov.cn/zhengce/zhengceku/2019-03/22/content_5375877.htm"
  22. )
  23. )
  24. ) == [("劳动节", "2019年5月1日至4日放假调休,共4天。4月28日(星期日)、5月5日(星期日)上班。")]
  25. def test_get_rules_2023():
  26. got = list(
  27. get_rules(
  28. get_paper(
  29. "http://www.gov.cn/zhengce/zhengceku/2022-12/08/content_5730844.htm"
  30. )
  31. )
  32. )
  33. assert got == [
  34. ("元旦", "2022年12月31日至2023年1月2日放假调休,共3天。"),
  35. ("春节", "1月21日至27日放假调休,共7天。1月28日(星期六)、1月29日(星期日)上班。"),
  36. ("清明节", "4月5日放假,共1天。"),
  37. ("劳动节", "4月29日至5月3日放假调休,共5天。4月23日(星期日)、5月6日(星期六)上班。"),
  38. ("端午节", "6月22日至24日放假调休,共3天。6月25日(星期日)上班。"),
  39. ("中秋节、国庆节", "9月29日至10月6日放假调休,共8天。10月7日(星期六)、10月8日(星期日)上班。"),
  40. ]
  41. def _normalize(iterable):
  42. return sorted(
  43. json.loads(json.dumps(list(iterable), cls=CustomJSONEncoder)),
  44. key=lambda x: x["date"],
  45. )
  46. def _description_parsing_cases():
  47. with open(
  48. workspace_path("scripts", "description_parsing_cases.json"),
  49. "r",
  50. encoding="utf-8",
  51. ) as f:
  52. return json.load(f)
  53. @pytest.mark.parametrize("case", _description_parsing_cases())
  54. def test_parse_description(case):
  55. year, description, expected = case["year"], case["description"], case["expected"]
  56. assert _normalize(DescriptionParser(description, year).parse()) == _normalize(
  57. expected
  58. ), case