aiida.manage package

Managing an AiiDA instance:

  • configuration file

  • profiles

  • databases

  • repositories

  • external components (such as Postgres, RabbitMQ)

Note

Modules in this sub package may require the database environment to be loaded

Submodules

Definition of caching mechanism and configuration for calculations.

aiida.manage.caching.get_use_cache(node_class=None, identifier=None)[source]

Return whether the caching mechanism should be used for the given entry point according to the configuration.

Parameters
  • node_class – the Node class or sub class to check if enabled for caching

  • identifier – the full entry point string of the process, e.g. aiida.calculations:arithmetic.add

Returns

boolean, True if caching is enabled, False otherwise

Raises

ValueError – if the configuration is invalid by defining the class both enabled and disabled

aiida.manage.caching.enable_caching(node_class=None, identifier=None)[source]

Context manager to enable caching, either for a specific node class, or globally.

Warning

this does not affect the behavior of the daemon, only the local Python interpreter.

Parameters
  • node_class – Node class for which caching should be enabled.

  • identifier – the full entry point string of the process, e.g. aiida.calculations:arithmetic.add

aiida.manage.caching.disable_caching(node_class=None, identifier=None)[source]

Context manager to disable caching, either for a specific node class, or globally.

Warning

this does not affect the behavior of the daemon, only the local Python interpreter.

Parameters
  • node_class – Node class for which caching should be disabled.

  • identifier – the full entry point string of the process, e.g. aiida.calculations:arithmetic.add

Testing tools for related projects like plugins.

Fixtures (pytest) and specific test classes & test runners (unittest) that set up a complete temporary AiiDA environment for plugin tests.

Filesystem:

  • temporary config (.aiida) folder

  • temporary repository folder

Database:

  • temporary database cluster via the pgtest package

  • with aiida database user

  • with aiida_db database

AiiDA:

  • set to use the temporary config folder

  • create and configure a profile

exception aiida.manage.fixtures.FixtureError(msg)[source]

Bases: Exception

Raised by FixtureManager, when it encounters a situation in which consistent behaviour can not be guaranteed

__init__(msg)[source]

Initialize self. See help(type(self)) for accurate signature.

__module__ = 'aiida.manage.fixtures'
__str__()[source]

Return str(self).

__weakref__

list of weak references to the object (if defined)

class aiida.manage.fixtures.FixtureManager[source]

Bases: object

Manage the life cycle of a completely separated and temporary AiiDA environment

  • No previously created database of profile is required to run tests using this environment

  • Tests using this environment will never pollute the user’s work environment

Example:

fixtures = FixtureManager()
fixtures.create_aiida_db()  # set up only the database
fixtures.create_profile()  # set up a profile (creates the db too if necessary)

# ready for testing

# run test 1

fixtures.reset_db()
# database ready for independent test 2

# run test 2

fixtures.destroy_all()
# everything cleaned up

Usage (unittest): See the PluginTestCase and the TestRunner.

Usage (pytest):

import pytest

@pytest.fixture(scope='session')
def aiida_profile():
    # set up a test profile for the duration of the tests
    with aiida.manage.fixtures.fixture_manager() as fixture_manager:
        yield fixture_manager

@pytest.fixture(scope='function')
def new_database(aiida_profile):
    # clear the database after each test
    yield aiida_profile
    aiida_profile.reset_db()

def test_my_stuff(new_database):
    # run a test
__dict__ = mappingproxy({'__module__': 'aiida.manage.fixtures', '__doc__': "\n Manage the life cycle of a completely separated and temporary AiiDA environment\n\n * No previously created database of profile is required to run tests using this environment\n * Tests using this environment will never pollute the user's work environment\n\n Example::\n\n fixtures = FixtureManager()\n fixtures.create_aiida_db() # set up only the database\n fixtures.create_profile() # set up a profile (creates the db too if necessary)\n\n # ready for testing\n\n # run test 1\n\n fixtures.reset_db()\n # database ready for independent test 2\n\n # run test 2\n\n fixtures.destroy_all()\n # everything cleaned up\n\n Usage (unittest): See the :py:class:`PluginTestCase` and the :py:class:`TestRunner`.\n\n\n Usage (pytest)::\n\n import pytest\n\n @pytest.fixture(scope='session')\n def aiida_profile():\n # set up a test profile for the duration of the tests\n with aiida.manage.fixtures.fixture_manager() as fixture_manager:\n yield fixture_manager\n\n @pytest.fixture(scope='function')\n def new_database(aiida_profile):\n # clear the database after each test\n yield aiida_profile\n aiida_profile.reset_db()\n\n def test_my_stuff(new_database):\n # run a test\n\n ", '_test_case': None, '__init__': <function FixtureManager.__init__>, '_backend': <property object>, 'create_db_cluster': <function FixtureManager.create_db_cluster>, 'create_aiida_db': <function FixtureManager.create_aiida_db>, 'create_profile': <function FixtureManager.create_profile>, 'reset_db': <function FixtureManager.reset_db>, 'init_db': <staticmethod object>, 'profile_dictionary': <property object>, 'user_dictionary': <property object>, 'db_host': <property object>, 'first_name': <property object>, 'last_name': <property object>, 'institution': <property object>, 'db_port': <property object>, 'repo_ok': <function FixtureManager.repo_ok>, 'repo': <property object>, '_return_dir': <function FixtureManager._return_dir>, 'email': <property object>, 'engine': <property object>, 'backend': <property object>, 'config_dir_ok': <property object>, 'config_dir': <property object>, 'db_user': <property object>, 'db_pass': <property object>, 'db_name': <property object>, 'root_dir': <property object>, 'root_dir_ok': <property object>, 'destroy_all': <function FixtureManager.destroy_all>, '_create_test_case': <function FixtureManager._create_test_case>, 'has_profile_open': <function FixtureManager.has_profile_open>, '__dict__': <attribute '__dict__' of 'FixtureManager' objects>, '__weakref__': <attribute '__weakref__' of 'FixtureManager' objects>})
__init__()[source]

