Skip to content

Commit

Permalink
add unit tests for silent=True/False
Browse files Browse the repository at this point in the history
  • Loading branch information
ks905383 committed Jun 3, 2024
1 parent 5568cca commit fd951e4
Showing 2 changed files with 81 additions and 3 deletions.
32 changes: 31 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
@@ -6,6 +6,8 @@
from geopandas import testing as gpdt
from unittest import TestCase
from shapely.geometry import Polygon
from unittest.mock import patch
from io import StringIO
try:
import xesmf as xe
_has_xesmf=True
@@ -14,7 +16,7 @@
_has_xesmf=False

from xagg.core import (process_weights,create_raster_polygons,get_pixel_overlaps,aggregate,NoOverlapError)

from xagg.options import set_options

##### process_weights() tests #####
def test_process_weights_null():
@@ -568,7 +570,35 @@ def test_aggregate_with_some_nans():



##### aggregate() silencing tests #####
# Create polygon covering multiple pixels
gdf = {'name':['test'],
'geometry':[Polygon([(0,0),(0,1),(1,1),(1,0),(0,0)])]}
gdf = gpd.GeoDataFrame(gdf,crs="EPSG:4326")

# Get pixel overlaps
with set_options(silent=True):
wm = get_pixel_overlaps(gdf,pix_agg)


@patch('sys.stdout', new_callable=StringIO)
def test_aggregate_silent_true(mock_stdout):
with set_options(silent=True):
# Get aggregate
agg = aggregate(ds,wm)


# Check that nothing was printed
assert mock_stdout.getvalue() == ''

@patch('sys.stdout', new_callable=StringIO)
def test_aggregate_silent_false(mock_stdout):
with set_options(silent=False):
# Get aggregate
agg = aggregate(ds,wm)


# Check that nothing was printed
assert mock_stdout.getvalue() != ''


52 changes: 50 additions & 2 deletions tests/test_wrappers.py
Original file line number Diff line number Diff line change
@@ -7,8 +7,11 @@
from geopandas import testing as gpdt
from unittest import TestCase
from shapely.geometry import Polygon
from unittest.mock import patch
from io import StringIO

from xagg.wrappers import (pixel_overlaps)
from xagg.wrappers import pixel_overlaps
from xagg.options import set_options


##### pixel_overlaps() tests #####
@@ -91,4 +94,49 @@ def test_pixel_overlaps_dataarray_wname():

assert np.allclose([v for v in df0.rel_area],[v for v in df_compare.rel_area])
assert np.allclose([v for v in df0.pix_idxs],[v for v in df_compare.pix_idxs])
assert np.allclose([v for v in df0.coords],[v for v in df_compare.coords])
assert np.allclose([v for v in df0.coords],[v for v in df_compare.coords])

##### pixel_overlaps() silent tests #####
@patch('sys.stdout', new_callable=StringIO)
def test_pixel_overlaps_silent_true(mock_stdout):

# Create dataarray
da = xr.DataArray(data=np.array([[0,1],[2,3]]),
coords={'lat':(['lat'],np.array([0,1])),
'lon':(['lon'],np.array([0,1]))},
dims=['lon','lat'],
name='tas')

# Create polygon covering one pixel
gdf_test = {'name':['test'],
'geometry':[Polygon([(-0.5,-0.5),(-0.5,0.5),(0.5,0.5),(0.5,-0.5),(-0.5,-0.5)])]}
gdf_test = gpd.GeoDataFrame(gdf_test,crs="EPSG:4326")

# Calculate pixel_overlaps through the wrapper function
with set_options(silent=True):
wm = pixel_overlaps(da,gdf_test)

# Check that nothing was printed
assert mock_stdout.getvalue() == ''


@patch('sys.stdout', new_callable=StringIO)
def test_pixel_overlaps_silent_false(mock_stdout):
# Create dataarray
da = xr.DataArray(data=np.array([[0,1],[2,3]]),
coords={'lat':(['lat'],np.array([0,1])),
'lon':(['lon'],np.array([0,1]))},
dims=['lon','lat'],
name='tas')

# Create polygon covering one pixel
gdf_test = {'name':['test'],
'geometry':[Polygon([(-0.5,-0.5),(-0.5,0.5),(0.5,0.5),(0.5,-0.5),(-0.5,-0.5)])]}
gdf_test = gpd.GeoDataFrame(gdf_test,crs="EPSG:4326")

# Calculate pixel_overlaps through the wrapper function
with set_options(silent=False):
wm = pixel_overlaps(da,gdf_test)

# Check that nothing was printed
assert mock_stdout.getvalue() != ''

0 comments on commit fd951e4

Please sign in to comment.