Skip to content

Commit

Permalink
Fix broken tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbernstein committed Nov 19, 2024
1 parent 707047f commit 419bddb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 36 deletions.
31 changes: 13 additions & 18 deletions src/palace/manager/api/monitor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from collections.abc import Callable

from sqlalchemy import and_, or_

from palace.manager.api.opds_for_distributors import OPDSForDistributorsAPI
Expand Down Expand Up @@ -47,23 +45,20 @@ def where_clause(self):
)
return ~self.MODEL_CLASS.id.in_(source_of_truth_subquery)

def post_delete_op(self, row: Loan | Hold) -> Callable:

def post_delete() -> None:
ce = CirculationEvent
event_type = (
CirculationEvent.CM_LOAN_EXPIRED
if isinstance(row, Loan)
else CirculationEvent.CM_HOLD_EXPIRED
)
self.services.analytics.collect_event(
library=row.library,
license_pool=row.license_pool,
event_type=event_type,
patron=row.patron,
)
def post_delete(self, row: Loan | Hold) -> None:
ce = CirculationEvent
event_type = (
CirculationEvent.CM_LOAN_EXPIRED
if isinstance(row, Loan)
else CirculationEvent.CM_HOLD_EXPIRED
)

return post_delete
self.services.analytics.collect_event(
library=row.library,
license_pool=row.license_pool,
event_type=event_type,
patron=row.patron,
)


class LoanReaper(LoanlikeReaperMonitor):
Expand Down
7 changes: 2 additions & 5 deletions src/palace/manager/core/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import datetime
import logging
import traceback
from collections.abc import Callable
from typing import TYPE_CHECKING

from sqlalchemy.exc import InvalidRequestError
Expand Down Expand Up @@ -884,10 +883,8 @@ def run_once(self, *args, **kwargs):
post_delete_ops = []
for i in qu.limit(self.BATCH_SIZE):
self.log.info("Deleting %r", i)
post_delete_op = self.post_delete_op(i)
if post_delete_op:
post_delete_ops.append(post_delete_op)
self.delete(i)
self.post_delete(i)
rows_deleted += 1

self._db.commit()
Expand All @@ -907,7 +904,7 @@ def delete(self, row):
"""
self._db.delete(row)

def post_delete_op(self, row) -> Callable | None:
def post_delete(self, row) -> None:
return None

def query(self):
Expand Down
25 changes: 12 additions & 13 deletions tests/manager/api/test_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,13 @@ def test_reaping(
assert {open_access_loan, sot_loan, unlimited_access_loan} == set(
inactive_patron.loans
)
assert 3 == len(inactive_patron.holds)
assert len(inactive_patron.holds) == 3

# The active patron's loans and holds are unaffected, either
# because they have not expired or because they have no known
# expiration date and were created relatively recently.
assert 2 == len(current_patron.loans)
assert 2 == len(current_patron.holds)

call_args_list = (
services_fixture.analytics_fixture.analytics_mock.collect_event.call_args_list
)
assert len(call_args_list) == 2
for call_args in call_args_list:
assert call_args.kwargs["event_type"] == CirculationEvent.CM_LOAN_EXPIRED
assert len(current_patron.loans) == 2
assert len(current_patron.holds) == 2

# Now fire up the hold reaper.
hold_monitor = HoldReaper(db.session)
Expand All @@ -196,12 +189,18 @@ def test_reaping(
assert [sot_hold] == inactive_patron.holds
assert 2 == len(current_patron.holds)

# verify expected circ event count and order for two monitor operations.
call_args_list = (
services_fixture.analytics_fixture.analytics_mock.collect_event.call_args_list
)
assert len(call_args_list) == 2
for call_args in call_args_list:
assert call_args.kwargs["event_type"] == CirculationEvent.CM_HOLD_EXPIRED
assert len(call_args_list) == 4
event_types = [call_args.kwargs["event_type"] for call_args in call_args_list]
assert event_types == [
CirculationEvent.CM_LOAN_EXPIRED,
CirculationEvent.CM_LOAN_EXPIRED,
CirculationEvent.CM_HOLD_EXPIRED,
CirculationEvent.CM_HOLD_EXPIRED,
]


class TestIdlingAnnotationReaper:
Expand Down

0 comments on commit 419bddb

Please sign in to comment.