Skip to content

Commit

Permalink
tests: fix unit test about loan overdue
Browse files Browse the repository at this point in the history
Co-Authored-by: Renaud Michotte <renaud.michotte@gmail.com>
Co-Authored-by: Bertrand Zuchuat <bertrand.zuchuat@rero.ch>
  • Loading branch information
zannkukai and Garfield-fr committed Jul 2, 2021
1 parent 7f52ff9 commit 61cd8ba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
25 changes: 12 additions & 13 deletions rero_ils/modules/loans/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,10 +652,8 @@ def concluded(cls, loan):
:return True|False
"""
states = [LoanState.ITEM_RETURNED, LoanState.CANCELLED]
return (
loan.get('state') in states and
return loan.get('state') in states and\
not loan_has_open_events(loan_pid=loan.get('pid'))
)

@classmethod
def age(cls, loan):
Expand Down Expand Up @@ -882,21 +880,23 @@ def loan_has_open_events(loan_pid=None):
:return True|False.
"""
search = NotificationsSearch().filter(
'term', loan__pid=loan_pid).source(['pid']).scan()
search = NotificationsSearch()\
.filter('term', loan__pid=loan_pid)\
.source(['pid']).scan()
for record in search:
transactions_count = PatronTransactionsSearch().filter(
'term', notification__pid=record.pid).filter(
'term', status='open').source().count()
if transactions_count:
transactions_count = PatronTransactionsSearch()\
.filter('term', notification__pid=record.pid)\
.filter('term', status='open')\
.source().count()
if transactions_count > 0:
return True
return False


def get_non_anonymized_loans(patron=None, org_pid=None):
"""Search all loans for non anonymized loans.
:param patron_pid: optional parameter to filter by patron_pid.
:param patron: optional parameter to filter by patron_pid.
:param org_pid: optional parameter to filter by organisation.
:return: loans.
"""
Expand All @@ -915,13 +915,12 @@ def get_non_anonymized_loans(patron=None, org_pid=None):
def anonymize_loans(
patron=None, org_pid=None,
dbcommit=False, reindex=False):
"""Anonymise loans.
"""Anonymize loans.
:param dbcommit - commit the changes in the db after the creation.
:param reindex - index the record after the creation.
:param patron_pid: optional parameter to filter by patron_pid.
:param patron: optional parameter to filter by patron.
:param org_pid: optional parameter to filter by organisation.
:param patron_data: patron data to check.
:return: loans.
"""
counter = 0
Expand Down
11 changes: 9 additions & 2 deletions tests/ui/loans/test_loans_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,24 @@ def test_loan_keep_and_to_anonymize(
assert not loan.can_anonymize(loan_data=loan)

patron.user.profile.keep_history = False
# when the patron asks to anonymise history the can_anonymize is true
# when the patron asks to anonymize history the can_anonymize is true
loan = Loan.get_record_by_pid(loan.pid)
assert loan.concluded(loan)
assert loan.can_anonymize(loan_data=loan)
loan.update(loan, dbcommit=True, reindex=True)

# test loans with fees
# Create a loan, update end_date to set this loan as overdue.
# Create notifications about this loan (overdue_loan_notification)
# This notification will create a new PatronTransaction with a fee.
# This will cause that this loan cannot be concluded and anonymize
item, patron, loan = item2_on_loan_martigny_patron_and_loan_on_loan
assert not loan.concluded(loan)
assert not loan.can_anonymize(loan_data=loan)
end_date = datetime.now(timezone.utc) - timedelta(days=7)
# we update the loan end_date, removing 1 year. We are now sure that all
# possible library exceptions don't conflict with `library.open_days`
# computation
end_date = datetime.now(timezone.utc) - timedelta(days=365)
loan['end_date'] = end_date.isoformat()
loan.update(loan, dbcommit=True, reindex=True)

Expand Down

0 comments on commit 61cd8ba

Please sign in to comment.