Skip to content

Commit

Permalink
Add common tests for input generators of common relax workflow
Browse files Browse the repository at this point in the history
The newly added test will automatically parametrize over all the
implementations of the common relax workflow that register an entry
point. The test checks that the signature of the `get_builder` method,
as built dynamically through the input generator specification in the
`define` method, respects the criteria of the common interface.

This means that all the required ports are present and that their valid
types are what they should be. This should hopefully catch if
implementations change the specification accidentally in a way that is
not intended.

In the future, ideally we can extend these tests with tests that
actually try to call `get_builder`, such that we can also verify that it
successfully returns a builder when using any of the values that are
defined as supported for the various arguments, such as for the
`spin_type`, `relax_type` and `electronic_type`.

However, this is technically not easy as calling `get_builder` may
depend on certain pre-conditions being fulfilled depending on the
implementation. For example, the Quantum ESPRESSO would require a
version of the `SsspFamily` to be installed. We would need to find a way
to automatically load these fixtures based on the implementation that is
being tested.
  • Loading branch information
sphuber committed Oct 8, 2021
1 parent 03556c6 commit f83cbba
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions tests/workflows/relax/test_implementations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# -*- coding: utf-8 -*-
"""Tests for the :mod:`aiida_common_workflows.workflows.relax.quantum_espresso` module."""
# pylint: disable=redefined-outer-name
import pytest

from aiida import engine
from aiida import orm
from aiida import plugins

from aiida_common_workflows.common.types import ElectronicType, RelaxType, SpinType
from aiida_common_workflows.generators.ports import InputGeneratorPort
from aiida_common_workflows.plugins import get_workflow_entry_point_names
from aiida_common_workflows.workflows.relax.workchain import CommonRelaxWorkChain


@pytest.fixture(scope='function', params=get_workflow_entry_point_names('relax'))
def workchain(request) -> CommonRelaxWorkChain:
"""Fixture that parametrizes over all the registered implementations of the ``CommonRelaxWorkChain``."""
return plugins.WorkflowFactory(request.param)


def test_spec(workchain):
"""Test that the input specification of all implementations respects the common interface."""
generator = workchain.get_input_generator()
generator_spec = generator.spec()

required_ports = {
'structure': {
'valid_type': plugins.DataFactory('structure')
},
'protocol': {
'valid_type': str
},
'spin_type': {
'valid_type': SpinType
},
'relax_type': {
'valid_type': RelaxType
},
'electronic_type': {
'valid_type': ElectronicType
},
'magnetization_per_site': {
'valid_type': list
},
'threshold_forces': {
'valid_type': float
},
'threshold_stress': {
'valid_type': float
},
'reference_workchain': {
'valid_type': orm.WorkChainNode
},
'engines': {}
}

for port_name, values in required_ports.items():
assert isinstance(generator_spec.inputs.get_port(port_name), (InputGeneratorPort, engine.PortNamespace))

if 'valid_type' in values:
assert generator_spec.inputs.get_port(port_name).valid_type is values['valid_type']

0 comments on commit f83cbba

Please sign in to comment.