Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add final set of Datacite field methods #189

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 187 additions & 0 deletions tests/sources/xml/test_datacite.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,193 @@ def test_get_locations_transforms_correctly_if_fields_missing():
assert Datacite.get_locations(source_record) is None


def test_get_notes_success():
source_record = create_datacite_source_record_stub(
"""
<resourceType resourceTypeGeneral="Dataset">Survey Data</resourceType>
<descriptions>
<description descriptionType="TechnicalInfo">Stata, 13</description>
</descriptions>
"""
)
assert Datacite.get_notes(source_record) == [
Note(value=["Survey Data"], kind="Datacite resource type"),
Note(value=["Stata, 13"], kind="TechnicalInfo"),
]


def test_get_notes_transforms_correctly_if_fields_blank():
source_record = create_datacite_source_record_stub(
"<descriptions><description /></descriptions>"
)
assert Datacite.get_notes(source_record) is None


def test_get_notes_transforms_correctly_if_fields_missing():
source_record = create_datacite_source_record_stub()
assert Datacite.get_notes(source_record) is None


def test_get_publishers_success():
source_record = create_datacite_source_record_stub(
"<publisher>Harvard Dataverse</publisher>"
)
assert Datacite.get_publishers(source_record) == [Publisher(name="Harvard Dataverse")]


def test_get_publishers_transforms_correctly_if_fields_blank():
source_record = create_datacite_source_record_stub("<publisher />")
assert Datacite.get_publishers(source_record) is None


def test_get_publishers_transforms_correctly_if_fields_missing():
source_record = create_datacite_source_record_stub()
assert Datacite.get_publishers(source_record) is None


def test_get_related_items_success():
metadata_insert = (
'<relatedIdentifiers><relatedIdentifier relatedIdentifierType="DOI" '
'relationType="IsCitedBy">10.1257/app.20150390</relatedIdentifier>'
'<relatedIdentifier relationType="IsVersionOf">10.5281/zenodo.5524464'
'</relatedIdentifier><relatedIdentifier relatedIdentifierType="ISBN" '
'relationType="Other">1234567.5524464</relatedIdentifier><relatedIdentifier '
'relatedIdentifierType="URL" relationType="IsPartOf">'
"https://zenodo.org/communities/astronomy-general</relatedIdentifier>"
"</relatedIdentifiers>"
)
source_record = create_datacite_source_record_stub(metadata_insert)
assert Datacite.get_related_items(source_record) == [
RelatedItem(relationship="IsCitedBy", uri="https://doi.org/10.1257/app.20150390"),
RelatedItem(relationship="IsVersionOf", uri="10.5281/zenodo.5524464"),
RelatedItem(relationship="Other", uri="1234567.5524464"),
RelatedItem(
relationship="IsPartOf",
uri="https://zenodo.org/communities/astronomy-general",
),
]


def test_get_related_items_transforms_correctly_if_fields_blank():
source_record = create_datacite_source_record_stub(
"<relatedIdentifiers><relatedIdentifier /></relatedIdentifiers>"
)
assert Datacite.get_related_items(source_record) is None


def test_get_related_items_transforms_correctly_if_fields_missing():
source_record = create_datacite_source_record_stub()
assert Datacite.get_related_items(source_record) is None


def test_get_rights_success():
source_record = create_datacite_source_record_stub(
"""
<rightsList>
<rights rightsURI="info:eu-repo/semantics/openAccess" />
<rights
rightsURI="http://creativecommons.org/publicdomain/zero/1.0">CC0 1.0</rights>
</rightsList>
"""
)
assert Datacite.get_rights(source_record) == [
Rights(description=None, kind=None, uri="info:eu-repo/semantics/openAccess"),
Rights(
description="CC0 1.0",
kind=None,
uri="http://creativecommons.org/publicdomain/zero/1.0",
),
]


def test_get_rights_transforms_correctly_if_fields_blank():
source_record = create_datacite_source_record_stub(
"<rightsList><rights /></rightsList>"
)
assert Datacite.get_rights(source_record) is None


def test_get_rights_transforms_correctly_if_fields_missing():
source_record = create_datacite_source_record_stub()
assert Datacite.get_rights(source_record) is None


def test_get_subjects_success():
source_record = create_datacite_source_record_stub(
"""
<subjects>
<subject>Social Sciences</subject>
<subject>Educational materials</subject>
<subject subjectScheme="LCSH"
>Adult education, education inputs, field experiments</subject>
<subject subjectScheme="LCSH">Education</subject>
</subjects>
"""
)
assert Datacite.get_subjects(source_record) == [
Subject(
value=["Social Sciences", "Educational materials"],
kind="Subject scheme not provided",
),
Subject(
value=[
"Adult education, education inputs, field experiments",
"Education",
],
kind="LCSH",
),
]


