Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: fix test_loan_operation_log #2945

Merged
merged 1 commit into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -824,6 +824,18 @@ def basic_query(channel):
indexer.bulk_index(ids)
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