-
Notifications
You must be signed in to change notification settings - Fork 4
/
conftest.py
303 lines (242 loc) · 9.19 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"""pytest plugin configuration.
For more information on writing pytest plugins see:
- https://docs.pytest.org/en/latest/writing_plugins.html
- https://docs.pytest.org/en/latest/reference.html#request
- https://docs.pytest.org/en/latest/example/simple.html
- https://github.com/pytest-dev/cookiecutter-pytest-plugin
"""
import shutil
import tempfile
from _pytest.config import Config # noqa: F401
from _pytest.config.argparsing import Parser # noqa: F401
from _pytest.nodes import Item # noqa: F401
import pytest
from aiida_crystal17.tests import (
get_test_structure,
get_test_structure_and_symm,
open_resource_binary,
resource_context,
)
from aiida_crystal17.tests.utils import AiidaTestApp
pytest_plugins = ["aiida.manage.tests.pytest_fixtures"]
CRY17_CALL_EXEC_MARKER = "cry17_calls_executable"
CRY17_NOTEBOOK_MARKER = "cry17_doc_notebooks"
CRY17_NO_MOCK_HELP = "Do not use mock executables for tests."
CRY17_WORKDIR_HELP = (
"Specify a work directory path for aiida calcjob execution. "
"If not specified, a temporary directory is used and deleted after tests execution."
)
CRY17_SKIP_EXEC_HELP = "skip tests marked with @pytest.mark.{}".format(
CRY17_CALL_EXEC_MARKER
)
CRY17_NB_TEST_HELP = "Only run tests marked {} (otherwise skipped)".format(
CRY17_NOTEBOOK_MARKER
)
class NotSet(object):
"""Indicate that a configuration file variable was not set."""
def pytest_addoption(parser):
# type: (Parser) -> None
"""Define pytest command-line and configuration file options.
Configuration file options are set in pytest.ini|tox.ini|setup.cfg as e.g.::
[pytest]
cry17_no_mock = false
Configuration options can be accessed via the `pytestconfig` fixture::
@pytest.fixture
def my_fixture(pytestconfig)
pytestconfig.getoption('cry17_no_mock')
pytestconfig.getini('cry17_no_mock')
"""
group = parser.getgroup("aiida_crystal17")
group.addoption(
"--cry17-no-mock",
action="store_true",
dest="cry17_no_mock",
default=False,
help=CRY17_NO_MOCK_HELP,
)
group.addoption(
"--cry17-workdir", dest="cry17_workdir", default=None, help=CRY17_WORKDIR_HELP
)
group.addoption(
"--cry17-skip-exec",
action="store_true",
dest="cry17_skip_exec",
default=False,
help=CRY17_SKIP_EXEC_HELP,
)
group.addoption(
"--cry17-nb-tests",
action="store_true",
dest="cry17_nb_tests",
default=False,
help=CRY17_NB_TEST_HELP,
)
group.addoption(
"--cry17-nb-tests-only",
action="store_true",
dest="cry17_nb_tests_only",
default=False,
help=CRY17_NB_TEST_HELP,
)
parser.addini("cry17_no_mock", CRY17_NO_MOCK_HELP, type="bool", default=NotSet())
parser.addini("cry17_workdir", CRY17_WORKDIR_HELP, default=NotSet())
def use_mock_exec(config):
"""Return whether mock executables should be used."""
if config.getoption("cry17_no_mock"):
return False
ini = config.getini("cry17_no_mock")
return True if isinstance(ini, NotSet) else not ini
def get_work_directory(config):
"""Return the aiida work directory to use."""
if config.getoption("cry17_workdir") is not None:
return config.getoption("cry17_workdir")
ini = config.getini("cry17_workdir")
if isinstance(ini, NotSet):
return None
return ini
def pytest_configure(config):
# type: (Config) -> None
"""Register pytest markers.
These will show in ``pytest --markers``
"""
config.addinivalue_line(
"markers",
"{}: mark tests that will call external executables".format(
CRY17_CALL_EXEC_MARKER
),
)
config.addinivalue_line(
"markers",
"{}: mark tests that will test document notebooks".format(
CRY17_NOTEBOOK_MARKER
),
)
def pytest_collection_modifyitems(config, items):
# type: (Config, list) -> None
"""Modify collected test items (may filter or re-order the items in-place).
If ``cry17_nb_tests_only == True``, deselect all tests not marked ``cry17_doc_notebooks``.
Add skip marker to tests marked:
- ``cry17_calls_executable`` if ``cry17_skip_exec == True``
- ``cry17_calls_executable(skip_non_mock=True)`` if ``cry17_no_mock == True``.
- ``cry17_doc_notebooks`` if ``cry17_nb_tests != True``
"""
if config.getoption("cry17_nb_tests_only", False):
# only run tests marked with CRY17_NOTEBOOK_MARKER
items[:] = [item for item in items if CRY17_NOTEBOOK_MARKER in item.keywords]
test_nbs = config.getoption("cry17_nb_tests", False) or config.getoption(
"cry17_nb_tests_only", False
)
for item in items: # type: Item
if (not test_nbs) and (CRY17_NOTEBOOK_MARKER in item.keywords):
item.add_marker(pytest.mark.skip(reason="cry17_nb_tests not specified"))
continue
if CRY17_CALL_EXEC_MARKER in item.keywords:
marker = item.get_closest_marker(CRY17_CALL_EXEC_MARKER)
if config.getoption("cry17_skip_exec", False):
item.add_marker(pytest.mark.skip(reason="cry17_skip_exec specified"))
elif marker.kwargs.get("skip_non_mock", False) and not use_mock_exec(
config
):
reason = marker.kwargs.get("reason", "")
item.add_marker(
pytest.mark.skip(
reason="cry17_no_mock specified and skip_non_mock=True: {}".format(
reason
)
)
)
def pytest_report_header(config):
"""Add header information for pytest execution."""
if use_mock_exec(config):
header = ["CRYSTAL17 Executables: mock_crystal17 mock_properties17"]
else:
header = ["CRYSTAL17 Executables: crystal17 properties17"]
workdir = get_work_directory(config)
workdir = workdir or "<TEMP>"
header.append("CRYSTAL17 Work Directory: {}".format(workdir))
return header
@pytest.fixture(scope="function")
def db_test_app(aiida_profile, pytestconfig):
"""Create a clean aiida database, profile and temporary work directory for the duration of a test.
:rtype: aiida_crystal17.tests.utils.AiidaTestApp
"""
if use_mock_exec(pytestconfig):
print("NB: using mock executable")
executables = {
"crystal17.basic": "mock_crystal17",
"crystal17.main": "mock_crystal17",
"crystal17.doss": "mock_properties17",
"crystal17.ech3": "mock_properties17",
"crystal17.newk": "mock_properties17",
"crystal17.ppan": "mock_properties17",
}
else:
executables = {
"crystal17.basic": "crystal17",
"crystal17.main": "crystal17",
"crystal17.doss": "properties17",
"crystal17.ech3": "properties17",
"crystal17.newk": "properties17",
"crystal17.ppan": "properties17",
}
test_workdir = get_work_directory(pytestconfig)
if test_workdir:
print("NB: using test workdir: {}".format(test_workdir))
work_directory = test_workdir
else:
work_directory = tempfile.mkdtemp()
yield AiidaTestApp(work_directory, executables, environment=aiida_profile)
aiida_profile.reset_db()
if not test_workdir:
shutil.rmtree(work_directory)
@pytest.fixture(scope="function")
def get_structure():
"""Return a function that returns a test `aiida.orm.StructureData` instance."""
return get_test_structure
@pytest.fixture(scope="function")
def get_structure_and_symm():
"""Return a function that returns test `aiida.orm.StructureData` and `SymmetryData` instances."""
return get_test_structure_and_symm
@pytest.fixture(scope="function")
def get_cif():
"""Return a function that returns a test `aiida.orm.CifData` instance.""" # noqa: D202
def _get_cif(name):
from aiida.orm import CifData
if name == "pyrite":
with open_resource_binary("cif", "pyrite.cif") as handle:
return CifData(file=handle)
raise ValueError(name)
return _get_cif
@pytest.fixture(scope="function")
def upload_basis_set_family():
"""Upload a basis set family."""
from aiida_crystal17.data.basis_set import BasisSetData
def _upload(folder_name="sto3g", group_name="sto3g", stop_if_existing=True):
with resource_context("basis_sets", folder_name) as path:
BasisSetData.upload_basisset_family(
path,
group_name,
"minimal basis sets",
stop_if_existing=stop_if_existing,
extension=".basis",
)
return BasisSetData.get_basis_group_map(group_name)
return _upload
@pytest.fixture()
def sanitise_calc_attr():
def _func(data: dict):
return {
k: v
for k, v in data.items()
if k
not in [
"job_id",
"submit_script_filename",
"scheduler_lastchecktime",
"detailed_job_info",
"last_job_info",
"remote_workdir",
"version",
]
}
return _func