~launchpad-pqm/launchpad/devel

« back to all changes in this revision

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

[r=bac][ui=none][bug=621140] Refactor get_email_notifications().

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
from canonical.launchpad.database import BugTask
19
19
from canonical.launchpad.helpers import get_contact_email_addresses
20
20
from canonical.launchpad.interfaces.message import IMessageSet
 
21
from canonical.testing import LaunchpadZopelessLayer
 
22
 
21
23
from lp.bugs.interfaces.bug import IBug, IBugSet
 
24
from lp.bugs.mail.bugnotificationrecipients import BugNotificationRecipients
 
25
from lp.bugs.scripts.bugnotification import (
 
26
    get_email_notifications, notification_batches,
 
27
    notification_comment_batches)
22
28
from lp.registry.interfaces.person import IPersonSet
23
29
from lp.registry.interfaces.product import IProductSet
24
 
from lp.bugs.mail.bugnotificationrecipients import BugNotificationRecipients
25
 
from lp.bugs.scripts.bugnotification import (
26
 
    get_email_notifications)
27
 
from canonical.testing import LaunchpadZopelessLayer
28
30
 
29
31
 
30
32
class MockBug:
94
96
    Using a real BugNotification won't allow us to set the bug to a mock
95
97
    object.
96
98
    """
 
99
 
97
100
    def __init__(self, message, bug, is_comment, date_emailed):
98
101
        self.message = message
99
102
        self.bug = bug
164
167
        also checks that the notifications got sent to the correct
165
168
        addresses.
166
169
        """
167
 
        email_notifications = get_email_notifications(
168
 
            notifications_to_send, date_emailed=self.now)
 
170
        email_notifications = get_email_notifications(notifications_to_send)
169
171
        to_addresses = set()
170
172
        sent_notifications = []
171
173
        for notifications, messages in email_notifications:
194
196
            notifications_to_send)
195
197
        self.assertEqual(sent_notifications, notifications_to_send)
196
198
 
197
 
 
198
199
    def test_catch_simple_exception_in_the_middle(self):
199
200
        # Make sure that the first and last notifications are sent even
200
201
        # if the middle one causes an exception to be raised.
248
249
        self.assertEqual(bug_four.id, 4)
249
250
 
250
251
 
 
252
class FakeNotification:
 
253
    """Used by TestNotificationCommentBatches and TestNotificationBatches."""
 
254
 
 
255
    class Message(object):
 
256
        pass
 
257
 
 
258
    def __init__(self, is_comment=False, bug=None, owner=None):
 
259
        self.is_comment = is_comment
 
260
        self.bug = bug
 
261
        self.message = self.Message()
 
262
        self.message.owner = owner
 
263
 
 
264
 
 
265
class TestNotificationCommentBatches(unittest.TestCase):
 
266
    """Tests of `notification_comment_batches`."""
 
267
 
 
268
    def test_with_nothing(self):
 
269
        # Nothing is generated if an empty list is passed in.
 
270
        self.assertEquals([], list(notification_comment_batches([])))
 
271
 
 
272
    def test_with_one_non_comment_notification(self):
 
273
        # Given a single non-comment notification, a single tuple is
 
274
        # generated.
 
275
        notification = FakeNotification(False)
 
276
        self.assertEquals(
 
277
            [(1, notification)],
 
278
            list(notification_comment_batches([notification])))
 
279
 
 
280
    def test_with_one_comment_notification(self):
 
281
        # Given a single comment notification, a single tuple is generated.
 
282
        notification = FakeNotification(True)
 
283
        self.assertEquals(
 
284
            [(1, notification)],
 
285
            list(notification_comment_batches([notification])))
 
286
 
 
287
    def test_with_two_notifications_comment_first(self):
 
288
        # Given two notifications, one a comment, one not, and the comment
 
289
        # first, two tuples are generated, both in the same group.
 
290
        notification1 = FakeNotification(True)
 
291
        notification2 = FakeNotification(False)
 
292
        notifications = [notification1, notification2]
 
293
        self.assertEquals(
 
294
            [(1, notification1), (1, notification2)],
 
295
            list(notification_comment_batches(notifications)))
 
