Skip to content

Commit

Permalink
Merge pull request #674 from royagrace/update_tests
Browse files Browse the repository at this point in the history
Update tests to get rid of extra files created when running tests
  • Loading branch information
jlmaurer authored Aug 5, 2024
2 parents a9bf37c + 4e7b657 commit 8cc9722
Show file tree
Hide file tree
Showing 8 changed files with 578 additions and 457 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [0.5.3]
### Fixed
* Updates dem-stitcher to 2.5.8 to ensure new (ARIA-managed) url for reading the Geoid EGM 2008. See this [issue](https://github.com/ACCESS-Cloud-Based-InSAR/dem-stitcher/issues/96).
* [674](https://github.com/dbekaert/RAiDER/pull/674#issue-2412467565) - Update tests to get rid of extra files created when running tests.

## [0.5.2]
### Changed
Expand Down
42 changes: 30 additions & 12 deletions test/test_HRRR_ztd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,40 @@
import shutil
import glob

from test import TEST_DIR
from test import TEST_DIR, WM, update_yaml, pushd

import numpy as np
import xarray as xr
from RAiDER.cli.raider import calcDelays

def test_scenario_1(data_for_hrrr_ztd, mocker):
SCENARIO_DIR = os.path.join(TEST_DIR, "scenario_1")
test_path = os.path.join(SCENARIO_DIR, 'raider_example_1.yaml')
mocker.patch('RAiDER.processWM.prepareWeatherModel',
side_effect=[str(data_for_hrrr_ztd)])
calcDelays([test_path])

new_data = xr.load_dataset(os.path.join(SCENARIO_DIR, 'HRRR_tropo_20200101T120000_ztd.nc'))
new_data1 = new_data.sel(x=-91.84, y=36.84, z=0, method='nearest')
golden_data = 2.2622863, 0.0361021 # hydro|wet
def test_scenario_1(tmp_path, data_for_hrrr_ztd, mocker):
with pushd(tmp_path):
dct_group = {
"aoi_group": {"bounding_box": [36, 37, -92, -91]},
"date_group": {"date_start": "20200101"},
"time_group": {"time": "12:00:00", "interpolate_time": "none"},
"weather_model": "HRRR",
"height_group": {"height_levels": [0, 50, 100, 500, 1000]},
"look_dir": "right",
"runtime_group": {"output_directory": "test/scenario_1"},
}

np.testing.assert_almost_equal(golden_data[0], new_data1['hydro'].data)
np.testing.assert_almost_equal(golden_data[1], new_data1['wet'].data)
cfg = update_yaml(dct_group, os.path.join(tmp_path, "temp.yaml"))

SCENARIO_DIR = os.path.join(tmp_path, TEST_DIR, "scenario_1")
mocker.patch(
"RAiDER.processWM.prepareWeatherModel", side_effect=[str(data_for_hrrr_ztd)]
)
calcDelays([os.path.join(tmp_path, "temp.yaml")])

new_data = xr.load_dataset(
os.path.join(
tmp_path, "test", "scenario_1", "HRRR_tropo_20200101T120000_ztd.nc"
)
)
new_data1 = new_data.sel(x=-91.84, y=36.84, z=0, method="nearest")
golden_data = 2.2622863, 0.0361021 # hydro|wet

np.testing.assert_almost_equal(golden_data[0], new_data1["hydro"].data)
np.testing.assert_almost_equal(golden_data[1], new_data1["wet"].data)
141 changes: 74 additions & 67 deletions test/test_checkArgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,169 +19,176 @@
SCENARIO_1 = os.path.join(TEST_DIR, "scenario_1")
SCENARIO_2 = os.path.join(TEST_DIR, "scenario_2")


@pytest.fixture(autouse=True)
def args():
d = DEFAULT_DICT
d['date_list'] = [datetime.datetime(2018, 1, 1)]
d['time'] = datetime.time(12,0,0)
d['aoi'] = BoundingBox([38, 39, -92, -91])
d['los'] = Zenith()
d['weather_model'] = GMAO()

for f in 'weather_files weather_dir'.split():
shutil.rmtree(f) if os.path.exists(f) else ''
d["date_list"] = [datetime.datetime(2018, 1, 1)]
d["time"] = datetime.time(12, 0, 0)
d["aoi"] = BoundingBox([38, 39, -92, -91])
d["los"] = Zenith()
d["weather_model"] = GMAO()

for f in "weather_files weather_dir".split():
shutil.rmtree(f) if os.path.exists(f) else ""
return d


def isWriteable(dirpath):
'''Test whether a directory is writeable'''
"""Test whether a directory is writeable"""
try:
filehandle = open(os.path.join(dirpath, 'tmp.txt'), 'w')
filehandle = open(os.path.join(dirpath, "tmp.txt"), "w")
filehandle.close()
return True
except IOError:
return False


def test_checkArgs_outfmt_1(args):
'''Test that passing height levels with hdf5 outformat works'''
"""Test that passing height levels with hdf5 outformat works"""
args = args
args.file_format = 'h5'
args.file_format = "h5"
args.heightlvls = [10, 100, 1000]
checkArgs(args)
assert os.path.splitext(args.wetFilenames[0])[-1] == '.h5'
assert os.path.splitext(args.wetFilenames[0])[-1] == ".h5"


def test_checkArgs_outfmt_2(args):
'''Test that passing a raster format with height levels throws an error'''
"""Test that passing a raster format with height levels throws an error"""
args = args
args.heightlvs = [10, 100, 1000]
args.file_format = 'GTiff'
args.file_format = "GTiff"
args = checkArgs(args)
assert os.path.splitext(args.wetFilenames[0])[-1] == '.nc'
assert os.path.splitext(args.wetFilenames[0])[-1] == ".nc"


def test_checkArgs_outfmt_3(args):
'''Test that passing a raster format with height levels throws an error'''
"""Test that passing a raster format with height levels throws an error"""
args = args
with pytest.raises(FileNotFoundError):
args.aoi = StationFile(os.path.join('fake_dir', 'stations.csv'))
args.aoi = StationFile(os.path.join("fake_dir", "stations.csv"))


def test_checkArgs_outfmt_4(args):
'''Test that passing a raster format with height levels throws an error'''
"""Test that passing a raster format with height levels throws an error"""
args = args
args.aoi = RasterRDR(
lat_file = os.path.join(SCENARIO_1, 'geom', 'lat.dat'),
lon_file = os.path.join(SCENARIO_1, 'geom', 'lon.dat'),
lat_file=os.path.join(SCENARIO_1, "geom", "lat.dat"),
lon_file=os.path.join(SCENARIO_1, "geom", "lon.dat"),
)
argDict = checkArgs(args)
assert argDict.aoi.type()=='radar_rasters'
assert argDict.aoi.type() == "radar_rasters"


def test_checkArgs_outfmt_5(args):
'''Test that passing a raster format with height levels throws an error'''
args = args
args.aoi = StationFile(os.path.join(SCENARIO_2, 'stations.csv'))
argDict = checkArgs(args)
assert pd.read_csv(argDict['wetFilenames'][0]).shape == (8, 4)
def test_checkArgs_outfmt_5(args, tmp_path):
with pushd(tmp_path):
args = args
args.aoi = StationFile(os.path.join(SCENARIO_2, "stations.csv"))
argDict = checkArgs(args)
assert pd.read_csv(argDict["wetFilenames"][0]).shape == (8, 4)


def test_checkArgs_outloc_1(args):
'''Test that the default output and weather model directories are correct'''
"""Test that the default output and weather model directories are correct"""
args = args
argDict = checkArgs(args)
out = argDict['output_directory']
wmLoc = argDict['weather_model_directory']
out = argDict["output_directory"]
wmLoc = argDict["weather_model_directory"]
assert os.path.abspath(out) == os.getcwd()
assert os.path.abspath(wmLoc) == os.path.join(os.getcwd(), 'weather_files')
assert os.path.abspath(wmLoc) == os.path.join(os.getcwd(), "weather_files")


def test_checkArgs_outloc_2(args, tmp_path):
'''Tests that the correct output location gets assigned when provided'''
"""Tests that the correct output location gets assigned when provided"""
with pushd(tmp_path):
args = args
args.output_directory = tmp_path
argDict = checkArgs(args)
out = argDict['output_directory']
out = argDict["output_directory"]
assert out == tmp_path


def test_checkArgs_outloc_2b(args, tmp_path):
''' Tests that the weather model directory gets passed through by itself'''
"""Tests that the weather model directory gets passed through by itself"""
with pushd(tmp_path):
args = args
args.output_directory = tmp_path
args.weather_model_directory = 'weather_dir'
args.weather_model_directory = "weather_dir"
argDict = checkArgs(args)
assert argDict['weather_model_directory'] == 'weather_dir'
assert argDict["weather_model_directory"] == "weather_dir"


def test_checkArgs_outloc_3(args, tmp_path):
'''Tests that the weather model directory gets created when needed'''
"""Tests that the weather model directory gets created when needed"""
with pushd(tmp_path):
args = args
args.output_directory = tmp_path
argDict = checkArgs(args)
assert os.path.isdir(argDict['weather_model_directory'])
assert os.path.isdir(argDict["weather_model_directory"])


def test_checkArgs_outloc_4(args):
'''Tests for creating writeable weather model directory'''
"""Tests for creating writeable weather model directory"""
args = args
argDict = checkArgs(args)

assert isWriteable(argDict['weather_model_directory'])
assert isWriteable(argDict["weather_model_directory"])


def test_filenames_1(args):
'''tests that the correct filenames are generated'''
"""tests that the correct filenames are generated"""
args = args
argDict = checkArgs(args)
assert 'Delay' not in argDict['wetFilenames'][0]
assert 'wet' in argDict['wetFilenames'][0]
assert 'hydro' in argDict['hydroFilenames'][0]
assert '20180101' in argDict['wetFilenames'][0]
assert '20180101' in argDict['hydroFilenames'][0]
assert len(argDict['hydroFilenames']) == 1
assert "Delay" not in argDict["wetFilenames"][0]
assert "wet" in argDict["wetFilenames"][0]
assert "hydro" in argDict["hydroFilenames"][0]
assert "20180101" in argDict["wetFilenames"][0]
assert "20180101" in argDict["hydroFilenames"][0]
assert len(argDict["hydroFilenames"]) == 1


def test_filenames_2(args):
'''tests that the correct filenames are generated'''
"""tests that the correct filenames are generated"""
args = args
args['output_directory'] = SCENARIO_2
args.aoi = StationFile(os.path.join(SCENARIO_2, 'stations.csv'))
args["output_directory"] = SCENARIO_2
args.aoi = StationFile(os.path.join(SCENARIO_2, "stations.csv"))
argDict = checkArgs(args)
assert '20180101' in argDict['wetFilenames'][0]
assert len(argDict['wetFilenames']) == 1
assert "20180101" in argDict["wetFilenames"][0]
assert len(argDict["wetFilenames"]) == 1


def test_makeDelayFileNames_1():
assert makeDelayFileNames(None, None, "h5", "name", "dir") == \
("dir/name_wet_ztd.h5", "dir/name_hydro_ztd.h5")
assert makeDelayFileNames(None, None, "h5", "name", "dir") == (
"dir/name_wet_ztd.h5",
"dir/name_hydro_ztd.h5",
)


def test_makeDelayFileNames_2():
assert makeDelayFileNames(None, (), "h5", "name", "dir") == \
("dir/name_wet_std.h5", "dir/name_hydro_std.h5")
assert makeDelayFileNames(None, (), "h5", "name", "dir") == (
"dir/name_wet_std.h5",
"dir/name_hydro_std.h5",
)


def test_makeDelayFileNames_3():
assert makeDelayFileNames(datetime.datetime(2020, 1, 1, 1, 2, 3), None, "h5", "model_name", "dir") == \
(
"dir/model_name_wet_20200101T010203_ztd.h5",
"dir/model_name_hydro_20200101T010203_ztd.h5"
assert makeDelayFileNames(
datetime.datetime(2020, 1, 1, 1, 2, 3), None, "h5", "model_name", "dir"
) == (
"dir/model_name_wet_20200101T010203_ztd.h5",
"dir/model_name_hydro_20200101T010203_ztd.h5",
)


def test_makeDelayFileNames_4():
assert makeDelayFileNames(datetime.datetime(1900, 12, 31, 1, 2, 3), "los", "h5", "model_name", "dir") == \
(
"dir/model_name_wet_19001231T010203_std.h5",
"dir/model_name_hydro_19001231T010203_std.h5"
assert makeDelayFileNames(
datetime.datetime(1900, 12, 31, 1, 2, 3), "los", "h5", "model_name", "dir"
) == (
"dir/model_name_wet_19001231T010203_std.h5",
"dir/model_name_hydro_19001231T010203_std.h5",
)


def test_get_raster_ext():
with pytest.raises(ValueError):
get_raster_ext('dummy_format')

get_raster_ext("dummy_format")
Loading

0 comments on commit 8cc9722

Please sign in to comment.