Initialize self. See help(type(self)) for accurate signature.

__module__ = 'aiida.manage.fixtures'
__weakref__

list of weak references to the object (if defined)

property _backend

Get the backend

_create_test_case()[source]

Create the test case for the correct backend which will be used to clean up

_return_dir(key)[source]

Return a path to a directory from the fs environment

_test_case = None
property backend
property config_dir
property config_dir_ok
create_aiida_db(pgtest=None)[source]

Create the necessary database on the temporary postgres instance.

By utilizing pgtest it is possible to forward initialization arguments to PGTest().

Parameters

pgtest – a dictionary containing input to PGTest()

create_db_cluster(pgtest=None)[source]

Create the database cluster using PGTest.

Parameters

pgtest – a dictionary containing input to PGTest()

create_profile(pgtest=None)[source]

Set AiiDA to use the test config dir and create a default profile there

Warning: the AiiDA dbenv must not be loaded when this is called! :param pgtest: a dictionary containing input to PGTest()

property db_host
property db_name
property db_pass
property db_port
property db_user
destroy_all()[source]

Remove all traces of the test run

property email
property engine
property first_name
has_profile_open()[source]
static init_db()[source]

Initialise the database state

property institution
property last_name
property profile_dictionary

Profile parameters

property repo
repo_ok()[source]
reset_db()[source]

Cleans all data from the database between tests

property root_dir
property root_dir_ok
property user_dictionary

User parameters

class aiida.manage.fixtures.PluginTestCase(methodName='runTest')[source]

Bases: unittest.case.TestCase

Set up a complete temporary AiiDA environment for plugin tests.

Note: This test class needs to be run through the TestRunner and will not work simply with python -m unittest discover.

Usage example:

MyTestCase(aiida.manage.fixtures.PluginTestCase):

    def setUp(self):
        # load my test data

    # optionally extend setUpClass / tearDownClass / tearDown if needed

    def test_my_plugin(self):
        # execute tests
__module__ = 'aiida.manage.fixtures'
backend = None
classmethod setUpClass()[source]

Hook method for setting up class fixture before running tests in the class.

tearDown()[source]

Hook method for deconstructing the test fixture after testing it.

class aiida.manage.fixtures.TestRunner(stream=None, descriptions=True, verbosity=1, failfast=False, buffer=False, resultclass=None, warnings=None, *, tb_locals=False)[source]

Bases: unittest.runner.TextTestRunner

Testrunner for unit tests using the fixture manager.

Usage example:

import unittest
from aiida.manage.fixtures import TestRunner

tests = unittest.defaultTestLoader.discover('.')
TestRunner().run(tests)
__module__ = 'aiida.manage.fixtures'
run(suite, backend='django')[source]

Run tests using fixture manager for specified backend.

Parameters
  • tests – A suite of tests, as returned e.g. by unittest.TestLoader.discover()

  • backend – Database backend to be used.

aiida.manage.fixtures.fixture_manager(backend='django', pgtest=None)[source]

Context manager for FixtureManager objects

Example test runner (unittest):

with fixture_manager(backend) as fixture_mgr:
    # ready for tests
# everything cleaned up

The variable pgtest can be used to accept a dictionary containing inputs to PGTest(). Typically, and the most common use would be if the user have several postgresql installations and want to use a particular pg_ctl, then the user might do the following:

Example test runner(unittest):

with fixture_manager(backend, pgtest={'pg_ctl': '/somepath/pg_ctl'}) as fixture_mgr:
    # ready for tests
# everything cleaned up

Example fixture (pytest):

def aiida_profile():
    with fixture_manager(backend) as fixture_mgr:
        yield fixture_mgr
Parameters
  • backend – database backend, either BACKEND_SQLA or BACKEND_DJANGO

  • pgtest – a dictionary containing the input passed to PGTest()

AiiDA manager for global settings

aiida.manage.manager.get_manager()[source]
aiida.manage.manager.reset_manager()[source]