test_time_cron.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import datetime
  2. import pytest
  3. from larigira.timegen_cron import CronAlarm
  4. @pytest.fixture
  5. def a_time():
  6. # 6th of august 2019 at 10:42
  7. return datetime.datetime(2019, 8, 6, 10, 42, 0)
  8. @pytest.fixture(params=("* * * * *", "* * * * * *"))
  9. def valid_cron(request):
  10. return request.param
  11. def CA(fmt, exclude=""):
  12. return CronAlarm(dict(cron_format=fmt, exclude=exclude))
  13. def test_valid_cron_format():
  14. CA("* * * * *")
  15. def test_valid_cron_format_six():
  16. CA("* * * * * *")
  17. def test_valid_cron_format_spaces_left(valid_cron):
  18. """if a format is valid, a format with left spaces is also valid"""
  19. CA(" " + valid_cron)
  20. def test_valid_cron_format_spaces_right(valid_cron):
  21. """if a format is valid, a format with right spaces is also valid"""
  22. CA(valid_cron + " ")
  23. def test_invalid_cron_format_four():
  24. with pytest.raises(ValueError):
  25. CA("* * * *")
  26. def test_never_equal(a_time):
  27. c = CA("* * * * *")
  28. nt = c.next_ring(a_time)
  29. assert nt.minute != a_time.minute
  30. def test_exclude_single(valid_cron):
  31. CA(valid_cron, valid_cron)
  32. def test_exclude_multi_newline(valid_cron):
  33. CA(valid_cron, valid_cron + "\n" + valid_cron)
  34. def test_exclude_multi_list(valid_cron):
  35. CA(valid_cron, [valid_cron, valid_cron])
  36. def test_exclude_works(a_time):
  37. c = CA("* * * * *")
  38. nt = c.next_ring(a_time)
  39. assert nt.day == 6
  40. c = CA("* * * * *", "* * 6 * *")
  41. nt = c.next_ring(a_time)
  42. assert nt is not None
  43. assert nt.day == 7
  44. def test_exclude_fails(a_time):
  45. """exclude fails if every specification in cron_format is excluded"""
  46. c = CA("* * * * *", "* * * * *")
  47. assert c.has_ring(a_time) is False
  48. assert c.next_ring(a_time) is None