Skip to content

Commit

Permalink
Make 'source_record' the first argument in OaiDc field methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jonavellecuerdo committed May 22, 2024
1 parent 628e003 commit 04343d4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
35 changes: 17 additions & 18 deletions tests/sources/xml/test_oai_dc.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import pytest

from bs4 import BeautifulSoup

import transmogrifier.models as timdex
from transmogrifier.sources.xml.oaidc import OaiDc


def create_oai_dc_source_record_stub(xml_insert: str = "") -> BeautifulSoup:
def create_oaidc_source_record_stub(xml_insert: str = "") -> BeautifulSoup:
xml_str = f"""
<records>
<record>
Expand All @@ -29,7 +28,7 @@ def create_oai_dc_source_record_stub(xml_insert: str = "") -> BeautifulSoup:
return BeautifulSoup(xml_str, "xml")


def test_oai_dc_transform_with_all_fields_transforms_correctly():
def test_oaidc_transform_with_all_fields_transforms_correctly():
source_records = OaiDc.parse_source_file(
"tests/fixtures/oai_dc/oaidc_record_all_fields.xml"
)
Expand Down Expand Up @@ -83,7 +82,7 @@ def test_oaidc_transform_with_optional_fields_blank_transforms_correctly():
)


def test_oai_dc_transform_with_optional_fields_missing_transforms_correctly():
def test_oaidc_transform_with_optional_fields_missing_transforms_correctly():
source_records = OaiDc.parse_source_file(
"tests/fixtures/oai_dc/oaidc_record_optional_fields_missing.xml"
)
Expand Down Expand Up @@ -121,7 +120,7 @@ def test_get_content_type_raises_key_error_if_source_missing(oai_dc_record_all_f


def test_get_contributors_success():
source_record = create_oai_dc_source_record_stub(
source_record = create_oaidc_source_record_stub(
"""
<dc:creator>Ye Li</dc:creator>
"""
Expand All @@ -147,12 +146,12 @@ def test_get_contributors_transforms_correctly_if_fields_missing(


def test_get_dates_success():
source_record = create_oai_dc_source_record_stub(
source_record = create_oaidc_source_record_stub(
"""
<dc:date>2008-06-19T17:55:27</dc:date>
"""
)
assert OaiDc.get_dates(source_record_id="abc", source_record=source_record) == [
assert OaiDc.get_dates(source_record=source_record, source_record_id="abc") == [
timdex.Date(kind="Unknown", value="2008-06-19T17:55:27")
]

Expand All @@ -162,7 +161,7 @@ def test_get_dates_transforms_correctly_if_fields_blank(
):
assert (
OaiDc.get_dates(
source_record_id="abc", source_record=oai_dc_record_optional_fields_blank
source_record=oai_dc_record_optional_fields_blank, source_record_id="abc"
)
== []
)
Expand All @@ -173,19 +172,19 @@ def test_get_dates_transforms_correctly_if_fields_missing(
):
assert (
OaiDc.get_dates(
source_record_id="abc", source_record=oai_dc_record_optional_fields_missing
source_record=oai_dc_record_optional_fields_missing, source_record_id="abc"
)
== []
)


def test_get_dates_transforms_correctly_if_date_invalid():
source_record = create_oai_dc_source_record_stub(
source_record = create_oaidc_source_record_stub(
"""
<dc:date>INVALID</dc:date>
"""
)
assert OaiDc.get_dates(source_record_id="abc", source_record=source_record) == []
assert OaiDc.get_dates(source_record=source_record, source_record_id="abc") == []


def test_get_identifiers_success(oai_dc_record_all_fields):
Expand All @@ -209,7 +208,7 @@ def test_get_identifiers_transforms_correctly_if_fields_missing(


def test_get_publishers_success():
source_record = create_oai_dc_source_record_stub(
source_record = create_oaidc_source_record_stub(
"""
<dc:publisher>MIT Libraries</dc:publisher>
"""
Expand All @@ -230,7 +229,7 @@ def test_get_publishers_transforms_correctly_if_fields_missing(


def test_get_subjects_success():
source_record = create_oai_dc_source_record_stub(
source_record = create_oaidc_source_record_stub(
"""
<dc:subject>Engineering</dc:subject>
<dc:subject>Science</dc:subject>
Expand All @@ -256,10 +255,10 @@ def test_get_subjects_transforms_correctly_if_fields_missing(


def test_get_summary_success():
source_record = create_oai_dc_source_record_stub(
source_record = create_oaidc_source_record_stub(
"""
<dc:description>Useful databases and other research tips for materials science.</dc:description>
"""
""" # noqa: E501
)
assert OaiDc.get_summary(source_record) == [
"Useful databases and other research tips for materials science."
Expand All @@ -279,7 +278,7 @@ def test_get_summary_transforms_properly_if_fields_missing(


def test_get_main_titles_success():
source_record = create_oai_dc_source_record_stub(
source_record = create_oaidc_source_record_stub(
"""
<dc:title>Materials Science &amp; Engineering</dc:title>
"""
Expand All @@ -288,7 +287,7 @@ def test_get_main_titles_success():


def test_get_main_titles_transforms_properly_if_fields_blank():
source_record = create_oai_dc_source_record_stub(
source_record = create_oaidc_source_record_stub(
"""
<dc:title></dc:title>
"""
Expand All @@ -297,7 +296,7 @@ def test_get_main_titles_transforms_properly_if_fields_blank():


def test_get_main_titles_transforms_properly_if_fields_missing():
source_record = create_oai_dc_source_record_stub()
source_record = create_oaidc_source_record_stub()
assert OaiDc.get_main_titles(source_record) == []


Expand Down
14 changes: 7 additions & 7 deletions transmogrifier/sources/xml/oaidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_optional_fields(self, source_record: Tag) -> dict | None:
fields["contributors"] = self.get_contributors(source_record) or None

# dates
fields["dates"] = self.get_dates(source_record_id, source_record) or None
fields["dates"] = self.get_dates(source_record, source_record_id) or None

# edition: not set in this transformation

Expand All @@ -63,7 +63,7 @@ def get_optional_fields(self, source_record: Tag) -> dict | None:
# languages: not set in this transformation

# links
fields["links"] = self.get_links(source_record_id, source_record) or None
fields["links"] = self.get_links(source_record, source_record_id) or None

# literary_form: not set in this transformation

Expand Down Expand Up @@ -102,16 +102,17 @@ def get_contributors(cls, source_record: Tag) -> list[timdex.Contributor]:
]

@classmethod
def get_dates(cls, source_record_id: str, source_record: Tag) -> list[timdex.Date]:
def get_dates(cls, source_record: Tag, source_record_id: str) -> list[timdex.Date]:
"""
Method to get TIMDEX "dates" field. This method broken out to allow subclasses
to override.
Return list of timdex.Date's if valid and present.
Args:
source_record_id: Source record ID.
source_record: A BeautifulSoup Tag representing a single OAI DC record in XML.
source_record_id: Source record ID.
"""
dates = []
for date in source_record.find_all("dc:date", string=True):
Expand All @@ -136,15 +137,14 @@ def get_identifiers(cls, source_record: Tag) -> list[timdex.Identifier]:
)
return identifiers

@classmethod
def get_links(cls, source_record_id: str, source_record: Tag) -> list[timdex.Link]:
def get_links(self, _source_record: Tag, _source_record_id: str) -> list[timdex.Link]:
"""
Method to get TIMDEX "links" field. This method broken out to allow subclasses
to override.
Args:
source_record_id: Source record ID.
source_record: A BeautifulSoup Tag representing a single OAI DC record in XML.
source_record_id: Source record ID.
"""
return []

Expand Down

0 comments on commit 04343d4

Please sign in to comment.