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.
Merge pull request #14 from mlibrary/DWI-14-check-zephir
DWI-14 check zephir
- Loading branch information
Showing
5 changed files
with
152 additions
and
6 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
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,22 @@ | ||
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 |
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import pytest | ||
import responses | ||
import json | ||
from aim.services import S | ||
from aim.digifeeds.check_zephir import check_zephir | ||
|
||
|
||
@pytest.fixture | ||
def item_data(): | ||
with open("tests/fixtures/digifeeds/item.json") as f: | ||
output = json.load(f) | ||
return output | ||
|
||
|
||
@pytest.fixture | ||
def barcode(): | ||
return "some_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 | ||
) | ||
add_status_mock = mocker.patch( | ||
"aim.digifeeds.check_zephir.DBClient.add_item_status", return_value=item_data | ||
) | ||
|
||
responses.get(f"{S.zephir_bib_api_url}/mdp.{barcode}", json={}, status=200) | ||
result = check_zephir(barcode) | ||
get_item_mock.assert_called_once() | ||
add_status_mock.assert_called_once() | ||
assert result.barcode == 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 | ||
) | ||
|
||
add_status_mock = mocker.patch( | ||
"aim.digifeeds.check_zephir.DBClient.add_item_status", return_value=item_data | ||
) | ||
|
||
result = check_zephir(barcode) | ||
get_item_mock.assert_called_once() | ||
add_status_mock.assert_not_called() | ||
assert result.barcode == 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 | ||
) | ||
add_status_mock = mocker.patch( | ||
"aim.digifeeds.check_zephir.DBClient.add_item_status", return_value=item_data | ||
) | ||
|
||
responses.get(f"{S.zephir_bib_api_url}/mdp.{barcode}", json={}, status=404) | ||
result = check_zephir(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 |