Skip to content

Commit

Permalink
Merge pull request #17 from unt-libraries/resource-type-hardening
Browse files Browse the repository at this point in the history
Prevent KeyErrors from happening when there's no resourceType in the desc_MD.
  • Loading branch information
somexpert authored Sep 5, 2018
2 parents 2c6cd46 + 4094b93 commit 7f54231
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Change Log
==========


1.2.1
-----

* Fixed bug which could raise KeyError when looking up transcription files for records with no resourceType.


1.2.0
-----

Expand Down
2 changes: 1 addition & 1 deletion aubreylib/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def __init__(self, identifier, metadataLocations, staticFileLocations,
# Get transcriptions data
self.transcriptions = get_transcriptions_data(
meta_id=self.meta_id,
resource_type=self.desc_MD['resourceType'][0]['content'],
resource_type=self.desc_MD.get('resourceType', [{}])[0].get('content'),
transcriptions_server_url=kwargs.get('transcriptions_server_url'),
)
# Get the fileSets within the fileSec
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name='aubreylib',
version='1.2.0',
version='1.2.1',
description='A helper library for the Aubrey access system.',
author='University of North Texas Libraries',
author_email='mark.phillips@unt.edu',
Expand Down
24 changes: 22 additions & 2 deletions tests/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ def test_get_dimensions_data_absent(self, mock_exists):

class TestGetTranscriptionsData:

def test_get_transcriptions_data_wrong_resource_type(self):
result = resource.get_transcriptions_data('metadc123', 'text', 'http://example.com')
@pytest.mark.parametrize('resource_type', [
'',
None,
'text',
])
def test_get_transcriptions_data_wrong_resource_type(self, resource_type):
result = resource.get_transcriptions_data('metadc123', resource_type, 'http://example.com')
assert result == {}

@pytest.mark.parametrize('url', [
Expand Down Expand Up @@ -196,3 +201,18 @@ def testResourceObjectTranscriptions(self, mocked_fileSet_file,
assert not ro.manifestation_dict[1][1]['has_vtt_chapters']
assert not ro.manifestation_dict[1][1]['has_vtt_thumbnails']
assert not ro.manifestation_dict[1][1]['has_vtt_metadata']

@patch.object(resource.ResourceObject, 'get_fileSet_file')
@patch('aubreylib.resource.get_desc_metadata')
def testResourceObjectEmptyDescMD(self, mocked_get_desc_metadata, mocked_fileSet_file):
"""Tests that a ResourceObject can instantiate with missing descriptive metadata keys."""
mocked_get_desc_metadata.return_value = {}
mocked_fileSet_file.return_value = {'file_mimetype': '',
'file_name': '',
'files_system': ''}
current_directory = os.path.dirname(os.path.abspath(__file__))
mets_path = '{0}/data/metapth12434.mets.xml'.format(current_directory)

resource.ResourceObject(identifier=mets_path, metadataLocations=[],
staticFileLocations=[], mimetypeIconsPath='', use=USE,
transcriptions_server_url='http://example.com')

0 comments on commit 7f54231

Please sign in to comment.