def test_get_subjects_transforms_correctly_if_fields_blank():
source_record = create_datacite_source_record_stub("<subjects><subject /></subjects>")
assert Datacite.get_subjects(source_record) is None


def test_get_subjects_transforms_correctly_if_fields_missing():
source_record = create_datacite_source_record_stub()
assert Datacite.get_subjects(source_record) is None


def test_get_summary_success():
metadata_insert = (
'<descriptions><description descriptionType="Abstract">Using a '
"randomized field experiment in India, we evaluate the effectiveness of adult "
"literacy and parental involvement interventions in improving children's "
"learning. Households were assigned to receive either adult literacy (language "
"and math) classes for mothers, training for mothers on how to enhance their "
"children's learning at home, or a combination of the two programs. All three "
"interventions had significant but modest impacts on childrens math scores. The "
"interventions also increased mothers' test scores in both language and math, as "
"well as a range of other outcomes reflecting greater involvement of mothers in "
"their children's education.</description>"
)
source_record = create_datacite_source_record_stub(metadata_insert)
assert Datacite.get_summary(source_record) == [
"Using a randomized field experiment in India, we evaluate the effectiveness "
"of adult literacy and parental involvement interventions in improving "
"children's learning. Households were assigned to receive either adult "
"literacy (language and math) classes for mothers, training for mothers on "
"how to enhance their children's learning at home, or a combination of the "
"two programs. All three interventions had significant but modest impacts on "
"childrens math scores. The interventions also increased mothers' test scores"
" in both language and math, as well as a range of other outcomes reflecting "
"greater involvement of mothers in their children's education."
]


def test_get_summarty_transforms_correctly_if_fields_blank():
source_record = create_datacite_source_record_stub(
'<descriptions><description descriptionType="Abstract" /></descriptions>'
)
assert Datacite.get_summary(source_record) is None


def test_get_summary_transforms_correctly_if_fields_missing():
source_record = create_datacite_source_record_stub()
assert Datacite.get_summary(source_record) is None


