Skip to content

Commit 0260868

Browse files
committed
test: topmodel models' and add test data
1 parent b319a31 commit 0260868

File tree

5 files changed

+90
-1
lines changed

5 files changed

+90
-1
lines changed

python/ngen_conf/tests/conftest.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
_soil_freeze_thaw_config_data_path = _datadir / "init_config_data" / "soil_freeze_thaw.txt"
2323
_soil_moisture_profile_config_data_path = _datadir / "init_config_data" / "soil_moisture_profile.txt"
2424
_lgar_config_data_path = _datadir / "init_config_data" / "lgar.txt"
25+
_topmodel_subcat_config_path = _datadir / "init_config_data" / "subcat.dat"
26+
_topmodel_params_config_path = _datadir / "init_config_data" / "params.dat"
27+
_topmodel_config_path = _datadir / "init_config_data" / "topmodel.run"
2528

2629

2730
"""
@@ -232,3 +235,26 @@ def soil_moisture_profile_init_config() -> str:
232235
def lgar_init_config() -> str:
233236
# drop eol char
234237
return _lgar_config_data_path.read_text().rstrip()
238+
239+
@pytest.fixture
240+
def topmodel_subcat_config_path() -> Path:
241+
return _topmodel_subcat_config_path
242+
243+
@pytest.fixture
244+
def topmodel_params_config_path() -> Path:
245+
return _topmodel_params_config_path
246+
247+
@pytest.fixture
248+
def topmodel_subcat_config(topmodel_subcat_config_path: Path) -> str:
249+
# drop eol char
250+
return topmodel_subcat_config_path.read_text().rstrip()
251+
252+
@pytest.fixture
253+
def topmodel_params_config(topmodel_params_config_path: Path) -> str:
254+
# drop eol char
255+
return topmodel_params_config_path.read_text().rstrip()
256+
257+
@pytest.fixture
258+
def topmodel_config() -> str:
259+
# drop eol char
260+
return _topmodel_config_path.read_text().rstrip()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Extracted study basin: Taegu Pyungkwang River
2+
0.032 5.0 50.0 3600.0 3600.0 0.05 3.28e-05 0.002 0 1.0 0.02 0.1
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1 1 1
2+
Extracted study basin: Taegu Pyungkwang River
3+
1 1.0
4+
1e-06 9.382756
5+
1
6+
0.0 500.0
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
1
2+
Taegu Pyungkwang River Catchment: Calibration Data
3+
/dev/null
4+
subcat.dat
5+
params.dat
6+
/dev/null
7+
/dev/null

python/ngen_conf/tests/test_init_config_models.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import warnings
24

35
import pytest
@@ -9,7 +11,12 @@
911
from ngen.config.init_config.pet import PET
1012
from ngen.config.init_config.soil_freeze_thaw import SoilFreezeThaw
1113
from ngen.config.init_config.soil_moisture_profile import SoilMoistureProfile
12-
from ngen.init_config import utils
14+
from ngen.config.init_config.topmodel import Topmodel, TopModelSubcat, TopModelParams
15+
16+
from typing import TYPE_CHECKING
17+
18+
if TYPE_CHECKING:
19+
from pathlib import Path
1320

1421

1522
def test_cfe(cfe_init_config: str):
@@ -114,6 +121,47 @@ def test_soil_moisture_profile(soil_moisture_profile_init_config: str):
114121
o = SoilMoistureProfile.from_ini_str(soil_moisture_profile_init_config)
115122
assert o.to_ini_str() == soil_moisture_profile_init_config
116123

124+
117125
def test_lgar(lgar_init_config: str):
118126
o = Lgar.from_ini_str(lgar_init_config)
119127
assert o.to_ini_str() == lgar_init_config
128+
129+
130+
def test_topmodel_subcat(topmodel_subcat_config: str):
131+
model = TopModelSubcat.parse_obj(topmodel_subcat_config)
132+
assert model.to_str() == topmodel_subcat_config
133+
134+
135+
def test_topmodel_params(topmodel_params_config: str):
136+
model = TopModelParams.parse_obj(topmodel_params_config)
137+
assert model.to_str() == topmodel_params_config
138+
139+
140+
def test_topmodel(topmodel_config: str):
141+
model = Topmodel.parse_obj(topmodel_config)
142+
assert model.to_str() == topmodel_config
143+
144+
145+
def test_topmodel_deserialize_and_serialize_linked_configs(
146+
topmodel_config: str,
147+
topmodel_subcat_config_path: Path,
148+
topmodel_params_config_path: Path,
149+
topmodel_subcat_config: str,
150+
topmodel_params_config: str,
151+
):
152+
model = Topmodel.parse_obj(topmodel_config)
153+
154+
# update paths to avoid resolution issues.
155+
# paths are relative to `_topmodel_config_path` (see conftest.py)
156+
# left unchanged, paths will not resolve correctly unless `pytest` is run
157+
# from the directory that contains `_topmodel_config_path`.
158+
# this fixes that
159+
model.subcat = model.subcat.with_path(topmodel_subcat_config_path)
160+
model.params = model.params.with_path(topmodel_params_config_path)
161+
162+
# read from file and deserialize into `pydantic` model
163+
assert model.subcat.read(), f"failed to deserialize from file {topmodel_subcat_config_path!s}"
164+
assert model.params.read(), f"failed to deserialize from file {topmodel_params_config_path!s}"
165+
166+
assert model.subcat.serialize() == topmodel_subcat_config
167+
assert model.params.serialize() == topmodel_params_config

0 commit comments

Comments
 (0)