-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e181090
commit b95a9db
Showing
2 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |