Browse Source

#453: deleteoldnotifications test

Rafał Pitoń 10 years ago
parent
commit
efebf3870e

+ 1 - 1
misago/notifications/management/commands/deleteoldnotifications.py

@@ -15,4 +15,4 @@ class Command(BaseCommand):
         cutoff_date = timezone.now() - cutoff
 
         Notification.objects.filter(date__lte=cutoff_date).delete()
-        self.stdout.write('Old notifications have been deleted.')
+        self.stdout.write('Old notifications have been deleted')

+ 45 - 0
misago/notifications/tests/test_deleteoldnotifications.py

@@ -0,0 +1,45 @@
+from datetime import timedelta
+
+from django.conf import settings
+from django.contrib.auth import get_user_model
+from django.test import TestCase
+from django.utils import timezone
+from django.utils.six import StringIO
+
+from misago.notifications import notify_user
+from misago.notifications.management.commands import deleteoldnotifications
+
+
+class DeleteOldNotificatinsTests(TestCase):
+    def test_regen_blank_avatar(self):
+        """command deletes old notifications"""
+        # create user
+        User = get_user_model()
+        user = User.objects.create_user("Bob", "bob@boberson.com", "Pass.123")
+
+        # notify him
+        notify_user(user, "Hello Bob!", "/")
+
+        # run command
+        command = deleteoldnotifications.Command()
+
+        out = StringIO()
+        command.execute(stdout=out)
+        command_output = out.getvalue().splitlines()[0].strip()
+
+        self.assertEqual(command_output, 'Old notifications have been deleted')
+        self.assertEqual(user.misago_notifications.count(), 1)
+
+        # outdate notifications
+        cutoff = timedelta(days=settings.MISAGO_NOTIFICATIONS_MAX_AGE * 2)
+        cutoff_date = timezone.now() - cutoff
+
+        user.misago_notifications.update(date=cutoff_date)
+
+        # run command again
+        out = StringIO()
+        command.execute(stdout=out)
+        command_output = out.getvalue().splitlines()[0].strip()
+
+        self.assertEqual(command_output, 'Old notifications have been deleted')
+        self.assertEqual(user.misago_notifications.count(), 0)