generated from mlibrary/python-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
206 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from aim.cli.main import app | ||
from aim.cli.main import app # pragma: no cover | ||
|
||
app() | ||
app() # pragma: no cover |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,17 @@ | ||
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): | ||
pass | ||
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): | ||
DBClient().add_item_status(barcode=barcode, status="not_found_in_alma") | ||
return None | ||
DBClient().add_item_status(barcode=barcode, status="added_to_digifeeds_set") |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import responses | ||
import pytest | ||
import json | ||
from responses import matchers | ||
from typer.testing import CliRunner | ||
from aim.cli.main import app | ||
from aim.services import S | ||
|
||
runner = CliRunner() | ||
|
||
|
||
@pytest.fixture | ||
def item_data(): | ||
with open("tests/fixtures/digifeeds/item.json") as f: | ||
output = json.load(f) | ||
return output | ||
|
||
|
||
@pytest.fixture | ||
def alma_base_url(): | ||
return "https://api-na.hosted.exlibrisgroup.com/almaws/v1" | ||
|
||
|
||
@responses.activate | ||
def test_add_to_db_where_item_is_not_in_digifeeds_set(item_data, alma_base_url): | ||
item_data["statuses"][0]["name"] = "in_zephir" | ||
get_add_url = f"{S.digifeeds_api_url}/items/some_barcode" | ||
|
||
get_item = responses.get(get_add_url, json={}, status=404) | ||
post_item = responses.post(get_add_url, json=item_data, status=200) | ||
add_item_status = responses.put( | ||
f"{get_add_url}/status/added_to_digifeeds_set", json=item_data, status=200 | ||
) | ||
|
||
add_to_digifeeds_url = f"{alma_base_url}/conf/sets/{S.digifeeds_set_id}" | ||
add_to_digifeeds_query = { | ||
"id_type": "BARCODE", | ||
"op": "add_members", | ||
"fail_on_invalid_id": "true", | ||
} | ||
add_to_digifeeds_body = {"members": {"member": [{"id": "some_barcode"}]}} | ||
add_to_digifeeds_set = responses.post( | ||
add_to_digifeeds_url, | ||
match=[ | ||
matchers.query_param_matcher(add_to_digifeeds_query), | ||
matchers.json_params_matcher(add_to_digifeeds_body), | ||
], | ||
json={}, | ||
status=200, | ||
) | ||
|
||
result = runner.invoke(app, ["digifeeds", "add-to-db", "some_barcode"]) | ||
assert get_item.call_count == 1 | ||
assert post_item.call_count == 1 | ||
assert add_to_digifeeds_set.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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,79 @@ | ||
import requests | ||
# from unittest.mock import patch | ||
# # from aim.digifeeds.alma_client import AlmaClient | ||
import json | ||
import pytest | ||
import responses | ||
from aim.digifeeds.add_to_db import add_to_db | ||
from aim.services import S | ||
|
||
|
||
def test_response(): | ||
response = requests.get("http://google.com") | ||
assert (response.status_code) == 200 | ||
@pytest.fixture | ||
def alma_base_url(): | ||
return "https://api-na.hosted.exlibrisgroup.com/almaws/v1" | ||
|
||
|
||
@pytest.fixture | ||
def item_data(): | ||
with open("tests/fixtures/digifeeds/item.json") as f: | ||
output = json.load(f) | ||
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 | ||
) | ||
result = add_to_db("my_barcode") | ||
get_item_mock.assert_called_once() | ||
assert result is None | ||
|
||
|
||
def test_add_to_db_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.db_client.DBClient.get_or_add_item", return_value=item_data | ||
) | ||
add_status_mock = mocker.patch("aim.digifeeds.db_client.DBClient.add_item_status") | ||
add_to_digifeeds_set_mock = mocker.patch( | ||
"aim.digifeeds.alma_client.AlmaClient.add_barcode_to_digifeeds_set" | ||
) | ||
result = add_to_db("my_barcode") | ||
get_item_mock.assert_called_once() | ||
add_status_mock.assert_called_once() | ||
add_to_digifeeds_set_mock.assert_called_once() | ||
assert result is None | ||
|
||
|
||
@responses.activate | ||
def test_add_to_db_barcode_that_is_not_in_alma(mocker, item_data, alma_base_url): | ||
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 | ||
) | ||
add_status_mock = mocker.patch("aim.digifeeds.db_client.DBClient.add_item_status") | ||
error_body = { | ||
"errorsExist": True, | ||
"errorList": { | ||
"error": [ | ||
{ | ||
"errorCode": "60120", | ||
"errorMessage": "The ID whatever is not valid for content type ITEM and identifier type BARCODE.", | ||
"trackingId": "E01-2609211329-8EKLP-AWAE1781893571", | ||
} | ||
] | ||
}, | ||
} | ||
add_to_digifeeds_url = f"{alma_base_url}/conf/sets/{S.digifeeds_set_id}" | ||
add_to_digifeeds_set = responses.post( | ||
add_to_digifeeds_url, | ||
json=error_body, | ||
status=400, | ||
) | ||
|
||
result = add_to_db("my_barcode") | ||
get_item_mock.assert_called_once() | ||
add_status_mock.assert_called_once_with( | ||
barcode="my_barcode", status="not_found_in_alma" | ||
) | ||
assert add_to_digifeeds_set.call_count == 1 | ||
assert result is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import pytest | ||
import json | ||
from aim.digifeeds.item import Item | ||
|
||
|
||
@pytest.fixture | ||
def item_data(): | ||
with open("tests/fixtures/digifeeds/item.json") as f: | ||
output = json.load(f) | ||
return output | ||
|
||
|
||
def test_has_status_is_true(item_data): | ||
result = Item(item_data).has_status("added_to_digifeeds_set") | ||
assert result is True | ||
|
||
|
||
def test_has_status_is_false(item_data): | ||
result = Item(item_data).has_status("in_zephir") | ||
assert result is False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"barcode": "some_barcode", | ||
"created_at": "2024-09-25T17:12:39", | ||
"statuses": [ | ||
{ | ||
"name": "added_to_digifeeds_set", | ||
"description": "Item has been added to the digifeeds set", | ||
"created_at": "2024-09-25T17:13:28" | ||
} | ||
] | ||
} |