Skip to content

Commit

Permalink
adds the cli command for check_zephir
Browse files Browse the repository at this point in the history
  • Loading branch information
niquerio committed Nov 8, 2024
1 parent 9a6a86f commit 2ebb3c6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
23 changes: 23 additions & 0 deletions aim/cli/digifeeds.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Digifeeds CLI
====================
"""

import typer
from typing_extensions import Annotated
from aim.digifeeds.add_to_db import add_to_db as add_to_digifeeds_db
from aim.digifeeds.list_barcodes_in_bucket import list_barcodes_in_bucket
from aim.digifeeds.check_zephir import check_zephir as check_zephir_for_barcode
from aim.digifeeds.database import models, main
import json
import sys
Expand Down Expand Up @@ -40,6 +42,27 @@ def add_to_db(
print("Item NOT added to digifeeds set")


@app.command()
def check_zephir(
barcode: Annotated[
str,
typer.Argument(
help="The barcode to check in zephir. It should NOT have mdp prefix. The barcode must already exist in the digifeeds database."
),
],
):
"""
Check if barcode has metadata in Zephir
"""

print(f"Checking Zephir for {barcode}")
item = check_zephir_for_barcode(barcode)
if item:
print(f"{barcode} is in Zephir")
else:
print(f"{barcode} is NOT in Zephir")


@app.command()
def load_statuses():
"""
Expand Down
34 changes: 30 additions & 4 deletions tests/cli/test_digifeeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,8 @@ def test_add_to_db_where_item_is_not_in_alma(item_data, mocker):


def test_load_statuses(mocker):
session_local_mock = mocker.patch(
"aim.digifeeds.database.main.SessionLocal")
load_statuse_mock = mocker.patch(
"aim.digifeeds.database.models.load_statuses")
session_local_mock = mocker.patch("aim.digifeeds.database.main.SessionLocal")
load_statuse_mock = mocker.patch("aim.digifeeds.database.models.load_statuses")
result = runner.invoke(app, ["digifeeds", "load-statuses"])
assert session_local_mock.call_count == 1
assert load_statuse_mock.call_count == 1
Expand All @@ -87,3 +85,31 @@ def test_list_barcodes_in_input_bucket(mocker):
assert list_barcodes_mock.call_count == 1
assert result.exit_code == 0
assert '["barcode1", "barcode2"]' == result.stdout


@responses.activate
def test_check_zephir_for_item_when_item_is_in_zephir(item_data):
db_url = f"{S.digifeeds_api_url}/items/some_barcode"
get_item = responses.get(db_url, json=item_data, status=200)
add_item_status = responses.put(
f"{db_url}/status/in_zephir", json=item_data, status=200
)
responses.get(f"{S.zephir_bib_api_url}/mdp.some_barcode", json={}, status=200)
result = runner.invoke(app, ["digifeeds", "check-zephir", "some_barcode"])
assert get_item.call_count == 1
assert add_item_status.call_count == 1
assert result.exit_code == 0
assert "some_barcode is in Zephir" in result.stdout


@responses.activate
def test_check_zephir_for_item_when_item_is_not_in_zephir(item_data):
db_url = f"{S.digifeeds_api_url}/items/some_barcode"
get_item = responses.get(db_url, json=item_data, status=200)
add_item_status = responses.put(f"{db_url}/status/in_zephir")
responses.get(f"{S.zephir_bib_api_url}/mdp.some_barcode", json={}, status=404)
result = runner.invoke(app, ["digifeeds", "check-zephir", "some_barcode"])
assert get_item.call_count == 1
assert add_item_status.call_count == 0
assert result.exit_code == 0
assert "some_barcode is NOT in Zephir" in result.stdout

0 comments on commit 2ebb3c6

Please sign in to comment.