Skip to content

Commit

Permalink
feat: add endpoint to retrieve product by code (#106)
Browse files Browse the repository at this point in the history
* Add endpoint /products/code/<code>

* Update tests
  • Loading branch information
raphodn authored Dec 29, 2023
1 parent ee2cbd8 commit 70f4567
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
17 changes: 16 additions & 1 deletion app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,27 @@ def get_user_proofs(
return crud.get_user_proofs(db, user=current_user)


@app.get(
"/api/v1/products/code/{product_code}",
response_model=schemas.ProductBase,
tags=["Products"],
)
def get_product_by_code(product_code: str, db: Session = Depends(get_db)):
db_product = crud.get_product_by_code(db, code=product_code)
if not db_product:
raise HTTPException(
status_code=404,
detail=f"Product with code {product_code} not found",
)
return db_product


@app.get(
"/api/v1/products/{product_id}",
response_model=schemas.ProductBase,
tags=["Products"],
)
def get_product(product_id: int, db: Session = Depends(get_db)):
def get_product_by_id(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(
Expand Down
13 changes: 9 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ def test_create_price(user, db=override_get_db()):
assert response.status_code == 201
assert response.json()["product_code"] == PRICE_1.product_code
assert "id" not in response.json()
assert "owner" not in response.json()
db_prices = crud.get_prices(next(db))
assert len(db_prices) == 1
# assert db_prices[0]["owner"] == user.user_id
Expand Down Expand Up @@ -238,7 +237,7 @@ def test_get_prices():
response = client.get("/api/v1/prices")
assert response.status_code == 200
assert len(response.json()["items"]) == 3
for price_field in ["product_id", "location_id", "proof_id"]:
for price_field in ["owner", "product_id", "location_id", "proof_id"]:
assert price_field in response.json()["items"][0]
for price_relationship in ["product", "location"]:
assert price_relationship in response.json()["items"][0]
Expand Down Expand Up @@ -325,12 +324,18 @@ def test_get_proofs(user):


def test_get_product(product):
# product exists
# by id: product exists
response = client.get(f"/api/v1/products/{product.id}")
assert response.status_code == 200
# product does not exist
# by id: product does not exist
response = client.get(f"/api/v1/products/{product.id+1}")
assert response.status_code == 404
# by code: product exists
response = client.get(f"/api/v1/products/code/{product.code}")
assert response.status_code == 200
# by code: product does not exist
response = client.get(f"/api/v1/products/code/{product.code+'X'}")
assert response.status_code == 404


def test_get_location(location):
Expand Down

0 comments on commit 70f4567

Please sign in to comment.