From 18ab7f33378b655af80e53ccbd7b0328c1436d90 Mon Sep 17 00:00:00 2001 From: Alexey Bezhan Date: Thu, 30 Aug 2018 14:27:57 +0100 Subject: [PATCH] Add updated history count to dao_update_notifications_by_reference For returned letter updates most notifications won't exist in the notifications table, so in order to find out whether the reference matches any known letters we need to check the count of updated history records. --- app/celery/letters_pdf_tasks.py | 2 +- app/celery/tasks.py | 6 ++-- app/dao/notifications_dao.py | 4 +-- .../notification_dao/test_notification_dao.py | 28 +++++++++++++------ 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/app/celery/letters_pdf_tasks.py b/app/celery/letters_pdf_tasks.py index 8a65ba333..2df7d680e 100644 --- a/app/celery/letters_pdf_tasks.py +++ b/app/celery/letters_pdf_tasks.py @@ -288,7 +288,7 @@ def update_letter_pdf_status(reference, status): update_dict={ 'status': status, 'updated_at': datetime.utcnow() - }) + })[0] def replay_letters_in_error(filename=None): diff --git a/app/celery/tasks.py b/app/celery/tasks.py index 3c2089942..bab249eea 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -339,7 +339,7 @@ def update_letter_notifications_to_sent_to_dvla(self, notification_references): # This task will be called by the FTP app to update notifications as sent to DVLA provider = get_current_provider(LETTER_TYPE) - updated_count = dao_update_notifications_by_reference( + updated_count, _ = dao_update_notifications_by_reference( notification_references, { 'status': NOTIFICATION_SENDING, @@ -357,7 +357,7 @@ def update_letter_notifications_to_sent_to_dvla(self, notification_references): def update_letter_notifications_to_error(self, notification_references): # This task will be called by the FTP app to update notifications as sent to DVLA - updated_count = dao_update_notifications_by_reference( + updated_count, _ = dao_update_notifications_by_reference( notification_references, { 'status': NOTIFICATION_TECHNICAL_FAILURE, @@ -465,7 +465,7 @@ def update_letter_notification(filename, temporary_failures, update): status = NOTIFICATION_TEMPORARY_FAILURE temporary_failures.append(update.reference) - updated_count = dao_update_notifications_by_reference( + updated_count, _ = dao_update_notifications_by_reference( references=[update.reference], update_dict={"status": status, "billable_units": update.page_count, diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 27d609562..1bc683813 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -465,14 +465,14 @@ def dao_update_notifications_by_reference(references, update_dict): synchronize_session=False ) - NotificationHistory.query.filter( + updated_history_count = NotificationHistory.query.filter( NotificationHistory.reference.in_(references) ).update( update_dict, synchronize_session=False ) - return updated_count + return updated_count, updated_history_count @statsd(namespace="dao") diff --git a/tests/app/dao/notification_dao/test_notification_dao.py b/tests/app/dao/notification_dao/test_notification_dao.py index 5aa53b87a..00333179c 100644 --- a/tests/app/dao/notification_dao/test_notification_dao.py +++ b/tests/app/dao/notification_dao/test_notification_dao.py @@ -1653,10 +1653,13 @@ def test_dao_update_notifications_by_reference_updated_notificaitons_and_history notification_1 = create_notification(template=sample_template, reference='ref') notification_2 = create_notification(template=sample_template, reference='ref') - updated_count = dao_update_notifications_by_reference(references=['ref'], - update_dict={"status": "delivered", - "billable_units": 2} - ) + updated_count, updated_history_count = dao_update_notifications_by_reference( + references=['ref'], + update_dict={ + "status": "delivered", + "billable_units": 2 + } + ) assert updated_count == 2 updated_1 = Notification.query.get(notification_1.id) assert updated_1.billable_units == 2 @@ -1665,6 +1668,7 @@ def test_dao_update_notifications_by_reference_updated_notificaitons_and_history assert updated_2.billable_units == 2 assert updated_2.status == 'delivered' + assert updated_history_count == 2 updated_history_1 = NotificationHistory.query.get(notification_1.id) assert updated_history_1.billable_units == 2 assert updated_history_1.status == 'delivered' @@ -1676,22 +1680,28 @@ def test_dao_update_notifications_by_reference_updated_notificaitons_and_history def test_dao_update_notifications_by_reference_returns_zero_when_no_notifications_to_update(notify_db): - updated_count = dao_update_notifications_by_reference(references=['ref'], - update_dict={"status": "delivered", - "billable_units": 2} - ) + updated_count, updated_history_count = dao_update_notifications_by_reference( + references=['ref'], + update_dict={ + "status": "delivered", + "billable_units": 2 + } + ) + assert updated_count == 0 + assert updated_history_count == 0 def test_dao_update_notifications_by_reference_set_returned_letter_status(sample_letter_template): notification = create_notification(template=sample_letter_template, reference='ref') - updated_count = dao_update_notifications_by_reference( + updated_count, updated_history_count = dao_update_notifications_by_reference( references=['ref'], update_dict={"status": "returned-letter"} ) assert updated_count == 1 + assert updated_history_count == 1 assert Notification.query.get(notification.id).status == 'returned-letter'