248
249
self.assertEqual(bug_four.id, 4)
252
class FakeNotification:
253
"""Used by TestNotificationCommentBatches and TestNotificationBatches."""
255
class Message(object):
258
def __init__(self, is_comment=False, bug=None, owner=None):
259
self.is_comment = is_comment
261
self.message = self.Message()
262
self.message.owner = owner
265
class TestNotificationCommentBatches(unittest.TestCase):
266
"""Tests of `notification_comment_batches`."""
268
def test_with_nothing(self):
269
# Nothing is generated if an empty list is passed in.
270
self.assertEquals([], list(notification_comment_batches([])))
272
def test_with_one_non_comment_notification(self):
273
# Given a single non-comment notification, a single tuple is
275
notification = FakeNotification(False)
278
list(notification_comment_batches([notification])))
280
def test_with_one_comment_notification(self):
281
# Given a single comment notification, a single tuple is generated.
282
notification = FakeNotification(True)
285
list(notification_comment_batches([notification])))
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]
294
[(1, notification1), (1, notification2)],
295
list(notification_comment_batches(notifications)))
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]
304
[(1, notification1), (1, notification2)],
305
list(notification_comment_batches(notifications)))
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]
315
[(1, notification1), (1, notification2), (1, notification3)],
316
list(notification_comment_batches(notifications)))
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
323
notification1 = FakeNotification(False)
324
notification2 = FakeNotification(True)
325
notification3 = FakeNotification(False)
326
notification4 = FakeNotification(True)
328
notification1, notification2,
329
notification3, notification4,
332
[(1, notification1), (1, notification2),
333
(1, notification3), (2, notification4)],
334
list(notification_comment_batches(notifications)))
337
class TestNotificationBatches(unittest.TestCase):
338
"""Tests of `notification_batches`."""
340
def test_with_nothing(self):
341
# Nothing is generated if an empty list is passed in.
342
self.assertEquals([], list(notification_batches([])))
344
def test_with_one_non_comment_notification(self):
345
# Given a single non-comment notification, a single batch is
347
notification = FakeNotification(False)
350
list(notification_batches([notification])))
352
def test_with_one_comment_notification(self):
353
# Given a single comment notification, a single batch is generated.
354
notification = FakeNotification(True)
357
list(notification_batches([notification])))
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]
366
[[notification1, notification2]],
367
list(notification_batches(notifications)))
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]
376
[[notification1, notification2]],
377
list(notification_batches(notifications)))
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]
387
[[notification1, notification2, notification3]],
388
list(notification_batches(notifications)))
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)
399
notification1, notification2,
400
notification3, notification4,
403
[[notification1, notification2, notification3], [notification4]],
404
list(notification_batches(notifications)))
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)
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)
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)
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)
432
def test_notifications_with_mixed_bugs_and_owners(self):
433
# Batches are grouped by bug and owner.
435
FakeNotification(bug=1, owner=1),
436
FakeNotification(bug=1, owner=2),
437
FakeNotification(bug=2, owner=2),
438
FakeNotification(bug=2, owner=1),
440
expected = [[notification] for notification in notifications]
441
observed = list(notification_batches(notifications))
442
self.assertEquals(expected, observed)
444
def test_notifications_with_mixed_bugs_and_owners_2(self):
445
# Batches are grouped by bug and owner.
447
FakeNotification(bug=1, owner=1),
448
FakeNotification(bug=1, owner=1),
449
FakeNotification(bug=2, owner=2),
450
FakeNotification(bug=2, owner=2),
452
expected = [notifications[0:2], notifications[2:4]]
453
observed = list(notification_batches(notifications))
454
self.assertEquals(expected, observed)
456
def test_notifications_with_mixed_bugs_owners_and_comments(self):
457
# Batches are grouped by bug, owner and comments.
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),
464
expected = [notifications[0:3], notifications[3:4]]
465
observed = list(notification_batches(notifications))
466
self.assertEquals(expected, observed)
251
469
def test_suite():
252
470
return unittest.TestLoader().loadTestsFromName(__name__)