Skip to content

Commit

Permalink
Merge branch 'main' into feat/8-unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TAnas0 committed Sep 28, 2024
2 parents 3820207 + cffa292 commit 28cade2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ uvicorn src.main:app --reload

## Testing

To test using `curl` commands, you can try creating a Service Area, and check if a latitude/longitude pair is contained in the registered Service Areas:

```bash
curl -X 'POST' 'http://127.0.0.1:8000/api/v1/serviceareas/' -H 'Content-Type: application/json' -d '{
"name": "Downtown Area 2",
"price": 25.50,
"geojson": "{\"type\": \"Polygon\", \"coordinates\": [[[30.0, 10.0], [40.0, 40.0], [20.0, 40.0], [10.0, 20.0], [30.0, 10.0]]]}"
}'

curl -X GET "http://localhost:8000/api/v1/serviceareas/check?lat=30&lng=20"
```

To run the unit tests:
```bash
pytest
Expand Down
6 changes: 3 additions & 3 deletions src/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from sqlalchemy import Column, Integer, String, Float, CheckConstraint
from .database import Base
from geoalchemy2 import Geometry
from geoalchemy2 import Geometry, shape
from sqlalchemy.orm import validates
from shapely.geometry import shape

Expand Down Expand Up @@ -81,8 +81,8 @@ def validate_price(self, key, price):
def validate_geojson(self, key, geojson):
try:
# Parse and validate the GeoJSON structure
geojson_dict = json.loads(geojson)
geometry = shape(geojson_dict["geometry"])
from geoalchemy2.shape import to_shape
geometry = to_shape(geojson)

if geometry.geom_type != "Polygon":
raise ValueError("GeoJSON must represent a POLYGON")
Expand Down
2 changes: 1 addition & 1 deletion src/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def delete_service_area(service_area_id: int, db: Session = Depends(get_db)):
return service_area_to_dict(db_service_area)


@router.get("/serviceareas/check/", response_model=List[ServiceAreaSchema])
@router.get("/serviceareas/check", response_model=List[ServiceAreaSchema])
def check_point_in_service_area(lat: float, lng: float, db: Session = Depends(get_db)):
# Create a point geometry using the latitude and longitude
point = f"SRID=4326;POINT({lng} {lat})"
Expand Down

0 comments on commit 28cade2

Please sign in to comment.