Changeset - 9b2c5e8b37ea
[Not reviewed]
default
0 5 0
Søren Løvborg - 10 years ago 2015-07-01 18:28:28
kwi@kwi.dk
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
5 files changed with 18 insertions and 51 deletions:
0 comments (0 inline, 0 general)
kallithea/tests/__init__.py
Show inline comments
 
@@ -54,7 +54,8 @@ from nose.plugins.skip import SkipTest
 

	
 
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 @@ class BaseTestCase(unittest.TestCase):
 
        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):
 

	
kallithea/tests/functional/test_admin_notifications.py
Show inline comments
 
@@ -8,12 +8,8 @@ from kallithea.lib import helpers as h
 

	
 

	
 
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()
kallithea/tests/functional/test_changeset_comments.py
Show inline comments
 
@@ -11,18 +11,7 @@ class TestChangeSetCommentsController(Te
 
            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()
kallithea/tests/functional/test_login.py
Show inline comments
 
@@ -15,12 +15,8 @@ fixture = Fixture()
 

	
 

	
 
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):
kallithea/tests/models/test_notifications.py
Show inline comments
 
@@ -34,23 +34,12 @@ class TestNotifications(BaseTestCase):
 

	
 
        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 @@ class TestNotifications(BaseTestCase):
 
                         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 @@ class TestNotifications(BaseTestCase):
 
                         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 @@ class TestNotifications(BaseTestCase):
 
        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 @@ class TestNotifications(BaseTestCase):
 
        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])
0 comments (0 inline, 0 general)