aiida.manage.tests package#

Testing infrastructure for easy testing of AiiDA plugins.

Submodules#

Collection of pytest fixtures that are intended for use in plugin packages.

To use these fixtures, simply create a conftest.py in the tests folder and add the following line:

pytest_plugins = [‘aiida.manage.tests.pytest_fixtures’]

This will make all the fixtures in this file available and ready for use. Simply use them as you would any other pytest fixture.

class aiida.manage.tests.pytest_fixtures.EntryPointManager(entry_points: EntryPoints)[源代码]#

基类:object

Manager to temporarily add or remove entry points.

__dict__ = mappingproxy({'__module__': 'aiida.manage.tests.pytest_fixtures', '__doc__': 'Manager to temporarily add or remove entry points.', '__init__': <function EntryPointManager.__init__>, 'eps': <function EntryPointManager.eps>, 'eps_select': <function EntryPointManager.eps_select>, '_validate_entry_point': <staticmethod(<function EntryPointManager._validate_entry_point>)>, 'add': <function EntryPointManager.add>, 'remove': <function EntryPointManager.remove>, '__dict__': <attribute '__dict__' of 'EntryPointManager' objects>, '__weakref__': <attribute '__weakref__' of 'EntryPointManager' objects>, '__annotations__': {}})#
__init__(entry_points: EntryPoints)[源代码]#
__module__ = 'aiida.manage.tests.pytest_fixtures'#
__weakref__#

list of weak references to the object (if defined)

static _validate_entry_point(entry_point_string: str | None, group: str | None, name: str | None) tuple[str, str][源代码]#

Validate the definition of the entry point.

参数:
  • entry_point_string – Fully qualified entry point string.

  • name – Entry point name.

  • group – Entry point group.

返回:

The entry point group and name.

抛出:
  • TypeError – If entry_point_string, group or name are not a string, when defined.

  • ValueError – If entry_point_string is not defined, nor a group and name.

  • ValueError – If entry_point_string is not a complete entry point string with group and name.

add(value: type | str, entry_point_string: str | None = None, *, name: str | None = None, group: str | None = None) None[源代码]#

Add an entry point.

参数:
  • value – The class or function to register as entry point. The resource needs to be importable, so it can’t be inlined. Alternatively, the fully qualified name can be passed as a string.

  • entry_point_string – Fully qualified entry point string.

  • name – Entry point name.

  • group – Entry point group.

返回:

The entry point group and name.

抛出:
  • TypeError – If entry_point_string, group or name are not a string, when defined.

  • ValueError – If entry_point_string is not defined, nor a group and name.

  • ValueError – If entry_point_string is not a complete entry point string with group and name.

eps() EntryPoints[源代码]#
eps_select(group, name=None) EntryPoints[源代码]#
remove(entry_point_string: str | None = None, *, name: str | None = None, group: str | None = None) None[源代码]#

Remove an entry point.

参数:
  • value – Entry point value, fully qualified import path name.

  • entry_point_string – Fully qualified entry point string.

  • name – Entry point name.

  • group – Entry point group.

返回:

The entry point group and name.

抛出:
  • TypeError – If entry_point_string, group or name are not a string, when defined.

  • ValueError – If entry_point_string is not defined, nor a group and name.

  • ValueError – If entry_point_string is not a complete entry point string with group and name.

aiida.manage.tests.pytest_fixtures.aiida_caplog(caplog)[源代码]#

A copy of pytest’s caplog fixture, which allows AIIDA_LOGGER to propagate.

aiida.manage.tests.pytest_fixtures.aiida_computer(tmp_path) Callable[[], Computer][源代码]#

Factory to return a aiida.orm.computers.Computer instance.

aiida.manage.tests.pytest_fixtures.aiida_computer_local(aiida_computer) Callable[[], Computer][源代码]#

Factory to return a aiida.orm.computers.Computer instance with core.local transport.

aiida.manage.tests.pytest_fixtures.aiida_computer_ssh(aiida_computer, ssh_key) Callable[[], Computer][源代码]#

Factory to return a aiida.orm.computers.Computer instance with core.ssh transport.

aiida.manage.tests.pytest_fixtures.aiida_instance(tmp_path_factory: pytest.tmpdir.TempPathFactory, aiida_manager: Manager, aiida_test_profile: str | None) t.Generator[Config, None, None][源代码]#

Return the Config instance that is used for the test session.

If an existing test profile is defined through the aiida_test_profile fixture, the configuration of the actual AiiDA instance is loaded and returned. If no test profile is defined, a completely independent and temporary AiiDA instance is generated in a temporary directory with a clean .aiida folder and basic configuration file. The currently loaded configuration and profile are stored in memory and are automatically restored at the end of the test session. The temporary instance is automatically deleted.

返回:

The configuration the AiiDA instance loaded for this test session.

aiida.manage.tests.pytest_fixtures.aiida_local_code_factory(aiida_localhost)[源代码]#

Get an AiiDA code on localhost.

Searches in the PATH for a given executable and creates an AiiDA code with provided entry point.

Usage:

def test_1(aiida_local_code_factory):
    code = aiida_local_code_factory('quantumespresso.pw', '/usr/bin/pw.x')
    # use code for testing ...
返回:

A function get_code(entry_point, executable) that returns the Code node.

返回类型:

object

aiida.manage.tests.pytest_fixtures.aiida_localhost(aiida_computer_local) Computer[源代码]#

Return a aiida.orm.computers.Computer instance representing localhost with core.local transport.

Usage:

def test(aiida_localhost):
    assert aiida_localhost.transport_type == 'core.local'
返回:

The computer.

aiida.manage.tests.pytest_fixtures.aiida_manager() Manager[源代码]#

