Skip to content

Commit

Permalink
test: topmodel models' and add test data
Browse files Browse the repository at this point in the history
  • Loading branch information
aaraney committed Aug 28, 2024
1 parent b319a31 commit 0260868
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
26 changes: 26 additions & 0 deletions python/ngen_conf/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
_soil_freeze_thaw_config_data_path = _datadir / "init_config_data" / "soil_freeze_thaw.txt"
_soil_moisture_profile_config_data_path = _datadir / "init_config_data" / "soil_moisture_profile.txt"
_lgar_config_data_path = _datadir / "init_config_data" / "lgar.txt"
_topmodel_subcat_config_path = _datadir / "init_config_data" / "subcat.dat"
_topmodel_params_config_path = _datadir / "init_config_data" / "params.dat"
_topmodel_config_path = _datadir / "init_config_data" / "topmodel.run"


"""
Expand Down Expand Up @@ -232,3 +235,26 @@ def soil_moisture_profile_init_config() -> str:
def lgar_init_config() -> str:
# drop eol char
return _lgar_config_data_path.read_text().rstrip()

@pytest.fixture
def topmodel_subcat_config_path() -> Path:
return _topmodel_subcat_config_path

@pytest.fixture
def topmodel_params_config_path() -> Path:
return _topmodel_params_config_path

@pytest.fixture
def topmodel_subcat_config(topmodel_subcat_config_path: Path) -> str:
# drop eol char
return topmodel_subcat_config_path.read_text().rstrip()

@pytest.fixture
def topmodel_params_config(topmodel_params_config_path: Path) -> str:
# drop eol char
return topmodel_params_config_path.read_text().rstrip()

@pytest.fixture
def topmodel_config() -> str:
# drop eol char
return _topmodel_config_path.read_text().rstrip()
2 changes: 2 additions & 0 deletions python/ngen_conf/tests/data/init_config_data/params.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Extracted study basin: Taegu Pyungkwang River
0.032 5.0 50.0 3600.0 3600.0 0.05 3.28e-05 0.002 0 1.0 0.02 0.1
6 changes: 6 additions & 0 deletions python/ngen_conf/tests/data/init_config_data/subcat.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1 1 1
Extracted study basin: Taegu Pyungkwang River
1 1.0
1e-06 9.382756
1
0.0 500.0
7 changes: 7 additions & 0 deletions python/ngen_conf/tests/data/init_config_data/topmodel.run
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1
Taegu Pyungkwang River Catchment: Calibration Data
/dev/null
subcat.dat
params.dat
/dev/null
/dev/null
50 changes: 49 additions & 1 deletion python/ngen_conf/tests/test_init_config_models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import warnings

import pytest
Expand All @@ -9,7 +11,12 @@
from ngen.config.init_config.pet import PET
from ngen.config.init_config.soil_freeze_thaw import SoilFreezeThaw
from ngen.config.init_config.soil_moisture_profile import SoilMoistureProfile
from ngen.init_config import utils
from ngen.config.init_config.topmodel import Topmodel, TopModelSubcat, TopModelParams

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from pathlib import Path


def test_cfe(cfe_init_config: str):
Expand Down Expand Up @@ -114,6 +121,47 @@ def test_soil_moisture_profile(soil_moisture_profile_init_config: str):
o = SoilMoistureProfile.from_ini_str(soil_moisture_profile_init_config)
assert o.to_ini_str() == soil_moisture_profile_init_config


def test_lgar(lgar_init_config: str):
o = Lgar.from_ini_str(lgar_init_config)
assert o.to_ini_str() == lgar_init_config


def test_topmodel_subcat(topmodel_subcat_config: str):
model = TopModelSubcat.parse_obj(topmodel_subcat_config)
assert model.to_str() == topmodel_subcat_config


def test_topmodel_params(topmodel_params_config: str):
model = TopModelParams.parse_obj(topmodel_params_config)
assert model.to_str() == topmodel_params_config


def test_topmodel(topmodel_config: str):
model = Topmodel.parse_obj(topmodel_config)
assert model.to_str() == topmodel_config


def test_topmodel_deserialize_and_serialize_linked_configs(
topmodel_config: str,
topmodel_subcat_config_path: Path,
topmodel_params_config_path: Path,
topmodel_subcat_config: str,
topmodel_params_config: str,
):
model = Topmodel.parse_obj(topmodel_config)

# update paths to avoid resolution issues.
# paths are relative to `_topmodel_config_path` (see conftest.py)
# left unchanged, paths will not resolve correctly unless `pytest` is run
# from the directory that contains `_topmodel_config_path`.
# this fixes that
model.subcat = model.subcat.with_path(topmodel_subcat_config_path)
model.params = model.params.with_path(topmodel_params_config_path)

# read from file and deserialize into `pydantic` model
assert model.subcat.read(), f"failed to deserialize from file {topmodel_subcat_config_path!s}"
assert model.params.read(), f"failed to deserialize from file {topmodel_params_config_path!s}"

assert model.subcat.serialize() == topmodel_subcat_config
assert model.params.serialize() == topmodel_params_config

0 comments on commit 0260868

Please sign in to comment.