diff --git a/kallithea/tests/__init__.py b/kallithea/tests/__init__.py --- a/kallithea/tests/__init__.py +++ b/kallithea/tests/__init__.py @@ -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): diff --git a/kallithea/tests/functional/test_admin_notifications.py b/kallithea/tests/functional/test_admin_notifications.py --- a/kallithea/tests/functional/test_admin_notifications.py +++ b/kallithea/tests/functional/test_admin_notifications.py @@ -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() diff --git a/kallithea/tests/functional/test_changeset_comments.py b/kallithea/tests/functional/test_changeset_comments.py --- a/kallithea/tests/functional/test_changeset_comments.py +++ b/kallithea/tests/functional/test_changeset_comments.py @@ -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() diff --git a/kallithea/tests/functional/test_login.py b/kallithea/tests/functional/test_login.py --- a/kallithea/tests/functional/test_login.py +++ b/kallithea/tests/functional/test_login.py @@ -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): diff --git a/kallithea/tests/models/test_notifications.py b/kallithea/tests/models/test_notifications.py --- a/kallithea/tests/models/test_notifications.py +++ b/kallithea/tests/models/test_notifications.py @@ -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])