Skip to content

Commit

Permalink
identifiers
Browse files Browse the repository at this point in the history
jonavellecuerdo committed Jul 25, 2024
1 parent 76aabc0 commit 32f9983
Showing 2 changed files with 107 additions and 44 deletions.
53 changes: 53 additions & 0 deletions tests/sources/xml/test_marc.py
Original file line number Diff line number Diff line change
@@ -1236,6 +1236,59 @@ def test_get_holdings_transforms_correctly_if_fields_missing():
assert Marc.get_holdings(source_record) is None


def test_get_identifiers_success():
source_record = create_marc_source_record_stub(
datafield_insert=(
"""
<datafield tag="010" ind1=" " ind2=" ">
<subfield code="a"> 2005022317</subfield>
</datafield>
<datafield tag="020" ind1=" " ind2=" ">
<subfield code="a">9781250185969</subfield>
<subfield code="q">hardcover</subfield>
</datafield>
<datafield tag="022" ind1="0" ind2=" ">
<subfield code="a">0033-0736</subfield>
</datafield>
<datafield tag="024" ind1="7" ind2=" ">
<subfield code="a">10.1596/978-0-8213-7468-9</subfield>
<subfield code="2">doi</subfield>
</datafield>
<datafield tag="035" ind1=" " ind2=" ">
<subfield code="a">(OCoLC)1312285564</subfield>
</datafield>
"""
)
)
assert Marc.get_identifiers(source_record) == [
timdex.Identifier(value="2005022317", kind="LCCN"),
timdex.Identifier(value="9781250185969. hardcover", kind="ISBN"),
timdex.Identifier(value="0033-0736", kind="ISSN"),
timdex.Identifier(
value="10.1596/978-0-8213-7468-9. doi", kind="Other Identifier"
),
timdex.Identifier(value="1312285564", kind="OCLC Number"),
]


def test_get_identifiers_transforms_correctly_if_fields_blank():
source_record = create_marc_source_record_stub(
datafield_insert=(
"""
<datafield tag="010" ind1=" " ind2=" ">
<subfield code="a"></subfield>
</datafield>
"""
)
)
assert Marc.get_identifiers(source_record) is None


def test_get_identifiers_transforms_correctly_if_fields_missing():
source_record = create_marc_source_record_stub()
assert Marc.get_identifiers(source_record) is None


def test_get_links_success():
source_record = create_marc_source_record_stub(
datafield_insert=(
98 changes: 54 additions & 44 deletions transmogrifier/sources/xml/marc.py
Original file line number Diff line number Diff line change
@@ -77,50 +77,7 @@ def get_optional_fields(self, source_record: Tag) -> dict | None:
fields["holdings"] = self.get_holdings(source_record)

# identifiers
identifier_marc_fields = [
{
"tag": "010",
"subfields": "a",
"kind": "LCCN",
},
{
"tag": "020",
"subfields": "aq",
"kind": "ISBN",
},
{
"tag": "022",
"subfields": "a",
"kind": "ISSN",
},
{
"tag": "024",
"subfields": "aq2",
"kind": "Other Identifier",
},
{
"tag": "035",
"subfields": "a",
"kind": "OCLC Number",
},
]
for identifier_marc_field in identifier_marc_fields:
for datafield in source_record.find_all(
"datafield", tag=identifier_marc_field["tag"]
):
if identifier_value := (
self.create_subfield_value_string_from_datafield(
datafield,
identifier_marc_field["subfields"],
". ",
)
):
fields.setdefault("identifiers", []).append(
timdex.Identifier(
value=identifier_value.strip().replace("(OCoLC)", ""),
kind=identifier_marc_field["kind"],
)
)
fields["identifiers"] = self.get_identifiers(source_record)

# languages
languages = []
@@ -850,6 +807,59 @@ def _get_holdings_electronic_items(
note=holding_note,
)

@classmethod
def get_identifiers(cls, source_record: Tag) -> list[timdex.Identifier] | None:
identifiers = []
identifier_marc_fields = [
{
"tag": "010",
"subfields": "a",
"kind": "LCCN",
},
{
"tag": "020",
"subfields": "aq",
"kind": "ISBN",
},
{
"tag": "022",
"subfields": "a",
"kind": "ISSN",
},
{
"tag": "024",
"subfields": "aq2",
"kind": "Other Identifier",
},
{
"tag": "035",
"subfields": "a",
"kind": "OCLC Number",
},
]
for identifier_marc_field in identifier_marc_fields:
identifiers.extend(
[
timdex.Identifier(
value=identifier.strip().replace("(OCoLC)", ""),
kind=identifier_marc_field["kind"],
)
for datafield in source_record.find_all(
"datafield", tag=identifier_marc_field["tag"]
)
if (
identifier := (
cls.create_subfield_value_string_from_datafield(
datafield,
identifier_marc_field["subfields"],
". ",
)
)
)
]
)
return identifiers or None

@classmethod
def get_links(cls, source_record: Tag) -> list[timdex.Link] | None:
links: list[timdex.Link] = []

0 comments on commit 32f9983

Please sign in to comment.