generated from NOAA-OWP/owp-open-source-project-template
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use pushd utility to fix tests resolution of test realization config
- Loading branch information
1 parent
932bf8f
commit 005a4e7
Showing
4 changed files
with
47 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |