From 09d01810fa09c38440190448d8d4d1b8cbf71410 Mon Sep 17 00:00:00 2001 From: Satyajeet Kanetkar Date: Tue, 18 Apr 2017 11:32:38 +0530 Subject: [PATCH] return saved Notification instances from notify_handler, add tests --- notifications/models.py | 5 +++++ notifications/tests/tests.py | 30 ++++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/notifications/models.py b/notifications/models.py index a564f73..780958f 100644 --- a/notifications/models.py +++ b/notifications/models.py @@ -266,6 +266,8 @@ def notify_handler(verb, **kwargs): else: recipients = [recipient] + new_notifications = [] + for recipient in recipients: newnotify = Notification( recipient=recipient, @@ -289,6 +291,9 @@ def notify_handler(verb, **kwargs): newnotify.data = kwargs newnotify.save() + new_notifications.append(newnotify) + + return new_notifications # connect the signal diff --git a/notifications/tests/tests.py b/notifications/tests/tests.py index 6b57db6..cfc6bef 100644 --- a/notifications/tests/tests.py +++ b/notifications/tests/tests.py @@ -21,7 +21,7 @@ import pytz import json from notifications import notify -from notifications.models import Notification +from notifications.models import Notification, notify_handler from notifications.utils import id2slug @@ -55,15 +55,37 @@ class NotificationManagersTest(TestCase): def setUp(self): self.message_count = 10 + self.other_user = User.objects.create(username="other1", password="pwd", email="example@example.com") + self.from_user = User.objects.create(username="from2", password="pwd", email="example@example.com") self.to_user = User.objects.create(username="to2", password="pwd", email="example@example.com") self.to_group = Group.objects.create(name="to2_g") + self.to_group.user_set.add(self.to_user) + self.to_group.user_set.add(self.other_user) + for i in range(self.message_count): notify.send(self.from_user, recipient=self.to_user, verb='commented', action_object=self.from_user) # Send notification to group notify.send(self.from_user, recipient=self.to_group, verb='commented', action_object=self.from_user) - self.message_count += 1 + self.message_count += self.to_group.user_set.count() + + def test_notify_send_return_val(self): + results = notify.send(self.from_user, recipient=self.to_user, verb='commented', action_object=self.from_user) + for r in results: + if r[0] is notify_handler: + self.assertEqual(len(r[1]), 1) + # only check types for now + self.assertEqual(type(r[1][0]), Notification) + + def test_notify_send_return_val_group(self): + results = notify.send(self.from_user, recipient=self.to_group, verb='commented', action_object=self.from_user) + for r in results: + if r[0] is notify_handler: + self.assertEqual(len(r[1]), self.to_group.user_set.count()) + for n in r[1]: + # only check types for now + self.assertEqual(type(n), Notification) def test_unread_manager(self): self.assertEqual(Notification.objects.unread().count(), self.message_count) @@ -84,7 +106,7 @@ class NotificationManagersTest(TestCase): def test_mark_all_as_read_manager(self): self.assertEqual(Notification.objects.unread().count(), self.message_count) Notification.objects.filter(recipient=self.to_user).mark_all_as_read() - self.assertEqual(Notification.objects.unread().count(), 0) + self.assertEqual(self.to_user.notifications.unread().count(), 0) @override_settings(NOTIFICATIONS_SOFT_DELETE=True) def test_mark_all_as_read_manager_with_soft_delete(self): @@ -100,7 +122,7 @@ class NotificationManagersTest(TestCase): def test_mark_all_as_unread_manager(self): self.assertEqual(Notification.objects.unread().count(), self.message_count) Notification.objects.filter(recipient=self.to_user).mark_all_as_read() - self.assertEqual(Notification.objects.unread().count(), 0) + self.assertEqual(self.to_user.notifications.unread().count(), 0) Notification.objects.filter(recipient=self.to_user).mark_all_as_unread() self.assertEqual(Notification.objects.unread().count(), self.message_count)