Skip to content

Commit

Permalink
Merge pull request #32 from mlibrary/DWI-47-run-through-all-barcode-s…
Browse files Browse the repository at this point in the history
…teps

Dwi 47 run through all barcode steps
  • Loading branch information
niquerio authored Dec 9, 2024
2 parents 1ac772a + cf65049 commit ce86f56
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 5 deletions.
18 changes: 17 additions & 1 deletion aim/cli/digifeeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing_extensions import Annotated
from aim.digifeeds.database import models, main
from aim.digifeeds import functions
from aim.digifeeds.item import get_item
from aim.digifeeds.item import get_item, process_item
from aim.services import S

import json
Expand Down Expand Up @@ -130,3 +130,19 @@ def move_to_pickup(
message="Item has been successfully moved to pickup",
barcode=barcode,
)


@app.command()
def process_barcode(
barcode: Annotated[
str,
typer.Argument(help="The barcode to run the digifeeds process on"),
],
):
"""
Runs through the whole process for a barcode: adding it to the digifeeds set,
checking zephir, and moving the item to the pickup google drive.
"""

item = get_item(barcode)
process_item(item)
55 changes: 54 additions & 1 deletion aim/digifeeds/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
from requests.exceptions import HTTPError


class NotAddedToDigifeedsSetError(Exception):
pass


class Item:
"""A Digifeeds Item
Expand Down Expand Up @@ -138,5 +142,54 @@ def in_zephir_for_long_enough(self) -> bool:
return False


def get_item(barcode: str) -> Item:
def get_item(barcode: str) -> None:
return Item(DBClient().get_or_add_item(barcode))


def process_item(item: Item) -> Item:
barcode = item.barcode
S.logger.info(
"add_to_digifeeds_set_start",
message="Start adding item to digifeeds set",
barcode=barcode,
)
add_to_set_item = item.add_to_digifeeds_set()
if add_to_set_item.has_status("not_found_in_alma"):
S.logger.info(
"not_found_in_alma", message="Item not found in alma.", barcode=barcode
)

if add_to_set_item.has_status("added_to_digifeeds_set"):
S.logger.info(
"added_to_digifeeds_set",
message="Item added to digifeeds set",
barcode=barcode,
)
else:
S.logger.error(
"not_added_to_digifeeds_set",
message="Item NOT added to digifeeds set",
barcode=barcode,
)
raise NotAddedToDigifeedsSetError()

check_zephir_item = add_to_set_item.check_zephir()
if check_zephir_item:
S.logger.info("in_zephir", message="Item is in zephir", barcode=barcode)
else:
S.logger.info("not_in_zephir", message="Item is NOT in zephir", barcode=barcode)
return check_zephir_item

move_to_pickup_item = check_zephir_item.move_to_pickup()
if move_to_pickup_item is None:
S.logger.info(
"not_in_zephir_long_enough",
message="Item has not been in zephir long enough",
barcode=barcode,
)
else:
S.logger.info(
"move_to_pickup_success",
message="Item has been successfully moved to pickup",
barcode=barcode,
)
25 changes: 25 additions & 0 deletions tests/cli/test_digifeeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from aim.services import S
from aim.digifeeds.item import Item
from aim.digifeeds import functions
from datetime import datetime

runner = CliRunner()

Expand All @@ -18,6 +19,17 @@ def item_data():
return output


@pytest.fixture
def item_in_zephir_too_recent(item_data):
zephir_status = {
"name": "in_zephir",
"description": "Item is in zephir",
"created_at": datetime.now().isoformat(timespec="seconds"),
}
item_data["statuses"].append(zephir_status)
return item_data


@responses.activate
def test_add_to_db_where_item_is_not_in_digifeeds_set(item_data):
item_data["statuses"][0]["name"] = "in_zephir"
Expand Down Expand Up @@ -142,3 +154,16 @@ def test_move_to_pickup_where_not_in_zephir(mocker):

assert "not_in_zephir_long_enough" in result.stdout
assert result.exit_code == 0


def test_process_barcode(mocker, item_in_zephir_too_recent):
item = Item(item_in_zephir_too_recent)
mocker.patch("aim.cli.digifeeds.get_item", return_value=item)

result = runner.invoke(app, ["digifeeds", "process-barcode", "some_barcode"])

assert "add_to_digifeeds_set_start" in result.stdout
assert "added_to_digifeeds_set" in result.stdout
assert "in_zephir" in result.stdout
assert "not_in_zephir_long_enough" in result.stdout
assert result.exit_code == 0
59 changes: 56 additions & 3 deletions tests/digifeeds/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
import json
import responses
from datetime import datetime, timedelta
from aim.digifeeds.item import Item, rclone, DBClient
from aim.digifeeds.item import (
Item,
process_item,
rclone,
DBClient,
NotAddedToDigifeedsSetError,
)
from requests.exceptions import HTTPError
from aim.services import S

Expand All @@ -16,13 +22,18 @@ def item_data():

@pytest.fixture
def item_in_zephir_for_long_enough(item_data):
item_data["statuses"][0]["name"] = "in_zephir"
zephir_status = {
"name": "in_zephir",
"description": "Item is in zephir",
"created_at": "2024-09-25T17:13:28",
}
item_data["statuses"].append(zephir_status)
return item_data


@pytest.fixture
def item_in_zephir_too_recent(item_in_zephir_for_long_enough):
item_in_zephir_for_long_enough["statuses"][0]["created_at"] = (
item_in_zephir_for_long_enough["statuses"][1]["created_at"] = (
datetime.now().isoformat(timespec="seconds")
)
return item_in_zephir_for_long_enough
Expand Down Expand Up @@ -259,3 +270,45 @@ def test_move_to_pickup_item_too_recent(item_in_zephir_too_recent):
result = item.move_to_pickup()

assert result is None


def test_process_item_not_added_to_digifeeds_set_and_not_found_in_alma(
mocker, item_data
):
item_data["statuses"][0]["name"] = "not_found_in_alma"
item = Item(item_data)

item_mock = mocker.MagicMock(Item)
item_mock.barcode.return_value = "some_barcode"
item_mock.add_to_digifeeds_set.return_value = item

with pytest.raises(Exception) as exc_info:
process_item(item_mock)
assert exc_info.type is NotAddedToDigifeedsSetError


def test_process_item_not_in_zephir_long_enough(item_in_zephir_too_recent):
item = Item(item_in_zephir_too_recent)
result = process_item(item)
assert result is None


def test_process_item_not_in_zephir(mocker):
item_mock = mocker.MagicMock(Item)
item_mock.barcode.return_value = "some_barcode"
item_mock.add_to_digifeeds_set.return_value = item_mock
item_mock.check_zephir.return_value = None

result = process_item(item_mock)
assert result is None


def test_process_item_move_to_pickup(mocker):
item_mock = mocker.MagicMock(Item)
item_mock.barcode.return_value = "some_barcode"
item_mock.add_to_digifeeds_set.return_value = item_mock
item_mock.check_zephir.return_value = item_mock
item_mock.move_to_pickup.return_value = item_mock

result = process_item(item_mock)
assert result is None

0 comments on commit ce86f56

Please sign in to comment.