Skip to content

Commit

Permalink
related_items
Browse files Browse the repository at this point in the history
  • Loading branch information
jonavellecuerdo committed Jul 29, 2024
1 parent ad1d046 commit ac00eaa
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 59 deletions.
97 changes: 97 additions & 0 deletions tests/sources/xml/test_marc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1731,6 +1731,103 @@ def test_get_publishers_transforms_correctly_if_fields_missing():
assert Marc.get_publishers(source_record) is None


def test_get_related_items_success():
source_record = create_marc_source_record_stub(
datafield_insert=(
"""
<datafield tag="765" ind1="0" ind2=" ">
<subfield code="t">Java 2 in plain English.</subfield>
</datafield>
<datafield tag="770" ind1="1" ind2=" ">
<subfield code="t">Geological Society of America data repository</subfield>
<subfield code="w">(DLC)sn 86025915</subfield>
<subfield code="w">(OCoLC)13535209</subfield>
</datafield>
<datafield tag="772" ind1="0" ind2="0">
<subfield code="a">Earthquake engineering and structural dynamics</subfield>
<subfield code="v">v. 14, no. 5</subfield>
</datafield>
<datafield tag="780" ind1="1" ind2="1">
<subfield code="t">Entertainment design</subfield>
<subfield code="x">1520-5150</subfield>
</datafield>
<datafield tag="785" ind1="0" ind2="0">
<subfield code="t">Protist</subfield>
<subfield code="w">(DLC)sn 98050216</subfield>
<subfield code="w">(OCoLC)39018023</subfield>
<subfield code="x">1434-4610</subfield>
</datafield>
<datafield tag="787" ind1="0" ind2=" ">
<subfield code="i">Part of:</subfield>
<subfield code="t">De historien des Ouden en Nieuwen Testaments</subfield>
</datafield>
<datafield tag="830" ind1=" " ind2="0">
<subfield code="a">Map and chart series (New York State Geological Survey) ;</subfield>
<subfield code="0">(DLC)n 84704569</subfield>
<subfield code="0">(DLC)n 84704570</subfield>
<subfield code="v">no. 53.</subfield>
<subfield code="x">0097-3793</subfield>
</datafield>
<datafield tag="510" ind1="2" ind2=" ">
<subfield code="a">Predicasts</subfield>
</datafield>
"""
)
)
assert Marc.get_related_items(source_record) == [
timdex.RelatedItem(
description="Java 2 in plain English",
relationship="Original Language Version",
),
timdex.RelatedItem(
description=(
"Geological Society of America data repository "
"(DLC)sn 86025915 "
"(OCoLC)13535209"
),
relationship="Has Supplement",
),
timdex.RelatedItem(
description="Earthquake engineering and structural dynamics",
relationship="Supplement To",
),
timdex.RelatedItem(
description=("Entertainment design 1520-5150"), relationship="Previous Title"
),
timdex.RelatedItem(
description="Protist (DLC)sn 98050216 (OCoLC)39018023 1434-4610",
relationship="Subsequent Title",
),
timdex.RelatedItem(
description="Part of: De historien des Ouden en Nieuwen Testaments",
relationship="Not Specified",
),
timdex.RelatedItem(
description="Map and chart series (New York State Geological Survey) ; no. 53. 0097-3793",
relationship="In Series",
),
timdex.RelatedItem(description="Predicasts", relationship="In Bibliography"),
]


def test_get_related_items_transforms_correctly_if_fields_blank():
source_record = create_marc_source_record_stub(
datafield_insert=(
"""
<datafield tag="765" ind1="0" ind2=" ">
<subfield code="t"></subfield>
</datafield>
"""
)
)
assert Marc.get_related_items(source_record) is None


def test_get_related_items_transforms_correctly_if_fields_missing():
source_record = create_marc_source_record_stub()
assert Marc.get_related_items(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
128 changes: 69 additions & 59 deletions transmogrifier/sources/xml/marc.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,65 +105,7 @@ def get_optional_fields(self, source_record: Tag) -> dict | None:
fields["publishers"] = self.get_publishers(source_record)

# related_items
related_item_marc_fields = [
{
"tag": "765",
"subfields": "abcdghikmnorstuwxyz",
"relationship": "Original Language Version",
},
{
"tag": "770",
"subfields": "abcdghikmnorstuwxyz",
"relationship": "Has Supplement",
},
{
"tag": "772",
"subfields": "abcdghikmnorstuwxyz",
"relationship": "Supplement To",
},
{
"tag": "780",
"subfields": "abcdghikmnorstuwxyz",
"relationship": "Previous Title",
},
{
"tag": "785",
"subfields": "abcdghikmnorstuwxyz",
"relationship": "Subsequent Title",
},
{
"tag": "787",
"subfields": "abcdghikmnorstuwxyz",
"relationship": "Not Specified",
},
{
"tag": "830",
"subfields": "adfghklmnoprstvwx",
"relationship": "In Series",
},
{
"tag": "510",
"subfields": "abcx",
"relationship": "In Bibliography",
},
]
for related_item_marc_field in related_item_marc_fields:
for datafield in source_record.find_all(
"datafield", tag=related_item_marc_field["tag"]
):
if related_item_value := (
self.create_subfield_value_string_from_datafield(
datafield,
related_item_marc_field["subfields"],
" ",
)
):
fields.setdefault("related_items", []).append(
timdex.RelatedItem(
description=related_item_value.rstrip(" ."),
relationship=related_item_marc_field["relationship"],
)
)
fields["related_items"] = self.get_related_items(source_record)

# rights not used in MARC

Expand Down Expand Up @@ -992,6 +934,74 @@ def get_publishers(cls, source_record: Tag) -> list[timdex.Publisher] | None:
)
return publishers or None

@classmethod
def get_related_items(cls, source_record: Tag) -> list[timdex.RelatedItem] | None:
related_items = []
related_item_marc_fields = [
{
"tag": "765",
"subfields": "abcdghikmnorstuwxyz",
"relationship": "Original Language Version",
},
{
"tag": "770",
"subfields": "abcdghikmnorstuwxyz",
"relationship": "Has Supplement",
},
{
"tag": "772",
"subfields": "abcdghikmnorstuwxyz",
"relationship": "Supplement To",
},
{
"tag": "780",
"subfields": "abcdghikmnorstuwxyz",
"relationship": "Previous Title",
},
{
"tag": "785",
"subfields": "abcdghikmnorstuwxyz",
"relationship": "Subsequent Title",
},
{
"tag": "787",
"subfields": "abcdghikmnorstuwxyz",
"relationship": "Not Specified",
},
{
"tag": "830",
"subfields": "adfghklmnoprstvwx",
"relationship": "In Series",
},
{
"tag": "510",
"subfields": "abcx",
"relationship": "In Bibliography",
},
]
for related_item_marc_field in related_item_marc_fields:
related_items.extend(
[
timdex.RelatedItem(
description=related_item_value.rstrip(" ."),
relationship=related_item_marc_field["relationship"],
)
for datafield in source_record.find_all(
"datafield", tag=related_item_marc_field["tag"]
)
if (
related_item_value := (
cls.create_subfield_value_string_from_datafield(
datafield,
related_item_marc_field["subfields"],
" ",
)
)
)
]
)
return related_items or None

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

0 comments on commit ac00eaa

Please sign in to comment.