Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent tests from deleting the default user #2480

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
aiida/backends/sqlalchemy/transition_06dj_to_07sqla.py|
aiida/backends/sqlalchemy/utils.py|
aiida/backends/testbase.py|
aiida/backends/testimplbase.py|
aiida/backends/tests/backup_script.py|
aiida/backends/tests/backup_setup_script.py|
aiida/backends/tests/base_dataclasses.py|
Expand Down
6 changes: 5 additions & 1 deletion aiida/backends/djsite/db/testbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def setUpClass_method(self):

def clean_db(self):
from aiida.backends.djsite.db import models
from aiida.manage import get_config

email = get_config().current_profile.default_user_email

# I first need to delete the links, because in principle I could not delete input nodes, only outputs.
# For simplicity, since I am deleting everything, I delete the links first
Expand All @@ -52,7 +55,8 @@ def clean_db(self):
models.DbLog.objects.all().delete()
models.DbNode.objects.all().delete() # pylint: disable=no-member
models.DbWorkflow.objects.all().delete() # pylint: disable=no-member
models.DbUser.objects.all().delete() # pylint: disable=no-member
# The default user is needed for many operations in AiiDA; do not delete it.
models.DbUser.objects.exclude(email=email).delete() # pylint: disable=no-member
models.DbComputer.objects.all().delete()
models.DbGroup.objects.all().delete()

Expand Down
5 changes: 4 additions & 1 deletion aiida/backends/sqlalchemy/tests/testbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ def clean_db(self):
from aiida.backends.sqlalchemy.models.log import DbLog
from aiida.backends.sqlalchemy.models.user import DbUser
from aiida.backends.sqlalchemy.models.workflow import DbWorkflow
from aiida.manage import get_config

email = get_config().current_profile.default_user_email

# Empty the relationship dbgroup.dbnode
dbgroups = self.test_session.query(DbGroup).all()
Expand All @@ -105,7 +108,7 @@ def clean_db(self):
# Then I delete the nodes, otherwise I cannot delete computers and users
self.test_session.query(DbNode).delete()
self.test_session.query(DbWorkflow).delete()
self.test_session.query(DbUser).delete()
self.test_session.query(DbUser).filter(DbUser.email != email).delete()
self.test_session.query(DbComputer).delete()
self.test_session.query(DbLog).delete()

Expand Down
31 changes: 20 additions & 11 deletions aiida/backends/testimplbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@
# For further information on the license, see the LICENSE.txt file #
# For further information please visit http://www.aiida.net #
###########################################################################
"""Base implementation of unittest classes for AiiDA.

The base implementation is extended by the different backends.
"""

from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
from abc import ABCMeta, abstractmethod

import six

from aiida.common.exceptions import InternalError
from aiida.common.exceptions import InternalError, NotExistent
from aiida import orm


Expand Down Expand Up @@ -45,21 +50,22 @@ class AiidaTestImplementation(object):
user_email = None # type: str

@abstractmethod
def setUpClass_method(self):
def setUpClass_method(self): # pylint: disable=invalid-name
"""
This class prepares the database (cleans it up and installs some basic entries).
You have also to set a self.computer and a self.user_email as explained in the docstring of the
AiidaTestImplemention docstring.
"""
pass

def setUp_method(self):
def setUp_method(self): # pylint: disable=invalid-name
pass

def tearDown_method(self):
def tearDown_method(self): # pylint: disable=invalid-name

pass

def tearDownClass_method(self):
def tearDownClass_method(self): # pylint: disable=invalid-name
"""
This class implements the tear down methods (e.g. cleans up the DB).
"""
Expand All @@ -84,13 +90,16 @@ def insert_data(self):
transport_type='local',
scheduler_type='pbspro',
workdir='/tmp/aiida',
backend=self.backend
).store()
backend=self.backend).store()

config = get_config()
email = config.current_profile.default_user_email
self.user = orm.User(email=email).store()
self.user_email = email
self.user_email = get_config().current_profile.default_user_email

# Since the default user is needed for many operations in AiiDA, it is not deleted by clean_db.
# In principle, it should therefore always exist - if not we create it anyhow.
try:
self.user = orm.User.objects.get(email=self.user_email)
except NotExistent:
self.user = orm.User(email=self.user_email).store()

def get_computer(self):
"""
Expand Down