From 3eba30a9e225bf9413935c0fee94c53f6bc9937d Mon Sep 17 00:00:00 2001 From: Monique Rio Date: Thu, 19 Sep 2024 17:13:18 +0000 Subject: [PATCH] adds tests for api interface --- aim/digifeeds/database/main.py | 8 +-- tests/conftest.py | 5 +- tests/digifeeds/database/test_main.py | 77 +++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 tests/digifeeds/database/test_main.py diff --git a/aim/digifeeds/database/main.py b/aim/digifeeds/database/main.py index c011f97..8b2b5db 100644 --- a/aim/digifeeds/database/main.py +++ b/aim/digifeeds/database/main.py @@ -11,7 +11,7 @@ app = FastAPI() # Dependency -def get_db(): +def get_db(): # pragma: no cover db = SessionLocal() try: yield db @@ -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") diff --git a/tests/conftest.py b/tests/conftest.py index bcb6939..9fb7637 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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 @@ -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) diff --git a/tests/digifeeds/database/test_main.py b/tests/digifeeds/database/test_main.py new file mode 100644 index 0000000..6c5da82 --- /dev/null +++ b/tests/digifeeds/database/test_main.py @@ -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"} \ No newline at end of file