296
 
 
297
    def test_with_two_notifications_comment_last(self):
 
298
        # Given two notifications, one a comment, one not, and the comment
 
299
        # last, two tuples are generated, both in the same group.
 
300
        notification1 = FakeNotification(False)
 
301
        notification2 = FakeNotification(True)
 
302
        notifications = [notification1, notification2]
 
303
        self.assertEquals(
 
304
            [(1, notification1), (1, notification2)],
 
305
            list(notification_comment_batches(notifications)))
 
306
 
 
307
    def test_with_three_notifications_comment_in_middle(self):
 
308
        # Given three notifications, one a comment, two not, and the comment
 
309
        # in the middle, three tuples are generated, all in the same group.
 
310
        notification1 = FakeNotification(False)
 
311
        notification2 = FakeNotification(True)
 
312
        notification3 = FakeNotification(False)
 
313
        notifications = [notification1, notification2, notification3]
 
314
        self.assertEquals(
 
315
            [(1, notification1), (1, notification2), (1, notification3)],
 
316
            list(notification_comment_batches(notifications)))
 
317
 
 
318
    def test_with_more_notifications(self):
 
319
        # Given four notifications - non-comment, comment, non-comment,
 
320
        # comment - four tuples are generated. The first three notifications
 
321
        # are in the first group, the last notification is in a group on its
 
322
        # own.
 
323
        notification1 = FakeNotification(False)
 
324
        notification2 = FakeNotification(True)
 
325
        notification3 = FakeNotification(False)
 
326
        notification4 = FakeNotification(True)
 
327
        notifications = [
 
328
            notification1, notification2,
 
329
            notification3, notification4,
 
330
            ]
 
331
        self.assertEquals(
 
332
            [(1, notification1), (1, notification2),
 
333
             (1, notification3), (2, notification4)],
 
334
            list(notification_comment_batches(notifications)))
 
335
 
 
336
 
 
337
class TestNotificationBatches(unittest.TestCase):
 
338
    """Tests of `notification_batches`."""
 
339
 
 
340
    def test_with_nothing(self):
 
341
        # Nothing is generated if an empty list is passed in.
 
342
        self.assertEquals([], list(notification_batches([])))
 
343
 
 
344
    def test_with_one_non_comment_notification(self):
 
345
        # Given a single non-comment notification, a single batch is
 
346
        # generated.
 
347
        notification = FakeNotification(False)
 
348
        self.assertEquals(
 
349
            [[notification]],
 
350
            list(notification_batches([notification])))
 
351
 
 
352
    def test_with_one_comment_notification(self):
 
353
        # Given a single comment notification, a single batch is generated.
 
354
        notification = FakeNotification(True)
 
355
        self.assertEquals(
 
356
            [[notification]],
 
357
            list(notification_batches([notification])))
 
358
 
 
359
    def test_with_two_notifications_comment_first(self):
 
360
        # Given two similar notifications, one a comment, one not, and the
 
361
        # comment first, a single batch is generated.
 
362
        notification1 = FakeNotification(True)
 
363
        notification2 = FakeNotification(False)
 
364
        notifications = [notification1, notification2]
 
365
        self.assertEquals(
 
366
            [[notification1, notification2]],
 
367
            list(notification_batches(notifications)))
 
368
 
 
369
    def test_with_two_notifications_comment_last(self):
 
370
        # Given two similar notifications, one a comment, one not, and the
 
371
        # comment last, a single batch is generated.
 
372
        notification1 = FakeNotification(False)
 
373
        notification2 = FakeNotification(True)
 
374
        notifications = [notification1, notification2]
 
375
        self.assertEquals(
 
376
            [[notification1, notification2]],
 
377
            list(notification_batches(notifications)))
 
378
 
 
379
    def test_with_three_notifications_comment_in_middle(self):
 
380
        # Given three similar notifications, one a comment, two not, and the
 
381
        # comment in the middle, one batch is generated.
 
382
        notification1 = FakeNotification(False)
 
383
        notification2 = FakeNotification(True)
 
384
        notification3 = FakeNotification(False)
 
385
        notifications = [notification1, notification2, notification3]
 
