Skip to content

Commit

Permalink
Test for renewal failure
Browse files Browse the repository at this point in the history
  • Loading branch information
ping committed Apr 3, 2023
1 parent cd0e952 commit e15ff69
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion odmpy/odm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ def run(custom_args: Optional[List[str]] = None, be_quiet: bool = False) -> None
selected_loan["title"],
colored(badreq_err.msg, "red"),
)
if selected_loan.get("availableCopies") == 0 and not [
if selected_loan.get("availableCopies", 0) == 0 and not [
h
for h in holds
if h["cardId"] == selected_loan["cardId"]
Expand Down
71 changes: 71 additions & 0 deletions tests/odmpy_libby_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import responses
from bs4 import BeautifulSoup
from ebooklib import epub
from responses import matchers

from odmpy.errors import LibbyNotConfiguredError, OdmpyRuntimeError
from odmpy.libby import LibbyClient, LibbyFormats
Expand Down Expand Up @@ -1090,6 +1091,76 @@ def test_mock_libby_renew(self):
run_command.insert(0, "--verbose")
run(run_command, be_quiet=not self.is_verbose)

@staticmethod
def _ret_libby_renew_failure(_: str) -> str:
counter_name = "renew_failure"
if OdmpyLibbyTests.get_counter(counter_name) == 0:
OdmpyLibbyTests.add_to_counter(counter_name)
return "1"
if OdmpyLibbyTests.get_counter(counter_name) == 1:
OdmpyLibbyTests.add_to_counter(counter_name)
return "y"
return ""

@responses.activate
@patch("builtins.input", new=_ret_libby_renew_failure.__func__) # type: ignore[attr-defined]
def test_mock_libby_renew_failure(self):
settings_folder = self._generate_fake_settings()
with self.test_data_dir.joinpath("audiobook", "sync.json").open(
"r", encoding="utf-8"
) as f:
sync_state = json.load(f)
responses.get(
"https://sentry-read.svc.overdrive.com/chip/sync",
content_type="application/json",
json=sync_state,
)
responses.put(
"https://sentry-read.svc.overdrive.com/card/123456789/loan/9999999",
content_type="application/json",
json={
"result": "upstream_failure",
"upstream": {
"userExplanation": "TestRenewFailure",
"errorCode": "999",
},
},
status=HTTPStatus.BAD_REQUEST,
)
responses.post(
"https://sentry-read.svc.overdrive.com/card/123456789/hold/9999999",
content_type="application/json",
match=[
matchers.json_params_matcher(
{"days_to_suspend": 0, "email_address": ""}
)
],
json={
"title": "Test Audiobook",
"holdListPosition": 2,
"ownedCopies": 10,
"estimatedWaitDays": 21,
},
)

run_command = ["libbyrenew", "--settings", str(settings_folder)]
if self.is_verbose:
run_command.insert(0, "--verbose")

with self.assertLogs(run.__module__, level="INFO") as context:
run(run_command, be_quiet=not self.is_verbose)

self.assertTrue(
[r.msg for r in context.records if r.msg.startswith("Renewing loan")]
)
self.assertTrue(
[
r.msg
for r in context.records
if r.msg.startswith("Hold successfully created for")
]
)

@staticmethod
def _ret_invalid_choice(text: str) -> str:
counter_name = "invalid_choice"
Expand Down

0 comments on commit e15ff69

Please sign in to comment.