Skip to content

Commit

Permalink
Add a couple more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangreen committed Jun 7, 2024
1 parent e181090 commit b95a9db
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/palace/manager/celery/tasks/patron_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ def sync_patron_activity(task: Task, patron_id: int, pin: str | None) -> None:
raise task.retry(countdown=wait_time)

for collection in patron.library.collections:
api_cls = registry[collection.protocol]
api_cls = registry.get(collection.protocol)
if api_cls is None:
task.log.error(
f"Collection {collection.name} ({collection.id}) has an unknown protocol {collection.protocol}."
)
continue
if not issubclass(api_cls, PatronActivityCirculationAPI):
continue

Expand Down
80 changes: 80 additions & 0 deletions tests/manager/celery/tasks/test_patron_activity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from unittest.mock import MagicMock, patch

import pytest
from celery.exceptions import MaxRetriesExceededError

from palace.manager.celery.tasks.patron_activity import sync_patron_activity
from palace.manager.service.integration_registry.license_providers import (
LicenseProvidersRegistry,
)
from tests.fixtures.celery import CeleryFixture
from tests.fixtures.services import ServicesFixture
from tests.mocks.circulation import (
MockBaseCirculationAPI,
MockPatronActivityCirculationAPI,
)


def test_sync_patron_activity(
celery_fixture: CeleryFixture,
services_fixture: ServicesFixture,
caplog: pytest.LogCaptureFixture,
):
registry: LicenseProvidersRegistry = (
services_fixture.services.integration_registry.license_providers()
)
registry.register(
MockPatronActivityCirculationAPI, canonical="MockPatronActivityCirculationAPI"
)
registry.register(MockBaseCirculationAPI, canonical="MockBaseCirculationAPI")

mock_patron = MagicMock()
broken_collection = MagicMock(id=2, protocol="OtherProtocol")
broken_collection.name = "Broken"
mock_patron.library.collections = [
MagicMock(protocol="MockPatronActivityCirculationAPI", id=12),
MagicMock(protocol="MockBaseCirculationAPI"),
broken_collection,
]
with (
patch("palace.manager.celery.tasks.patron_activity.get_one") as mock_get_one,
patch(
"palace.manager.celery.tasks.patron_activity.sync_patron_activity_collection"
) as mock_activity_collection,
):
mock_get_one.return_value = mock_patron
sync_patron_activity.delay(100, "1234").wait()

# The patron was looked up
assert mock_get_one.call_count == 1

# We logged a message about the collection with a unknown protocol
assert (
"Collection Broken (2) has an unknown protocol OtherProtocol."
in caplog.messages
)

# And the collection that supports activity sync had a task created for it.
mock_activity_collection.delay.assert_called_once_with(12, 100, "1234")


@patch("palace.manager.celery.tasks.patron_activity.exponential_backoff")
def test_sync_patron_activity_failures(
mock_backoff: MagicMock,
celery_fixture: CeleryFixture,
caplog: pytest.LogCaptureFixture,
):
# Make sure our backoff function doesn't delay the test.
mock_backoff.return_value = 0

# If the patron doesn't exist we retry up to 3 times, then fail.
with (
pytest.raises(MaxRetriesExceededError),
patch(
"palace.manager.celery.tasks.patron_activity.get_one", return_value=None
) as mock_get_one,
):
sync_patron_activity.delay(1, "1234").wait()

assert "Patron 1 not found. Trying again in 0 seconds." in caplog.messages
assert mock_get_one.call_count == 4

0 comments on commit b95a9db

Please sign in to comment.