Skip to content

Commit 8149c70

Browse files
committed
Expand test coverage.
1 parent ab49ecb commit 8149c70

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

src/palace/manager/integration/license/overdrive/api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,11 +612,9 @@ def _get_book_list_page(
612612
availability_queue = extractor_class.availability_link_list(content_dict)
613613
return availability_queue, next_link
614614

615-
def _get_headers(self, auth_token: str | None) -> dict[str, str]:
615+
def _get_headers(self, auth_token: str) -> dict[str, str]:
616616
if auth_token:
617617
return {"Authorization": f"Bearer {auth_token}", "User-Agent": "Palace"}
618-
else:
619-
return {}
620618

621619
def book_info_initial_endpoint(
622620
self,
@@ -787,6 +785,7 @@ def _process_request(
787785
elif path.endswith("metadata") and path.startswith("/v1/"):
788786
books[data["id"].lower()]["metadata"] = data
789787
else:
788+
# this should never happen
790789
raise RuntimeError(f"Unknown URL: {response.url}")
791790

792791
def recently_changed_ids(
@@ -949,6 +948,7 @@ def patron_request(
949948
permissions.
950949
"""
951950
patron_credential = self._get_patron_oauth_credential(patron, pin)
951+
assert patron_credential.credential
952952
headers = self._get_headers(patron_credential.credential)
953953
if extra_headers:
954954
headers.update(extra_headers)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"subtitle": "Imperial Radch Series, Book 1", "links": {"self": {"href": "http://api.overdrive.com/v1/collections/v1L1BCgAAAA2C/products/2a005d55-a417-4053-b90d-7a38ca6d2065/metadata", "type": "application/vnd.overdrive.api+json"}}, "series": "Imperial Radch", "sortTitle": "Ancillary Justice Imperial Radch Series Book 01", "starRating": 4.3, "isPublicPerformanceAllowed": false, "images": {"cover150Wide": {"href": "http://images.contentreserve.com/ImageType-150/0017-1/C22/288/A0/{2a005d55-a417-4053-b90d-7a38ca6d2065}Img150.jpg", "type": "image/jpeg"}, "cover300Wide": {"href": "http://images.contentreserve.com/ImageType-400/0017-1/C22/288/A0/{2a005d55-a417-4053-b90d-7a38ca6d2065}Img400.jpg", "type": "image/jpeg"}, "cover": {"href": "http://images.contentreserve.com/ImageType-100/0017-1/{2a005d55-a417-4053-b90d-7a38ca6d2065}Img100.jpg", "type": "image/jpeg"}, "thumbnail": {"href": "http://images.contentreserve.com/ImageType-200/0017-1/{2a005d55-a417-4053-b90d-7a38ca6d2065}Img200.jpg", "type": "image/jpeg"}}, "shortDescription": "<p><b>The only novel ever to win the Hugo, Nebula, and Arthur C. Clarke Awards and the first book in Ann Leckie's <i>New York Times</i> bestselling trilogy. </b><p><b><br></b><p>On a remote, icy planet, the soldier known as Breq is drawing closer to completing her quest.<p><br><p>Once, she was the Justice of Toren - a colossal starship with an artificial intelligence linking thousands of soldiers in the service of the Radch, the empire that conquered the galaxy. <p><br><p>Now, an act of treachery has ripped it all away, leaving her with one fragile human body, unanswered questions, and a burning desire for vengeance. <p><br><p><br><p><b>In the Ancillary world: </b><p>1. <i>Ancillary Justice</i><p>2. <i>Ancillary Sword</i><p>3. <i>Ancillary Mercy</i>", "subjects": [{"value": "Fiction"}, {"value": "Science Fiction"}, {"value": "Thriller"}], "title": "Ancillary Justice", "crossRefId": 1223507, "mediaType": "eBook", "id": "2a005d55-a417-4053-b90d-7a38ca6d2065", "languages": [{"code": "en", "name": "English"}], "isOwnedByCollections": true, "publishDate": "2013-10-01T00:00:00-04:00", "awards": [{"source": "Science Fiction and Fantasy Writers of America", "value": "Nebula Award"}, {"source": "World Science Fiction Society", "value": "Hugo Award"}], "publishDateText": "10/01/2013", "isPublicDomain": false, "publisher": "Orbit", "popularity": 4874, "fullDescription": "<p><b>The only novel ever to win the Hugo, Nebula, and Arthur C. Clarke Awards and the first book in Ann Leckie's <i>New York Times</i> bestselling trilogy. </b><p><b><br></b><p>On a remote, icy planet, the soldier known as Breq is drawing closer to completing her quest.<p><br><p>Once, she was the Justice of Toren - a colossal starship with an artificial intelligence linking thousands of soldiers in the service of the Radch, the empire that conquered the galaxy. <p><br><p>Now, an act of treachery has ripped it all away, leaving her with one fragile human body, unanswered questions, and a burning desire for vengeance. <p><br><p><br><p><b>In the Ancillary world: </b><p>1. <i>Ancillary Justice</i><p>2. <i>Ancillary Sword</i><p>3. <i>Ancillary Mercy</i>", "reviews": [{"content": "\"If you don't know the <i>Ancillary </i>series by now, you probably should. Ann Leckie's sociopolitical space opera almost singlehandedly breathed new cool into the stereotype of spaceships trundling through far-off systems amid laser battles. ... [<i>Ancillary Mercy</i>] earns the credit it's received: As a capstone to a series that shook genre expectations, as our closing installment of an immersively realized world, and as the poignant story of a ship that learned to sing.\"&#8212;<b><i><i>NPR Books </i>on<i> Ancillary Mercy</i></b></i>", "source": "John Scalzi", "premium": false}, {"content": "\"Powerful.\"&#8212;<b><i><i>The New York Times </i>on<i> Ancillary Sword</i></b></i>", "source": "i09.com (included in 'This Fall's Must-Read Science Fiction and Fantasy Books')", "premium": false}, {"content": "\"Unexpected, compelling and very cool. Ann Leckie nails it...I've never met a heroine like Breq before. I consider this a very good thing indeed.\"&#8212;<b><i>John Scalzi</b></i>", "source": "Liz Bourke, Tor.com", "premium": false}, {"content": "\"<i>Ancillary Justice </i>is the mind-blowing space opera you've been needing...This is a novel that will thrill you like the page-turner it is, but stick with you for a long time afterward.\"&#8212;<b><i><i>i09.com </i><i>(included in 'This Fall's Must-Read Science Fiction and Fantasy Books')</i></b></i>", "source": "Elizabeth Bear", "premium": false}, {"content": "\"It's not every day a debut novel by an author you'd never heard of before derails your entire afternoon with its brilliance. But when my review copy of <i>Ancillary Justice</i> arrived, that's exactly what it did. In fact, it arrowed upward to reach a pretty high position on my list of best space opera novels ever.\"&#8212;<b><i>Liz Bourke, <i>Tor.com</i></b></i>", "source": "Publishers Weekly", "premium": false}, {"content": "\"Establishes Leckie as an heir to Banks and Cherryh.\"&#8212;<b><i>Elizabeth Bear</b></i>", "source": "The Guardian", "premium": false}, {"content": "\"By turns thrilling, moving and awe-inspiring.\"&#8212;<b><i><i>The Guardian</i></b></i>", "source": "Library Journal", "premium": false}, {"content": "\"Leckie does a very good job of setting this complex equation up... This is an altogether promising debut.\"&#8212;<b><i><i>Kirkus</i></b></i>", "source": "RT Book Reviews", "premium": false}, {"content": "\"Leckie's debut gives casual and hardcore sci-fi fans alike a wonderful read.\"&#8212;<b><i><i>RT Book Reviews</i></b></i>", "source": "Justin Landon Staffer's Book Review", "premium": false}, {"content": "\"A sharply written space opera with a richly imagined sense of detail and place, this debut novel from Ann Leckie works as both an evocative science fiction tale and an involving character study...it's also a strongly female-driven piece, tackling ideas about politics and gender in a way that's both engaging and provocative...<i>Ancillary Justice </i>is a gripping read that's well worth a look.\"&#8212;<b><i><i>SFX </i>(UK)</b></i>", "source": "Paul Graham Raven", "premium": false}, {"content": "\"It engages, it excites, and it challenges the way the reader views our \nworld. Leckie may be a former Secretary of the Science Fiction Writers \nof America, but she's the President of this year's crop of debut \nnovelists. Ancillary Justice might be the best science fiction novel of \nthis very young decade.\"&#8212;<b><i>Justin Landon <i>Staffer's Book Review</i></b></i>", "source": "Jared Shurin Pornokitsch", "premium": false}], "formats": [{"partCount": 0, "rights": [{"type": "Copying", "value": 0}, {"type": "Printing", "value": 0}, {"type": "Lending", "value": 0}, {"type": "ReadAloud", "value": 0}, {"type": "ExpirationRights", "value": 0}], "identifiers": [{"audience": "library", "type": "ISBN", "value": "9780316327695"}], "fileName": "AncillaryJustice9780316246637", "fileSize": 931145, "samples": [{"url": "https://samples.overdrive.com/ancillary-justice-c22288?.epub-sample.overdrive.com", "source": "From the book", "formatType": "ebook-overdrive"}], "onSaleDate": "10/01/2013", "id": "ebook-epub-adobe", "name": "Adobe EPUB eBook"}, {"partCount": 0, "identifiers": [{"audience": "retailer", "type": "ASIN", "value": "B00BAXFDLM"}, {"audience": "library", "type": "ISBN", "value": "9780316327695"}], "fileName": "AncillaryJustice9780316246637", "fileSize": 0, "samples": [{"url": "https://samples.overdrive.com/ancillary-justice-c22288?.epub-sample.overdrive.com", "source": "From the book", "formatType": "ebook-overdrive"}], "onSaleDate": "10/01/2013", "id": "ebook-kindle", "name": "Kindle Book"}, {"partCount": 0, "identifiers": [{"audience": "library", "type": "ISBN", "value": "9780316327695"}], "fileName": "AncillaryJustice9780316246637", "fileSize": 0, "samples": [{"url": "https://samples.overdrive.com/ancillary-justice-c22288?.epub-sample.overdrive.com", "source": "From the book", "formatType": "ebook-overdrive"}], "onSaleDate": "10/01/2013", "id": "ebook-overdrive", "name": "OverDrive Read"}], "creators": [{"fileAs": "Leckie, Ann", "bioText": "<p>Ann Leckie has worked as a waitress, a receptionist, a rodman on a land-surveying crew, a lunch lady, and a recording engineer. The author of many published short stories, and secretary of the Science Fiction Writers of America, she lives in St. Louis, Missouri, with her husband, children, and cats.", "role": "Author", "name": "Ann Leckie"}]}

tests/manager/integration/license/overdrive/test_api.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from unittest.mock import MagicMock, create_autospec, patch
66

77
import pytest
8+
from httpx import HTTPStatusError
89

910
from palace.manager.api.circulation.data import HoldInfo, LoanInfo
1011
from palace.manager.api.circulation.exceptions import (
@@ -2763,6 +2764,9 @@ async def test_fetch_book_info_list(
27632764
availability_data, availability_json = overdrive_api_fixture.sample_json(
27642765
"overdrive_availability_information.json"
27652766
)
2767+
metadata_data, metadata_json = overdrive_api_fixture.sample_json(
2768+
"bibliographic_information_book_list_test.json"
2769+
)
27662770
(
27672771
overdrive_book_list_with_next_link_data,
27682772
overdrive_book_list_with_next_link_json,
@@ -2775,12 +2779,47 @@ async def test_fetch_book_info_list(
27752779
200, content=overdrive_book_list_with_next_link_data
27762780
)
27772781
mock_async_client.queue_response(200, content=availability_data)
2782+
mock_async_client.queue_response(200, content=metadata_data)
27782783

27792784
initial_endpoint = api.book_info_initial_endpoint(start=None, page_size=1)
27802785

27812786
book_info_list, next_endpoint = await api.fetch_book_info_list(
2782-
initial_endpoint, fetch_metadata=False, fetch_availability=True
2787+
initial_endpoint, fetch_metadata=True, fetch_availability=True
27832788
)
27842789
assert next_endpoint
27852790
assert book_info_list
27862791
assert len(book_info_list) == 1
2792+
assert book_info_list[0]["metadata"]
2793+
assert book_info_list[0]["availabilityV2"]
2794+
2795+
async def test_fetch_book_info_list_retry_and_error(
2796+
self,
2797+
overdrive_api_fixture: OverdriveAPIFixture,
2798+
):
2799+
(
2800+
overdrive_book_list_with_next_link_data,
2801+
overdrive_book_list_with_next_link_json,
2802+
) = overdrive_api_fixture.sample_json("overdrive_book_list_with_next_link.json")
2803+
api = overdrive_api_fixture.api
2804+
mock_async_client = overdrive_api_fixture.mock_async_client
2805+
2806+
# test recovery after failure with book list page
2807+
mock_async_client.queue_response(
2808+
502,
2809+
content="error",
2810+
)
2811+
2812+
mock_async_client.queue_response(
2813+
200, content=overdrive_book_list_with_next_link_data
2814+
)
2815+
2816+
# test retry and failure with metadata and availabililty
2817+
for x in range(8):
2818+
# error for 4 attempts for availability and metadata
2819+
mock_async_client.queue_response(500, content="500 Internal Server Error")
2820+
2821+
with pytest.raises(HTTPStatusError, match="500 Internal Server Error") as e:
2822+
initial_endpoint = api.book_info_initial_endpoint(start=None, page_size=1)
2823+
await api.fetch_book_info_list(
2824+
initial_endpoint, fetch_metadata=True, fetch_availability=True
2825+
)

0 commit comments

Comments
 (0)