Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: GET /locations/id endpoint to get location details #37

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading