diff --git a/python/ngen_conf/src/ngen/config/utils.py b/python/ngen_conf/src/ngen/config/utils.py new file mode 100644 index 00000000..91847bb4 --- /dev/null +++ b/python/ngen_conf/src/ngen/config/utils.py @@ -0,0 +1,22 @@ +from contextlib import contextmanager +from os import getcwd, chdir +from typing import TYPE_CHECKING +if TYPE_CHECKING: + from pathlib import Path + +@contextmanager +def pushd(path: 'Path') -> None: + """Change working directory to `path` for duration of the context + + Args: + path (Path): path to cd to + """ + #save current working dir + cwd = getcwd() + #change to new path + chdir(path) + try: + yield #yield context + finally: + #when finished, return to original working dir + chdir(cwd) diff --git a/python/ngen_conf/tests/data/test_config.json b/python/ngen_conf/tests/data/test_config.json index 27655cfa..8d7555e4 100644 --- a/python/ngen_conf/tests/data/test_config.json +++ b/python/ngen_conf/tests/data/test_config.json @@ -19,7 +19,7 @@ "name": "bmi_fortran", "model_type_name": "NoahOWP", "main_output_variable": "QINSUR", - "init_config": "data/NOAH/cat-1.input", + "init_config": "NOAH/cat-1.input", "allow_exceed_end_time": true, "fixed_time_step": false, "uses_forcing_file": false, @@ -33,7 +33,7 @@ "SOLDN": "land_surface_radiation~incoming~shortwave__energy_flux", "SFCPRS": "land_surface_air__pressure" }, - "library_file": "data/NOAH/libfackenoah.dylib" + "library_file": "NOAH/libfackenoah.dylib" } }, { @@ -42,7 +42,7 @@ "name": "bmi_c", "model_type_name": "CFE", "main_output_variable": "Q_OUT", - "init_config": "data/cfe/config.txt", + "init_config": "cfe/config.txt", "allow_exceed_end_time": true, "fixed_time_step": false, "uses_forcing_file": false, @@ -53,7 +53,7 @@ "ice_fraction_xinan": "sloth_ice_fraction_xinan", "soil_moisture_profile": "sloth_smp" }, - "library_file": "data/CFE/libfakecfe.so", + "library_file": "CFE/libfakecfe.so", "registration_function": "register_bmi_cfe" } }, @@ -63,7 +63,7 @@ "name": "bmi_c++", "model_type_name": "SLOTH", "main_output_variable": "z", - "library_file": "data/sloth/libfakesloth.dylib", + "library_file": "sloth/libfakesloth.dylib", "init_config": "/dev/null", "allow_exceed_end_time": true, "fixed_time_step": false, @@ -81,7 +81,7 @@ ], "forcing": { "file_pattern": "cat-*.csv", - "path": "data/forcing", + "path": "forcing", "provider": "CsvPerFeature" } }, @@ -92,6 +92,6 @@ }, "routing": { "t_route_connection_path": "/local/ngen/workdir/extern/t-route/src/ngen_routing", - "t_route_config_file_with_path": "data/routing/fake_config.yaml" + "t_route_config_file_with_path": "routing/fake_config.yaml" } -} \ No newline at end of file +} diff --git a/python/ngen_conf/tests/test_cfe_sloth.py b/python/ngen_conf/tests/test_cfe_sloth.py index 47126b7a..5ec08f18 100644 --- a/python/ngen_conf/tests/test_cfe_sloth.py +++ b/python/ngen_conf/tests/test_cfe_sloth.py @@ -1,21 +1,13 @@ -import json -import pytest from pathlib import Path -from ngen.config.realization import Realization, NgenRealization +from ngen.config.realization import NgenRealization +from ngen.config.utils import pushd -@pytest.fixture() -def data(): - test_dir = Path(__file__).parent - test_file = test_dir/'data/test_config.json' - with open(test_file) as fp: - data = json.load(fp) - data['routing']['t_route_config_file_with_path'] = test_dir/data['routing']['t_route_config_file_with_path'] - data['global']['forcing']['path'] = test_dir/data['global']['forcing']['path'] - return data - -def test_ngen_global_realization(data): - g = NgenRealization(**data) +def test_ngen_global_realization(): """ TODO write a test of serializing to json and then reading the result back and validating??? with open("generated.json", 'w') as fp: fp.write( g.json(by_alias=True, exclude_none=True, indent=4)) - """ \ No newline at end of file + """ + test_dir = Path(__file__).parent + test_file = test_dir/'data/test_config.json' + with pushd(test_file.parent): + NgenRealization.parse_file(test_file) diff --git a/python/ngen_conf/tests/test_conf.py b/python/ngen_conf/tests/test_conf.py index 114d9900..4215331f 100644 --- a/python/ngen_conf/tests/test_conf.py +++ b/python/ngen_conf/tests/test_conf.py @@ -1,48 +1,23 @@ -import pytest, json +import pytest from pathlib import Path from ngen.config.realization import NgenRealization from ngen.config.hydrofabric import CatchmentGeoJSON, NexusGeoJSON +from ngen.config.utils import pushd @pytest.fixture() def testdir(): testdir = Path(__file__).parent return testdir -@pytest.fixture() -def catchmentdata(testdir): +def test_catchment(testdir: Path): test_file = testdir/'data/hydrofabric/test_catchment_config.geojson' - with open(test_file) as fp: - catchmentdata = json.load(fp) - return catchmentdata + CatchmentGeoJSON.parse_file(test_file) -@pytest.fixture() -def nexusdata(testdir): +def test_nexus(testdir: Path): test_file = testdir/'data/hydrofabric/test_nexus_config.geojson' - with open(test_file) as fp: - nexusdata = json.load(fp) - return nexusdata + NexusGeoJSON.parse_file(test_file) -@pytest.fixture() -def realizationdata(testdir): +def test_ngen_realization_config(testdir: Path): test_file = testdir/'data/test_config.json' - with open(test_file) as fp: - realizationdata = json.load(fp) - realizationdata['routing']['t_route_config_file_with_path'] = testdir/realizationdata['routing']['t_route_config_file_with_path'] - return realizationdata - -def test_catchment(catchmentdata): - CatchmentGeoJSON(**catchmentdata) - -def test_nexus(nexusdata): - NexusGeoJSON(**nexusdata) - -def test_ngen_realization_config(realizationdata): - g = NgenRealization(**realizationdata) - - - - - - - - + with pushd(test_file.parent): + NgenRealization.parse_file(test_file)