Merge pull request #166 from satyanash/master

return saved Notification instances from notify_handler, add tests
This commit is contained in:
Zhongyuan 2017-04-18 14:52:23 +08:00 committed by GitHub
commit 3423404c7d
2 changed files with 31 additions and 4 deletions

View file

@ -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

View file

@ -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)