From bc3653733f643dc8ac2d6efd55ed5331387ec1ea Mon Sep 17 00:00:00 2001
From: Markus Scheidgen <markus.scheidgen@gmail.com>
Date: Sun, 23 Dec 2018 13:51:25 +0100
Subject: [PATCH] Fix repair repository db issues after reset.

---
 nomad/infrastructure.py |  7 ++++---
 tests/conftest.py       | 12 ------------
 tests/test_api.py       |  4 ++--
 3 files changed, 6 insertions(+), 17 deletions(-)

diff --git a/nomad/infrastructure.py b/nomad/infrastructure.py
index 406c996074..02f6499bda 100644
--- a/nomad/infrastructure.py
+++ b/nomad/infrastructure.py
@@ -270,6 +270,7 @@ def repository_db_connection(dbname=None, with_trans=True):
 
 def reset_repository_db():
     """ Drops the existing NOMAD-coe repository postgres schema and creates a new minimal one. """
+    old_repository_db = repository_db
     if repository_db is not None:
         repository_db.expunge_all()
         repository_db.invalidate()
@@ -289,6 +290,6 @@ def reset_repository_db():
             logger.info('(re-)created repository db postgres schema')
 
     # try tp repair existing db connections
-    old_db = repository_db
-    setup_repository_db()
-    old_db.bind = repository_db_conn
+    if old_repository_db is not None:
+        setup_repository_db()
+        old_repository_db.bind = repository_db_conn
diff --git a/tests/conftest.py b/tests/conftest.py
index cfc1e0e57c..1fe866c385 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -117,18 +117,6 @@ def repository_db(monkeysession):
     session.close()
 
 
-@pytest.fixture(scope='function')
-def repair_repository_db():
-    """
-    Binds a new connectino to the existing session repository db.
-    Necessary if tests delete the connection.
-    """
-    yield None
-    olddb = infrastructure.repository_db
-    infrastructure.setup_repository_db()
-    olddb.bind = infrastructure.repository_db_conn
-
-
 @pytest.fixture(scope='session')
 def test_user(repository_db):
     return coe_repo.ensure_test_user(email='sheldon.cooper@nomad-fairdi.tests.de')
diff --git a/tests/test_api.py b/tests/test_api.py
index 3f2f73914b..dff8256ae1 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -84,13 +84,13 @@ class TestAdmin:
         return create_auth_headers(admin_user)
 
     @pytest.mark.timeout(10)
-    def test_reset(self, client, admin_user_auth, repair_repository_db):
+    def test_reset(self, client, admin_user_auth, repository_db):
         rv = client.post('/admin/reset', headers=admin_user_auth)
         assert rv.status_code == 200
 
     # TODO disabled as this will destroy the session repository_db beyond repair.
     # @pytest.mark.timeout(10)
-    # def test_remove(self, client, admin_user_auth, repair_repository_db):
+    # def test_remove(self, client, admin_user_auth, repository_db):
     #     rv = client.post('/admin/remove', headers=admin_user_auth)
     #     assert rv.status_code == 200
 
-- 
GitLab