forked from nens/threedi-results-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
67 lines (46 loc) · 2.03 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
"""Main pytest configuration file: fixtures + a qgis import mechanism fix.
Pytest automatically uses a ``conftest.py`` file, when found. Note that you
can have also have such files in subdirectories. The fixtures in this file
*stay* available there, except when you override them.
"""
from ThreeDiToolbox import PLUGIN_DIR
import os
import pytest
import shutil
def fix_import_mechanism():
"""Make pytest work in combination with qgis.
We prevent Qgis from grabbing python's import mechanism. Qgis overrides
something, which breaks pytest by causing an infinite import loop.
"""
os.environ["QGIS_NO_OVERRIDE_IMPORT"] = "KEEPYOURPAWSOFF"
fix_import_mechanism() # Needs to be called right away.
data_dir = PLUGIN_DIR / "tests" / "data"
bergermeer_dir = data_dir / "testmodel" / "v2_bergermeer"
results_3di_path = bergermeer_dir / "results_3di.nc"
@pytest.fixture()
def threedi_result():
"""Fixture: return a instance of ThreediResult
The instance contains result data of the model 'v2_bergermeer'. It
contains both results and aggregate result data.
"""
# Late import, otherwise we get circular import errors.
from ThreeDiToolbox.datasource.threedi_results import ThreediResult
return ThreediResult(file_path=results_3di_path)
@pytest.fixture()
def ts_datasources(tmp_path):
"""Fixture: return ts_datasources with one threedi_result (the one above) preloaded.
Note that the test data is first copied, so it is safe to modify.
"""
# Late import, otherwise we get circular import errors.
from ThreeDiToolbox.tool_result_selection.models import TimeseriesDatasourceModel
shutil.copytree(bergermeer_dir, tmp_path / "v2_bergermeer")
copied_results_3di_path = tmp_path / "v2_bergermeer" / "results_3di.nc"
result = TimeseriesDatasourceModel()
test_values = {
"active": False,
"name": "bergermeer v2 from main conftest.py",
"file_path": copied_results_3di_path,
"type": "netcdf-groundwater",
}
result.insertRows([test_values])
return result