Commits

Tetsuya Morimoto committed f74973d

added WeeklyScheduler component tests

  • Participants
  • Parent commits 6bc9a73

Comments (0)

Files changed (3)

File src/traccron/scheduler.py

 from traccron.api import ISchedulerType
 from traccron.core import CronConfig
 
+
 class SchedulerType(ISchedulerType):
     """
     Define a sort of scheduling. Base class for any scheduler type
         SchedulerType.__init__(self)
 
     def getId(self):
-        return "weekly"
+        return 'weekly'
 
     def getHint(self):
-        return "ex: 0@12h00 fire every monday at 12h00"
+        return 'ex: 0@12h00 fire every monday at 12h00'
 
     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_wday) + "@" + str(currentTime.tm_hour) + "h" + str(currentTime.tm_min)
-        else:
-            return False
+        self._output_comp_debug_log(currentTime, schedule_value)
+        return schedule_value == '%s@%sh%s' % (currentTime.tm_wday,
+                                               currentTime.tm_hour,
+                                               currentTime.tm_min)
 
 
 class MonthlyScheduler(Component, SchedulerType):

File tests/test_weekly_scheduler.py

+# -*- coding: utf-8 -*-
+from time import localtime, time
+
+import pytest
+from utils import compat_attrgetter
+
+time_now = time()
+local_now = localtime(time_now)
+local_now_plus_1min = localtime(time_now + 60)
+local_now_plus_1hour = localtime(time_now + 3600)
+local_now_plus_1day = localtime(time_now + 86400)
+local_now_plus_7day = localtime(time_now + 604800)
+
+_getter = compat_attrgetter('tm_wday', 'tm_hour', 'tm_min')
+sch_value_now = '%s@%sh%s' % _getter(local_now)
+sch_value_now_plus_1min = '%s@%sh%s' % _getter(local_now_plus_1min)
+sch_value_now_plus_1hour = '%s@%sh%s' % _getter(local_now_plus_1hour)
+sch_value_now_plus_1day = '%s@%sh%s' % _getter(local_now_plus_1day)
+sch_value_now_plus_7day = '%s@%sh%s' % _getter(local_now_plus_7day)
+
+
+def pytest_funcarg__weekly_scheduler(request, component):
+    weekly_scheduler = component['weekly_scheduler']
+    return weekly_scheduler
+
+
+def test_weekly_scheduler_basic(weekly_scheduler):
+    assert 'weekly' == weekly_scheduler.getId()
+
+
+@pytest.mark.parametrize(('cur_time', 'sch_value', 'expected'), [
+    (local_now, sch_value_now, True),
+    (local_now, sch_value_now_plus_1min, False),
+    (local_now, sch_value_now_plus_1hour, False),
+    (local_now, sch_value_now_plus_1day, False),
+    (local_now, sch_value_now_plus_7day, True),
+    (local_now, None, False),
+    (local_now, '', False),
+])
+def test_weekly_scheduler_compareTime(weekly_scheduler,
+                                      cur_time, sch_value, expected):
+    assert expected is weekly_scheduler.compareTime(cur_time, sch_value)

File tests/utils.py

 # -*- coding: utf-8 -*-
+import sys
 from itertools import ifilterfalse
 
 
         for msg, is_logged in log_status.items():
             assert is_logged, 'unlogged message: "%s"' % msg
     return True
+
+
+def compat_attrgetter(*args):
+    from operator import attrgetter
+    if sys.version_info[:2] == (2, 4):
+        return lambda obj: tuple(attrgetter(attr)(obj) for attr in args)
+    else:  # version >= 2.5
+        return attrgetter(*args)