Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangreen committed Apr 18, 2024
1 parent c8649f2 commit 1f3fc57
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 37 deletions.
16 changes: 13 additions & 3 deletions tests/api/test_bibliotheca.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from io import BytesIO, StringIO
from typing import TYPE_CHECKING, cast
from unittest import mock
from unittest.mock import MagicMock, create_autospec
from unittest.mock import MagicMock, create_autospec, patch

import pytest
from pymarc import parse_xml_to_array
Expand All @@ -25,7 +25,13 @@
ItemListParser,
PatronCirculationParser,
)
from api.circulation import CirculationAPI, FulfillmentInfo, HoldInfo, LoanInfo
from api.circulation import (
CirculationAPI,
FulfillmentInfo,
HoldInfo,
LoanInfo,
PatronActivityThread,
)
from api.circulation_exceptions import (
AlreadyCheckedOut,
AlreadyOnHold,
Expand Down Expand Up @@ -462,7 +468,11 @@ def test_sync_bookshelf(self, bibliotheca_fixture: BibliothecaAPITestFixture):
bibliotheca_fixture.api.queue_response(
200, content=bibliotheca_fixture.files.sample_data("checkouts.xml")
)
circulation.sync_bookshelf(patron, "dummy pin")

with patch.object(
PatronActivityThread, "api", return_value=bibliotheca_fixture.api
):
circulation.sync_bookshelf(patron, "dummy pin")

# The patron should have two loans and two holds.
l1, l2 = patron.loans
Expand Down
21 changes: 12 additions & 9 deletions tests/api/test_circulationapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import datetime
from datetime import timedelta
from typing import cast
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch

import flask
import pytest
Expand All @@ -17,6 +17,7 @@
FulfillmentInfo,
HoldInfo,
LoanInfo,
PatronActivityThread,
)
from api.circulation_exceptions import (
AlreadyCheckedOut,
Expand Down Expand Up @@ -1441,21 +1442,23 @@ def test_patron_activity(
data = api_bibliotheca_files_fixture.sample_data("checkouts.xml")
mock_bibliotheca.queue_response(200, content=data)

loans, holds, complete = circulation.patron_activity(
circulation_api.patron, "1234"
)
with patch.object(PatronActivityThread, "api", return_value=mock_bibliotheca):
loans, holds, complete = circulation.patron_activity(
circulation_api.patron, "1234"
)
assert 2 == len(loans)
assert 2 == len(holds)
assert True == complete
assert complete is True

mock_bibliotheca.queue_response(500, content="Error")

loans, holds, complete = circulation.patron_activity(
circulation_api.patron, "1234"
)
with patch.object(PatronActivityThread, "api", return_value=mock_bibliotheca):
loans, holds, complete = circulation.patron_activity(
circulation_api.patron, "1234"
)
assert 0 == len(loans)
assert 0 == len(holds)
assert False == complete
assert complete is False

def test_can_fulfill_without_loan(self, circulation_api: CirculationAPIFixture):
"""Can a title can be fulfilled without an active loan? It depends on
Expand Down
45 changes: 20 additions & 25 deletions tests/api/test_overdrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
from requests import Response
from sqlalchemy.orm.exc import StaleDataError

from api.circulation import CirculationAPI, FulfillmentInfo, HoldInfo, LoanInfo
from api.circulation import (
CirculationAPI,
FulfillmentInfo,
HoldInfo,
LoanInfo,
PatronActivityThread,
)
from api.circulation_exceptions import (
CannotFulfill,
CannotHold,
Expand Down Expand Up @@ -59,6 +65,7 @@
LicensePoolDeliveryMechanism,
Measurement,
MediaTypes,
Patron,
Representation,
RightsStatus,
Subject,
Expand Down Expand Up @@ -125,6 +132,10 @@ def sample_json(self, filename):
data = self.data.sample_data(filename)
return data, json.loads(data)

def sync_bookshelf(self, patron: Patron):
with patch.object(PatronActivityThread, "api", return_value=self.api):
return self.circulation.sync_bookshelf(patron, "dummy pin")


@pytest.fixture(scope="function")
def overdrive_api_fixture(
Expand Down Expand Up @@ -2603,9 +2614,7 @@ def test_sync_bookshelf_creates_local_loans(
overdrive_api_fixture.api.queue_response(200, content=holds_data)

patron = db.patron()
loans, holds = overdrive_api_fixture.circulation.sync_bookshelf(
patron, "dummy pin"
)
loans, holds = overdrive_api_fixture.sync_bookshelf(patron)

# All four loans in the sample data were created.
assert isinstance(loans, list)
Expand Down Expand Up @@ -2645,9 +2654,7 @@ def test_sync_bookshelf_creates_local_loans(
patron.last_loan_activity_sync = None
overdrive_api_fixture.api.queue_response(200, content=loans_data)
overdrive_api_fixture.api.queue_response(200, content=holds_data)
loans, holds = overdrive_api_fixture.circulation.sync_bookshelf(
patron, "dummy pin"
)
loans, holds = overdrive_api_fixture.sync_bookshelf(patron)
assert isinstance(loans, list)
assert 4 == len(loans)
assert loans.sort() == patron.loans.sort()
Expand Down Expand Up @@ -2678,9 +2685,7 @@ def test_sync_bookshelf_removes_loans_not_present_on_remote(

# Sync with Overdrive, and the loan not present in the sample
# data is removed.
loans, holds = overdrive_api_fixture.circulation.sync_bookshelf(
patron, "dummy pin"
)
loans, holds = overdrive_api_fixture.sync_bookshelf(patron)

assert isinstance(loans, list)
assert 4 == len(loans)
Expand Down Expand Up @@ -2708,9 +2713,7 @@ def test_sync_bookshelf_ignores_loans_from_other_sources(
overdrive_api_fixture.api.queue_response(200, content=loans_data)
overdrive_api_fixture.api.queue_response(200, content=holds_data)

loans, holds = overdrive_api_fixture.circulation.sync_bookshelf(
patron, "dummy pin"
)
overdrive_api_fixture.sync_bookshelf(patron)
assert 5 == len(patron.loans)
assert gutenberg_loan in patron.loans

Expand All @@ -2726,9 +2729,7 @@ def test_sync_bookshelf_creates_local_holds(
overdrive_api_fixture.api.queue_response(200, content=holds_data)
patron = db.patron()

loans, holds = overdrive_api_fixture.circulation.sync_bookshelf(
patron, "dummy pin"
)
loans, holds = overdrive_api_fixture.sync_bookshelf(patron)
# All four loans in the sample data were created.
assert isinstance(holds, list)
assert 4 == len(holds)
Expand All @@ -2738,9 +2739,7 @@ def test_sync_bookshelf_creates_local_holds(
patron.last_loan_activity_sync = None
overdrive_api_fixture.api.queue_response(200, content=loans_data)
overdrive_api_fixture.api.queue_response(200, content=holds_data)
loans, holds = overdrive_api_fixture.circulation.sync_bookshelf(
patron, "dummy pin"
)
loans, holds = overdrive_api_fixture.sync_bookshelf(patron)
assert isinstance(holds, list)
assert 4 == len(holds)
assert sorted(holds) == sorted(patron.holds)
Expand All @@ -2766,9 +2765,7 @@ def test_sync_bookshelf_removes_holds_not_present_on_remote(
overdrive_api_fixture.api.queue_response(200, content=holds_data)

# The hold not present in the sample data has been removed
loans, holds = overdrive_api_fixture.circulation.sync_bookshelf(
patron, "dummy pin"
)
loans, holds = overdrive_api_fixture.sync_bookshelf(patron)
assert isinstance(holds, list)
assert 4 == len(holds)
assert holds == patron.holds
Expand Down Expand Up @@ -2799,9 +2796,7 @@ def test_sync_bookshelf_ignores_holds_from_other_collections(

# overdrive_api_fixture.api doesn't know about the hold, but it was not
# destroyed, because it came from a different collection.
loans, holds = overdrive_api_fixture.circulation.sync_bookshelf(
patron, "dummy pin"
)
overdrive_api_fixture.sync_bookshelf(patron)
assert 5 == len(patron.holds)
assert overdrive_hold in patron.holds

Expand Down

0 comments on commit 1f3fc57

Please sign in to comment.