Skip to content

Commit

Permalink
adds integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
niquerio committed Sep 26, 2024
1 parent 5f91423 commit 6bb5767
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 9 deletions.
4 changes: 2 additions & 2 deletions aim/__main__.py
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
2 changes: 2 additions & 0 deletions aim/cli/digifeeds.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import typer
from aim.digifeeds.add_to_db import add_to_db as add_to_digifeeds_db
from aim.digifeeds.database import models, main

app = typer.Typer()


@app.command()
def add_to_db(barcode: str):
add_to_digifeeds_db(barcode)
print(f'Adding barcode "{barcode}" to database')


Expand Down
2 changes: 1 addition & 1 deletion aim/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
app.add_typer(digifeeds.app, name="digifeeds")


if __name__ == "__main__":
if __name__ == "__main__": # pragma: no cover
app()
17 changes: 16 additions & 1 deletion aim/digifeeds/add_to_db.py
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")
19 changes: 18 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pytest-cov = "^5.0.0"
ruff = "^0.6.6"
responses = "^0.25.3"
pytest-socket = "^0.7.0"
pytest-mock = "^3.14.0"


[tool.pytest.ini_options]
Expand Down
58 changes: 58 additions & 0 deletions tests/cli/test_digifeeds.py
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
81 changes: 77 additions & 4 deletions tests/digifeeds/test_add_to_db.py
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
20 changes: 20 additions & 0 deletions tests/digifeeds/test_item.py
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
11 changes: 11 additions & 0 deletions tests/fixtures/digifeeds/item.json
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"
}
]
}

0 comments on commit 6bb5767

Please sign in to comment.