Commits

Tetsuya Morimoto committed 6bc9a73

added HourlyScheduler component tests

  • Participants
  • Parent commits 0679eb1

Comments (0)

Files changed (2)

src/traccron/scheduler.py

 
     def __init__(self):
         self.cronconf = CronConfig(self.env)
-        self._comp_debug = 'compareTime: %s, %s, %s'
 
     def getId(self):
         """
     def _get_task_schedule_value_list(self, task):
         return self.cronconf.get_schedule_value_list(task, self)
 
+    def _output_comp_debug_log(self, currentTime, schedule_value):
+        self.env.log.debug('compareTime: %s, %s, %s' % (
+                           self.getId(), currentTime, schedule_value))
+
 
 class DailyScheduler(Component, SchedulerType):
     """
         return 'ex: 8h30 fire every day at 8h30'
 
     def compareTime(self, currentTime, schedule_value):
-        msg = self._comp_debug % (self.getId(), currentTime, schedule_value)
-        self.env.log.debug(msg)
+        self._output_comp_debug_log(currentTime, schedule_value)
         return schedule_value == '%sh%s' % currentTime[3:5]
 
 
         SchedulerType.__init__(self)
 
     def getId(self):
-        return "hourly"
+        return 'hourly'
 
     def getHint(self):
-        return "ex: 45 fire every hour at 0h45 then 1h45 and so on"
+        return 'ex: 45 fire every hour at 0h45 then 1h45 and so on'
 
     def compareTime(self, currentTime, schedule_value):
-        # compare value with current
-        if schedule_value:
-            self.env.log.debug(self.getId() + " compare currentTime=" + str(currentTime) + " with schedule_value " + schedule_value)
-        else:
-            self.env.log.debug(self.getId() + " compare currentTime=" + str(currentTime) + " with NO schedule_value ")
-        if schedule_value:
-            return schedule_value == str(currentTime.tm_min)
-        else:
-            return False
+        self._output_comp_debug_log(currentTime, schedule_value)
+        return schedule_value == str(currentTime.tm_min)
 
 
 class WeeklyScheduler(Component, SchedulerType):

tests/test_hourly_scheduler.py

+# -*- coding: utf-8 -*-
+from time import localtime, time
+
+import pytest
+
+time_now = time()
+local_now = localtime(time_now)
+local_now_plus_1min = localtime(time_now + 60)
+local_now_plus_1hour = localtime(time_now + 3600)
+
+
+def pytest_funcarg__hourly_scheduler(request, component):
+    hourly_scheduler = component['hourly_scheduler']
+    return hourly_scheduler
+
+
+def test_hourly_scheduler_basic(hourly_scheduler):
+    assert 'hourly' == hourly_scheduler.getId()
+
+
+@pytest.mark.parametrize(('cur_time', 'sch_value', 'expected'), [
+    (local_now, '%s' % local_now[4], True),
+    (local_now, '%s' % local_now_plus_1min[4], False),
+    (local_now, '%s' % local_now_plus_1hour[4], True),
+    (local_now, None, False),
+    (local_now, '', False),
+])
+def test_hourly_scheduler_compareTime(hourly_scheduler,
+                                      cur_time, sch_value, expected):
+    assert expected is hourly_scheduler.compareTime(cur_time, sch_value)