From 74f011ce1d112445f18a132de060edf612683e53 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Wed, 22 Nov 2023 12:02:11 +0100 Subject: [PATCH] feat: GET /products/id endpoint to get product details (#46) * New endpoint to fetch product data per id * Add tests * Remove async (not supported yet) --- app/api.py | 21 ++++++++++++++++----- app/crud.py | 12 ++++++++---- tests/test_api.py | 18 +++++++++++++++++- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/app/api.py b/app/api.py index 1bd5747d..3d3c6c3c 100644 --- a/app/api.py +++ b/app/api.py @@ -95,7 +95,7 @@ def main_page(request: Request): @app.post("/auth") -async def authentication( +def authentication( form_data: Annotated[OAuth2PasswordRequestForm, Depends()], db: Session = Depends(get_db), ): @@ -134,7 +134,7 @@ async def authentication( @app.get("/prices", response_model=Page[schemas.PriceBase]) -async def get_price( +def get_price( filters: schemas.PriceFilter = FilterDepends(schemas.PriceFilter), db: Session = Depends(get_db), ): @@ -142,7 +142,7 @@ async def get_price( @app.post("/prices", response_model=schemas.PriceBase) -async def create_price( +def create_price( price: schemas.PriceCreate, background_tasks: BackgroundTasks, current_user: schemas.UserBase = Depends(get_current_user), @@ -208,8 +208,19 @@ def get_user_proofs( return crud.get_user_proofs(db, user=current_user) +@app.get("/products/{product_id}", response_model=schemas.ProductBase) +def get_product(product_id: int, db: Session = Depends(get_db)): + db_product = crud.get_product_by_id(db, id=product_id) + if not db_product: + raise HTTPException( + status_code=404, + detail=f"Product with id {product_id} not found", + ) + return db_product + + @app.get("/locations/{location_id}", response_model=schemas.LocationBase) -async def get_location(location_id: int, db: Session = Depends(get_db)): +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( @@ -220,7 +231,7 @@ async def get_location(location_id: int, db: Session = Depends(get_db)): @app.get("/status") -async def status_endpoint(): +def status_endpoint(): return {"status": "running"} diff --git a/app/crud.py b/app/crud.py index ed087a97..6804252a 100644 --- a/app/crud.py +++ b/app/crud.py @@ -70,6 +70,10 @@ def delete_user(db: Session, user_id: UserBase): # Products # ------------------------------------------------------------------------------ +def get_product_by_id(db: Session, id: int): + return db.query(Product).filter(Product.id == id).first() + + def get_product_by_code(db: Session, code: str): return db.query(Product).filter(Product.code == code).first() @@ -195,6 +199,10 @@ def create_proof_file(file: UploadFile) -> tuple[str, str]: # Locations # ------------------------------------------------------------------------------ +def get_location_by_id(db: Session, id: int): + return db.query(Location).filter(Location.id == id).first() + + def get_location_by_osm_id_and_type( db: Session, osm_id: int, osm_type: LocationOSMType ): @@ -206,10 +214,6 @@ 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) diff --git a/tests/test_api.py b/tests/test_api.py index 57d2cd0c..d4db62aa 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -8,7 +8,7 @@ from app import crud from app.api import app, get_db from app.db import Base -from app.schemas import LocationCreate, PriceCreate, UserBase +from app.schemas import LocationCreate, PriceCreate, ProductCreate, UserBase # database setup # ------------------------------------------------------------------------------ @@ -40,6 +40,7 @@ def override_get_db(): client = TestClient(app) USER = UserBase(user_id="user1", token="user1__Utoken") +PRODUCT = ProductCreate(code="8001505005707") LOCATION = LocationCreate(osm_id=3344841823, osm_type="NODE") PRICE_1 = PriceCreate( product_code="1111111111111", @@ -57,6 +58,12 @@ def user(db=override_get_db()): return db_user +@pytest.fixture(scope="module") +def product(db=override_get_db()): + db_product = crud.create_product(next(db), PRODUCT) + return db_product + + @pytest.fixture(scope="module") def location(db=override_get_db()): db_location = crud.create_location(next(db), LOCATION) @@ -148,6 +155,15 @@ def test_get_proofs(user): assert response.status_code == 200 +def test_get_product(product): + # product exists + response = client.get(f"/products/{product.id}") + assert response.status_code == 200 + # product does not exist + response = client.get(f"/products/{product.id+1}") + assert response.status_code == 404 + + def test_get_location(location): # location exists response = client.get(f"/locations/{location.id}")