Skip to content

Commit

Permalink
Fixes for loans tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangreen committed Jun 11, 2024
1 parent ffaa79a commit 7d33551
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
53 changes: 51 additions & 2 deletions tests/manager/api/controller/test_loan.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
from collections.abc import Generator
from decimal import Decimal
from unittest.mock import MagicMock, patch
from unittest.mock import MagicMock, create_autospec, patch
from urllib.parse import quote

import feedparser
Expand Down Expand Up @@ -68,6 +68,7 @@
from tests.fixtures.library import LibraryFixture
from tests.fixtures.redis import RedisFixture
from tests.fixtures.services import ServicesFixture
from tests.mocks.circulation import MockPatronActivityCirculationAPI
from tests.mocks.mock import DummyHTTPClient


Expand Down Expand Up @@ -103,6 +104,12 @@ def __init__(
self.identifier_identifier = self.identifier.identifier
self.identifier_type = self.identifier.type

# Make sure our collection has a PatronActivityCirculationAPI setup, so we can test the
# patron activity sync tasks.
self.manager.d_circulation.add_remote_api(
self.pool, MockPatronActivityCirculationAPI(db.session, self.collection)
)


@pytest.fixture(scope="function")
def loan_fixture(
Expand Down Expand Up @@ -1107,7 +1114,7 @@ def test_revoke_loan(
{"Authorization": loan_fixture.valid_auth}
)

# Create a new loan.
# Create a loan and revoke it
with loan_fixture.request_context_with_library("/", headers=headers):
patron = loan_fixture.manager.loans.authenticated_patron_from_request()
assert isinstance(patron, Patron)
Expand All @@ -1134,6 +1141,48 @@ def test_revoke_loan(
countdown=5,
)

@pytest.mark.parametrize(
*OPDSSerializationTestHelper.PARAMETRIZED_SINGLE_ENTRY_ACCEPT_HEADERS
)
def test_revoke_loan_no_patron_activity_support(
self,
loan_fixture: LoanFixture,
accept_header: str | None,
expected_content_type: str,
):
serialization_helper = OPDSSerializationTestHelper(
accept_header, expected_content_type
)
headers = serialization_helper.merge_accept_header(
{"Authorization": loan_fixture.valid_auth}
)

# Create a loan and revoke it
with loan_fixture.request_context_with_library("/", headers=headers):
patron = loan_fixture.manager.loans.authenticated_patron_from_request()
assert isinstance(patron, Patron)
loan, newly_created = loan_fixture.pool.loan_to(patron)

mock_supports_patron_activity = create_autospec(
loan_fixture.manager.d_circulation.supports_patron_activity,
return_value=False,
)
loan_fixture.manager.d_circulation.supports_patron_activity = (
mock_supports_patron_activity
)
loan_fixture.manager.d_circulation.queue_checkin(loan_fixture.pool)

with patch(
"palace.manager.api.controller.loan.sync_patron_activity"
) as sync_task:
response = loan_fixture.manager.loans.revoke(loan_fixture.pool_id)

assert 200 == response.status_code
serialization_helper.verify_and_get_single_entry_feed_links(response)

mock_supports_patron_activity.assert_called_once_with(loan_fixture.pool)
sync_task.apply_async.assert_not_called()

def test_revoke_loan_exception(
self,
loan_fixture: LoanFixture,
Expand Down
7 changes: 7 additions & 0 deletions tests/mocks/circulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ def api_for_license_pool(self, licensepool: LicensePool) -> MockBaseCirculationA
self.remotes[source] = remote
return self.remotes[source]

def add_remote_api(
self, licensepool: LicensePool, api: MockBaseCirculationAPI
) -> None:
source = licensepool.data_source.name
assert source is not None
self.remotes[source] = api


class MockPatronActivityCirculationAPI(
MockBaseCirculationAPI, PatronActivityCirculationAPI
Expand Down

0 comments on commit 7d33551

Please sign in to comment.