Skip to content

Commit

Permalink
feat: GET /locations/id endpoint to get location details (#37)
Browse files Browse the repository at this point in the history
* New endpoint to fetch location data per id

* Add tests & typing
  • Loading branch information
raphodn authored Nov 21, 2023
1 parent 003de11 commit 4a4eca1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
11 changes: 11 additions & 0 deletions app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@ def get_user_proofs(
return crud.get_user_proofs(db, user=current_user)


@app.get("/locations/{location_id}", response_model=schemas.LocationBase)
async def get_location(location_id: int, db: Session = Depends(get_db)):
db_location = crud.get_location_by_id(db, id=location_id)
if not db_location:
raise HTTPException(
status_code=404,
detail=f"Location with id {location_id} not found",
)
return db_location


@app.get("/status")
async def status_endpoint():
return {"status": "running"}
Expand Down
4 changes: 4 additions & 0 deletions app/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ def get_location_by_osm_id_and_type(
)


def get_location_by_id(db: Session, id: int):
return db.query(Location).filter(Location.id == id).first()


def create_location(db: Session, location: LocationCreate):
db_location = Location(**location.model_dump())
db.add(db_location)
Expand Down
21 changes: 19 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from app import crud
from app.api import app, get_db
from app.db import Base
from app.schemas import PriceCreate, UserBase
from app.schemas import LocationCreate, PriceCreate, UserBase

# database setup
# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -40,7 +40,7 @@ def override_get_db():
client = TestClient(app)

USER = UserBase(user_id="user1", token="user1__Utoken")

LOCATION = LocationCreate(osm_id=3344841823, osm_type="NODE")
PRICE_1 = PriceCreate(
product_code="1111111111111",
price=3.5,
Expand All @@ -57,6 +57,12 @@ def user(db=override_get_db()):
return db_user


@pytest.fixture(scope="module")
def location(db=override_get_db()):
db_location = crud.create_location(next(db), LOCATION)
return db_location


# Tests
# ------------------------------------------------------------------------------
def test_hello():
Expand Down Expand Up @@ -107,6 +113,8 @@ def test_get_prices():
response = client.get("/prices")
assert response.status_code == 200
assert len(response.json()["items"]) == 1
for price_field in ["location_id", "proof_id"]:
assert price_field in response.json()["items"][0]


def test_get_prices_pagination():
Expand Down Expand Up @@ -138,3 +146,12 @@ def test_get_proofs(user):
headers={"Authorization": f"Bearer {user.token}"},
)
assert response.status_code == 200


def test_get_location(location):
# location exists
response = client.get(f"/locations/{location.id}")
assert response.status_code == 200
# location does not exist
response = client.get(f"/locations/{location.id+1}")
assert response.status_code == 404

0 comments on commit 4a4eca1

Please sign in to comment.