Skip to content

Commit d3e1b10

Browse files
committed
Additional tests for BMI error messages
1 parent b8b0d0c commit d3e1b10

2 files changed

Lines changed: 30 additions & 12 deletions

File tree

PyStemmusScope/bmi/implementation.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,6 @@
4747
)
4848

4949

50-
def ipython_info():
51-
"""Get ipython info: if the code is being run from notebook or terminal."""
52-
ip = False
53-
if "ipykernel" in sys.modules:
54-
ip = "notebook"
55-
elif "IPython" in sys.modules:
56-
ip = "terminal"
57-
return ip
58-
59-
6050
def load_state(config: dict) -> h5py.File:
6151
"""Load the STEMMUS_SCOPE model state.
6252
@@ -85,7 +75,7 @@ def get_variable(state: h5py.File, varname: str) -> np.ndarray:
8575
if varname in MODEL_VARNAMES:
8676
msg = "Varname is missing in get_variable! Contact devs."
8777
else:
88-
msg = "Uknown variable name"
78+
msg = "Unknown variable name"
8979
raise ValueError(msg)
9080

9181

@@ -117,7 +107,7 @@ def set_variable(
117107
else:
118108
if varname in MODEL_OUTPUT_VARNAMES and varname not in MODEL_INPUT_VARNAMES:
119109
msg = "This variable is a model output variable only. You cannot set it."
120-
if varname in MODEL_VARNAMES:
110+
elif varname in MODEL_VARNAMES:
121111
msg = "Varname is missing in set_variable! Contact devs."
122112
else:
123113
msg = "Uknown variable name"

tests/test_bmi_docker.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import platform
22
from distutils.dir_util import copy_tree
33
from pathlib import Path
4+
from unittest.mock import patch
45
import docker
56
import docker.errors
67
import numpy as np
@@ -218,6 +219,7 @@ def test_model_update(self, initialized_model):
218219
initialized_model.update()
219220

220221

222+
@pytest.mark.skipif(not docker_available(), reason="Docker not available")
221223
class TestUpdatedModel:
222224
# Many of these should be available after init
223225
def test_get_current_time(self, updated_model):
@@ -259,6 +261,32 @@ def test_get_value(self, updated_model):
259261
updated_model.get_value("respiration", dest)
260262
assert dest[0] != 0.0
261263

264+
def test_set_output_var(self, updated_model):
265+
src = np.zeros(1)
266+
with pytest.raises(ValueError, match="output variable"):
267+
updated_model.set_value("respiration", src)
268+
269+
def test_get_wrong_value(self, updated_model):
270+
dest = np.zeros(1)
271+
with pytest.raises(ValueError, match="Unknown variable"):
272+
updated_model.get_value("nonsense_variable", dest)
273+
274+
def test_dev_error_variables(self, updated_model):
275+
dest = np.zeros(1)
276+
with patch("PyStemmusScope.bmi.implementation.MODEL_VARNAMES", ("NONSENSE",)):
277+
with pytest.raises(ValueError, match="Contact devs"):
278+
updated_model.get_value("NONSENSE", dest)
279+
280+
def test_set_value(self, updated_model):
281+
gridsize = updated_model.get_grid_size(
282+
updated_model.get_var_grid("soil_temperature")
283+
)
284+
src = np.zeros(gridsize) + 10.0
285+
dest = np.zeros(gridsize)
286+
updated_model.set_value("soil_temperature", src)
287+
updated_model.get_value("soil_temperature", dest)
288+
np.testing.assert_array_equal(src, dest)
289+
262290
def test_set_value_inds(self, updated_model):
263291
dest = np.zeros(1)
264292
updated_model.set_value_at_indices(

0 commit comments

Comments
 (0)