Skip to content

Commit

Permalink
move config tests from model to kernel config
Browse files Browse the repository at this point in the history
  • Loading branch information
savente93 committed Mar 29, 2024
1 parent 0ce2b5d commit e8e350b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 42 deletions.
44 changes: 42 additions & 2 deletions tests/components/test_kernel_config_component.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from os.path import abspath, isabs, join
from os.path import abspath, isabs, isfile, join
from pathlib import Path

import pytest

from hydromt.components.kernel_config import (
DEFAULT_KERNEL_CONFIG_PATH,
KernelConfigComponent,
)
from hydromt.io.path import make_config_paths_abs, make_config_paths_relative
from hydromt.io.readers import configread
from hydromt.io.readers import configread, read_yaml
from hydromt.models import Model

ABS_PATH = Path(abspath(__name__))

Expand Down Expand Up @@ -71,3 +76,38 @@ def test_make_rel_abs(tmpdir, test_config_dict):
if isinstance(p, Path)
]
), parsed_config["section2"]


def test_set_config(tmpdir):
model = Model(root=tmpdir)
model.add_component("config", KernelConfigComponent(model))
config_component = model.get_component("config", KernelConfigComponent)
config_component.set("global.name", "test")
assert config_component._data is not None
assert "name" in config_component._data["global"]
assert config_component.get_config_value("global.name") == "test"


def test_write_config(tmpdir):
model = Model(root=tmpdir)
model.add_component("config", KernelConfigComponent(model))
config_component = model.get_component("config", KernelConfigComponent)
config_component.set("global.name", "test")
write_path = join(tmpdir, DEFAULT_KERNEL_CONFIG_PATH)
assert not isfile(write_path)
config_component.write()
assert isfile(write_path)
read_contents = read_yaml(write_path)
assert read_contents == {"global": {"name": "test"}}


def test_get_config_abs_path(tmpdir):
model = Model(root=tmpdir)
model.add_component("config", KernelConfigComponent(model))
config_component = model.get_component("config", KernelConfigComponent)
abs_path = str(tmpdir.join("test.file"))
config_component.set("global.file", "test.file")
assert str(config_component.get_config_value("global.file")) == "test.file"
assert (
str(config_component.get_config_value("global.file", abs_path=True)) == abs_path
)
40 changes: 0 additions & 40 deletions tests/models/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@

from hydromt.components.base import ModelComponent
from hydromt.components.grid import GridComponent
from hydromt.components.kernel_config import (
DEFAULT_KERNEL_CONFIG_PATH,
KernelConfigComponent,
)
from hydromt.components.region import ModelRegionComponent
from hydromt.data_catalog import DataCatalog
from hydromt.io.readers import read_yaml
from hydromt.models import Model
from hydromt.models.model import _check_data
from hydromt.plugins import PLUGINS
Expand Down Expand Up @@ -357,41 +352,6 @@ def test_model_set_geoms(tmpdir):
assert model._geoms["geom_wgs84"].crs.to_epsg() == model.crs.to_epsg()


def test_set_config(tmpdir):
model = Model(root=tmpdir)
model.add_component("config", KernelConfigComponent(model))
config_component = model.get_component("config", KernelConfigComponent)
config_component.set("global.name", "test")
assert config_component._data is not None
assert "name" in config_component._data["global"]
assert config_component.get_config_value("global.name") == "test"


def test_write_config(tmpdir):
model = Model(root=tmpdir)
model.add_component("config", KernelConfigComponent(model))
config_component = model.get_component("config", KernelConfigComponent)
config_component.set("global.name", "test")
write_path = join(tmpdir, DEFAULT_KERNEL_CONFIG_PATH)
assert not isfile(write_path)
config_component.write()
assert isfile(write_path)
read_contents = read_yaml(write_path)
assert read_contents == {"global": {"name": "test"}}


def test_get_config_abs_path(tmpdir):
model = Model(root=tmpdir)
model.add_component("config", KernelConfigComponent(model))
config_component = model.get_component("config", KernelConfigComponent)
abs_path = str(tmpdir.join("test.file"))
config_component.set("global.file", "test.file")
assert str(config_component.get_config_value("global.file")) == "test.file"
assert (
str(config_component.get_config_value("global.file", abs_path=True)) == abs_path
)


@pytest.mark.skip(reason="Needs implementation of all raster Drivers.")
def test_maps_setup(tmpdir):
dc_param_fn = join(DATADIR, "parameters_data.yml")
Expand Down

0 comments on commit e8e350b

Please sign in to comment.