Skip to content

Commit

Permalink
Adding some tests for BatchPoster and rules_mapper_base. Also made so…
Browse files Browse the repository at this point in the history
…me tweaks to clean-up Flake8 errors.
  • Loading branch information
bltravis committed Aug 8, 2024
1 parent 9a598c5 commit eae329c
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import urllib.parse
import uuid
from abc import abstractmethod
from copy import copy
from textwrap import wrap
from typing import List, Tuple

Expand All @@ -14,7 +13,7 @@
from dateutil.parser import parse
from folio_uuid.folio_uuid import FOLIONamespaces, FolioUUID
from folioclient import FolioClient
from pymarc import Field, Leader, Record, Subfield
from pymarc import Field, Record, Subfield

from folio_migration_tools.custom_exceptions import (
TransformationFieldMappingError,
Expand Down Expand Up @@ -144,6 +143,8 @@ def set_005_as_updated_date(marc_record: Record, folio_object: dict, legacy_ids)
try:
f005 = marc_record["005"].data[:14]
parsed_date = datetime.datetime.strptime(f005, "%Y%m%d%H%M%S").isoformat()
if "metadata" in folio_object:
folio_object["metadata"]["updatedDate"] = parsed_date
except Exception as exception:
if "005" in marc_record:
Helper.log_data_issue(
Expand Down Expand Up @@ -799,12 +800,10 @@ def save_source_record(
)
# Since they all should be UTF encoded, make the leader align.
try:
temp_leader = Leader(str(marc_record.leader))
temp_leader[9] = "a"
marc_record.leader = temp_leader
marc_record.leader[9] = "a"
except Exception as ee:
logging.exception(
"Something is wrong with the marc records leader: %s, %s", marc_record.leader, ee
"Something is wrong with the marc record's leader: %s, %s", marc_record.leader, ee
)
srs_record_string = RulesMapperBase.get_srs_string(
marc_record,
Expand Down
64 changes: 30 additions & 34 deletions src/folio_migration_tools/migration_tasks/batch_poster.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,7 @@ def post_record_batch(self, batch, failed_recs_file, row):

def post_extra_data(self, row: str, num_records: int, failed_recs_file):
(object_name, data) = row.split("\t")
try:
endpoint = get_extradata_endpoint(object_name, data)
except KeyError:
if self.task_configuration.extradata_endpoints:
if endpoint := self.task_configuration.extradata_endpoints.get(object_name):
pass
else:
raise
endpoint = self.get_extradata_endpoint(object_name, data)
url = f"{self.folio_client.okapi_url}/{endpoint}"
body = data
response = self.post_objects(url, body)
Expand All @@ -235,6 +228,35 @@ def post_extra_data(self, row: str, num_records: int, failed_recs_file):
self.num_failures,
)

@staticmethod
def get_extradata_endpoint(
task_configuration: TaskConfiguration, object_name: str, string_object: str
):
object_types = {
"precedingSucceedingTitles": "preceding-succeeding-titles",
"precedingTitles": "preceding-succeeding-titles",
"succeedingTitles": "preceding-succeeding-titles",
"boundwithPart": "inventory-storage/bound-with-parts",
"notes": "notes",
"course": "coursereserves/courses",
"courselisting": "coursereserves/courselistings",
"contacts": "organizations-storage/contacts",
"interfaces": "organizations-storage/interfaces",
"account": "accounts",
"feefineaction": "feefineactions",
"bankInfo": "organizations/banking-information",
}
object_types.update(task_configuration.extradata_endpoints)
if object_name == "instructor":
instructor = json.loads(string_object)
return f'coursereserves/courselistings/{instructor["courseListingId"]}/instructors'

if object_name == "interfaceCredential":
credential = json.loads(string_object)
return f'organizations-storage/interfaces/{credential["interfaceId"]}/credentials'

return object_types[object_name]

def post_single_records(self, row: str, num_records: int, failed_recs_file):
if self.api_info["is_batch"]:
raise TypeError("This record type supports batch processing, use post_batch method")
Expand Down Expand Up @@ -642,32 +664,6 @@ def chunks(records, number_of_chunks):
yield records[i : i + number_of_chunks]


def get_extradata_endpoint(object_name: str, string_object: str):
object_types = {
"precedingSucceedingTitles": "preceding-succeeding-titles",
"precedingTitles": "preceding-succeeding-titles",
"succeedingTitles": "preceding-succeeding-titles",
"boundwithPart": "inventory-storage/bound-with-parts",
"notes": "notes",
"course": "coursereserves/courses",
"courselisting": "coursereserves/courselistings",
"contacts": "organizations-storage/contacts",
"interfaces": "organizations-storage/interfaces",
"account": "accounts",
"feefineaction": "feefineactions",
"bankInfo": "organizations/banking-information",
}
if object_name == "instructor":
instructor = json.loads(string_object)
return f'coursereserves/courselistings/{instructor["courseListingId"]}/instructors'

if object_name == "interfaceCredential":
credential = json.loads(string_object)
return f'organizations-storage/interfaces/{credential["interfaceId"]}/credentials'

return object_types[object_name]


def get_human_readable(size, precision=2):
suffixes = ["B", "KB", "MB", "GB", "TB"]
suffix_index = 0
Expand Down
28 changes: 26 additions & 2 deletions tests/test_batch_poster.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from folio_uuid.folio_namespaces import FOLIONamespaces
from unittest.mock import Mock

from folio_migration_tools.migration_tasks import batch_poster
from folio_migration_tools.migration_tasks.batch_poster import BatchPoster
from folio_uuid.folio_namespaces import FOLIONamespaces


def test_get_object_type():
Expand Down Expand Up @@ -36,9 +37,32 @@ def test_get_extradata_endpoint_interface_credential():
extradata = 'interfaceCredential\t{"interfaceId": "7e131c38-5384-44ed-9f4a-da6ca2f36498"}'
(object_name, data) = extradata.split("\t")

endpoint = batch_poster.get_extradata_endpoint(object_name, data)
batch_poster_task = Mock(spec=BatchPoster)
batch_poster_task.task_configuration = Mock()
batch_poster_task.task_configuration.extradata_endpoints = {}

endpoint = BatchPoster.get_extradata_endpoint(
batch_poster_task.task_configuration, object_name, data
)

assert (
endpoint
== "organizations-storage/interfaces/7e131c38-5384-44ed-9f4a-da6ca2f36498/credentials"
)


def test_get_extradata_endpoint_from_task_configuration():
extradata = 'otherData\t{"otherDataId": "7e131c38-5384-44ed-9f4a-da6ca2f36498"}'
(object_name, data) = extradata.split("\t")

batch_poster_task = Mock(spec=BatchPoster)
batch_poster_task.task_configuration = Mock()
batch_poster_task.task_configuration.extradata_endpoints = {
"otherData": "otherdata-endpoint/endpoint"
}

endpoint = BatchPoster.get_extradata_endpoint(
batch_poster_task.task_configuration, object_name, data
)

assert endpoint == "otherdata-endpoint/endpoint"
52 changes: 51 additions & 1 deletion tests/test_rules_mapper_base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import datetime
import io
import json
from unittest import mock
from unittest.mock import Mock, patch
from uuid import uuid4

import pytest
Expand Down Expand Up @@ -36,7 +39,7 @@ def test_datetime_from_005():
}
}
RulesMapperBase.set_005_as_updated_date(record, instance, "some_id")
assert instance["metadata"]["updatedDate"] != "1994-02-23T15:10:47"
assert instance["metadata"]["updatedDate"] == "1994-02-23T15:10:47"


