Test logging and error message

This commit is contained in:
Pea Tyczynska
2020-05-06 17:43:34 +01:00
parent 131372bdd8
commit 879d15b736
2 changed files with 12 additions and 5 deletions

View File

@@ -83,13 +83,13 @@ def create_letters_pdf(self, notification_id):
except Exception: except Exception:
try: try:
current_app.logger.exception( current_app.logger.exception(
"RETRY: calling create-letter-pdf task for notification {} failed".format(notification_id) f"RETRY: calling create-letter-pdf task for notification {notification_id} failed"
) )
self.retry(queue=QueueNames.RETRY) self.retry(queue=QueueNames.RETRY)
except self.MaxRetriesExceededError: except self.MaxRetriesExceededError:
message = "RETRY FAILED: Max retries reached. " \ message = f"RETRY FAILED: Max retries reached. " \
"The task create-letter-pdf failed for notification {}. " \ f"The task create-letter-pdf failed for notification id {notification_id}. " \
"Notification has been updated to technical-failure".format(notification_id) f"Notification has been updated to technical-failure"
update_notification_status_by_id(notification_id, NOTIFICATION_TECHNICAL_FAILURE) update_notification_status_by_id(notification_id, NOTIFICATION_TECHNICAL_FAILURE)
raise NotificationTechnicalFailureException(message) raise NotificationTechnicalFailureException(message)

View File

@@ -97,11 +97,15 @@ def test_create_letters_pdf_retries_upon_error(mocker, sample_letter_notificatio
mock_celery = mocker.patch('app.celery.letters_pdf_tasks.notify_celery.send_task', side_effect=Exception()) mock_celery = mocker.patch('app.celery.letters_pdf_tasks.notify_celery.send_task', side_effect=Exception())
mocker.patch('app.celery.letters_pdf_tasks.get_letter_pdf_filename', return_value='LETTER.PDF') mocker.patch('app.celery.letters_pdf_tasks.get_letter_pdf_filename', return_value='LETTER.PDF')
mock_retry = mocker.patch('app.celery.letters_pdf_tasks.create_letters_pdf.retry') mock_retry = mocker.patch('app.celery.letters_pdf_tasks.create_letters_pdf.retry')
mock_logger = mocker.patch('app.celery.tasks.current_app.logger.exception')
create_letters_pdf(sample_letter_notification.id) create_letters_pdf(sample_letter_notification.id)
assert mock_celery.called assert mock_celery.called
assert mock_retry.called assert mock_retry.called
mock_logger.assert_called_once_with(
f"RETRY: calling create-letter-pdf task for notification {sample_letter_notification.id} failed"
)
def test_create_letters_pdf_sets_technical_failure_max_retries(mocker, sample_letter_notification): def test_create_letters_pdf_sets_technical_failure_max_retries(mocker, sample_letter_notification):
@@ -111,9 +115,12 @@ def test_create_letters_pdf_sets_technical_failure_max_retries(mocker, sample_le
'app.celery.letters_pdf_tasks.create_letters_pdf.retry', side_effect=MaxRetriesExceededError) 'app.celery.letters_pdf_tasks.create_letters_pdf.retry', side_effect=MaxRetriesExceededError)
mock_update_noti = mocker.patch('app.celery.letters_pdf_tasks.update_notification_status_by_id') mock_update_noti = mocker.patch('app.celery.letters_pdf_tasks.update_notification_status_by_id')
with pytest.raises(NotificationTechnicalFailureException): with pytest.raises(NotificationTechnicalFailureException) as e:
create_letters_pdf(sample_letter_notification.id) create_letters_pdf(sample_letter_notification.id)
assert e.value.args[0] == f"RETRY FAILED: Max retries reached. " \
f"The task create-letter-pdf failed for notification id {sample_letter_notification.id}. " \
f"Notification has been updated to technical-failure"
assert mock_celery.called assert mock_celery.called
assert mock_retry.called assert mock_retry.called
mock_update_noti.assert_called_once_with(sample_letter_notification.id, 'technical-failure') mock_update_noti.assert_called_once_with(sample_letter_notification.id, 'technical-failure')