Commit 5195f8b3 by Őry Máté

dashboard: more tests

parent 89325b9c
import unittest import unittest
from mock import patch, MagicMock from mock import patch, MagicMock
from django.contrib.auth.models import User
from ..models import Notification from ..models import Notification
from ..tasks import local_periodic_tasks from ..tasks import local_periodic_tasks
...@@ -9,22 +10,47 @@ from ..tasks import local_periodic_tasks ...@@ -9,22 +10,47 @@ from ..tasks import local_periodic_tasks
@patch.object(local_periodic_tasks, 'send_mail') @patch.object(local_periodic_tasks, 'send_mail')
@patch.object(Notification, 'objects') @patch.object(Notification, 'objects')
class EmailNotificationTestCase(unittest.TestCase): class EmailNotificationTestCase(unittest.TestCase):
def get_fake_notification(self, user=None, **kwargs):
if user is None:
user = MagicMock(spec=User)
user.profile.__unicode__.return_value = "user"
user.email = "mail"
user.profile.email_notifications = True
user.profile.preferred_language = "en"
params = {"to": user, "subject": "subj", "message": "msg",
"status": Notification.STATUS.new}
params.update(kwargs)
return MagicMock(spec=Notification, **params)
def test_not_sending(self, no, sm): def test_not_sending(self, no, sm):
fake = MagicMock(spec=Notification) fake = [self.get_fake_notification()]
fake.to.profile.email_notifications = False fake[0].to.profile.email_notifications = False
no.filter.return_value = [fake] no.filter.return_value = fake
local_periodic_tasks.send_email_notifications() local_periodic_tasks.send_email_notifications()
assert not sm.called assert not sm.called
def test_sending(self, no, sm): def test_sending(self, no, sm):
fake = MagicMock(spec=Notification) fake = [self.get_fake_notification()]
fake.to.profile.__unicode__.return_value = "user" no.filter.return_value = fake
fake.to.email = "mail"
fake.to.profile.email_notifications = True
fake.to.profile.preferred_language = "en"
fake.subject = "subj"
fake.message = "msg"
no.filter.return_value = [fake]
local_periodic_tasks.send_email_notifications() local_periodic_tasks.send_email_notifications()
assert sm.called assert sm.called
assert fake.status == fake.STATUS.delivered assert all(i.status == i.STATUS.delivered for i in fake)
def test_sending_more(self, no, sm):
fake = [self.get_fake_notification(), self.get_fake_notification()]
fake.append(self.get_fake_notification(fake[0].to))
no.filter.return_value = fake
local_periodic_tasks.send_email_notifications()
self.assertEquals(sm.call_count, 2)
assert all(i.status == i.STATUS.delivered for i in fake)
def test_sending_some(self, no, sm):
fake = [self.get_fake_notification(), self.get_fake_notification()]
fake.append(self.get_fake_notification(fake[0].to))
fake[1].to.profile.email_notifications = False
no.filter.return_value = fake
local_periodic_tasks.send_email_notifications()
self.assertEquals(
[i.status == i.STATUS.delivered for i in fake],
[True, False, True])
self.assertEquals(sm.call_count, 1)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment