Skip to content

Commit 1480f27

Browse files
committed
Rename test file to test_bmi_docker. Add first batch of tests.
1 parent 7ecbff5 commit 1480f27

2 files changed

Lines changed: 46 additions & 5 deletions

File tree

PyStemmusScope/bmi/implementation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ def get_grid_z(self, grid: int, z: np.ndarray) -> np.ndarray:
534534
)
535535
return z
536536
else:
537-
raise ValueError()
537+
raise ValueError(f"Grid {grid} has no dimension `z`.")
538538

539539
def get_grid_shape(self, grid: int, shape: np.ndarray) -> np.ndarray:
540540
"""Get dimensions of the computational grid."""
Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
import platform
12
from distutils.dir_util import copy_tree
23
from pathlib import Path
34
import docker
45
import docker.errors
6+
import numpy as np
57
import pytest
68
import requests
79
from PyStemmusScope import config_io
810
from PyStemmusScope import forcing_io
911
from PyStemmusScope import soil_io
10-
from PyStemmusScope.bmi.implementation import StemmusScopeBmi
1112
from PyStemmusScope.bmi.docker_utils import pull_image
13+
from PyStemmusScope.bmi.implementation import StemmusScopeBmi
1214
from . import data_folder
13-
import platform
1415

1516

1617
SCOPE_INPUTDATA_v2_1 = "https://github.com/Christiaanvandertol/SCOPE/raw/2.1/input/"
@@ -22,7 +23,7 @@
2223
def docker_available():
2324
try:
2425
docker.APIClient()
25-
26+
2627
# Github Actions windows runners couldn't pull the image:
2728
if platform.system() == "Windows":
2829
pull_image("ghcr.io/ecoextreml/stemmus_scope:1.5.0")
@@ -34,7 +35,7 @@ def docker_available():
3435
if "404 Client Error" in str(err): # Can't find image
3536
return False
3637
else:
37-
raise err # Unknown error.
38+
raise err # Unknown error.
3839

3940

4041
cfg_file = data_folder / "config_file_docker.txt"
@@ -100,6 +101,46 @@ def prepare_data_config(tmpdir_factory, prep_input_data) -> Path:
100101
@pytest.mark.skipif(not docker_available(), reason="Docker not available")
101102
def test_initialize(prepare_data_config):
102103
model = StemmusScopeBmi()
104+
105+
assert model.get_component_name() == "STEMMUS_SCOPE"
106+
with pytest.raises(ValueError, match="STEMMUS_SCOPE process is not running"):
107+
model.update()
108+
103109
model.initialize(str(prepare_data_config))
110+
111+
assert isinstance(model.get_input_item_count(), int)
112+
assert isinstance(model.get_output_item_count(), int)
113+
assert "soil_temperature" in model.get_input_var_names()
114+
assert "respiration" in model.get_output_var_names()
115+
116+
assert model.get_var_grid("respiration") == 0
117+
assert model.get_var_grid("soil_temperature") == 1
118+
119+
assert model.get_var_type("soil_temperature") == "float64"
120+
121+
# model.get_grid_size needs to have .update() run.
122+
model.update()
123+
124+
dest = np.zeros(model.get_grid_size(0))
125+
np.testing.assert_almost_equal(model.get_grid_x(0, x=dest), np.array([-107.80752563]))
126+
np.testing.assert_almost_equal(model.get_grid_y(0, y=dest), np.array([37.93380356]))
127+
128+
with pytest.raises(ValueError, match="has no dimension `z`"):
129+
model.get_grid_z(0, z=dest)
130+
104131
model.update()
132+
133+
dest = np.zeros(1)
134+
model.get_value("respiration", dest)
135+
assert dest[0] != 0.
136+
137+
dest = np.zeros(1)
138+
model.set_value_at_indices(
139+
"soil_temperature",
140+
inds=np.array([0]),
141+
src=np.array([0.]),
142+
)
143+
model.get_value_at_indices("soil_temperature", dest, inds=np.array([0]))
144+
assert dest[0] == 0.
145+
105146
model.finalize()

0 commit comments

Comments
 (0)