Skip to content

Commit

Permalink
adds tests for api interface
Browse files Browse the repository at this point in the history
  • Loading branch information
niquerio committed Sep 19, 2024
1 parent f75ebbf commit 5da3d9c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
8 changes: 4 additions & 4 deletions aim/digifeeds/database/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
app = FastAPI()

# Dependency
def get_db():
def get_db(): # pragma: no cover
db = SessionLocal()
try:
yield db
Expand All @@ -29,11 +29,11 @@ def get_item(barcode: str, db: Session = Depends(get_db)) -> schemas.Item:
db_item = crud.get_item(barcode=barcode, db=db)
if db_item is None:
raise HTTPException(status_code=404, detail="Item not found")

return db_item

@app.post("/items/", response_model_by_alias=False)
def create_item(item: schemas.ItemCreate, db: Session = Depends(get_db)) -> schemas.Item:
@app.post("/items/{barcode}", response_model_by_alias=False)
def create_item(barcode: str, db: Session = Depends(get_db)) -> schemas.Item:
item = schemas.ItemCreate(barcode=barcode)
db_item = crud.get_item(barcode=item.barcode, db=db)
if db_item:
raise HTTPException(status_code=400, detail="Item already exists")
Expand Down
5 changes: 3 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sqlalchemy as sa
from sqlalchemy import StaticPool, create_engine
from sqlalchemy.orm import sessionmaker
from aim.digifeeds.database.main import app, get_db
from aim.digifeeds.database.models import Base, load_statuses, Status

from aim.services import S
Expand Down Expand Up @@ -72,9 +73,9 @@ def end_savepoint(session, transaction):
# dependency override as before, it uses the one provided by the
# session fixture.
@pytest.fixture()
def client(session):
def client(db_session):
def override_get_db():
yield session
yield db_session

app.dependency_overrides[get_db] = override_get_db
yield TestClient(app)
Expand Down
77 changes: 77 additions & 0 deletions tests/digifeeds/database/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from aim.digifeeds.database import crud, models
from aim.digifeeds.database.schemas import ItemCreate
import pytest

@pytest.fixture()
def valid_item(db_session):
return crud.add_item(db=db_session, item=ItemCreate(barcode="valid_barcode"))

@pytest.fixture()
def valid_in_zephir_item(db_session):
item = crud.add_item(db=db_session, item=ItemCreate(barcode="in_zephir_item"))
status = crud.get_status(db=db_session, name="in_zephir")
crud.add_item_status(db=db_session, item=item, status=status)
db_session.refresh(item)
return item

def test_get_statuses(client):
response = client.get("/statuses")
assert response.status_code == 200, response.text

def test_get_items(client, valid_item, valid_in_zephir_item, db_session):
valid_item
valid_in_zephir_item
response = client.get("/items")
assert response.status_code == 200, response.text
assert len(response.json()) == 2

def test_get_items_with_in_zephir_true(client, valid_item, valid_in_zephir_item):
valid_item
valid_in_zephir_item
response = client.get("/items", params={"in_zephir":True})
assert response.status_code == 200, response.text
assert len(response.json()) == 1
assert response.json()[0]["barcode"] == valid_in_zephir_item.barcode

def test_get_items_with_in_zephir_false(client, valid_item, valid_in_zephir_item):
valid_item
valid_in_zephir_item
response = client.get("/items", params={"in_zephir":False})
assert response.status_code == 200, response.text
assert len(response.json()) == 1
assert response.json()[0]["barcode"] == valid_item.barcode


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

def test_get_item_not_found(client):
response = client.get("/items/some_barcode_that_does_not_exist")
assert response.status_code == 404
assert response.json() == {"detail": "Item not found"}

def test_create_item(client):
response = client.post("items/new_barcode")
assert response.status_code == 200, response.text

def test_create_existing_item(client, valid_item):
response = client.post(f"items/{valid_item.barcode}")
assert response.status_code == 400
assert response.json() == {"detail": "Item already exists"}

def test_update_item_success(client, valid_item):
response = client.put(f"items/{valid_item.barcode}/status/in_zephir")
assert response.status_code == 200, response.text

def test_update_nonexisting_item(client):
response = client.put("/items/some_barcode_that_does_not_exist/status/in_zephir")
assert response.status_code == 404
assert response.json() == {"detail": "Item not found"}

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"}

0 comments on commit 5da3d9c

Please sign in to comment.