Skip to content

Commit 12938a0

Browse files
tests for updates
1 parent 188b041 commit 12938a0

3 files changed

Lines changed: 110 additions & 2 deletions

File tree

testing/test_1_import.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ def test_name_space():
1919
'get_bounding_box',
2020
'spatial_resample',
2121
'temporal_resample',
22-
'get_data_tables',
2322
'subset_time_by_timezone',
2423
'delete_temp_files',
2524
]
@@ -66,3 +65,25 @@ def test_dataset_variables():
6665
)
6766
assert isinstance(supported_variables, list)
6867
assert len(supported_variables) > 0
68+
69+
70+
def test_data_conversion_functions() -> None:
71+
72+
# check conversion functions class and default functions
73+
assert 'DataConversionFunctions' in dir(xarray_data_accessor)
74+
from xarray_data_accessor import DataConversionFunctions
75+
76+
assert isinstance(DataConversionFunctions, type)
77+
assert 'points_to_tables' in dir(DataConversionFunctions)
78+
79+
# check the factory
80+
assert 'DataConversionFactory' in dir(xarray_data_accessor)
81+
from xarray_data_accessor import DataConversionFactory
82+
83+
assert DataConversionFunctions.get_factory()
84+
factory = DataConversionFunctions.get_factory()
85+
86+
assert isinstance(factory.get_functions(), dict)
87+
for name, func in factory.get_functions().items():
88+
assert isinstance(name, str)
89+
assert callable(func)

testing/test_3_era5_manipulations.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import pandas as pd
55
from pathlib import Path
66
import xarray_data_accessor
7+
from xarray_data_accessor import (
8+
DataConversionFunctions,
9+
)
710
import pytest
811
from rasterio.enums import Resampling
912
from typing import (
@@ -132,7 +135,7 @@ def test_to_table(test_dataset, test_dir) -> None:
132135
'.xlsx': pd.read_excel,
133136
}
134137
for suffix, func in suffix_dict.items():
135-
tables_dict = xarray_data_accessor.get_data_tables(
138+
tables_dict = DataConversionFunctions.points_to_tables(
136139
xarray_dataset=test_dataset,
137140
variables=None,
138141
# lon/lat coords, not perfectly aligned with grid

testing/test_5_gssha.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
"""Tests conversion to GSSHA format."""
2+
import xarray_data_accessor as xda
3+
import xarray as xr
4+
import pytest
5+
from pathlib import Path
6+
7+
8+
@pytest.fixture
9+
def test_dir() -> Path:
10+
"""Gets the test data directory."""
11+
TEST_DIR = Path.cwd() / 'testing/test_data'
12+
if not TEST_DIR.exists():
13+
TEST_DIR = Path.cwd() / 'test_data'
14+
return TEST_DIR
15+
16+
17+
@pytest.fixture
18+
def test_dataset(test_dir) -> xr.Dataset:
19+
"""Gets the test dataset."""
20+
# get test netcdf file into a xr.Dataset
21+
test_netcdf = test_dir / 'cds_era5_dataset.nc'
22+
ds = xr.open_dataset(test_netcdf)
23+
ds = ds.rio.write_crs(ds.attrs['EPSG'])
24+
return ds
25+
26+
27+
def test_factory() -> None:
28+
"""Make sure the function was correctly registered."""
29+
assert 'ConvertToGSSHA' in xda.DataConversionFactory.get_converter_classes().keys()
30+
gssha_obj = xda.DataConversionFactory.get_converter_classes()[
31+
'ConvertToGSSHA']
32+
assert isinstance(gssha_obj, object)
33+
assert issubclass(
34+
gssha_obj,
35+
xda.data_converters.base.DataConverterBase,
36+
)
37+
for func_name in gssha_obj.get_conversion_functions().keys():
38+
assert func_name in dir(xda.DataConversionFunctions)
39+
40+
41+
def test_precipitation_input(test_dataset) -> None:
42+
out_path = xda.DataConversionFunctions.make_gssha_precipitation_input(
43+
test_dataset,
44+
precipitation_variable='2m_temperature',
45+
)
46+
47+
assert out_path.exists()
48+
assert out_path.suffix == '.gag'
49+
out_path.unlink()
50+
51+
52+
def test_to_grass_ascii(test_dataset) -> None:
53+
54+
# test with correct HMET variable
55+
out_list = xda.DataConversionFunctions.make_gssha_grass_ascii(
56+
test_dataset,
57+
variable='2m_temperature',
58+
hmet_variable='Dry Bulb Temperature',
59+
start_time=test_dataset.time.values[0],
60+
end_time=test_dataset.time.values[1],
61+
)
62+
assert isinstance(out_list, list)
63+
assert len(out_list) == 2
64+
for file in out_list:
65+
assert file.exists()
66+
assert file.suffix == '.asc'
67+
file.unlink()
68+
69+
# test with incorrect HMET variable
70+
71+
72+
def test_hmet_wes_ascii(test_dataset) -> None:
73+
74+
out_path = xda.DataConversionFunctions.make_gssha_hmet_wes(
75+
test_dataset,
76+
variable_to_hmet={'2m_temperature': 'Dry Bulb Temperature'},
77+
start_time=test_dataset.time.values[0],
78+
end_time=test_dataset.time.values[1],
79+
file_suffix='.test',
80+
)
81+
assert out_path.exists()
82+
assert out_path.suffix == '.test'
83+
assert out_path.name.replace('.test', '') == 'hmet_wes'
84+
out_path.unlink()

0 commit comments

Comments
 (0)