Skip to content

Commit

Permalink
Changes None return to NotFoundError
Browse files Browse the repository at this point in the history
also adds missing tests
  • Loading branch information
niquerio committed Nov 20, 2024
1 parent d1641a3 commit 34d6fb7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 24 deletions.
17 changes: 12 additions & 5 deletions aim/digifeeds/database/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from sqlalchemy.orm import Session, joinedload
from aim.digifeeds.database import schemas
from aim.digifeeds.database import models
from aim.services import S


class NotFoundError(Exception):
Expand Down Expand Up @@ -36,7 +35,11 @@ def get_item(db: Session, barcode: str):
)
)

return db.scalars(stmnt).first()
item = db.scalars(stmnt).first()
if item is None:
raise NotFoundError()
else:
return item


def get_items_total(db: Session, filter: schemas.ItemFilters = None):
Expand Down Expand Up @@ -123,7 +126,13 @@ def get_status(db: Session, name: str):
Returns:
aim.digifeeds.database.models.Status: Status object
"""
return db.query(models.Status).filter(models.Status.name == name).first()
stmnt = select(models.Status).filter_by(name=name)

status = db.scalars(stmnt).first()
if status is None:
raise NotFoundError()
else:
return status


def get_statuses(db: Session):
Expand Down Expand Up @@ -158,8 +167,6 @@ def add_item_status(db: Session, item: models.Item, status: models.Status):

def delete_item(db: Session, barcode: str):
db_item = get_item(db=db, barcode=barcode)
if db_item is None:
raise NotFoundError()
# need to load this now so the statuses show up in the return
item = schemas.Item(**db_item.__dict__)
db.delete(db_item)
Expand Down
28 changes: 17 additions & 11 deletions aim/digifeeds/database/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ def get_item(
The item can be fetched by the barcode of the item.
"""

db_item = crud.get_item(barcode=barcode, db=db)
if db_item is None:
try:
db_item = crud.get_item(barcode=barcode, db=db)
except NotFoundError:
raise HTTPException(status_code=404, detail="Item not found")
return db_item

Expand All @@ -115,11 +116,13 @@ def create_item(
"""

item = schemas.ItemCreate(barcode=barcode)
db_item = crud.get_item(barcode=item.barcode, db=db)
if db_item:
try:
crud.get_item(barcode=item.barcode, db=db)
except NotFoundError:
db_item = crud.add_item(item=item, db=db)
return db_item
else:
raise HTTPException(status_code=400, detail="Item already exists")
db_item = crud.add_item(item=item, db=db)
return db_item


desc_put_404 = """
Expand Down Expand Up @@ -152,16 +155,19 @@ def update_item(
This is how to add a status to an existing item.
"""

db_status = crud.get_status(name=status_name, db=db)
if db_status is None:
try:
db_status = crud.get_status(name=status_name, db=db)
except NotFoundError:
raise HTTPException(status_code=404, detail="Status not found")
db_item = crud.get_item(barcode=barcode, db=db)
if db_item is None:

try:
db_item = crud.get_item(barcode=barcode, db=db)
except NotFoundError:
raise HTTPException(status_code=404, detail="Item not found")

return crud.add_item_status(db=db, item=db_item, status=db_status)


# TODO this doesn't properly show the list of statuses upon deletion. I probably don't care.
@app.delete(
"/items/{barcode}",
response_model_by_alias=False,
Expand Down
28 changes: 20 additions & 8 deletions tests/digifeeds/database/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
add_item_status,
get_items_total,
delete_item,
NotFoundError,
)
from aim.digifeeds.database import models
from sqlalchemy import select
from aim.digifeeds.database.schemas import ItemCreate
from aim.digifeeds.database.models import ItemStatus
import pytest


class TestCrud:
Expand All @@ -20,8 +23,9 @@ def test_get_item(self, db_session):
assert (item_in_db.barcode) == "valid_barcode"

def test_get_item_that_does_not_exist(self, db_session):
item_in_db = get_item(barcode="does not exist", db=db_session)
assert (item_in_db) is None
with pytest.raises(Exception) as exc_info:
get_item(barcode="does not exist", db=db_session)
assert exc_info.type is NotFoundError

def test_get_items_and_total_any(self, db_session):
item1 = add_item(db=db_session, item=ItemCreate(barcode="valid_barcode"))
Expand Down Expand Up @@ -108,8 +112,9 @@ def test_get_status_that_exists(self, db_session):
assert (status.name) == "in_zephir"

def test_get_status_that_does_not_exist(self, db_session):
status = get_status(db=db_session, name="does_not_exist")
assert (status) is None
with pytest.raises(Exception) as exc_info:
get_status(name="does not exist", db=db_session)
assert exc_info.type is NotFoundError

def test_get_statuses(self, db_session):
statuses = get_statuses(db=db_session)
Expand All @@ -121,7 +126,14 @@ def test_delete_item(self, db_session):
status = get_status(db=db_session, name="in_zephir")
add_item_status(db=db_session, item=item, status=status)
delete_item(db=db_session, barcode=item.barcode)
item_result = get_item(db=db_session, barcode=item.barcode)
item_statuses = db_session.query(ItemStatus).all()
assert item_result is None
item_result = db_session.scalars(
select(models.Item).filter_by(barcode=item.barcode)
).all()
item_statuses = db_session.scalars(select(models.ItemStatus)).all()
assert item_result == []
assert item_statuses == []

def test_delete_non_existent_item(self, db_session):
with pytest.raises(Exception) as exc_info:
delete_item(barcode="does not exist", db=db_session)
assert exc_info.type is NotFoundError
11 changes: 11 additions & 0 deletions tests/digifeeds/database/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,14 @@ def test_update_existing_item_with_nonexistent_status(client, valid_item):
response = client.put(f"/items/{valid_item.barcode}/status/non_existent_status")
assert response.status_code == 404
assert response.json() == {"detail": "Status not found"}


def test_delete_item(client, valid_item):
response = client.delete(f"/items/{valid_item.barcode}")
assert response.status_code == 200, response.text


def test_delete_not_existent_item(client):
response = client.delete("/items/barcode_does_not_exist")
assert response.status_code == 404
assert response.json() == {"detail": "Item not found"}

0 comments on commit 34d6fb7

Please sign in to comment.