# HG changeset patch # User Søren Løvborg # Date 1435768108 -7200 # Node ID 9b2c5e8b37ea862b2e77af4fb39d8249fea12243 # Parent 03dd2a5776400d58670071530910f7526d13d259 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 diff -r 03dd2a577640 -r 9b2c5e8b37ea kallithea/tests/__init__.py --- 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): diff -r 03dd2a577640 -r 9b2c5e8b37ea kallithea/tests/functional/test_admin_notifications.py --- 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() diff -r 03dd2a577640 -r 9b2c5e8b37ea kallithea/tests/functional/test_changeset_comments.py --- 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() diff -r 03dd2a577640 -r 9b2c5e8b37ea kallithea/tests/functional/test_login.py --- 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): diff -r 03dd2a577640 -r 9b2c5e8b37ea kallithea/tests/models/test_notifications.py --- 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])