forked from aiidateam/aiida-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests: Add tests for calcfunction as
WorkChain
class member methods
The test defines a workchain that defines a calcfunction as a class member in two ways: * A proper staticmethod * A class attribute Both versions work, but the former is more correct and is the only one that can be called from an instance of the workchain. The other has to be invoked by retrieving the calcfunction as an attribute and then calling it. The former is the only one that is offcially documented. The tests verify that both methods of declaring the calcfunction can be called within the `WorkChain` and that it also works when submitting the workchain to the daemon. A test is also added that verifies that caching of the calcfunctions functions properly. The changes also broke one existing test. The test took a calcfunction, originally defined on the module level, and modifying it within the test function scope. The goal of the test was twofold: * Check that changing the source code would be recognized by the caching mechanism and so an invocation of the changed function would not be cached from an invocation of the original * Check that it is possible to cache from a function defined inside the scope of a function. The changes to the dynamically built `FunctionProcess`, notably changing the type from `func.__name__` to `func.__qualname__` stopped the second point from working. In the original code, the type name would be simply `tests.engine.test_calcfunctions.add_function`, both for the module level function as well as the inlined function. However, with the change this becomes: `tests.engine.test_calcfunctions.TestCalcFunction.test_calcfunction_caching_change_code.<locals>.add_calcfunction` this can no longer be loaded by the `ProcessNode.process_class` property and so `is_valid_cache` returns `False`, whereas in the original code it was a valid cache as the process class could be loaded. Arguably, the new code is more correct, but it is breaking. Before inlined functions were valid cache sources, but that is no longer the case. In exchange, class member functions are now valid cache sources where they weren't before. Arguably, it is preferable to support class member functions over inline functions. The broken test is fixed by moving the inlined `add_calcfunction` to a separate module such that it becomes a valid cache source again.
- Loading branch information
Showing
5 changed files
with
131 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Definition of a calculation function used in ``test_calcfunctions.py``.""" | ||
from aiida.engine import calcfunction | ||
from aiida.orm import Int | ||
|
||
|
||
@calcfunction | ||
def add_calcfunction(data): | ||
"""Calcfunction mirroring a ``test_calcfunctions`` calcfunction but has a slightly different implementation.""" | ||
return Int(data.value + 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters