Skip to content

Commit

Permalink
more tests; pin shapely=1.8 in bare-minimum CI (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathause committed Nov 29, 2023
1 parent 894fbce commit 5975cd4
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 6 deletions.
1 change: 0 additions & 1 deletion ci/requirements/py310-shapely_lt_2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ dependencies:
- pygeos
- rasterio
- shapely<2
- setuptools
- xarray
# for testing
- pytest
Expand Down
2 changes: 1 addition & 1 deletion ci/requirements/py39-bare-minimum.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies:
- packaging
- pooch
- rasterio
- setuptools
- shapely=1.8 # fixing shapely for now
- xarray
# for testing
- pytest
Expand Down
2 changes: 1 addition & 1 deletion regionmask/core/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def _from_mapping(lon_or_obj, name):
"directly"
)

msg += "." if has_cf_xarray else "or try installing cf_xarray."
msg += "." if has_cf_xarray else " or try installing cf_xarray."

raise KeyError(msg)

Expand Down
2 changes: 2 additions & 0 deletions regionmask/tests/test_OneRegion.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def test_attributes():

assert np.allclose(r.centroid, (0.5, 0.5))

assert r.__repr__() == "<regionmask._OneRegion: Unit Square (USq / 1)>"


def test_polygon_input():

Expand Down
19 changes: 16 additions & 3 deletions regionmask/tests/test_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,17 @@ def test_mask_3D_overlap_default(drop, method):


@pytest.mark.parametrize("method", MASK_METHODS)
def test_mask_3D_overlap_empty(method):
@pytest.mark.parametrize("overlap", (True, False))
def test_mask_3D_overlap_empty(method, overlap):

region = copy.copy(dummy_region_overlap)
region.overlap = overlap

match = "No gridpoint belongs to any region. "

lon = lat = [10, 11]
with pytest.warns(UserWarning, match=match + "Returning an empty mask"):
result = dummy_region_overlap.mask_3D(lon, lat, drop=True, method=method)
result = region.mask_3D(lon, lat, drop=True, method=method)

coords = {"lat": lat, "lon": lon}
expected = expected_mask_3D(False, coords=coords, overlap=True).isel(
Expand All @@ -437,7 +441,7 @@ def test_mask_3D_overlap_empty(method):
xr.testing.assert_equal(result, expected)

with pytest.warns(UserWarning, match=match + "Returning an all-False mask."):
result = dummy_region_overlap.mask_3D(lon, lat, drop=False, method=method)
result = region.mask_3D(lon, lat, drop=False, method=method)

assert result.shape == (4, 2, 2)
assert not result.any()
Expand All @@ -461,6 +465,15 @@ def test_mask_overlap_unstructured(drop, method):
xr.testing.assert_equal(result, expected)


@pytest.mark.parametrize("flag", ("foo", "bar"))
def test_mask_flag_error_wrong_flagname(flag):

with pytest.raises(
ValueError, match="`flag` must be one of `None`, `'abbrevs'` and `'names'`"
):
dummy_region.mask(dummy_ds, flag=flag)


def test_mask_flag():

expected = expected_mask_2D()
Expand Down
26 changes: 26 additions & 0 deletions regionmask/tests/test_mask_cf_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@
MASK_METHODS = ["mask", "mask_3D"]


@requires_cf_xarray
@pytest.mark.parametrize("method", MASK_METHODS)
def test_mask_get_coords_missing(method):

mask = getattr(dummy_region, method)

with pytest.raises(KeyError, match="'Could not get ``lon`` from ``lon_or_obj``"):
mask({})

with pytest.raises(KeyError, match="'Could not get ``lat`` from ``lon_or_obj``"):
mask({"lon": [1]})


@pytest.mark.skipif(has_cf_xarray, reason="must not have cf_xarray")
@pytest.mark.parametrize("method", MASK_METHODS)
def test_mask_get_coords_missing_no_cf_xarray(method):

mask = getattr(dummy_region, method)

with pytest.raises(KeyError, match="try installing cf_xarray"):
mask({})

with pytest.raises(KeyError, match="try installing cf_xarray"):
mask({"lon": [1]})


@pytest.mark.skipif(has_cf_xarray, reason="must not have cf_xarray")
@pytest.mark.parametrize("method", MASK_METHODS)
def test_mask_use_cf_required(method):
Expand Down

0 comments on commit 5975cd4

Please sign in to comment.