From fd951e41b128793121d787311cc5c20b34bd6e5f Mon Sep 17 00:00:00 2001 From: Kevin Schwarzwald Date: Mon, 3 Jun 2024 18:57:55 -0400 Subject: [PATCH] add unit tests for silent=True/False --- tests/test_core.py | 32 +++++++++++++++++++++++++- tests/test_wrappers.py | 52 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 3 deletions(-) diff --git a/tests/test_core.py b/tests/test_core.py index 84dc45b..793be90 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -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() != '' diff --git a/tests/test_wrappers.py b/tests/test_wrappers.py index 24666a2..f0360a2 100644 --- a/tests/test_wrappers.py +++ b/tests/test_wrappers.py @@ -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]) \ No newline at end of file + 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() != ''