Skip to content

Commit

Permalink
Raise a clear exception when the wrong type is passed to the function
Browse files Browse the repository at this point in the history
  • Loading branch information
mailletf committed Aug 4, 2024
1 parent 0904788 commit fbdb4e6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ fn polygon_to_geohashes(

let mut polygons = Vec::<Polygon<f64>>::new();

let geom = py_polygon.extract::<Geometry>()?;
let geom: Geometry = match py_polygon.extract::<Geometry>() {
Ok(geometry) => geometry,
Err(e) => {
return Err(pyo3::exceptions::PyValueError::new_err(
format!("Exception while trying to extract Geometry. This function requires a Shapely Polygon or MultiPolygon. ({:?})", e
)))
}
};

if let Err(e) = {
match geom.0 {
GtGeometry::Polygon(polygon) => {
Expand Down
10 changes: 8 additions & 2 deletions tests/test_geohasher.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
from polygon_geohasher.polygon_geohasher import (
polygon_to_geohashes as polygon_to_geohashes_py,
)
import pytest

# def test_invalid():
# assert geohash_polygon.polygon_to_geohashes("bouya", 3, True) == set()

def test_exception_when_invalid():
with pytest.raises(
ValueError,
match=r"Exception while trying to extract Geometry. This function requires a Shapely Polygon or MultiPolygon.*",
):
geohash_polygon.polygon_to_geohashes({}, 3, True)


def test_simple_polygon():
Expand Down

0 comments on commit fbdb4e6

Please sign in to comment.