Skip to content

Commit

Permalink
Refactor: Raise explicit errors with coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
fjugipe committed Feb 8, 2024
1 parent e87e51a commit 14e9bc1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
17 changes: 15 additions & 2 deletions api/routers/edr.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from shapely import buffer
from shapely import geometry
from shapely import wkt
from shapely.errors import GEOSException

router = APIRouter(prefix="/collections/observations")

Expand Down Expand Up @@ -100,8 +101,14 @@ async def get_data_position(
point = wkt.loads(coords)
assert point.geom_type == "Point"
poly = buffer(point, 0.0001, quad_segs=1) # Roughly 10 meters around the point
except GEOSException:
raise HTTPException(status_code=422, detail={"coords": f"Invalid or unparseable wkt provided: {coords}"})
except AssertionError:
raise HTTPException(status_code=422, detail={"coords": f"Invalid geometric type: {point.geom_type}"})
except Exception:
raise HTTPException(status_code=422, detail={"coords": "Invalid coordinates: {}".format(coords)})
raise HTTPException(
status_code=422, detail={"coords": f"Unexpected error occurred during wkt parsing: {coords}"}
)

return await get_data_area(poly.wkt, parameter_name, datetime, f)

Expand All @@ -121,8 +128,14 @@ async def get_data_area(
try:
poly = wkt.loads(coords)
assert poly.geom_type == "Polygon"
except GEOSException:
raise HTTPException(status_code=422, detail={"coords": f"Invalid or unparseable wkt provided: {coords}"})
except AssertionError:
raise HTTPException(status_code=422, detail={"coords": f"Invalid geometric type: {poly.geom_type}"})
except Exception:
raise HTTPException(status_code=422, detail={"coords": "Invalid coordinates: {}".format(coords)})
raise HTTPException(
status_code=422, detail={"coords": f"Unexpected error occurred during wkt parsing: {coords}"}
)

range = get_datetime_range(datetime)
get_obs_request = dstore.GetObsRequest(
Expand Down
12 changes: 6 additions & 6 deletions api/test/test_edr.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,16 @@ def test_get_area_with_incorrect_coords():
response = client.get("/collections/observations/area?coords=POLYGON((22.12 59.86, 24.39 60.41))")

assert response.status_code == 422
assert response.json() == {
"detail": {"coords": "Invalid or unparseable wkt provided: POLYGON((22.12 59.86, 24.39 60.41))"}
}


def test_get_area_with_incorrect_geometry_type():
response = client.get("/collections/observations/area?coords=POINT(22.12 59.86)")

assert response.status_code == 422
assert response.json() == {"detail": {"coords": "Invalid geometric type: Point"}}


def test_get_position_with_normal_query():
Expand Down Expand Up @@ -158,7 +162,7 @@ def test_get_position_with_incorrect_coords():
response = client.get("/collections/observations/position?coords=POINT(60.41)")

assert response.status_code == 422
assert response.json() == {"detail": {"coords": "Invalid coordinates: POINT(60.41)"}}
assert response.json() == {"detail": {"coords": "Invalid or unparseable wkt provided: POINT(60.41)"}}


def test_get_position_with_incorrect_geometry_type():
Expand All @@ -168,11 +172,7 @@ def test_get_position_with_incorrect_geometry_type():
)

assert response.status_code == 422
assert response.json() == {
"detail": {
"coords": "Invalid coordinates: POLYGON((22.12 59.86, 24.39 60.41, 24.39 60.41, 24.39 59.86, 22.12 59.86))"
}
}
assert response.json() == {"detail": {"coords": "Invalid geometric type: Polygon"}}


def create_mock_obs_response(json_data):
Expand Down

1 comment on commit 14e9bc1

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API Unit Test Coverage Report
FileStmtsMissCoverMissing
\_\_init\_\_.py00100% 
datastore_pb2.py584621%23–68
datastore_pb2_grpc.py432347%37–52, 85–87, 92–94, 99–101, 106–108, 112–136, 174, 191, 208, 225
dependencies.py31487%23–24, 31, 38
grpc_getter.py8450%12–16
locustfile.py15150%1–31
main.py22386%27, 37, 47
metadata_endpoints.py19479%16, 33–66, 70
formatters
   \_\_init\_\_.py120100% 
   base_formatter.py7186%18
   covjson.py52198%69
routers
   \_\_init\_\_.py00100% 
   edr.py631183%34–57, 108–109, 135–136
   records.py00100% 
test
   test_covjson.py600100% 
   test_edr.py940100% 
TOTAL48411277% 

API Unit Test Coverage Summary

Tests Skipped Failures Errors Time
15 0 💤 0 ❌ 0 🔥 1.589s ⏱️

Please sign in to comment.