Skip to content

Commit

Permalink
get_holdings
Browse files Browse the repository at this point in the history
  • Loading branch information
jonavellecuerdo committed Jul 24, 2024
1 parent fab75c6 commit efaa696
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 41 deletions.
55 changes: 55 additions & 0 deletions tests/sources/xml/test_marc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,61 @@ def test_get_edition_transforms_correctly_if_fields_missing():
assert Marc.get_edition(source_record) is None


def test_get_holdings_success():
source_record = create_marc_source_record_stub(
datafield_insert=(
"""
<datafield tag="985" ind1=" " ind2=" ">
<subfield code="aa">STACK</subfield>
<subfield code="t">BOOK</subfield>
<subfield code="bb">PL2687.L8.A28 1994</subfield>
<subfield code="i">HUM</subfield>
</datafield>
<datafield tag="985" ind1=" " ind2=" ">
<subfield code="aa">OCC</subfield>
<subfield code="t">BOOK</subfield>
<subfield code="g">pt.A</subfield>
<subfield code="bb">QD79.C4.C485 1983</subfield>
<subfield code="i">LSA</subfield>
</datafield>
"""
)
)
assert Marc.get_holdings(source_record) == [
timdex.Holding(
call_number="PL2687.L8.A28 1994",
collection="Stacks",
format="Print volume",
location="Hayden Library",
),
timdex.Holding(
call_number="QD79.C4.C485 1983",
collection="Off Campus Collection",
format="Print volume",
location="Library Storage Annex",
note="pt.A",
),
]


def test_get_holdings_transforms_correctly_if_fields_blank():
source_record = create_marc_source_record_stub(
datafield_insert=(
"""
<datafield tag="985" ind1=" " ind2=" ">
<subfield code="aa"></subfield>
</datafield>
"""
)
)
assert Marc.get_holdings(source_record) is None


def test_get_holdings_transforms_correctly_if_fields_missing():
source_record = create_marc_source_record_stub()
assert Marc.get_holdings(source_record) is None


def test_marc_record_missing_leader_skips_record(caplog):
marc_xml_records = Marc.parse_source_file(
"tests/fixtures/marc/marc_record_missing_leader.xml"
Expand Down
92 changes: 51 additions & 41 deletions transmogrifier/sources/xml/marc.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,48 +73,10 @@ def get_optional_fields(self, source_record: Tag) -> dict | None:
# funding_information

# holdings
fields["holdings"] = self.get_holdings(source_record)

# physical items
for datafield in source_record.find_all("datafield", tag="985"):
holding_call_number_value = self.create_subfield_value_string_from_datafield(
datafield, ["bb"]
)
holding_collection_value = Marc.json_crosswalk_code_to_name(
self.create_subfield_value_string_from_datafield(datafield, ["aa"]),
self.holdings_collection_crosswalk,
source_record_id,
"985 $aa",
)
holding_format_value = Marc.json_crosswalk_code_to_name(
self.create_subfield_value_string_from_datafield(datafield, "t"),
self.holdings_format_crosswalk,
source_record_id,
"985 $t",
)
holding_location_value = Marc.json_crosswalk_code_to_name(
self.create_subfield_value_string_from_datafield(datafield, "i"),
self.holdings_location_crosswalk,
source_record_id,
"985 $i",
)
holding_note_value = self.create_subfield_value_string_from_datafield(
datafield, "g", ", "
)
if (
holding_call_number_value
or holding_collection_value
or holding_format_value
or holding_location_value
or holding_note_value
):
fields.setdefault("holdings", []).append(
timdex.Holding(
call_number=holding_call_number_value or None,
collection=holding_collection_value or None,
format=holding_format_value or None,
location=holding_location_value or None,
note=holding_note_value or None,
)
)

# electronic portfolio items
for field_986 in source_record.find_all("datafield", tag="986"):
electronic_item_collection = self.get_single_subfield_string(field_986, "j")
Expand Down Expand Up @@ -867,6 +829,54 @@ def get_edition(cls, source_record: Tag) -> str | None:
]
return " ".join(edition_values) or None

@classmethod
def get_holdings(cls, source_record: Tag) -> list[timdex.Holding] | None:
holdings = []
for datafield in source_record.find_all("datafield", tag="985"):
holding_call_number = cls.create_subfield_value_string_from_datafield(
datafield, ["bb"]
)
holding_collection = Marc.json_crosswalk_code_to_name(
code=cls.create_subfield_value_string_from_datafield(datafield, ["aa"]),
crosswalk=cls.holdings_collection_crosswalk,
record_id=cls.get_source_record_id(source_record),
field_name="985 $aa",
)
holding_format = Marc.json_crosswalk_code_to_name(
code=cls.create_subfield_value_string_from_datafield(datafield, "t"),
crosswalk=cls.holdings_format_crosswalk,
record_id=cls.get_source_record_id(source_record),
field_name="985 $t",
)
holding_location = Marc.json_crosswalk_code_to_name(
code=cls.create_subfield_value_string_from_datafield(datafield, "i"),
crosswalk=cls.holdings_location_crosswalk,
record_id=cls.get_source_record_id(source_record),
field_name="985 $i",
)
holding_note = cls.create_subfield_value_string_from_datafield(
datafield, "g", ", "
)
if any(
[
holding_call_number,
holding_collection,
holding_format,
holding_location,
holding_note,
]
):
holdings.append(
timdex.Holding(
call_number=holding_call_number or None,
collection=holding_collection or None,
format=holding_format or None,
location=holding_location or None,
note=holding_note or None,
)
)
return holdings or None

@staticmethod
def get_main_titles(xml: Tag) -> list[str]:
"""
Expand Down

0 comments on commit efaa696

Please sign in to comment.