Commits

Tetsuya Morimoto committed e55c40a

refactored UnreachableMilestoneTask component tests

Comments (0)

Files changed (2)

src/traccron/task.py

     """
     implements(ICronTask, ITemplateProvider)
 
+    ahead_days = 3
+
     select_near_milestone_ticket = """
         SELECT t.id , t.owner, t.reporter, t.milestone, t.summary, m.due
         FROM ticket t, milestone m
                 notifier.notify_opened_ticket(recipient, _dico[milestone])
 
     def wake_up(self, *args):
-        delay = 3
+        ahead = UnreachableMilestoneTask.ahead_days
         if len(args) > 0:
-            delay = int(args[0])
+            ahead = int(args[0])
 
         # look opened ticket in near milestone
         db = self.env.get_db_cnx()
         cursor = db.cursor()
-        # select ticket whom milestone are due in less than specified delay
-        delay_time = datetime.now(utc) + timedelta(days=delay)
+        # select ticket whom milestone are due in less than some days ahead
+        ahead_time = datetime.now(utc) + timedelta(days=ahead)
         cursor.execute(self.select_near_milestone_ticket,
-                       (to_utimestamp(delay_time),))
+                       (to_utimestamp(ahead_time),))
 
         dico, dico_owner, dico_reporter = {}, {}, {}
         for ticket, owner, reporter, milestone, summary, due in cursor:

tests/test_unreachable_milestone_task.py

         'notify opened ticket: repo2, ms1, ReporterOpenedTicketNotification',
     ]
     assert has_log_message(caplog, expected_messages)
+
+
+@pytest.mark.parametrize(('args', 'add_days'), [
+    ((), 5),
+    ((), 2),
+    ((2,), 0),
+    ((7, 'test'), 5),
+    ((7, 'test'), 10),
+])
+def test_unreachable_milestone_task_wake_up_args(unreachable_milestone_task,
+                                                 caplog, args, add_days):
+
+    from traccron.task import UnreachableMilestoneTask
+    env = unreachable_milestone_task.env
+    caplog.setLevel(logging.DEBUG, logger=env.log.name)
+
+    # create tickets
+    duedate = datetime.now(utc) + timedelta(days=add_days)
+    create_milestone_ticket(env, 'ms1', duedate, 'owner1', 'repo1')
+
+    unreachable_milestone_task.wake_up(*args)
+    expected_messages = [
+        'applying config',
+        'stop existing ticker',
+        'ticker is disabled',
+        'action controllers for ticket workflow',
+        'Creating new milestone',
+    ]
+
+    if not args:
+        args = (UnreachableMilestoneTask.ahead_days,)
+
+    if add_days <= args[0]:
+        expected_messages.extend([
+            'warning ticket',
+            'notify unreachable milestone',
+            'notify opened ticket',
+        ])
+    assert has_log_message(caplog, expected_messages)