Skip to content

Commit

Permalink
tests: fix test_loan_operation_log
Browse files Browse the repository at this point in the history
Co-Authored-by: Peter Weber <peter.weber@rero.ch>
  • Loading branch information
rerowep committed Jun 8, 2022
1 parent 8230f52 commit c2c7a82
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
14 changes: 1 addition & 13 deletions rero_ils/modules/loans/logs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"""Loans logs API."""

import hashlib
from datetime import date

from invenio_search import RecordsSearch

Expand Down Expand Up @@ -190,22 +189,11 @@ def _get_patron_data(cls, patron_pid):
patron_type = PatronType.get_record_by_pid(
extracted_data_from_ref(patron['patron']['type']['$ref']))

def get_age(birth_date):
"""Calculate age from a birthdate.
:param Date birth_date: Date of birth.
:returns: Age
:rtype: int
"""
today = date.today()
return today.year - birth_date.year - (
(today.month, today.day) < (birth_date.month, birth_date.day))

hashed_pid = hashlib.md5(patron.pid.encode()).hexdigest()
data = {
'name': patron.formatted_name,
'type': patron_type['name'] if patron_type else None,
'age': get_age(patron.user.profile.birth_date),
'age': patron.age,
'postal_code': patron.user.profile.postal_code,
'gender': patron.user.profile.gender or 'other',
'pid': patron.pid,
Expand Down
14 changes: 13 additions & 1 deletion rero_ils/modules/patrons/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

"""API for manipulating patrons."""
from copy import deepcopy
from datetime import datetime
from datetime import date, datetime
from functools import partial

from elasticsearch_dsl import Q
Expand Down Expand Up @@ -825,6 +825,18 @@ def basic_query(channel):
process_bulk_queue.apply_async()


@property
def age(self):
"""Calculate age from birthdate.
:returns: Age
:rtype: int
"""
birth_date = self.user.profile.birth_date
today = date.today()
return today.year - birth_date.year - (
(today.month, today.day) < (birth_date.month, birth_date.day))

class PatronsIndexer(IlsRecordsIndexer):
"""Holdings indexing class."""

Expand Down
4 changes: 3 additions & 1 deletion tests/ui/loans/test_loans_operation_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from utils import flush_index, login_user_for_view

from rero_ils.modules.loans.logs.api import LoanOperationLog
from rero_ils.modules.patrons.api import Patron


def test_loan_operation_log(client, operation_log_data,
Expand All @@ -37,6 +38,7 @@ def test_loan_operation_log(client, operation_log_data,
LoanOperationLog._schema)
operation_log.validate()
log_data = LoanOperationLog.get_record(operation_log.id)
patron = Patron.get_record_by_pid(log_data['loan']['patron']['pid'])
assert log_data['operation'] == 'create'
assert log_data['user_name'] == 'Pedronni, Marie'
assert log_data['date'] == loan_validated_martigny['transaction_date']
Expand All @@ -52,7 +54,7 @@ def test_loan_operation_log(client, operation_log_data,
'hashed_pid': 'e11ff43bff5be4cf70350e2d15149e29',
'name': 'Roduit, Louis',
'type': 'children',
'age': 74,
'age': patron.age,
'postal_code': '1920',
'gender': 'other',
'local_codes': ['code1']
Expand Down

0 comments on commit c2c7a82

Please sign in to comment.