386
        self.assertEquals(
 
387
            [[notification1, notification2, notification3]],
 
388
            list(notification_batches(notifications)))
 
389
 
 
390
    def test_with_more_notifications(self):
 
391
        # Given four similar notifications - non-comment, comment,
 
392
        # non-comment, comment - two batches are generated. The first three
 
393
        # notifications are in the first batch.
 
394
        notification1 = FakeNotification(False)
 
395
        notification2 = FakeNotification(True)
 
396
        notification3 = FakeNotification(False)
 
397
        notification4 = FakeNotification(True)
 
398
        notifications = [
 
399
            notification1, notification2,
 
400
            notification3, notification4,
 
401
            ]
 
402
        self.assertEquals(
 
403
            [[notification1, notification2, notification3], [notification4]],
 
404
            list(notification_batches(notifications)))
 
405
 
 
406
    def test_notifications_for_same_bug(self):
 
407
        # Batches are grouped by bug.
 
408
        notifications = [FakeNotification(bug=1) for number in range(5)]
 
409
        observed = list(notification_batches(notifications))
 
410
        self.assertEquals([notifications], observed)
 
411
 
 
412
    def test_notifications_for_different_bugs(self):
 
413
        # Batches are grouped by bug.
 
414
        notifications = [FakeNotification(bug=number) for number in range(5)]
 
415
        expected = [[notification] for notification in notifications]
 
416
        observed = list(notification_batches(notifications))
 
417
        self.assertEquals(expected, observed)
 
418
 
 
419
    def test_notifications_for_same_owner(self):
 
420
        # Batches are grouped by owner.
 
421
        notifications = [FakeNotification(owner=1) for number in range(5)]
 
422
        observed = list(notification_batches(notifications))
 
423
        self.assertEquals([notifications], observed)
 
424
 
 
425
    def test_notifications_for_different_owners(self):
 
426
        # Batches are grouped by owner.
 
427
        notifications = [FakeNotification(owner=number) for number in range(5)]
 
428
        expected = [[notification] for notification in notifications]
 
429
        observed = list(notification_batches(notifications))
 
430
        self.assertEquals(expected, observed)
 
431
 
 
432
    def test_notifications_with_mixed_bugs_and_owners(self):
 
433
        # Batches are grouped by bug and owner.
 
434
        notifications = [
 
435
            FakeNotification(bug=1, owner=1),
 
436
            FakeNotification(bug=1, owner=2),
 
437
            FakeNotification(bug=2, owner=2),
 
438
            FakeNotification(bug=2, owner=1),
 
439
            ]
 
440
        expected = [[notification] for notification in notifications]
 
441
        observed = list(notification_batches(notifications))
 
442
        self.assertEquals(expected, observed)
 
443
 
 
444
    def test_notifications_with_mixed_bugs_and_owners_2(self):
 
445
        # Batches are grouped by bug and owner.
 
446
        notifications = [
 
447
            FakeNotification(bug=1, owner=1),
 
448
            FakeNotification(bug=1, owner=1),
 
449
            FakeNotification(bug=2, owner=2),
 
450
            FakeNotification(bug=2, owner=2),
 
451
            ]
 
452
        expected = [notifications[0:2], notifications[2:4]]
 
453
        observed = list(notification_batches(notifications))
 
454
        self.assertEquals(expected, observed)
 
455
 
 
456
    def test_notifications_with_mixed_bugs_owners_and_comments(self):
 
457
        # Batches are grouped by bug, owner and comments.
 
458
        notifications = [
 
459
            FakeNotification(is_comment=False, bug=1, owner=1),
 
460
            FakeNotification(is_comment=False, bug=1, owner=1),
 
461
            FakeNotification(is_comment=True, bug=1, owner=1),
 
462
            FakeNotification(is_comment=False, bug=1, owner=2),
 
463
            ]
 
464
        expected = [notifications[0:3], notifications[3:4]]
 
465
        observed = list(notification_batches(notifications))
 
466
        self.assertEquals(expected, observed)
 
467
 
 
468
 
251
469
def test_suite():
252
470
    return unittest.TestLoader().loadTestsFromName(__name__)