Skip to content

Commit

Permalink
use pushd utility to fix tests resolution of test realization config
Browse files Browse the repository at this point in the history
  • Loading branch information
aaraney authored and hellkite500 committed Jul 18, 2023
1 parent 13d2a6b commit 4b79b1a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 58 deletions.
22 changes: 22 additions & 0 deletions python/ngen_conf/src/ngen/config/utils.py
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 8 additions & 8 deletions python/ngen_conf/tests/data/test_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"
}
},
{
Expand All @@ -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,
Expand All @@ -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"
}
},
Expand All @@ -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,
Expand All @@ -81,7 +81,7 @@
],
"forcing": {
"file_pattern": "cat-*.csv",
"path": "data/forcing",
"path": "forcing",
"provider": "CsvPerFeature"
}
},
Expand All @@ -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"
}
}
}
24 changes: 8 additions & 16 deletions python/ngen_conf/tests/test_cfe_sloth.py
Original file line number Diff line number Diff line change
@@ -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))
"""
"""
test_dir = Path(__file__).parent
test_file = test_dir/'data/test_config.json'
with pushd(test_file.parent):
NgenRealization.parse_file(test_file)
43 changes: 9 additions & 34 deletions python/ngen_conf/tests/test_conf.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 4b79b1a

Please sign in to comment.