def test_generate_name_identifier_url_orcid_scheme(datacite_record_all_fields):
assert next(datacite_record_all_fields).contributors[0].identifier == [
"https://orcid.org/0000-0000-0000-0000"
Expand Down
168 changes: 100 additions & 68 deletions transmogrifier/sources/xml/datacite.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def get_optional_fields(self, source_record: Tag) -> dict | None:
oai_datacite XML.
"""
fields: dict = {}
source_record_id = self.get_source_record_id(source_record)

# alternate_titles
fields["alternate_titles"] = self.get_alternate_titles(source_record)
Expand Down Expand Up @@ -64,83 +63,22 @@ def get_optional_fields(self, source_record: Tag) -> dict | None:
fields["locations"] = self.get_locations(source_record)

# notes
if resource_type := source_record.metadata.find("resourceType", string=True):
fields.setdefault("notes", []).append(
timdex.Note(
value=[str(resource_type.string)],
kind="Datacite resource type",
)
)
descriptions = source_record.metadata.find_all("description", string=True)
for description in descriptions:
if "descriptionType" not in description.attrs:
logger.warning(
"Datacite record %s missing required Datacite attribute "
"@descriptionType",
source_record_id,
)
if description.get("descriptionType") != "Abstract":
fields.setdefault("notes", []).append(
timdex.Note(
value=[description.string],
kind=description.get("descriptionType") or None,
)
)
fields["notes"] = self.get_notes(source_record)
ehanson8 marked this conversation as resolved.
Show resolved Hide resolved

# publishers
if publisher := source_record.metadata.find("publisher", string=True):
fields["publishers"] = [timdex.Publisher(name=publisher.string)]
else:
logger.warning(
"Datacite record %s missing required Datacite field publisher",
source_record_id,
)
fields["publishers"] = self.get_publishers(source_record)

# related_items, uses related_identifiers retrieved for identifiers
for related_identifier in [
ri
for ri in source_record.metadata.find_all("relatedIdentifier", string=True)
if ri.get("relationType") != "IsIdenticalTo"
]:
fields.setdefault("related_items", []).append(
timdex.RelatedItem(
uri=self.generate_related_item_identifier_url(related_identifier),
relationship=related_identifier.get("relationType")
or "Not specified",
)
)
fields["related_items"] = self.get_related_items(source_record)

# rights
for right in [
r
for r in source_record.metadata.find_all("rights")
if r.string or r.get("rightsURI")
]:
fields.setdefault("rights", []).append(
timdex.Rights(
description=right.string or None, uri=right.get("rightsURI") or None
)
)
fields["rights"] = self.get_rights(source_record)

# subjects
subjects_dict: dict[str, list[str]] = {}
for subject in source_record.metadata.find_all("subject", string=True):
if not subject.get("subjectScheme"):
subjects_dict.setdefault("Subject scheme not provided", []).append(
subject.string
)
else:
subjects_dict.setdefault(subject["subjectScheme"], []).append(
subject.string
)
fields["subjects"] = [
timdex.Subject(value=value, kind=key) for key, value in subjects_dict.items()
] or None
fields["subjects"] = self.get_subjects(source_record)

# summary
fields["summary"] = [
d.string for d in descriptions if d.get("descriptionType") == "Abstract"
] or None
fields["summary"] = self.get_summary(source_record)

return fields

Expand Down Expand Up @@ -425,6 +363,100 @@ def get_locations(cls, source_record: Tag) -> list[timdex.Location] | None:
)
] or None

@classmethod
def get_notes(cls, source_record: Tag) -> list[timdex.Note] | None:
notes = []
notes.extend(list(cls._get_resource_type_note(source_record)))
notes.extend(list(cls._get_description_notes(source_record)))
return notes or None

@classmethod
def _get_resource_type_note(cls, source_record: Tag) -> Iterator[timdex.Note]:
if resource_type := source_record.metadata.find("resourceType", string=True):
yield timdex.Note(
value=[str(resource_type.string)],
kind="Datacite resource type",
)

@classmethod
def _get_description_notes(cls, source_record: Tag) -> Iterator[timdex.Note]:
descriptions = source_record.metadata.find_all("description", string=True)
ehanson8 marked this conversation as resolved.
Show resolved Hide resolved
for description in descriptions:
if "descriptionType" not in description.attrs:
logger.warning(
"Datacite record %s missing required Datacite attribute "
"@descriptionType",
cls.get_source_record_id(source_record),
)
if description.get("descriptionType") != "Abstract":
yield timdex.Note(
value=[str(description.string)],
kind=description.get("descriptionType") or None,
)
ehanson8 marked this conversation as resolved.
Show resolved Hide resolved

@classmethod
def get_publishers(cls, source_record: Tag) -> list[timdex.Publisher] | None:
publishers = []
if publisher := source_record.metadata.find("publisher", string=True):
publishers.append(timdex.Publisher(name=str(publisher.string)))
else:
logger.warning(
"Datacite record %s missing required Datacite field publisher",
cls.get_source_record_id(source_record),
)
return publishers or None
ehanson8 marked this conversation as resolved.
Show resolved Hide resolved

@classmethod
def get_related_items(cls, source_record: Tag) -> list[timdex.RelatedItem] | None:
return [
timdex.RelatedItem(
uri=cls.generate_related_item_identifier_url(related_identifier),
relationship=related_identifier.get("relationType") or "Not specified",
)
for related_identifier in source_record.metadata.find_all(
"relatedIdentifier", string=True
)
if related_identifier.get("relationType") != "IsIdenticalTo"
] or None

@classmethod
def get_rights(cls, source_record: Tag) -> list[timdex.Rights] | None:
return [
timdex.Rights(
description=rights.string or None,
jonavellecuerdo marked this conversation as resolved.
Show resolved Hide resolved
uri=rights.get("rightsURI") or None,
)
for rights in source_record.metadata.find_all("rights")
if rights.string or rights.get("rightsURI")
] or None

@classmethod
def get_subjects(cls, source_record: Tag) -> list[timdex.Subject] | None:
subjects_dict: dict[str, list[str]] = {}

for subject in source_record.metadata.find_all("subject", string=True):
if not subject.get("subjectScheme"):
subjects_dict.setdefault("Subject scheme not provided", []).append(
str(subject.string)
)
else:
subjects_dict.setdefault(subject["subjectScheme"], []).append(
str(subject.string)
)

return [
timdex.Subject(value=subject_value, kind=subject_scheme)
for subject_scheme, subject_value in subjects_dict.items()
] or None
ehanson8 marked this conversation as resolved.
Show resolved Hide resolved

@classmethod
def get_summary(cls, source_record: Tag) -> list[str] | None:
return [
str(description.string)
for description in source_record.metadata.find_all("description", string=True)
if description.get("descriptionType") == "Abstract"
] or None

@classmethod
def get_main_titles(cls, source_record: Tag) -> list[str]:
"""
Expand Down
Loading