Skip to content

Commit

Permalink
get_contents
Browse files Browse the repository at this point in the history
  • Loading branch information
jonavellecuerdo committed Jul 23, 2024
1 parent b1c293a commit a87f328
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
37 changes: 37 additions & 0 deletions tests/sources/xml/test_marc.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,43 @@ def test_get_content_type_transforms_correctly_if_code_blank():
assert Marc.get_content_type(source_record) is None


def test_get_contents_success():
source_record = create_marc_source_record_stub(
datafield_insert=(
"""
<datafield tag="505" ind1="0" ind2="0">
<subfield code="a">General observations -- Methodology -- Initial phase</subfield>
<subfield code="g">Miscellaneous information.</subfield>
</datafield>
"""
)
)
assert Marc.get_contents(source_record) == [
"General observations",
"Methodology",
"Initial phase",
"Miscellaneous information",
]


def test_get_contents_transforms_correctly_if_fields_blank():
source_record = create_marc_source_record_stub(
datafield_insert=(
"""
<datafield tag="505" ind1="0" ind2="0">
<subfield code="a"></subfield>
</datafield>
"""
)
)
assert Marc.get_contents(source_record) is None


def test_get_contents_transforms_correctly_if_fields_missing():
source_record = create_marc_source_record_stub()
assert Marc.get_contents(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
24 changes: 17 additions & 7 deletions transmogrifier/sources/xml/marc.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,7 @@ def get_optional_fields(self, source_record: Tag) -> dict | None:
fields["content_type"] = self.get_content_type(source_record)

# contents
for datafield in source_record.find_all("datafield", tag="505"):
for contents_value in self.create_subfield_value_list_from_datafield(
datafield,
"agrt",
):
for contents_item in contents_value.split(" -- "):
fields.setdefault("contents", []).append(contents_item.rstrip(" ./-"))
fields["contents"] = self.get_contents(source_record)

# contributors
contributor_marc_fields = [
Expand Down Expand Up @@ -832,6 +826,22 @@ def get_content_type(cls, source_record: Tag) -> list[str] | None:
return [content_type]
return None

@classmethod
def get_contents(cls, source_record: Tag) -> list[str] | None:
contents = []
for datafield in source_record.find_all("datafield", tag="505"):
for contents_value in cls.create_subfield_value_list_from_datafield(
datafield,
"agrt",
):
contents.extend(
[
contents_item.rstrip(" ./-")
for contents_item in contents_value.split(" -- ")
]
)
return contents or None

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

0 comments on commit a87f328

Please sign in to comment.