changeset 5214:9b2c5e8b37ea

notification tests: delete notifications before (not after) tests Don't clean notifications (and changeset comments) *after* the test, but *before* the test. Other unit tests don't care if they leave notifications in the database, and neither should these. Rather, they should ensure their *own* preconditions before testing. Admittedly, currently only one test leaves a notification in the database, but more could come along at any time (and why worry?): TestPullrequestsController.test_create_with_existing_reviewer
author Søren Løvborg <kwi@kwi.dk>
date Wed, 01 Jul 2015 18:28:28 +0200
parents 03dd2a577640
children 148360f533a4
files kallithea/tests/__init__.py kallithea/tests/functional/test_admin_notifications.py kallithea/tests/functional/test_changeset_comments.py kallithea/tests/functional/test_login.py kallithea/tests/models/test_notifications.py
diffstat 5 files changed, 18 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/tests/__init__.py	Tue Jun 09 14:58:13 2015 +0000
+++ b/kallithea/tests/__init__.py	Wed Jul 01 18:28:28 2015 +0200
@@ -54,7 +54,8 @@
 
 from kallithea.lib.compat import unittest
 from kallithea import is_windows
-from kallithea.model.db import User
+from kallithea.model.db import Notification, User, UserNotification
+from kallithea.model.meta import Session
 from kallithea.tests.parameterized import parameterized
 from kallithea.lib.utils2 import safe_str
 
@@ -183,6 +184,15 @@
         init_stack(self.wsgiapp.config)
         unittest.TestCase.__init__(self, *args, **kwargs)
 
+    def remove_all_notifications(self):
+        Notification.query().delete()
+
+        # Because query().delete() does not (by default) trigger cascades.
+        # http://docs.sqlalchemy.org/en/rel_0_7/orm/collections.html#passive-deletes
+        UserNotification.query().delete()
+
+        Session().commit()
+
 
 class TestController(BaseTestCase):
 
--- a/kallithea/tests/functional/test_admin_notifications.py	Tue Jun 09 14:58:13 2015 +0000
+++ b/kallithea/tests/functional/test_admin_notifications.py	Wed Jul 01 18:28:28 2015 +0200
@@ -8,12 +8,8 @@
 
 
 class TestNotificationsController(TestController):
-
-    def tearDown(self):
-        for n in Notification.query().all():
-            inst = Notification.get(n.notification_id)
-            Session().delete(inst)
-        Session().commit()
+    def setUp(self):
+        self.remove_all_notifications()
 
     def test_index(self):
         self.log_user()
--- a/kallithea/tests/functional/test_changeset_comments.py	Tue Jun 09 14:58:13 2015 +0000
+++ b/kallithea/tests/functional/test_changeset_comments.py	Wed Jul 01 18:28:28 2015 +0200
@@ -11,18 +11,7 @@
             Session().delete(x)
         Session().commit()
 
-        for x in Notification.query().all():
-            Session().delete(x)
-        Session().commit()
-
-    def tearDown(self):
-        for x in ChangesetComment.query().all():
-            Session().delete(x)
-        Session().commit()
-
-        for x in Notification.query().all():
-            Session().delete(x)
-        Session().commit()
+        self.remove_all_notifications()
 
     def test_create(self):
         self.log_user()
--- a/kallithea/tests/functional/test_login.py	Tue Jun 09 14:58:13 2015 +0000
+++ b/kallithea/tests/functional/test_login.py	Wed Jul 01 18:28:28 2015 +0200
@@ -15,12 +15,8 @@
 
 
 class TestLoginController(TestController):
-
-    def tearDown(self):
-        for n in Notification.query().all():
-            Session().delete(n)
-
-        Session().commit()
+    def setUp(self):
+        self.remove_all_notifications()
         self.assertEqual(Notification.query().all(), [])
 
     def test_index(self):
--- a/kallithea/tests/models/test_notifications.py	Tue Jun 09 14:58:13 2015 +0000
+++ b/kallithea/tests/models/test_notifications.py	Wed Jul 01 18:28:28 2015 +0200
@@ -34,23 +34,12 @@
 
         super(TestNotifications, self).__init__(methodName=methodName)
 
-    def _clean_notifications(self):
-        for n in Notification.query().all():
-            Session().delete(n)
-
-        Session().commit()
-        self.assertEqual(Notification.query().all(), [])
-
     def setUp(self):
-        self._clean_notifications()
-
-    def tearDown(self):
-        self._clean_notifications()
-
-    def test_create_notification(self):
+        self.remove_all_notifications()
         self.assertEqual([], Notification.query().all())
         self.assertEqual([], UserNotification.query().all())
 
+    def test_create_notification(self):
         usrs = [self.u1, self.u2]
         notification = NotificationModel().create(created_by=self.u1,
                                            subject=u'subj', body=u'hi there',
@@ -74,9 +63,6 @@
                          set(usrs))
 
     def test_user_notifications(self):
-        self.assertEqual([], Notification.query().all())
-        self.assertEqual([], UserNotification.query().all())
-
         notification1 = NotificationModel().create(created_by=self.u1,
                                             subject=u'subj', body=u'hi there1',
                                             recipients=[self.u3])
@@ -91,9 +77,6 @@
                          sorted([notification2, notification1]))
 
     def test_delete_notifications(self):
-        self.assertEqual([], Notification.query().all())
-        self.assertEqual([], UserNotification.query().all())
-
         notification = NotificationModel().create(created_by=self.u1,
                                            subject=u'title', body=u'hi there3',
                                     recipients=[self.u3, self.u1, self.u2])
@@ -112,10 +95,6 @@
         self.assertEqual(un, [])
 
     def test_delete_association(self):
-
-        self.assertEqual([], Notification.query().all())
-        self.assertEqual([], UserNotification.query().all())
-
         notification = NotificationModel().create(created_by=self.u1,
                                            subject=u'title', body=u'hi there3',
                                     recipients=[self.u3, self.u1, self.u2])
@@ -159,9 +138,6 @@
         self.assertNotEqual(u2notification, None)
 
     def test_notification_counter(self):
-        self.assertEqual([], Notification.query().all())
-        self.assertEqual([], UserNotification.query().all())
-
         NotificationModel().create(created_by=self.u1,
                             subject=u'title', body=u'hi there_delete',
                             recipients=[self.u3, self.u1])