def test_date_from_008():
Expand Down Expand Up @@ -208,3 +211,50 @@ def test_create_srs_uuid():
assert str(created_id) == "6734f228-cba2-54c7-b129-c6437375a864"
created_id_2 = RulesMapperBase.create_srs_id(FOLIONamespaces.instances, "some_url", "id_1")
assert str(created_id) != str(created_id_2)


@pytest.fixture
def folio_record():
return {
"id": str(uuid4()),
"title": "Sample Title",
}


@pytest.fixture
def marc_record():
record = Record()
record.add_field(Field(tag="001", data="123456"))
return record


def test_save_source_record(caplog, folio_record, marc_record):
record_type = FOLIONamespaces.instances
folio_client = Mock(spec=FolioClient)
folio_client.okapi_url = "https://folio-snapshot.dev.folio.org"
legacy_ids = ["legacy_id_1", "legacy_id_2"]
suppress = False
srs_records = []

with io.StringIO() as srs_records_file:
RulesMapperBase.save_source_record(
srs_records_file,
record_type,
folio_client,
marc_record,
folio_record,
legacy_ids,
suppress,
)
srs_records_file.seek(0)
srs_records.extend(srs_records_file.readlines())

log_messages = [call.message for call in caplog.records]
assert not any(
message.startswith("Something is wrong with the marc record's leader:")
for message in log_messages
)

assert len(srs_records) == 1
assert srs_records[0].startswith('{"id": "')
assert srs_records[0].endswith('"}\n')

0 comments on commit eae329c

Please sign in to comment.