-
Notifications
You must be signed in to change notification settings - Fork 5
/
conftest.py
276 lines (213 loc) · 6.8 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
"""Initialise a text database and profile for pytest."""
import os
from pathlib import Path
import shutil
from aiida.common import exceptions
from aiida.common.folders import SandboxFolder
from aiida.engine.utils import instantiate_process
from aiida.manage.manager import get_manager
from aiida.orm import InstalledCode, load_code
from aiida.plugins import CalculationFactory
import pytest
pytest_plugins = ["aiida.manage.tests.pytest_fixtures"]
@pytest.fixture(scope="function", autouse=True)
def clear_database_auto(aiida_profile_clean):
"""Automatically clear database in between tests."""
@pytest.fixture(scope="session")
def filepath_tests():
"""
Return the absolute filepath of the `tests` folder.
Warning
-------
If this file moves with respect to the `tests` folder, the implementation should
change.
Parameters
----------
None
Returns
-------
Path
Absolute filepath of `tests` folder.
"""
return Path(__file__).resolve()
@pytest.fixture(scope="function")
def fixture_sandbox():
"""
Return a `SandboxFolder` fixture.
This fixture returns a `SandboxFolder` instance for temporary file operations
within a test function.
Yields
------
SandboxFolder
A `SandboxFolder` instance for temporary file operations.
"""
with SandboxFolder() as folder:
yield folder
@pytest.fixture
def fixture_localhost(aiida_localhost):
"""
Return a localhost `Computer` fixture.
Parameters
----------
aiida_localhost : fixture
A fixture providing a localhost `Computer` instance.
Returns
-------
`Computer`
A localhost `Computer` instance.
"""
localhost = aiida_localhost
localhost.set_default_mpiprocs_per_machine(1)
return localhost
@pytest.fixture(scope="function")
def janus_code(aiida_local_code_factory):
"""
Fixture to get the janus code.
Parameters
----------
aiida_local_code_factory : fixture
A fixture providing a factory for creating local codes.
Returns
-------
`Code`
The janus code instance.
"""
janus_path = shutil.which("janus") or os.environ.get("JANUS_PATH")
return aiida_local_code_factory(executable=janus_path, entry_point="mlip.sp")
@pytest.fixture
def fixture_code(fixture_localhost):
"""
Return a configured `InstalledCode` instance to run calculations on localhost.
Parameters
----------
fixture_localhost : fixture
A fixture providing a localhost `Computer` instance.
Notes
-----
This fixture returns a function that can be called with the entry point name.
If the code with the specified label already exists, it loads and returns it.
Otherwise, it creates a new `InstalledCode` instance with the provided
parameters.
"""
def _fixture_code(entry_point_name):
"""
Create an `InstalledCode` to run calculations of a given entry point.
Parameters
----------
entry_point_name : str
The entry point name for the calculation plugin.
Returns
-------
aiida.orm.nodes.data.code.Code
An `InstalledCode` instance.
"""
label = f"test.{entry_point_name}"
janus_path = os.environ.get("JANUS_PATH")
try:
return load_code(label=label)
except exceptions.NotExistent:
return InstalledCode(
label=label,
computer=fixture_localhost,
filepath_executable=janus_path,
default_calc_job_plugin=entry_point_name,
)
return _fixture_code
@pytest.fixture
def generate_calc_job():
"""
Fixture to construct a new `CalcJob` instance and prepare it for submission.
This fixture returns a function that constructs a new `CalcJob` instance
for testing purposes.
"""
def _generate_calc_job(folder, entry_point_name, inputs=None):
"""
Generate a mock `CalcInfo` for testing calculation jobs.
Parameters
----------
folder : SandboxFolder
The temporary folder for storing raw input files.
entry_point_name : str
The entry point name of the `CalcJob` class to be instantiated.
inputs : Optional[Dict[str, Any]], optional
A dictionary of inputs for the calculation job, by default None.
Returns
-------
CalcInfo
The `CalcInfo` object returned by `prepare_for_submission`.
Notes
-----
This function constructs a new instance of the specified `CalcJob` class
using the provided inputs, and calls `prepare_for_submission`.
The resulting `CalcInfo` object is returned.
"""
manager = get_manager()
runner = manager.get_runner()
process_class = CalculationFactory(entry_point_name)
process = instantiate_process(runner, process_class, **inputs)
return process.prepare_for_submission(folder)
return _generate_calc_job
@pytest.fixture(scope="session")
def test_folder():
"""
Fixture to provide the path of the tests folder.
Returns
-------
Path: the path of the tests folder.
"""
return Path(__file__).resolve().parent
# Fixture to provide the path to the example file
@pytest.fixture(scope="session")
def example_path(test_folder):
"""
Fixture to provide the path to the example file.
Returns
-------
Path: The path to the example file.
"""
return test_folder.parent / "examples" / "calculations"
@pytest.fixture(scope="session")
def model_folder(test_folder):
"""
Fixture to provide the path to the example file.
Returns
-------
Path: The path to the example file.
"""
return test_folder / "data" / "input_files" / "mace"
@pytest.fixture(scope="session")
def structure_folder(test_folder):
"""
Fixture to provide the path to the example file.
Returns
-------
Path: The path to the example file.
"""
return test_folder / "calculations" / "structures"
@pytest.fixture(scope="session")
def workflow_structure_folder(test_folder):
"""
Fixture to provide the path to the example file.
Returns
-------
Path: The path to the example file.
"""
return test_folder / "workflows" / "structures"
@pytest.fixture(scope="session")
def workflow_invalid_folder(test_folder):
"""
Fixture to provide the path to the example file.
Returns
-------
Path: The path to the example file.
"""
return test_folder / "workflows" / "invalid"
@pytest.fixture(scope="session")
def config_folder(test_folder):
"""
Fixture to provide the path to the example file.
Returns
-------
Path: The path to the example file.
"""
return test_folder / "calculations" / "configs"