Skip to content

Commit

Permalink
tests for resource_type
Browse files Browse the repository at this point in the history
  • Loading branch information
phette23 committed Oct 30, 2023
1 parent b701adc commit d0547dd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
13 changes: 6 additions & 7 deletions migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,14 @@ def type(self):
# There are many fields that could be used to determine the resource type. Priority:
# 1. mods/typeOfResource, 2. local/courseWorkType, 3. TBD (there are more...)
# mods/typeOfResourceWrapper/typeOfResource
wrapper = self.xml.get("mods", {}).get("typeOfResourceWrapper", [])
# Take the first typeOfResource value we find
wrapper = self.xml.get("mods", {}).get("typeOfResourceWrapper")
if type(wrapper) == list:
rtype = wrapper[0].get("typeOfResource")
if type(rtype) == dict:
rtype = rtype.get("#text")
if rtype in resource_type_map:
return {"id": resource_type_map[rtype]}
elif type(wrapper) == dict:
wrapper = wrapper[0]
if type(wrapper) == dict:
rtype = wrapper.get("typeOfResource")
if type(rtype) == list:
rtype = rtype[0]
if type(rtype) == dict:
rtype = rtype.get("#text")
if rtype in resource_type_map:
Expand Down
27 changes: 27 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,30 @@ def test_title(input, expect):
def test_addl_titles(input, expect):
r = Record(input)
assert m(r)["additional_titles"] == expect


# Resource Type
@pytest.mark.parametrize(
"input, expect",
[
( # regular mapping
x(
"<mods><typeOfResourceWrapper><typeOfResource>Event documentation</typeOfResource></typeOfResourceWrapper></mods>"
),
{"id": "event"},
),
( # multiple <typeOfResource> elements
x(
"<mods><typeOfResourceWrapper><typeOfResource>moving image</typeOfResource><typeOfResource>mixed material</typeOfResource></typeOfResourceWrapper></mods>"
),
{"id": "image"},
),
( # default to publication
x("<mods></mods>"),
{"id": "publication"},
),
],
)
def test_type(input, expect):
r = Record(input)
assert m(r)["resource_type"] == expect

0 comments on commit d0547dd

Please sign in to comment.