Return the global instance of the Manager.

返回:

The global manager instance.

aiida.manage.tests.pytest_fixtures.aiida_profile(aiida_manager: Manager, aiida_test_profile: str | None, aiida_profile_factory: Callable[[dict[str, Any] | None], Profile], config_psql_dos: Callable[[dict[str, Any] | None], dict[str, Any]]) Generator[Profile, None, None][源代码]#

Return a loaded AiiDA test profile.

If a test profile has been declared, as returned by the aiida_test_profile fixture, that is loaded and yielded. Otherwise, a temporary and fully isolated AiiDA instance is created, complete with a loaded test profile, that are all automatically cleaned up at the end of the test session. The storage backend used for the profile is PsqlDosBackend.

aiida.manage.tests.pytest_fixtures.aiida_profile_clean(aiida_profile)[源代码]#

Provide an AiiDA test profile, with the storage reset at test function setup.

aiida.manage.tests.pytest_fixtures.aiida_profile_clean_class(aiida_profile)[源代码]#

Provide an AiiDA test profile, with the storage reset at test class setup.

aiida.manage.tests.pytest_fixtures.aiida_profile_factory(aiida_instance: Config, aiida_manager: Manager) t.Callable[[dict[str, t.Any]], Profile][源代码]#

Create a temporary profile, add it to the config of the loaded AiiDA instance and load the profile.

The default configuration is complete except for the configuration of the storage, which should be provided through the custom_configuration argument. The storage will be fully reset and initalised, destroying all data that it contains and recreate the default user, making the profile ready for use.

aiida.manage.tests.pytest_fixtures.aiida_test_profile() str | None[源代码]#

Return the name of the AiiDA test profile if defined.

The name is taken from the AIIDA_TEST_PROFILE environment variable.

返回:

The name of the profile to you for the test session or None if not defined.

aiida.manage.tests.pytest_fixtures.clear_database(clear_database_after_test)[源代码]#

Alias for ‘clear_database_after_test’.

Clears the database after each test. Use of the explicit ‘clear_database_after_test’ is preferred.

aiida.manage.tests.pytest_fixtures.clear_database_after_test(aiida_profile)[源代码]#

Clear the database after the test.

aiida.manage.tests.pytest_fixtures.clear_database_before_test(aiida_profile)[源代码]#

Clear the database before the test.

aiida.manage.tests.pytest_fixtures.clear_database_before_test_class(aiida_profile)[源代码]#

Clear the database before a test class.

aiida.manage.tests.pytest_fixtures.clear_profile()[源代码]#

Clear the currently loaded profile.

This ensures that the contents of the profile are reset as well as the Manager, which may hold references to data that will be destroyed. The daemon will also be stopped if it was running.

aiida.manage.tests.pytest_fixtures.config_psql_dos(tmp_path_factory: pytest.tmpdir.TempPathFactory, postgres_cluster: dict[str, str]) t.Callable[[dict[str, t.Any] | None], dict[str, t.Any]][源代码]#

Return a profile configuration for the PsqlDosBackend.

aiida.manage.tests.pytest_fixtures.daemon_client(aiida_profile)[源代码]#

Return a daemon client for the configured test profile for the test session.

The daemon will be automatically stopped at the end of the test session.

aiida.manage.tests.pytest_fixtures.entry_points(monkeypatch) EntryPointManager[源代码]#

Return an instance of the EntryPointManager which allows to temporarily add or remove entry points.

This fixture monkey patches the entry point caches returned by the aiida.plugins.entry_point.eps() and aiida.plugins.entry_point.eps_select() functions to class methods of the EntryPointManager so that we can dynamically add / remove entry points. Note that we do not need a deepcopy here as eps() returns an immutable EntryPoints tuple type.

aiida.manage.tests.pytest_fixtures.postgres_cluster()[源代码]#

Create a temporary and isolated PostgreSQL cluster using pgtest and cleanup after the yield.

参数:
  • database_name – Name of the database.

  • database_username – Username to use for authentication.

  • database_password – Password to use for authentication.

返回:

Dictionary with parameters to connect to the PostgreSQL cluster.

aiida.manage.tests.pytest_fixtures.recursive_merge(left: dict[Any, Any], right: dict[Any, Any]) None[源代码]#

Recursively merge the right dictionary into the left dictionary.

参数:
  • left – Base dictionary.

  • right – Dictionary to recurisvely merge on top of left dictionary.

aiida.manage.tests.pytest_fixtures.ssh_key(tmp_path_factory) Generator[Path, None, None][源代码]#

Generate a temporary SSH key pair for the test session and return the filepath of the private key.

The filepath of the public key is the same as the private key, but it adds the .pub file extension.

aiida.manage.tests.pytest_fixtures.started_daemon_client(daemon_client)[源代码]#

Ensure that the daemon is running for the test profile and return the associated client.

aiida.manage.tests.pytest_fixtures.stopped_daemon_client(daemon_client)[源代码]#

Ensure that the daemon is not running for the test profile and return the associated client.

aiida.manage.tests.pytest_fixtures.submit_and_await(started_daemon_client)[源代码]#

Submit a process and wait for it to achieve the given state.

aiida.manage.tests.pytest_fixtures.suppress_deprecations(wrapper=None, enabled=None, adapter=None, proxy=<class 'FunctionWrapper'>)[源代码]#

Decorator that suppresses all DeprecationWarning.

aiida.manage.tests.pytest_fixtures.temp_dir()[源代码]#

Get a temporary directory.

E.g. to use as the working directory of an AiiDA computer.

返回:

The path to the directory

返回类型:

str

aiida.manage.tests.pytest_fixtures.temporary_event_loop()[源代码]#

Create a temporary loop for independent test case