From 44be290b4ee5a58e5cd7f1c6adb6047f5cffa01d Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 22 Feb 2024 16:44:53 -0500 Subject: [PATCH 1/2] HOTFIX_ItemSorting --- CHANGELOG.md | 1 + api/utils.py | 14 ++++---- tests/unit/test_api_fulfill_blueprint.py | 2 +- tests/unit/test_api_utils.py | 44 ++++++++++++++++++------ 4 files changed, 42 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59d0376713..e4532de898 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - NYPL records not being added due to SQLAlchemy error - 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 priority and media type priority ## 2023-09-05 version -- v0.12.3 ## Removed diff --git a/api/utils.py b/api/utils.py index 39bf5e2a83..51411c12b5 100644 --- a/api/utils.py +++ b/api/utils.py @@ -366,7 +366,7 @@ def formatEdition( editionDict['items'].append(itemDict) editionDict['items']\ - .sort(key=lambda x: cls.SOURCE_PRIORITY[x['source']]) + .sort(key=lambda x: (cls.SOURCE_PRIORITY[x['source']], cls.sortByMediaType(x['links'][0]))) if records is not None: itemsByLink = {} @@ -391,12 +391,12 @@ def formatEdition( def sortByMediaType(link): scores = { 'application/webpub+json': 1, - 'text/html': 2, - 'application/pdf': 3, - 'application/html+edd': 4, - 'application/x.html+edd': 4, - 'application/epub+xml': 5, - 'application/epub+zip': 5, + 'application/pdf': 2, + 'application/epub+xml': 3, + 'application/epub+zip': 3, + 'text/html': 4, + 'application/html+edd': 5, + 'application/x.html+edd': 5, 'application/html+catalog': 6 } diff --git a/tests/unit/test_api_fulfill_blueprint.py b/tests/unit/test_api_fulfill_blueprint.py index e13e4b925b..06653d4367 100644 --- a/tests/unit/test_api_fulfill_blueprint.py +++ b/tests/unit/test_api_fulfill_blueprint.py @@ -89,4 +89,4 @@ def test_itemFulfill_redirect(self, testApp, mockS3, mockDB, mocker): headers={'Authorization': 'Bearer Whatever'} ) assert response.status_code == 302 - assert response.location == "example.com/example.pdf" \ No newline at end of file + assert response.location == "http://localhost/fulfill/example.com/example.pdf" \ No newline at end of file diff --git a/tests/unit/test_api_utils.py b/tests/unit/test_api_utils.py index d535ba2306..75c3b6f70a 100644 --- a/tests/unit/test_api_utils.py +++ b/tests/unit/test_api_utils.py @@ -101,6 +101,15 @@ def testWebpubLink(self, MockDBObject): url='testURI', flags={'test': True} ) + + @pytest.fixture + def testPDFLink(self, MockDBObject): + return MockDBObject( + id='li2', + media_type='application/pdf', + url='testURI', + flags={'test': True} + ) @pytest.fixture def testRights(self, MockDBObject): @@ -130,6 +139,16 @@ def testWebpubItem(self, MockDBObject, testWebpubLink): physical_location={}, source='gutenberg' ) + + @pytest.fixture + def testPDFItem(self, MockDBObject, testPDFLink): + return MockDBObject( + id='it3', + links=[testPDFLink], + rights=[], + physical_location={}, + source='gutenberg' + ) @pytest.fixture def testEdition(self, MockDBObject, testItem, mocker): @@ -491,16 +510,19 @@ 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): + 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']) == 2 + 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]['flags']['reader'] is\ + assert formattedEdition['items'][1]['links'][0]['mediaType'] ==\ + 'application/pdf' + assert formattedEdition['items'][2]['links'][0]['flags']['reader'] is\ False def test_formatRecord(self, testRecord, mocker): @@ -646,21 +668,21 @@ def test_addWorkMeta(self): def test_sortByMediaType(self): testList = [ - {'id': 2, 'mediaType': 'text/html'}, - {'id': 1, 'mediaType': 'application/epub+xml'}, - {'id': 4, 'mediaType': 'application/html+edd'}, - {'id': 1, 'mediaType': 'application/epub+zip'}, - {'id': 5, 'mediaType': 'application/webpub+json'}, - {'id': 3, 'mediaType': 'application/pdf'} + {'id': 4, 'mediaType': 'text/html'}, + {'id': 3, 'mediaType': 'application/epub+xml'}, + {'id': 5, 'mediaType': 'application/html+edd'}, + {'id': 3, 'mediaType': 'application/epub+zip'}, + {'id': 1, 'mediaType': 'application/webpub+json'}, + {'id': 2, 'mediaType': 'application/pdf'} ] shuffle(testList) testList.sort(key=APIUtils.sortByMediaType) - assert [i['id'] for i in testList] == [5, 2, 3, 4, 1, 1] + assert [i['id'] for i in testList] == [1, 2, 3, 3, 4, 5] shuffle(testList) testList.sort(key=APIUtils.sortByMediaType) - assert [i['id'] for i in testList] == [5, 2, 3, 4, 1, 1] + assert [i['id'] for i in testList] == [1, 2, 3, 3, 4, 5] def test_getPresignedUrlFromObjectUrl(self, mocker): mockGenerateUrl = mocker.patch.object(APIUtils, 'generate_presigned_url') From 59ce63bd751357c7e8715870cd3aad168f95d296 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 23 Feb 2024 10:36:46 -0500 Subject: [PATCH 2/2] Reverted test change --- CHANGELOG.md | 2 +- tests/unit/test_api_fulfill_blueprint.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4532de898..3c904bd845 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ - NYPL records not being added due to SQLAlchemy error - 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 priority and media type priority +- Adjusted sorting for item source and media type priority ## 2023-09-05 version -- v0.12.3 ## Removed diff --git a/tests/unit/test_api_fulfill_blueprint.py b/tests/unit/test_api_fulfill_blueprint.py index 06653d4367..e13e4b925b 100644 --- a/tests/unit/test_api_fulfill_blueprint.py +++ b/tests/unit/test_api_fulfill_blueprint.py @@ -89,4 +89,4 @@ def test_itemFulfill_redirect(self, testApp, mockS3, mockDB, mocker): headers={'Authorization': 'Bearer Whatever'} ) assert response.status_code == 302 - assert response.location == "http://localhost/fulfill/example.com/example.pdf" \ No newline at end of file + assert response.location == "example.com/example.pdf" \ No newline at end of file