fetch_test.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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/content/2018-12/06/content_5346276.htm",
  15. "http://www.gov.cn/zhengce/content/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/content/2019-03/22/content_5375877.htm"
  22. )
  23. )
  24. ) == [("劳动节", "2019年5月1日至4日放假调休,共4天。4月28日(星期日)、5月5日(星期日)上班。")]
  25. def _normalize(iterable):
  26. return sorted(
  27. json.loads(json.dumps(list(iterable), cls=CustomJSONEncoder)),
  28. key=lambda x: x["date"],
  29. )
  30. def _description_parsing_cases():
  31. with open(
  32. workspace_path("scripts", "description_parsing_cases.json"),
  33. "r",
  34. encoding="utf-8",
  35. ) as f:
  36. return json.load(f)
  37. @pytest.mark.parametrize("case", _description_parsing_cases())
  38. def test_parse_description(case):
  39. year, description, expected = case["year"], case["description"], case["expected"]
  40. assert _normalize(DescriptionParser(description, year).parse()) == _normalize(
  41. expected
  42. ), case