changeset 7665:8fbcdfe364d4

tests: make test_forks teardown more stable It would sometimes fail like: kallithea/tests/functional/test_forks.py:35: in teardown_method Session().delete(self.u1) data/env/lib/python2.7/site-packages/sqlalchemy/orm/session.py:1871: in delete self._delete_impl(state, instance, head=True) data/env/lib/python2.7/site-packages/sqlalchemy/orm/session.py:1888: in _delete_impl self.identity_map.add(state) data/env/lib/python2.7/site-packages/sqlalchemy/orm/identity.py:149: in add orm_util.state_str(state), state.key)) E InvalidRequestError: Can't attach instance <User at 0x7f93d2f81a10>; another instance with key (<class 'kallithea.model.db.User'>, (10,), None) is already present in this session.
author Mads Kiilerich <mads@kiilerich.com>
date Thu, 03 Jan 2019 01:03:14 +0100
parents efba2fd4edf0
children 4473f1094d3d
files kallithea/tests/functional/test_forks.py
diffstat 1 files changed, 18 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/tests/functional/test_forks.py	Mon Apr 08 01:16:34 2019 +0200
+++ b/kallithea/tests/functional/test_forks.py	Thu Jan 03 01:03:14 2019 +0100
@@ -27,12 +27,12 @@
     def setup_method(self, method):
         self.username = u'forkuser'
         self.password = u'qweqwe'
-        self.u1 = fixture.create_user(self.username, password=self.password,
-                                      email=u'fork_king@example.com')
+        u1 = fixture.create_user(self.username, password=self.password, email=u'fork_king@example.com')
+        self.u1_id = u1.user_id
         Session().commit()
 
     def teardown_method(self, method):
-        Session().delete(self.u1)
+        fixture.destroy_user(self.u1_id)
         Session().commit()
 
     def test_index(self):
@@ -46,16 +46,21 @@
     def test_no_permissions_to_fork(self):
         usr = self.log_user(TEST_USER_REGULAR_LOGIN,
                             TEST_USER_REGULAR_PASS)['user_id']
-        user_model = UserModel()
-        user_model.revoke_perm(usr, 'hg.fork.repository')
-        user_model.grant_perm(usr, 'hg.fork.none')
-        u = UserModel().get(usr)
-        u.inherit_default_permissions = False
-        Session().commit()
-        # try create a fork
-        repo_name = self.REPO
-        self.app.post(url(controller='forks', action='fork_create',
-                          repo_name=repo_name), {'_authentication_token': self.authentication_token()}, status=403)
+        try:
+            user_model = UserModel()
+            user_model.revoke_perm(usr, 'hg.fork.repository')
+            user_model.grant_perm(usr, 'hg.fork.none')
+            u = UserModel().get(usr)
+            u.inherit_default_permissions = False
+            Session().commit()
+            # try create a fork
+            repo_name = self.REPO
+            self.app.post(url(controller='forks', action='fork_create',
+                              repo_name=repo_name), {'_authentication_token': self.authentication_token()}, status=403)
+        finally:
+            user_model.revoke_perm(usr, 'hg.fork.none')
+            user_model.grant_perm(usr, 'hg.fork.repository')
+            Session().commit()
 
     def test_index_with_fork(self):
         self.log_user()