Skip to content

Commit

Permalink
notifications: split notifications es and creations tests.
Browse files Browse the repository at this point in the history
* Improves units testing to have a separate test for functionality.

Co-Authored-by: Aly Badr <[email protected]>
  • Loading branch information
Aly Badr committed Jun 19, 2019
1 parent 5782a78 commit be1b568
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 32 deletions.
17 changes: 6 additions & 11 deletions rero_ils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,9 @@ def _(x):
},
'notification-creation': {
'task':
'rero-ils.modules.tasks.create_over_and_due_soon_notifications',
'schedule': crontab(hour=4, minute=00)
'rero_ils.modules.notifications.tasks.create_over_and_due_soon_notifications',
'schedule': crontab(minute="*/5")
# TODO: in production set this up once a day
},
# 'mef-harvester': {
# 'task': 'rero_ils.modules.apiharvester.tasks.harvest_records',
Expand Down Expand Up @@ -662,19 +663,13 @@ def _(x):
search_type=None,
record_serializers={
'application/json': (
'rero_ils.modules.serializers' ':json_v1_response'
),
'application/can-delete+json': (
'rero_ils.modules.serializers' ':can_delete_json_v1_response'
'rero_ils.modules.serializers:json_v1_response'
)
},
search_serializers={
'application/json': (
'invenio_records_rest.serializers' ':json_v1_search'
),
'application/can-delete+json': (
'rero_ils.modules.serializers' ':can_delete_json_v1_search'
),
'rero_ils.modules.serializers:json_v1_search'
)
},
record_loaders={
'application/json': lambda: Notification(request.get_json()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"description": "Number of days before due date for sending the availability notification.",
"type": "integer",
"default": 5,
"minimum": 1
"minimum": 0
},
"number_of_days_after_due_date": {
"title": "Number of days after due data.",
Expand Down
9 changes: 9 additions & 0 deletions rero_ils/modules/notifications/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,18 @@
def create_over_and_due_soon_notifications():
"""Creates due_soon and overdue notifications."""
over_due_loans = get_overdue_loans()
no_over_due_loans = 0
for loan in over_due_loans:
loan.create_notification(notification_type='overdue')
no_over_due_loans += 1

due_soon_loans = get_due_soon_loans()
no_due_soon_loans = 0
for loan in due_soon_loans:
loan.create_notification(notification_type='due_soon')
no_due_soon_loans += 1

return 'created {no_over_due_loans} overdue loans, '\
'{no_due_soon_loans} due soon loans'.format(
no_over_due_loans=no_over_due_loans,
no_due_soon_loans=no_due_soon_loans)
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ def run(self):
'invenio_celery.tasks': [
'rero_ils_oaiharvest = rero_ils.modules.ebooks.tasks',
'rero_ils_mefharvest = rero_ils.modules.apiharvester.tasks',
'rero_ils_notifications = rero_ils.modules.notifications.tasks',
],
'invenio_records.jsonresolver': [
'organisations = rero_ils.modules.organisations.jsonresolver',
Expand Down
2 changes: 1 addition & 1 deletion tests/api/test_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def test_items_serializers(
patron_martigny_no_email,
librarian_martigny_no_email,
librarian_sion_no_email,
loan_pending
loan_pending_martigny
):
"""Test record retrieval."""
login_user(client, librarian_martigny_no_email)
Expand Down
51 changes: 33 additions & 18 deletions tests/ui/notifications/test_notifications_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,23 @@
notification_id_fetcher as fetcher


def test_notification_create_and_es_mapping(
dummy_notification, loan_validated_martigny, mailbox):
"""Test notification creation."""
def test_notification_organisation_pid(
org_martigny, notification_availability_martigny):
"""Test organisation pid has been added during the indexing."""
search = NotificationsSearch()
pid = notification_availability_martigny.get('pid')
notification = next(search.filter('term', pid=pid).scan())
assert notification.organisation.pid == org_martigny.pid

# test notification can_delete
assert notification_availability_martigny.get_links_to_me() == {}
assert notification_availability_martigny.can_delete


def test_notification_es_mapping(
dummy_notification, loan_validated_martigny):
"""Test notification elasticsearch mapping."""

search = NotificationsSearch()
mapping = get_mapping(search.Meta.index)
assert mapping
Expand All @@ -51,6 +65,22 @@ def test_notification_create_and_es_mapping(
loan_ref = '{loan_url}{pid}'.format(**notif_data)
notif['loan'] = {"$ref": loan_ref}

Notification.create(notif, dbcommit=True, delete_pid=True, reindex=True)

assert mapping == get_mapping(search.Meta.index)


def test_notification_create(
es_clear, dummy_notification, loan_validated_martigny, mailbox):
"""Test notification creation."""
notif = deepcopy(dummy_notification)
notif_data = {
'loan_url': 'https://ils.rero.ch/api/loans/',
'pid': loan_validated_martigny.get('loan_pid')
}
loan_ref = '{loan_url}{pid}'.format(**notif_data)
notif['loan'] = {"$ref": loan_ref}

notification = Notification.create(
notif, dbcommit=True, delete_pid=True, reindex=True)
assert notification == notif
Expand All @@ -60,24 +90,9 @@ def test_notification_create_and_es_mapping(
notification = Notification.get_record_by_pid(pid)
assert notification == notif

assert mapping == get_mapping(search.Meta.index)

fetched_pid = fetcher(notification.id, notification)
assert fetched_pid.pid_value == pid
assert fetched_pid.pid_type == 'notif'

notification.dispatch()
assert len(mailbox) == 1


def test_notification_organisation_pid(
org_martigny, notification_availability_martigny):
"""Test organisation pid has been added during the indexing."""
search = NotificationsSearch()
pid = notification_availability_martigny.get('pid')
notification = next(search.filter('term', pid=pid).scan())
assert notification.organisation.pid == org_martigny.pid

# test notification can_delete
assert notification_availability_martigny.get_links_to_me() == {}
assert notification_availability_martigny.can_delete
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ <h1 translate>Circulation Policy</h1>
Days before due date is required.
</div>
<div *ngIf="number_of_days_before_due_date.errors.min" translate>
Days before due date must be greater than 0.
Days before due date must be greater or equal to 0.
</div>
</div>
</div>
Expand Down

0 comments on commit be1b568

Please sign in to comment.