Commits

Tetsuya Morimoto  committed 37b2c20

added AutoPostponeTask component tests

  • Participants
  • Parent commits e55c40a

Comments (0)

Files changed (2)

File src/traccron/task.py

     Scan closed milestone for still opened ticket then posptone those tickets
     to the next milestone
     """
+    implements(ICronTask)
 
-    implements(ICronTask)
+    select_next_milestone = """
+        SELECT m.name, m.completed, m.due
+        FROM milestone m
+        WHERE m.completed is NULL or m.completed = 0
+        AND m.due not NULL and m.due > 0
+        ORDER BY m.due ASC LIMIT 1
+    """
+
+    select_closed_milestone_ticket = """
+        SELECT t.id , t.milestone
+        FROM ticket t, milestone m
+        WHERE t.status != 'closed'
+        AND t.milestone = m.name
+        AND m.completed not NULL and m.completed > 0
+    """
 
     def wake_up(self, *args):
         db = self.env.get_db_cnx()
         cursor = db.cursor()
         # find still opened more recent milestone
         # select ticket whom milestone are due in less than specified delay
-        cursor.execute("""
-            SELECT m.name  FROM milestone m
-            WHERE  m.completed is NULL or m.completed = 0
-            AND m.due not NULL and m.due > 0
-            ORDER BY m.due ASC LIMIT 1
-        """)
         next_milestone = None
-        for name, in cursor:
+        cursor.execute(self.select_next_milestone)
+        for name, completed, due in cursor:
             next_milestone = name
 
+        if not next_milestone:
+            msg = 'No opened milestone found. Cannot postpone tickets'
+            self.env.log.debug(msg)
+            return
+
         # select ticket whom milestone are due in less than specified delay
-        cursor.execute("""
-            SELECT t.id , t.milestone  FROM ticket t, milestone m
-            WHERE t.status != 'closed'
-            AND    t.milestone = m.name
-            AND    m.completed not NULL and m.completed > 0
-        """)
-        if next_milestone:
-            for id, milestone in cursor:
-                mess = "ticket %s is opened in closed milestone %s. Should postpone this ticket to %s" % (id, milestone, next_milestone)
-                self.env.log.debug(mess)
-                ticket = Ticket(self.env, id)
-                ticket.populate({'milestone': next_milestone})
-                ticket.save_changes(self.getId(), mess)
-        else:
-            self.env.log.debug("No opened milestone found. Cannot postpone tickets")
+        cursor.execute(self.select_closed_milestone_ticket)
+        for ticket_id, milestone in cursor:
+            msg = 'Ticket %s is opened in closed milestone %s. ' \
+                  'Should postpone this ticket to %s' % (
+                  ticket_id, milestone, next_milestone)
+            self.env.log.debug(msg)
+            ticket = Ticket(self.env, ticket_id)
+            ticket.populate({'milestone': next_milestone})
+            ticket.save_changes(self.getId(), msg)
 
     def getId(self):
-        return "auto_postpone"
+        return 'auto_postpone'
 
     def getDescription(self):
         return self.__doc__

File tests/test_auto_postpone_task.py

+# -*- coding: utf-8 -*-
+import logging
+from datetime import datetime, timedelta
+
+import pytest
+from utils import create_milestone_ticket, get_or_create_milestone
+from utils import has_log_message
+
+from trac.util.datefmt import utc
+
+
+def pytest_funcarg__auto_postpone_task(request, component):
+    auto_postpone_task = component['auto_postpone_task']
+    return auto_postpone_task
+
+
+def test_auto_postpone_task_getId(auto_postpone_task):
+    assert 'auto_postpone' == auto_postpone_task.getId()
+
+
+def test_auto_postpone_task_wake_up_no_milestone(auto_postpone_task, caplog):
+    env = auto_postpone_task.env
+    caplog.setLevel(logging.DEBUG, logger=env.log.name)
+
+    # create milestone
+    m1 = get_or_create_milestone(env)
+    auto_postpone_task.wake_up()
+    expected_messages = [
+        'applying config',
+        'stop existing ticker',
+        'ticker is disabled',
+        'action controllers for ticket workflow',
+        'Creating new milestone',
+        'No opened milestone found. Cannot postpone tickets',
+    ]
+    assert has_log_message(caplog, expected_messages)
+
+
+def test_auto_postpone_task_wake_up(auto_postpone_task, caplog):
+    env = auto_postpone_task.env
+    caplog.setLevel(logging.DEBUG, logger=env.log.name)
+
+    # create milestone & ticket
+    m1, t1 = create_milestone_ticket(env, 'ms1', 0, 'owner1', 'reporter1')
+    t1['status'] = 'new'
+    t1.save_changes()
+    m1.completed = datetime.now(utc)
+    m1.update()
+
+    duedate_plus_5days = datetime.now(utc) + timedelta(days=5)
+    m2 = get_or_create_milestone(env, 'ms2', duedate_plus_5days)
+    auto_postpone_task.wake_up()
+    expected_messages = [
+        'applying config',
+        'stop existing ticker',
+        'ticker is disabled',
+        'action controllers for ticket workflow',
+        'Creating new milestone',
+        'Updating milestone',
+        'Ticket',
+    ]
+    assert has_log_message(caplog, expected_messages)