Skip to content

Commit

Permalink
Modified sorting and added tests
Browse files Browse the repository at this point in the history
Your Name committed Mar 1, 2024
1 parent 147330c commit 6f2aab2
Showing 3 changed files with 62 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@
- Bardo CCE API and Hathi DataFiles URL updated
- Deleted Tugboat configuration as Tugboat is no longer used and no longer builds
- Adjusted sorting for item source and media type priority
- Removed item media type sorting to fix 500 error with search endpoint for filtering by format
- Fixed 500 error with search endpoint for filtering by format by adjusting item sorting

## 2023-09-05 version -- v0.12.3
## Removed
23 changes: 18 additions & 5 deletions api/utils.py
Original file line number Diff line number Diff line change
@@ -365,9 +365,22 @@ def formatEdition(

editionDict['items'].append(itemDict)

editionDict['items']\
.sort(key=lambda x: (cls.SOURCE_PRIORITY[x['source']]))

emptyListFlag = False

#This for loop is meant to check if a empty links array exists in a item dictionary
#If one exists, then sorting by source priority and empty/non-empty priority occurs
#Otherwise, sorting by source and link media type occurs
for itemDict in editionDict['items']:
if itemDict['links'] == []:
editionDict['items']\
.sort(key=lambda x: (cls.SOURCE_PRIORITY[x['source']], x['links'] == []))
emptyListFlag = True
break

if emptyListFlag == False:
editionDict['items']\
.sort(key=lambda x: (cls.SOURCE_PRIORITY[x['source']], cls.sortByMediaType(x['links'][0])))

if records is not None:
itemsByLink = {}
for item in editionDict['items']:
@@ -400,8 +413,8 @@ def sortByMediaType(link):
'application/html+catalog': 6
}

return scores.get(link['mediaType'], 7)

return scores.get(link['mediaType'], 7)
@classmethod
def formatRecord(cls, record, itemsByLink):
outRecord = {
44 changes: 43 additions & 1 deletion tests/unit/test_api_utils.py
Original file line number Diff line number Diff line change
@@ -129,7 +129,7 @@ def testItem(self, MockDBObject, testLink, testRights):
physical_location={'name': 'test'},
source='hathitrust'
)

@pytest.fixture
def testWebpubItem(self, MockDBObject, testWebpubLink):
return MockDBObject(
@@ -149,6 +149,16 @@ def testPDFItem(self, MockDBObject, testPDFLink):
physical_location={},
source='gutenberg'
)

@pytest.fixture
def testEmptyItem(self, MockDBObject):
return MockDBObject(
id='it4',
links=[],
rights=[],
physical_location={},
source='gutenberg'
)

@pytest.fixture
def testEdition(self, MockDBObject, testItem, mocker):
@@ -510,6 +520,38 @@ def test_formatEdition_v1_reader_flag(self, testEdition, testWebpubItem):
assert formattedEdition['items'][1]['links'][0]['mediaType'] ==\
'application/epub+xml'

def test_formatEdition_v2_reader_flag(self, testEdition, testWebpubItem, testPDFItem):
testEdition.items.append(testPDFItem)
testEdition.items.append(testWebpubItem)


formattedEdition = APIUtils.formatEdition(testEdition, reader='v2')

assert len(formattedEdition['items']) == 3
assert formattedEdition['items'][0]['item_id'] == 'it2'
assert formattedEdition['items'][0]['links'][0]['mediaType'] ==\
'application/webpub+json'
assert formattedEdition['items'][1]['links'][0]['mediaType'] ==\
'application/pdf'
assert formattedEdition['items'][2]['links'][0]['flags']['reader'] is\
False

def test_formatEdition_v2_reader_flag_2(self, testEdition, testWebpubItem, testPDFItem, testEmptyItem):
testEdition.items.append(testEmptyItem)
testEdition.items.append(testWebpubItem)


formattedEdition = APIUtils.formatEdition(testEdition, reader='v2')

assert len(formattedEdition['items']) == 3
assert formattedEdition['items'][0]['item_id'] == 'it2'
assert formattedEdition['items'][0]['links'][0]['mediaType'] ==\
'application/webpub+json'
assert formattedEdition['items'][1]['links'] ==\
[]
assert formattedEdition['items'][2]['links'][0]['flags']['reader'] is\
False

def test_formatRecord(self, testRecord, mocker):
testLinkItems = {
'url1': {'item_id': 1, 'url': 'url1'},

0 comments on commit 6f2aab2

Please sign in to comment.