Skip to content

Commit

Permalink
Update post-transform methods to handle None values (#147)
Browse files Browse the repository at this point in the history
Why these changes are being introduced:
* When a field method returns "None", the resulting "fields" dictionary will
set a key-value pair, where key = <field_name> and value = None. This will result
in the .setdefault() method being ignored during the post-transform methods
as it will only set the value in the "fields" dictionary to an empty list if
the key is not present.

How this addresses that need:
* Check if field methods that are updated by post-transform methods are
already set to None. If None, set to an empty list.

Side effects of this change:
* None

Relevant ticket(s):
* https://mitlibraries.atlassian.net/browse/GDT-217
* https://mitlibraries.atlassian.net/browse/GDT-210
  • Loading branch information
jonavellecuerdo authored Mar 12, 2024
1 parent a019673 commit dd2c4ac
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
44 changes: 43 additions & 1 deletion tests/sources/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,24 @@ def test_create_dates_and_locations_from_publishers_success():
}


def test_create_locations_from_spatial_subjects():
def test_create_dates_and_locations_from_publishers_when_fields_are_none_success():
fields = {
"publishers": [
timdex.Publisher(name="Publisher", date="Date", location="Location")
],
"dates": None,
"locations": None,
}
assert Transformer.create_dates_and_locations_from_publishers(fields) == {
"publishers": [
timdex.Publisher(name="Publisher", date="Date", location="Location")
],
"dates": [timdex.Date(kind="Publication date", value="Date")],
"locations": [timdex.Location(value="Location", kind="Place of Publication")],
}


def test_create_locations_from_spatial_subjects_success():
fields = {
"subjects": [
timdex.Subject(
Expand All @@ -70,6 +87,31 @@ def test_create_locations_from_spatial_subjects():
}


def test_create_locations_from_spatial_subjects_when_field_is_none_success():
fields = {
"subjects": [
timdex.Subject(
value=["Some city, Some country"], kind="Dublin Core; Spatial"
),
timdex.Subject(value=["City 1", "City 2"], kind="Dublin Core; Spatial"),
],
"locations": None,
}
assert Transformer.create_locations_from_spatial_subjects(fields) == {
"subjects": [
timdex.Subject(
value=["Some city, Some country"], kind="Dublin Core; Spatial"
),
timdex.Subject(value=["City 1", "City 2"], kind="Dublin Core; Spatial"),
],
"locations": [
timdex.Location(value="Some city, Some country", kind="Place Name"),
timdex.Location(value="City 1", kind="Place Name"),
timdex.Location(value="City 2", kind="Place Name"),
],
}


def test_xmltransformer_initializes_with_expected_attributes(oai_pmh_records):
transformer = XMLTransformer("cool-repo", oai_pmh_records)
assert transformer.source == "cool-repo"
Expand Down
12 changes: 9 additions & 3 deletions transmogrifier/sources/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,13 +384,17 @@ def create_dates_and_locations_from_publishers(fields: dict) -> dict:
publisher_date = timdex.Date(
kind="Publication date", value=publisher.date
)
if publisher_date not in fields.setdefault("dates", []):
if fields.get("dates") is None:
fields["dates"] = []
if publisher_date not in fields["dates"]:
fields["dates"].append(publisher_date)
if publisher.location:
publisher_location = timdex.Location(
kind="Place of Publication", value=publisher.location
)
if publisher_location not in fields.setdefault("locations", []):
if fields.get("locations") is None:
fields["locations"] = []
if publisher_location not in fields["locations"]:
fields["locations"].append(publisher_location)
return fields

Expand All @@ -414,6 +418,8 @@ def create_locations_from_spatial_subjects(fields: dict) -> dict:
for subject in spatial_subjects:
for place_name in subject.value:
subject_location = timdex.Location(value=place_name, kind="Place Name")
if subject_location not in fields.setdefault("locations", []):
if fields.get("locations") is None:
fields["locations"] = []
if subject_location not in fields["locations"]:
fields["locations"].append(subject_location)
return fields

0 comments on commit dd2c4ac

Please sign in to comment.