~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/bugs/scripts/tests/test_bugnotification.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-08-17 19:51:38 UTC
  • mfrom: (13627.2.15 bug-813322-2)
  • Revision ID: launchpad@pqm.canonical.com-20110817195138-yox3nrvqtq27gpb5
[r=stevenk][bug=813322] Defer duplicate bug recipient list
        calculation until processing by the cron job.

Show diffs side-by-side

added added

removed removed

Lines of Context:
66
66
    get_activity_key,
67
67
    notification_batches,
68
68
    notification_comment_batches,
 
69
    process_deferred_notifications,
69
70
    )
70
71
from lp.registry.interfaces.person import IPersonSet
71
72
from lp.registry.interfaces.product import IProductSet
600
601
        commit()
601
602
        login('test@canonical.com')
602
603
        self.layer.switchDbUser(config.malone.bugnotification_dbuser)
603
 
        self.now = datetime.now(pytz.timezone('UTC'))
 
604
        self.now = datetime.now(pytz.UTC)
604
605
        self.ten_minutes_ago = self.now - timedelta(minutes=10)
605
606
        self.notification_set = getUtility(IBugNotificationSet)
606
607
        for notification in self.notification_set.getNotificationsToSend():
1190
1191
        for name in names:
1191
1192
            template = get_email_template(name, 'bugs')
1192
1193
            self.assertTrue(re.search('^-- $', template, re.MULTILINE))
 
1194
 
 
1195
 
 
1196
class TestDeferredNotifications(TestCaseWithFactory):
 
1197
 
 
1198
    layer = LaunchpadZopelessLayer
 
1199
 
 
1200
    def setUp(self):
 
1201
        super(TestDeferredNotifications, self).setUp()
 
1202
        self.notification_set = getUtility(IBugNotificationSet)
 
1203
        # Ensure there are no outstanding notifications.
 
1204
        for notification in self.notification_set.getNotificationsToSend():
 
1205
            notification.destroySelf()
 
1206
        self.ten_minutes_ago = datetime.now(pytz.UTC) - timedelta(minutes=10)
 
1207
 
 
1208
    def _make_deferred_notification(self):
 
1209
        bug = self.factory.makeBug()
 
1210
        empty_recipients = BugNotificationRecipients()
 
1211
        message = getUtility(IMessageSet).fromText(
 
1212
            'subject', 'a comment.', bug.owner,
 
1213
            datecreated=self.ten_minutes_ago)
 
1214
        self.notification_set.addNotification(
 
1215
            bug, False, message, empty_recipients, None, deferred=True)
 
1216
 
 
1217
    def test_deferred_notifications(self):
 
1218
        # Create some deferred notifications and show that processing them
 
1219
        # puts then in the state where they are ready to send.
 
1220
        num = 5
 
1221
        for i in xrange(num):
 
1222
            self._make_deferred_notification()
 
1223
        deferred = self.notification_set.getDeferredNotifications()
 
1224
        self.assertEqual(num, deferred.count())
 
1225
        process_deferred_notifications(deferred)
 
1226
        # Now that are all in the PENDING state.
 
1227
        ready_to_send = self.notification_set.getNotificationsToSend()
 
1228
        self.assertEqual(num, len(ready_to_send))
 
1229
        # And there are no longer any deferred.
 
1230
        deferred = self.notification_set.getDeferredNotifications()
 
1231
        self.assertEqual(0, deferred.count())