Skip to content

Commit dd2c4ac

Browse files
Update post-transform methods to handle None values (#147)
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
1 parent a019673 commit dd2c4ac

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

tests/sources/test_transformer.py

+43-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,24 @@ def test_create_dates_and_locations_from_publishers_success():
4646
}
4747

4848

49-
def test_create_locations_from_spatial_subjects():
49+
def test_create_dates_and_locations_from_publishers_when_fields_are_none_success():
50+
fields = {
51+
"publishers": [
52+
timdex.Publisher(name="Publisher", date="Date", location="Location")
53+
],
54+
"dates": None,
55+
"locations": None,
56+
}
57+
assert Transformer.create_dates_and_locations_from_publishers(fields) == {
58+
"publishers": [
59+
timdex.Publisher(name="Publisher", date="Date", location="Location")
60+
],
61+
"dates": [timdex.Date(kind="Publication date", value="Date")],
62+
"locations": [timdex.Location(value="Location", kind="Place of Publication")],
63+
}
64+
65+
66+
def test_create_locations_from_spatial_subjects_success():
5067
fields = {
5168
"subjects": [
5269
timdex.Subject(
@@ -70,6 +87,31 @@ def test_create_locations_from_spatial_subjects():
7087
}
7188

7289

90+
def test_create_locations_from_spatial_subjects_when_field_is_none_success():
91+
fields = {
92+
"subjects": [
93+
timdex.Subject(
94+
value=["Some city, Some country"], kind="Dublin Core; Spatial"
95+
),
96+
timdex.Subject(value=["City 1", "City 2"], kind="Dublin Core; Spatial"),
97+
],
98+
"locations": None,
99+
}
100+
assert Transformer.create_locations_from_spatial_subjects(fields) == {
101+
"subjects": [
102+
timdex.Subject(
103+
value=["Some city, Some country"], kind="Dublin Core; Spatial"
104+
),
105+
timdex.Subject(value=["City 1", "City 2"], kind="Dublin Core; Spatial"),
106+
],
107+
"locations": [
108+
timdex.Location(value="Some city, Some country", kind="Place Name"),
109+
timdex.Location(value="City 1", kind="Place Name"),
110+
timdex.Location(value="City 2", kind="Place Name"),
111+
],
112+
}
113+
114+
73115
def test_xmltransformer_initializes_with_expected_attributes(oai_pmh_records):
74116
transformer = XMLTransformer("cool-repo", oai_pmh_records)
75117
assert transformer.source == "cool-repo"

transmogrifier/sources/transformer.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,17 @@ def create_dates_and_locations_from_publishers(fields: dict) -> dict:
384384
publisher_date = timdex.Date(
385385
kind="Publication date", value=publisher.date
386386
)
387-
if publisher_date not in fields.setdefault("dates", []):
387+
if fields.get("dates") is None:
388+
fields["dates"] = []
389+
if publisher_date not in fields["dates"]:
388390
fields["dates"].append(publisher_date)
389391
if publisher.location:
390392
publisher_location = timdex.Location(
391393
kind="Place of Publication", value=publisher.location
392394
)
393-
if publisher_location not in fields.setdefault("locations", []):
395+
if fields.get("locations") is None:
396+
fields["locations"] = []
397+
if publisher_location not in fields["locations"]:
394398
fields["locations"].append(publisher_location)
395399
return fields
396400

@@ -414,6 +418,8 @@ def create_locations_from_spatial_subjects(fields: dict) -> dict:
414418
for subject in spatial_subjects:
415419
for place_name in subject.value:
416420
subject_location = timdex.Location(value=place_name, kind="Place Name")
417-
if subject_location not in fields.setdefault("locations", []):
421+
if fields.get("locations") is None:
422+
fields["locations"] = []
423+
if subject_location not in fields["locations"]:
418424
fields["locations"].append(subject_location)
419425
return fields

0 commit comments

Comments
 (0)