test_time_every.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. from datetime import timedelta, datetime
  2. from pprint import pprint
  3. import pytest
  4. from larigira.timegen_every import FrequencyAlarm, SingleAlarm
  5. from larigira.timegen import timegenerate
  6. def eq_(a, b, reason=None):
  7. '''migrating tests from nose'''
  8. if reason is not None:
  9. assert a == b, reason
  10. else:
  11. assert a == b
  12. @pytest.fixture
  13. def now():
  14. return datetime.now()
  15. @pytest.fixture(params=['seconds', 'human', 'humanlong', 'coloned'])
  16. def onehour(now, request):
  17. '''a FrequencyAlarm: every hour for one day'''
  18. intervals = dict(seconds=3600, human='1h', humanlong='30m 1800s',
  19. coloned='01:00:00')
  20. return FrequencyAlarm({
  21. 'start': now - timedelta(days=1),
  22. 'interval': intervals[request.param],
  23. 'end': now + days(1)})
  24. @pytest.fixture(params=[1, '1'])
  25. def onehour_monday(request):
  26. weekday = request.param
  27. yield FrequencyAlarm({
  28. 'interval': 3600*12,
  29. 'weekdays': [weekday],
  30. 'start': 0
  31. })
  32. @pytest.fixture(params=[7, '7'])
  33. def onehour_sunday(request):
  34. weekday = request.param
  35. yield FrequencyAlarm({
  36. 'interval': 3600*12,
  37. 'weekdays': [weekday],
  38. 'start': 0
  39. })
  40. @pytest.fixture(params=['seconds', 'human', 'coloned'])
  41. def tenseconds(now, request):
  42. '''a FrequencyAlarm: every 10 seconds for one day'''
  43. intervals = dict(seconds=10, human='10s', coloned='00:10')
  44. return FrequencyAlarm({
  45. 'start': now - timedelta(days=1),
  46. 'interval': intervals[request.param],
  47. 'end': now + days(1)})
  48. def days(n):
  49. return timedelta(days=n)
  50. def test_single_creations(now):
  51. return SingleAlarm({
  52. 'timestamp': now
  53. })
  54. def test_freq_creations(now):
  55. return FrequencyAlarm({
  56. 'start': now - timedelta(days=1),
  57. 'interval': 3600,
  58. 'end': now})
  59. @pytest.mark.timeout(1)
  60. def test_single_ring(now):
  61. dt = now + days(1)
  62. s = SingleAlarm({'timestamp': dt})
  63. eq_(s.next_ring(), dt)
  64. eq_(s.next_ring(now), dt)
  65. assert s.next_ring(dt) is None, "%s - %s" % (str(s.next_ring(dt)), str(dt))
  66. assert s.next_ring(now + days(2)) is None
  67. assert s.has_ring(dt)
  68. assert not s.has_ring(now)
  69. assert not s.has_ring(now + days(2))
  70. @pytest.mark.timeout(1)
  71. def test_single_all(now):
  72. dt = now + timedelta(days=1)
  73. s = SingleAlarm({'timestamp': dt})
  74. eq_(list(s.all_rings()), [dt])
  75. eq_(list(s.all_rings(now)), [dt])
  76. eq_(list(s.all_rings(now + days(2))), [])
  77. def test_freq_short(now, tenseconds):
  78. f = tenseconds
  79. assert now in f.all_rings(now - days(3))
  80. assert f.next_ring(now) is not None
  81. assert f.next_ring(now) != now
  82. assert f.next_ring(now) > now
  83. assert now not in f.all_rings(now)
  84. for r in f.all_rings(now):
  85. assert r > now
  86. @pytest.mark.timeout(1)
  87. def test_freq_ring(now, onehour):
  88. f = onehour
  89. assert now in f.all_rings(now - days(3))
  90. assert f.next_ring(now) is not None
  91. assert f.next_ring(now) != now
  92. assert f.next_ring(now) > now
  93. assert now not in f.all_rings(now)
  94. for r in f.all_rings(now):
  95. assert r > now
  96. allr = list(f.all_rings(now))
  97. eq_(len(allr), 24)
  98. eq_(len(tuple(f.all_rings(now + days(2)))), 0)
  99. allr = tuple(f.all_rings(now - days(20)))
  100. eq_(f.next_ring(now - days(20)), now - days(1))
  101. eq_(len(allr), 49, pprint(allr))
  102. def test_weekday_skip(onehour_monday):
  103. t = datetime.fromtimestamp(0)
  104. for _ in range(20): # 20 is an arbitrary number
  105. t = onehour_monday.next_ring(t)
  106. assert t.isoweekday() == 1 # monday; don't get confused by .weekday()
  107. def test_weekday_skip_2(onehour_sunday):
  108. t = datetime.fromtimestamp(0)
  109. for _ in range(20): # 20 is an arbitrary number
  110. t = onehour_sunday.next_ring(t)
  111. assert t.isoweekday() == 7 # monday; don't get confused by .weekday()
  112. def test_sunday_is_not_0():
  113. with pytest.raises(ValueError) as excinfo:
  114. FrequencyAlarm({
  115. 'interval': 3600*12,
  116. 'weekdays': [0],
  117. 'start': 0
  118. })
  119. assert 'Not a valid weekday:' in excinfo.value.args[0]
  120. def test_single_registered():
  121. timegenerate({
  122. 'kind': 'single',
  123. 'timestamp': 1234567890
  124. })
  125. def test_frequency_registered():
  126. timegenerate({
  127. 'kind': 'frequency',
  128. 'start': 1234567890,
  129. 'interval': 60*15
  130. })