diff --git a/aim/cli/digifeeds.py b/aim/cli/digifeeds.py index 2bbffdc..0bba685 100644 --- a/aim/cli/digifeeds.py +++ b/aim/cli/digifeeds.py @@ -4,11 +4,9 @@ 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.move_to_pickup import move_to_pickup as move_volume_to_pickup from aim.digifeeds.database import models, main +from aim.digifeeds import functions + import json import sys @@ -17,7 +15,7 @@ @app.command() -def add_to_db( +def add_to_digifeeds_set( barcode: Annotated[ str, typer.Argument(help="The barcode to be added to the database"), @@ -34,7 +32,7 @@ def add_to_db( barcode (str): Barcode of item """ print(f'Adding barcode "{barcode}" to database') - item = add_to_digifeeds_db(barcode) + item = functions.add_to_digifeeds_set(barcode) if item.has_status("not_found_in_alma"): print("Item not found in alma.") if item.has_status("added_to_digifeeds_set"): @@ -57,7 +55,7 @@ def check_zephir( """ print(f"Checking Zephir for {barcode}") - item = check_zephir_for_barcode(barcode) + item = functions.check_zephir(barcode) if item: print(f"{barcode} is in Zephir") else: @@ -78,7 +76,7 @@ def list_barcodes_in_input_bucket(): """ List the barcodes currently in the input directory in the S3 bucket. """ - json.dump(list_barcodes_in_bucket(), sys.stdout) + json.dump(functions.list_barcodes_in_input_bucket(), sys.stdout) @app.command() @@ -94,7 +92,7 @@ def move_to_pickup( folder in the bucket and prefixed with the date and time. """ print(f'Moving barcode "{barcode}" from the s3 bucket to the google drive') - item = move_volume_to_pickup(barcode) + item = functions.move_to_pickup(barcode) if item is None: print("Item has not been in zephir long enough") else: diff --git a/aim/digifeeds/add_to_db.py b/aim/digifeeds/add_to_db.py deleted file mode 100644 index f411c58..0000000 --- a/aim/digifeeds/add_to_db.py +++ /dev/null @@ -1,43 +0,0 @@ -from aim.digifeeds.alma_client import AlmaClient -from aim.digifeeds.db_client import DBClient -from aim.digifeeds.item import Item -from requests.exceptions import HTTPError - - -def add_to_db(barcode: str): - """Add a barcode to the digifeeds database - - Args: - barcode (str): Barcode of the item - - Raises: - ext_inst: HTTPError - - Returns: - aim.digifeeds.database.models.Item: Item object - """ - item = Item(DBClient().get_or_add_item(barcode)) - if not item.has_status("added_to_digifeeds_set"): - try: - AlmaClient().add_barcode_to_digifeeds_set(barcode) - except HTTPError as ext_inst: - errorList = ext_inst.response.json()["errorList"]["error"] - if any(e["errorCode"] == "60120" for e in errorList): - if not item.has_status("not_found_in_alma"): - item = Item( - DBClient().add_item_status( - barcode=barcode, status="not_found_in_alma" - ) - ) - return item - elif any(e["errorCode"] == "60115" for e in errorList): - # 60115 means the barcode is already in the set. That means the - # db entry from this barcdoe needs to have - # added_to_digifeeds_set - pass - else: - raise ext_inst - item = Item( - DBClient().add_item_status(barcode=barcode, status="added_to_digifeeds_set") - ) - return item diff --git a/aim/digifeeds/check_zephir.py b/aim/digifeeds/check_zephir.py deleted file mode 100644 index 2b93fa8..0000000 --- a/aim/digifeeds/check_zephir.py +++ /dev/null @@ -1,22 +0,0 @@ -from aim.services import S -from aim.digifeeds.db_client import DBClient -from aim.digifeeds.item import Item -import requests - - -def check_zephir(barcode: str): - raw_item = DBClient().get_item(barcode) - if raw_item is None: - raise Exception("Item not found in database") - - item = Item(raw_item) - - if item.has_status("in_zephir"): - return item - - response = requests.get(f"{S.zephir_bib_api_url}/mdp.{barcode}") - if response.status_code == 200: - DBClient().add_item_status(barcode=barcode, status="in_zephir") - return item - else: - return None diff --git a/aim/digifeeds/functions.py b/aim/digifeeds/functions.py new file mode 100644 index 0000000..87b9b44 --- /dev/null +++ b/aim/digifeeds/functions.py @@ -0,0 +1,102 @@ +from aim.services import S +from aim.digifeeds.db_client import DBClient +from aim.digifeeds.item import Item +import requests +import boto3 +from pathlib import Path +from rclone_python import rclone +from datetime import datetime +from aim.digifeeds.alma_client import AlmaClient +from requests.exceptions import HTTPError + + +def add_to_digifeeds_set(barcode: str): + """ + Adds a barcode to the digifeeds set and to the digifeeds database if it + doesn't already exist. If the barcode is in the digifeeds set already but + doesn't have an added_to_digifeeds_set status in the digifeeds db, that + status gets added. + + Args: + barcode (str): Barcode of the item + + Raises: + ext_inst: HTTPError + + Returns: + aim.digifeeds.database.models.Item: Item object + """ + item = Item(DBClient().get_or_add_item(barcode)) + if not item.has_status("added_to_digifeeds_set"): + try: + AlmaClient().add_barcode_to_digifeeds_set(barcode) + except HTTPError as ext_inst: + errorList = ext_inst.response.json()["errorList"]["error"] + if any(e["errorCode"] == "60120" for e in errorList): + if not item.has_status("not_found_in_alma"): + item = Item( + DBClient().add_item_status( + barcode=barcode, status="not_found_in_alma" + ) + ) + return item + elif any(e["errorCode"] == "60115" for e in errorList): + # 60115 means the barcode is already in the set. That means the + # db entry from this barcdoe needs to have + # added_to_digifeeds_set + pass + else: + raise ext_inst + item = Item( + DBClient().add_item_status(barcode=barcode, status="added_to_digifeeds_set") + ) + return item + + +def move_to_pickup(barcode: str): + item = Item(DBClient().get_or_add_item(barcode)) + + if not item.in_zephir_for_long_enough: + return None + + DBClient().add_item_status(barcode=barcode, status="copying_start") + rclone.copyto( + f"{S.digifeeds_s3_rclone_remote}:{S.digifeeds_s3_input_path}/{barcode}.zip", + f"{S.digifeeds_gdrive_rclone_remote}:{barcode}.zip", + ) + DBClient().add_item_status(barcode=barcode, status="copying_end") + timestamp = datetime.now().strftime("%F_%H-%M-%S") + rclone.moveto( + f"{S.digifeeds_s3_rclone_remote}:{S.digifeeds_s3_input_path}/{barcode}.zip", + f"{S.digifeeds_s3_rclone_remote}:{S.digifeeds_s3_processed_path}/{timestamp}_{barcode}.zip", + ) + final_raw_item = DBClient().add_item_status( + barcode=barcode, status="pending_deletion" + ) + + return final_raw_item + + +def list_barcodes_in_input_bucket(): + s3 = boto3.client( + "s3", + aws_access_key_id=S.digifeeds_s3_access_key, + aws_secret_access_key=S.digifeeds_s3_secret_access_key, + ) + prefix = S.digifeeds_s3_input_path + "/" + response = s3.list_objects_v2(Bucket=S.digifeeds_s3_bucket, Prefix=prefix) + barcodes = [Path(object["Key"]).stem for object in response["Contents"]] + return barcodes + + +def check_zephir(barcode: str): + item = Item(DBClient().get_or_add_item(barcode)) + if item.has_status("in_zephir"): + return item + + response = requests.get(f"{S.zephir_bib_api_url}/mdp.{barcode}") + if response.status_code == 200: + DBClient().add_item_status(barcode=barcode, status="in_zephir") + return item + else: + return None diff --git a/aim/digifeeds/list_barcodes_in_bucket.py b/aim/digifeeds/list_barcodes_in_bucket.py deleted file mode 100644 index 18002ce..0000000 --- a/aim/digifeeds/list_barcodes_in_bucket.py +++ /dev/null @@ -1,15 +0,0 @@ -import boto3 -from aim.services import S -from pathlib import Path - - -def list_barcodes_in_bucket(): - s3 = boto3.client( - "s3", - aws_access_key_id=S.digifeeds_s3_access_key, - aws_secret_access_key=S.digifeeds_s3_secret_access_key, - ) - prefix = S.digifeeds_s3_input_path + "/" - response = s3.list_objects_v2(Bucket=S.digifeeds_s3_bucket, Prefix=prefix) - barcodes = [Path(object["Key"]).stem for object in response["Contents"]] - return barcodes diff --git a/aim/digifeeds/move_to_pickup.py b/aim/digifeeds/move_to_pickup.py deleted file mode 100644 index d3a6376..0000000 --- a/aim/digifeeds/move_to_pickup.py +++ /dev/null @@ -1,33 +0,0 @@ -from rclone_python import rclone -from aim.digifeeds.item import Item -from aim.digifeeds.db_client import DBClient -from aim.services import S -from datetime import datetime - - -def move_to_pickup(barcode: str): - raw_item = DBClient().get_item(barcode) - if raw_item is None: - raise Exception("Item not found in database") - - item = Item(raw_item) - - if not item.in_zephir_for_long_enough: - return None - - DBClient().add_item_status(barcode=barcode, status="copying_start") - rclone.copyto( - f"{S.digifeeds_s3_rclone_remote}:{S.digifeeds_s3_input_path}/{barcode}.zip", - f"{S.digifeeds_gdrive_rclone_remote}:{barcode}.zip", - ) - DBClient().add_item_status(barcode=barcode, status="copying_end") - timestamp = datetime.now().strftime("%F_%H-%M-%S") - rclone.moveto( - f"{S.digifeeds_s3_rclone_remote}:{S.digifeeds_s3_input_path}/{barcode}.zip", - f"{S.digifeeds_s3_rclone_remote}:{S.digifeeds_s3_processed_path}/{timestamp}_{barcode}.zip", - ) - final_raw_item = DBClient().add_item_status( - barcode=barcode, status="pending_deletion" - ) - - return final_raw_item diff --git a/docs/api/aim.digifeeds.add_to_db.rst b/docs/api/aim.digifeeds.add_to_db.rst deleted file mode 100644 index 8b8406c..0000000 --- a/docs/api/aim.digifeeds.add_to_db.rst +++ /dev/null @@ -1,7 +0,0 @@ -aim.digifeeds.add\_to\_db module -================================ - -.. automodule:: aim.digifeeds.add_to_db - :members: - :undoc-members: - :show-inheritance: diff --git a/docs/api/aim.digifeeds.check_zephir.rst b/docs/api/aim.digifeeds.check_zephir.rst deleted file mode 100644 index 5beaa2a..0000000 --- a/docs/api/aim.digifeeds.check_zephir.rst +++ /dev/null @@ -1,7 +0,0 @@ -aim.digifeeds.check\_zephir module -================================== - -.. automodule:: aim.digifeeds.check_zephir - :members: - :undoc-members: - :show-inheritance: diff --git a/docs/api/aim.digifeeds.functions.rst b/docs/api/aim.digifeeds.functions.rst new file mode 100644 index 0000000..ea51545 --- /dev/null +++ b/docs/api/aim.digifeeds.functions.rst @@ -0,0 +1,7 @@ +aim.digifeeds.functions module +============================== + +.. automodule:: aim.digifeeds.functions + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/api/aim.digifeeds.list_barcodes_in_bucket.rst b/docs/api/aim.digifeeds.list_barcodes_in_bucket.rst deleted file mode 100644 index 93a2f66..0000000 --- a/docs/api/aim.digifeeds.list_barcodes_in_bucket.rst +++ /dev/null @@ -1,7 +0,0 @@ -aim.digifeeds.list\_barcodes\_in\_bucket module -=============================================== - -.. automodule:: aim.digifeeds.list_barcodes_in_bucket - :members: - :undoc-members: - :show-inheritance: diff --git a/docs/api/aim.digifeeds.move_to_pickup.rst b/docs/api/aim.digifeeds.move_to_pickup.rst deleted file mode 100644 index 8a5ccbd..0000000 --- a/docs/api/aim.digifeeds.move_to_pickup.rst +++ /dev/null @@ -1,7 +0,0 @@ -aim.digifeeds.move\_to\_pickup module -===================================== - -.. automodule:: aim.digifeeds.move_to_pickup - :members: - :undoc-members: - :show-inheritance: diff --git a/docs/api/aim.digifeeds.rst b/docs/api/aim.digifeeds.rst index 9380920..a56a99b 100644 --- a/docs/api/aim.digifeeds.rst +++ b/docs/api/aim.digifeeds.rst @@ -20,10 +20,7 @@ Submodules .. toctree:: :maxdepth: 4 - aim.digifeeds.add_to_db aim.digifeeds.alma_client - aim.digifeeds.check_zephir aim.digifeeds.db_client + aim.digifeeds.functions aim.digifeeds.item - aim.digifeeds.list_barcodes_in_bucket - aim.digifeeds.move_to_pickup diff --git a/tests/cli/test_digifeeds.py b/tests/cli/test_digifeeds.py index 4e729cc..21a4053 100644 --- a/tests/cli/test_digifeeds.py +++ b/tests/cli/test_digifeeds.py @@ -6,7 +6,7 @@ from aim.cli.main import app from aim.services import S from aim.digifeeds.item import Item -import aim.cli.digifeeds as digifeeds_cli +from aim.digifeeds import functions runner = CliRunner() @@ -38,7 +38,7 @@ def test_add_to_db_where_item_is_not_in_digifeeds_set(item_data): "fail_on_invalid_id": "true", } add_to_digifeeds_body = {"members": {"member": [{"id": "some_barcode"}]}} - add_to_digifeeds_set = responses.post( + add_to_digifeeds_set_stub = responses.post( add_to_digifeeds_url, match=[ matchers.query_param_matcher(add_to_digifeeds_query), @@ -48,10 +48,10 @@ def test_add_to_db_where_item_is_not_in_digifeeds_set(item_data): status=200, ) - result = runner.invoke(app, ["digifeeds", "add-to-db", "some_barcode"]) + result = runner.invoke(app, ["digifeeds", "add-to-digifeeds-set", "some_barcode"]) assert get_item.call_count == 1 assert post_item.call_count == 1 - assert add_to_digifeeds_set.call_count == 1 + assert add_to_digifeeds_set_stub.call_count == 1 assert add_item_status.call_count == 1 assert result.exit_code == 0 assert 'Adding barcode "some_barcode" to database' in result.stdout @@ -61,9 +61,9 @@ def test_add_to_db_where_item_is_not_in_digifeeds_set(item_data): def test_add_to_db_where_item_is_not_in_alma(item_data, mocker): item_data["statuses"][0]["name"] = "not_found_in_alma" item = Item(item_data) - mocker.patch("aim.cli.digifeeds.add_to_digifeeds_db", return_value=item) + mocker.patch.object(functions, "add_to_digifeeds_set", return_value=item) - result = runner.invoke(app, ["digifeeds", "add-to-db", "some_barcode"]) + result = runner.invoke(app, ["digifeeds", "add-to-digifeeds-set", "some_barcode"]) assert "Item not found in alma" in result.stdout assert "Item NOT added to digifeeds set" in result.stdout @@ -78,8 +78,9 @@ def test_load_statuses(mocker): def test_list_barcodes_in_input_bucket(mocker): - list_barcodes_mock = mocker.patch( - "aim.cli.digifeeds.list_barcodes_in_bucket", + list_barcodes_mock = mocker.patch.object( + functions, + "list_barcodes_in_input_bucket", return_value=["barcode1", "barcode2"], ) result = runner.invoke(app, ["digifeeds", "list-barcodes-in-input-bucket"]) @@ -119,7 +120,7 @@ def test_check_zephir_for_item_when_item_is_not_in_zephir(item_data): def test_move_to_pickup_success(mocker, item_data): item = Item(item_data) move_volume_to_pickup_mock = mocker.patch.object( - digifeeds_cli, "move_volume_to_pickup", return_value=item + functions, "move_to_pickup", return_value=item ) result = runner.invoke(app, ["digifeeds", "move-to-pickup", "some_barcode"]) @@ -131,7 +132,7 @@ def test_move_to_pickup_success(mocker, item_data): def test_move_to_pickup_where_not_in_zephir(mocker): move_volume_to_pickup_mock = mocker.patch.object( - digifeeds_cli, "move_volume_to_pickup", return_value=None + functions, "move_to_pickup", return_value=None ) result = runner.invoke(app, ["digifeeds", "move-to-pickup", "some_barcode"]) diff --git a/tests/digifeeds/test_add_to_db.py b/tests/digifeeds/test_add_to_digifeeds_set.py similarity index 61% rename from tests/digifeeds/test_add_to_db.py rename to tests/digifeeds/test_add_to_digifeeds_set.py index f834292..5d8177c 100644 --- a/tests/digifeeds/test_add_to_db.py +++ b/tests/digifeeds/test_add_to_digifeeds_set.py @@ -3,7 +3,9 @@ import json import pytest import responses -from aim.digifeeds.add_to_db import add_to_db +from aim.digifeeds.functions import add_to_digifeeds_set +from aim.digifeeds.alma_client import AlmaClient +from aim.digifeeds.db_client import DBClient from requests.exceptions import HTTPError from aim.services import S @@ -15,11 +17,11 @@ def item_data(): return output -def test_add_to_db_barcode_thats_in_the_digifeeds_set(mocker, item_data): - get_item_mock = mocker.patch( - "aim.digifeeds.db_client.DBClient.get_or_add_item", return_value=item_data +def test_add_to_digifeeds_set_for_barcode_thats_in_the_digifeeds_set(mocker, item_data): + get_item_mock = mocker.patch.object( + DBClient, "get_or_add_item", return_value=item_data ) - result = add_to_db("my_barcode") + result = add_to_digifeeds_set("my_barcode") get_item_mock.assert_called_once() assert result.barcode == "some_barcode" @@ -29,11 +31,11 @@ def test_add_to_db_barcode_thats_in_the_digifeeds_set_but_doesnt_have_status( mocker, item_data ): item_data["statuses"][0]["name"] = "some_other_status" - get_item_mock = mocker.patch( - "aim.digifeeds.db_client.DBClient.get_or_add_item", return_value=item_data + get_item_mock = mocker.patch.object( + DBClient, "get_or_add_item", return_value=item_data ) - add_status_mock = mocker.patch( - "aim.digifeeds.db_client.DBClient.add_item_status", return_value=item_data + add_status_mock = mocker.patch.object( + DBClient, "add_item_status", return_value=item_data ) error_body = { "errorsExist": True, @@ -48,34 +50,37 @@ def test_add_to_db_barcode_thats_in_the_digifeeds_set_but_doesnt_have_status( }, } add_to_digifeeds_url = f"{S.alma_api_url}/conf/sets/{S.digifeeds_set_id}" - add_to_digifeeds_set = responses.post( + add_to_digifeeds_set_stub = responses.post( add_to_digifeeds_url, json=error_body, status=400, ) - result = add_to_db("some_barcode") + result = add_to_digifeeds_set("some_barcode") get_item_mock.assert_called_once() add_status_mock.assert_called_once_with( barcode="some_barcode", status="added_to_digifeeds_set" ) - assert add_to_digifeeds_set.call_count == 1 + assert add_to_digifeeds_set_stub.call_count == 1 assert result.barcode == "some_barcode" -def test_add_to_db_barcode_thats_not_in_the_digifeeds_set(mocker, item_data): +def test_add_to_digifeeds_set_for_barcode_thats_not_in_the_digifeeds_set( + mocker, item_data +): item_data["statuses"][0]["name"] = "some_other_status" - get_item_mock = mocker.patch( - "aim.digifeeds.add_to_db.DBClient.get_or_add_item", return_value=item_data + get_item_mock = mocker.patch.object( + DBClient, "get_or_add_item", return_value=item_data ) - add_status_mock = mocker.patch( - "aim.digifeeds.add_to_db.DBClient.add_item_status", return_value=item_data + add_status_mock = mocker.patch.object( + DBClient, "add_item_status", return_value=item_data ) - add_to_digifeeds_set_mock = mocker.patch( - "aim.digifeeds.add_to_db.AlmaClient.add_barcode_to_digifeeds_set", + add_to_digifeeds_set_mock = mocker.patch.object( + AlmaClient, + "add_barcode_to_digifeeds_set", return_value=item_data, ) - result = add_to_db("some_barcode") + result = add_to_digifeeds_set("some_barcode") get_item_mock.assert_called_once() add_status_mock.assert_called_once() add_to_digifeeds_set_mock.assert_called_once() @@ -83,13 +88,13 @@ def test_add_to_db_barcode_thats_not_in_the_digifeeds_set(mocker, item_data): @responses.activate -def test_add_to_db_barcode_that_is_not_in_alma(mocker, item_data): +def test_add_to_digifeeds_set_barcode_that_is_not_in_alma(mocker, item_data): item_data["statuses"][0]["name"] = "some_other_status" - get_item_mock = mocker.patch( - "aim.digifeeds.db_client.DBClient.get_or_add_item", return_value=item_data + get_item_mock = mocker.patch.object( + DBClient, "get_or_add_item", return_value=item_data ) - add_status_mock = mocker.patch( - "aim.digifeeds.db_client.DBClient.add_item_status", return_value=item_data + add_status_mock = mocker.patch.object( + DBClient, "add_item_status", return_value=item_data ) error_body = { "errorsExist": True, @@ -104,28 +109,28 @@ def test_add_to_db_barcode_that_is_not_in_alma(mocker, item_data): }, } add_to_digifeeds_url = f"{S.alma_api_url}/conf/sets/{S.digifeeds_set_id}" - add_to_digifeeds_set = responses.post( + add_to_digifeeds_set_stub = responses.post( add_to_digifeeds_url, json=error_body, status=400, ) - result = add_to_db("some_barcode") + result = add_to_digifeeds_set("some_barcode") get_item_mock.assert_called_once() add_status_mock.assert_called_once_with( barcode="some_barcode", status="not_found_in_alma" ) - assert add_to_digifeeds_set.call_count == 1 + assert add_to_digifeeds_set_stub.call_count == 1 assert result.barcode == "some_barcode" @responses.activate -def test_add_to_db_barcode_that_causes_alma_error(mocker, item_data): +def test_add_to_digifeeds_set_barcode_that_causes_alma_error(mocker, item_data): item_data["statuses"][0]["name"] = "some_other_status" - get_item_mock = mocker.patch( - "aim.digifeeds.db_client.DBClient.get_or_add_item", return_value=item_data + get_item_mock = mocker.patch.object( + DBClient, "get_or_add_item", return_value=item_data ) - add_status_mock = mocker.patch("aim.digifeeds.db_client.DBClient.add_item_status") + add_status_mock = mocker.patch.object(DBClient, "add_item_status") error_body = { "errorsExist": True, "errorList": { @@ -139,15 +144,15 @@ def test_add_to_db_barcode_that_causes_alma_error(mocker, item_data): }, } add_to_digifeeds_url = f"{S.alma_api_url}/conf/sets/{S.digifeeds_set_id}" - add_to_digifeeds_set = responses.post( + add_to_digifeeds_set_stub = responses.post( add_to_digifeeds_url, json=error_body, status=400, ) with pytest.raises(Exception) as exc_info: - add_to_db("my_barcode") + add_to_digifeeds_set("my_barcode") assert exc_info.type is HTTPError - assert add_to_digifeeds_set.call_count == 1 + assert add_to_digifeeds_set_stub.call_count == 1 get_item_mock.assert_called_once() add_status_mock.assert_not_called() diff --git a/tests/digifeeds/test_check_zephir.py b/tests/digifeeds/test_check_zephir.py index 2ba3fbd..3afcadb 100644 --- a/tests/digifeeds/test_check_zephir.py +++ b/tests/digifeeds/test_check_zephir.py @@ -2,7 +2,8 @@ import responses import json from aim.services import S -from aim.digifeeds.check_zephir import check_zephir +from aim.digifeeds import functions +from aim.digifeeds.functions import check_zephir @pytest.fixture @@ -19,11 +20,11 @@ def barcode(): @responses.activate def test_barcode_is_in_zephir(mocker, item_data, barcode): - get_item_mock = mocker.patch( - "aim.digifeeds.check_zephir.DBClient.get_item", return_value=item_data + get_item_mock = mocker.patch.object( + functions.DBClient, "get_or_add_item", return_value=item_data ) - add_status_mock = mocker.patch( - "aim.digifeeds.check_zephir.DBClient.add_item_status", return_value=item_data + add_status_mock = mocker.patch.object( + functions.DBClient, "add_item_status", return_value=item_data ) responses.get(f"{S.zephir_bib_api_url}/mdp.{barcode}", json={}, status=200) @@ -35,12 +36,12 @@ def test_barcode_is_in_zephir(mocker, item_data, barcode): def test_barcode_already_has_in_zephir_status(mocker, item_data, barcode): item_data["statuses"][0]["name"] = "in_zephir" - get_item_mock = mocker.patch( - "aim.digifeeds.check_zephir.DBClient.get_item", return_value=item_data + get_item_mock = mocker.patch.object( + functions.DBClient, "get_or_add_item", return_value=item_data ) - add_status_mock = mocker.patch( - "aim.digifeeds.check_zephir.DBClient.add_item_status", return_value=item_data + add_status_mock = mocker.patch.object( + functions.DBClient, "add_item_status", return_value=item_data ) result = check_zephir(barcode) @@ -51,11 +52,12 @@ def test_barcode_already_has_in_zephir_status(mocker, item_data, barcode): @responses.activate def test_barcode_not_in_zephir(mocker, item_data, barcode): - get_item_mock = mocker.patch( - "aim.digifeeds.check_zephir.DBClient.get_item", return_value=item_data + get_item_mock = mocker.patch.object( + functions.DBClient, "get_or_add_item", return_value=item_data ) - add_status_mock = mocker.patch( - "aim.digifeeds.check_zephir.DBClient.add_item_status", return_value=item_data + + add_status_mock = mocker.patch.object( + functions.DBClient, "add_item_status", return_value=item_data ) responses.get(f"{S.zephir_bib_api_url}/mdp.{barcode}", json={}, status=404) @@ -63,10 +65,3 @@ def test_barcode_not_in_zephir(mocker, item_data, barcode): get_item_mock.assert_called_once() add_status_mock.assert_not_called() assert result is None - - -def test_barcdoe_is_not_in_db(mocker, barcode): - mocker.patch("aim.digifeeds.check_zephir.DBClient.get_item", return_value=None) - with pytest.raises(Exception) as exc_info: - check_zephir(barcode) - assert exc_info.type is Exception diff --git a/tests/digifeeds/test_list_barcodes_in_bucket.py b/tests/digifeeds/test_list_barcodes_in_bucket.py index f44fb8f..2323d39 100644 --- a/tests/digifeeds/test_list_barcodes_in_bucket.py +++ b/tests/digifeeds/test_list_barcodes_in_bucket.py @@ -1,7 +1,7 @@ import boto3 from moto import mock_aws from aim.services import S -from aim.digifeeds.list_barcodes_in_bucket import list_barcodes_in_bucket +from aim.digifeeds.functions import list_barcodes_in_input_bucket @mock_aws @@ -22,5 +22,5 @@ def test_list_barcodes_in_bucket(): ) barcode2.put(Body="some text") - result = list_barcodes_in_bucket() + result = list_barcodes_in_input_bucket() assert result == ["barcode1", "barcode2"] diff --git a/tests/digifeeds/test_move_to_pickup.py b/tests/digifeeds/test_move_to_pickup.py index 5b69657..5cefd44 100644 --- a/tests/digifeeds/test_move_to_pickup.py +++ b/tests/digifeeds/test_move_to_pickup.py @@ -1,4 +1,4 @@ -from aim.digifeeds.move_to_pickup import move_to_pickup, rclone, DBClient +from aim.digifeeds.functions import move_to_pickup, rclone, DBClient import json import pytest from datetime import datetime @@ -30,7 +30,7 @@ def test_move_to_pickup_success(mocker, item_in_zephir_for_long_enough): rclone_moveto_mock = mocker.patch.object(rclone, "moveto") get_item_mock = mocker.patch.object( DBClient, - "get_item", + "get_or_add_item", return_value=item_in_zephir_for_long_enough, ) add_status_mock = mocker.patch.object( @@ -48,19 +48,10 @@ def test_move_to_pickup_success(mocker, item_in_zephir_for_long_enough): assert result is not None -def test_move_to_pickup_no_item(mocker): - get_item_mock = mocker.patch.object(DBClient, "get_item", return_value=None) - with pytest.raises(Exception) as exc_info: - move_to_pickup("some_barcode") - - get_item_mock.assert_called_once() - assert str(exc_info.value) == "Item not found in database" - - def test_move_to_pickup_item_too_recent(mocker, item_in_zephir_too_recent): get_item_mock = mocker.patch.object( DBClient, - "get_item", + "get_or_add_item", return_value=item_in_zephir_too_recent, ) result = move_to_pickup(item_in_zephir_too_recent["barcode"])