diff --git a/aim/digifeeds/database/crud.py b/aim/digifeeds/database/crud.py index 24e414b..e0c6b9a 100644 --- a/aim/digifeeds/database/crud.py +++ b/aim/digifeeds/database/crud.py @@ -60,6 +60,24 @@ def get_items_query(db: Session, filter: schemas.ItemFilters = None): query = query.filter( ~models.Item.statuses.any(models.ItemStatus.status_name == "in_zephir") ) + elif filter == "pending_deletion": + query = query.filter( + models.Item.statuses.any( + models.ItemStatus.status_name == "pending_deletion" + ) + ) + elif filter == "not_pending_deletion": + query = query.filter( + ~models.Item.statuses.any( + models.ItemStatus.status_name == "pending_deletion" + ) + ) + elif filter == "not_found_in_alma": + query = query.filter( + models.Item.statuses.any( + models.ItemStatus.status_name == "not_found_in_alma" + ) + ) return query diff --git a/aim/digifeeds/database/schemas.py b/aim/digifeeds/database/schemas.py index 9678879..f806f9c 100644 --- a/aim/digifeeds/database/schemas.py +++ b/aim/digifeeds/database/schemas.py @@ -53,7 +53,7 @@ class ItemFilters(str, Enum): not_in_zephir = "not_in_zephir" pending_deletion = "pending_deletion" not_pending_deletion = "not_pending_deletion" - not_in_alma = "not_in_alma" + not_found_in_alma = "not_found_in_alma" class ItemCreate(ItemBase): diff --git a/tests/digifeeds/database/test_crud.py b/tests/digifeeds/database/test_crud.py index a2399b3..221b273 100644 --- a/tests/digifeeds/database/test_crud.py +++ b/tests/digifeeds/database/test_crud.py @@ -60,6 +60,47 @@ def test_get_items_and_total_not_in_zephir(self, db_session): assert (items[0]) == item2 assert count == 1 + def test_get_items_and_total_pending_deletion(self, db_session): + item1 = add_item(db=db_session, item=ItemCreate(barcode="valid_barcode")) + item2 = add_item(db=db_session, item=ItemCreate(barcode="valid_barcode2")) + status = get_status(db=db_session, name="pending_deletion") + add_item_status(db=db_session, item=item1, status=status) + items = get_items(db=db_session, filter="pending_deletion", limit=2, offset=0) + count = get_items_total(db=db_session, filter="pending_deletion") + db_session.refresh(item1) + db_session.refresh(item2) + assert (len(items)) == 1 + assert (items[0]) == item1 + assert count == 1 + + def test_get_items_and_total_not_pending_deletion(self, db_session): + item1 = add_item(db=db_session, item=ItemCreate(barcode="valid_barcode")) + item2 = add_item(db=db_session, item=ItemCreate(barcode="valid_barcode2")) + status = get_status(db=db_session, name="pending_deletion") + add_item_status(db=db_session, item=item1, status=status) + items = get_items( + db=db_session, filter="not_pending_deletion", limit=2, offset=0 + ) + count = get_items_total(db=db_session, filter="pending_deletion") + db_session.refresh(item1) + db_session.refresh(item2) + assert (len(items)) == 1 + assert (items[0]) == item2 + assert count == 1 + + def test_get_items_and_total_not_found_in_alma(self, db_session): + item1 = add_item(db=db_session, item=ItemCreate(barcode="valid_barcode")) + item2 = add_item(db=db_session, item=ItemCreate(barcode="valid_barcode2")) + status = get_status(db=db_session, name="not_found_in_alma") + add_item_status(db=db_session, item=item1, status=status) + items = get_items(db=db_session, filter="not_found_in_alma", limit=2, offset=0) + count = get_items_total(db=db_session, filter="not_found_in_alma") + db_session.refresh(item1) + db_session.refresh(item2) + assert (len(items)) == 1 + assert (items[0]) == item1 + assert count == 1 + def test_get_status_that_exists(self, db_session): status = get_status(db=db_session, name="in_zephir") assert (status.name) == "in_zephir"