Skip to content

Commit

Permalink
Fixes failing tests and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
jermnelson committed Jan 8, 2024
1 parent b6d15c4 commit ae429d1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
4 changes: 2 additions & 2 deletions ils_middleware/dags/stanford_folio.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from airflow.operators.python import PythonOperator
from airflow.utils.task_group import TaskGroup

from ils_middleware.tasks.amazon.s3 import get_from_s3, send_to_s3
from ils_middleware.tasks.amazon.sqs import SubscribeOperator, parse_messages

from ils_middleware.tasks.sinopia.local_metadata import new_local_admin_metadata
Expand All @@ -16,7 +15,8 @@
)

from ils_middleware.tasks.sinopia.login import sinopia_login
from ils_middleware.tasks.sinopia.metadata_check import existing_metadata_check

# from ils_middleware.tasks.sinopia.metadata_check import existing_metadata_check
from ils_middleware.tasks.folio.build import build_records
from ils_middleware.tasks.folio.login import FolioLogin
from ils_middleware.tasks.folio.graph import construct_graph
Expand Down
15 changes: 7 additions & 8 deletions ils_middleware/tasks/folio/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
def _default_transform(**kwargs) -> tuple:
folio_field = kwargs["folio_field"]
values = kwargs.get("values", [])
logger.debug(f"field: {folio_field} values: {values} type: {type(values)}")
return folio_field, values


Expand Down Expand Up @@ -119,29 +118,29 @@ def _instance_type_id(**kwargs) -> tuple:
raise ValueError(f"instanceTypeId for {name} not found")
return "instanceTypeId", ident


def _folio_hrid(folio_client: FolioClient) -> str:
"""Queries for instance hrid, increments, and saves back to folio"""
endpoint = "/hrid-settings-storage/hrid-settings"
hrid_settings = folio_client.folio_get_single_object(endpoint)
instance_count = hrid_settings['instances']['startNumber']
instance_count = hrid_settings["instances"]["startNumber"]
new_instance_count = instance_count + 1
if hrid_settings['commonRetainLeadingZeroes']:
if hrid_settings["commonRetainLeadingZeroes"]:
number = str(new_instance_count).zfill(11)
else:
number = new_instance_count
instance_hrid = f"{hrid_settings['instances']['prefix']}{number}"
hrid_settings['instances']['startNumber'] = new_instance_count
hrid_settings["instances"]["startNumber"] = new_instance_count

# Puts new instance startNumber back into FOLIO
hrid_put_result = requests.put(f"{folio_client.okapi_url}{endpoint}",
headers=folio_client.okapi_headers)
hrid_put_result = requests.put(
f"{folio_client.okapi_url}{endpoint}", headers=folio_client.okapi_headers
)
hrid_put_result.raise_for_status()

return instance_hrid




def _language(**kwargs) -> tuple:
values = kwargs["values"]

Expand Down
21 changes: 17 additions & 4 deletions tests/tasks/folio/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ def __init__(self, *args):
{"id": "6a2533a7-4de2-4e64-8466-074c2fa9308c", "name": "General note"},
]

self.okapi_headers = {"tenant": "sul"}
self.okapi_url = "https://okapi.edu"

def folio_get_single_object(self, endpoint):
match endpoint:

case "/hrid-settings-storage/hrid-settings":
return {
"instances": {"startNumber": 1, "prefix": "i"},
"commonRetainLeadingZeroes": True,
}


@pytest.fixture
def mock_folio_client(monkeypatch):
Expand All @@ -94,7 +106,7 @@ def test_happypath_build_records(
)
record = test_task_instance().xcom_pull(key=instance_uri)

assert record["hrid"].startswith(instance_uri)
assert record["hrid"] == "i00000000002"
assert record["metadata"]["createdByUserId"].startswith(
"21eaac74-1b29-5546-a13b-bc2e7e4717c6"
)
Expand Down Expand Up @@ -164,7 +176,7 @@ def test_instance_format_ids(mock_task_instance): # noqa: F811
assert (format_ids[1][0]).startswith("8d511d33-5e85-4c5d-9bce-6e3c9cd0c324")


def test_inventory_record(mock_task_instance): # noqa: F811
def test_inventory_record(mock_task_instance, mock_requests_okapi): # noqa: F811
record = _inventory_record(
instance_uri=instance_uri,
task_instance=test_task_instance(),
Expand All @@ -174,11 +186,12 @@ def test_inventory_record(mock_task_instance): # noqa: F811
username="test_user",
tenant="sul",
)
assert record["hrid"].startswith(instance_uri)
assert record["hrid"].startswith("i00000000002")


def test_inventory_record_existing_metadata(
mock_task_instance, # noqa: F811
mock_requests_okapi, # noqa
): # noqa: F811
metadata = {
"createdDate": "2021-12-06T15:45:28.140795",
Expand All @@ -193,7 +206,7 @@ def test_inventory_record_existing_metadata(
folio_client=MockFolioClient(),
metadata=metadata,
)
assert record["hrid"].startswith(instance_uri)
assert record["hrid"].startswith("i00000000002")
assert record["metadata"]["createdDate"].startswith("2021-12-06T15:45:28.140795")


Expand Down

0 comments on commit ae429d1

Please sign in to comment.