From 6965478afc3081aff8d087618a6c9f6e09031549 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Thu, 3 Oct 2019 10:12:00 +0100 Subject: [PATCH 1/2] explicitly name days in celery unit tests make it easier to understand what's happening --- tests/app/celery/test_nightly_tasks.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/app/celery/test_nightly_tasks.py b/tests/app/celery/test_nightly_tasks.py index bb2c29748..8eeedbe0b 100644 --- a/tests/app/celery/test_nightly_tasks.py +++ b/tests/app/celery/test_nightly_tasks.py @@ -394,7 +394,7 @@ def test_delete_dvla_response_files_older_than_seven_days_does_not_remove_files( remove_s3_mock.assert_not_called() -@freeze_time("2018-01-17 17:00:00") +@freeze_time("Thursday 17th January 2018 17:00") def test_alert_if_letter_notifications_still_sending(sample_letter_template, mocker): two_days_ago = datetime(2018, 1, 15, 13, 30) create_notification(template=sample_letter_template, status='sending', sent_at=two_days_ago) @@ -421,7 +421,7 @@ def test_alert_if_letter_notifications_still_sending_a_day_ago_no_alert(sample_l assert not mock_create_ticket.called -@freeze_time("2018-01-17 17:00:00") +@freeze_time("Thursday 17th January 2018 17:00") def test_alert_if_letter_notifications_still_sending_only_alerts_sending(sample_letter_template, mocker): two_days_ago = datetime(2018, 1, 15, 13, 30) create_notification(template=sample_letter_template, status='sending', sent_at=two_days_ago) @@ -439,7 +439,7 @@ def test_alert_if_letter_notifications_still_sending_only_alerts_sending(sample_ ) -@freeze_time("2018-01-17 17:00:00") +@freeze_time("Thursday 17th January 2018 17:00") def test_alert_if_letter_notifications_still_sending_alerts_for_older_than_offset(sample_letter_template, mocker): three_days_ago = datetime(2018, 1, 14, 13, 30) create_notification(template=sample_letter_template, status='sending', sent_at=three_days_ago) @@ -455,7 +455,7 @@ def test_alert_if_letter_notifications_still_sending_alerts_for_older_than_offse ) -@freeze_time("2018-01-14 17:00:00") +@freeze_time("Sunday 14th January 2018 17:00") def test_alert_if_letter_notifications_still_sending_does_nothing_on_the_weekend(sample_letter_template, mocker): yesterday = datetime(2018, 1, 13, 13, 30) create_notification(template=sample_letter_template, status='sending', sent_at=yesterday) @@ -467,7 +467,7 @@ def test_alert_if_letter_notifications_still_sending_does_nothing_on_the_weekend assert not mock_create_ticket.called -@freeze_time("2018-01-15 17:00:00") +@freeze_time("Monday 15th January 2018 17:00") def test_monday_alert_if_letter_notifications_still_sending_reports_thursday_letters(sample_letter_template, mocker): thursday = datetime(2018, 1, 11, 13, 30) yesterday = datetime(2018, 1, 14, 13, 30) @@ -485,7 +485,7 @@ def test_monday_alert_if_letter_notifications_still_sending_reports_thursday_let ) -@freeze_time("2018-01-16 17:00:00") +@freeze_time("Tuesday 16th January 2018 17:00") def test_tuesday_alert_if_letter_notifications_still_sending_reports_friday_letters(sample_letter_template, mocker): friday = datetime(2018, 1, 12, 13, 30) yesterday = datetime(2018, 1, 14, 13, 30) From 8285ef5f89f45287d0bb9e23fd184b0565cb5784 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Tue, 8 Oct 2019 18:16:45 +0100 Subject: [PATCH 2/2] only check for dvla response files on mon/weds/fri dvla don't process 2nd class files on tues and thurs --- app/celery/nightly_tasks.py | 16 ++++++++++++---- tests/app/celery/test_nightly_tasks.py | 10 ++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/app/celery/nightly_tasks.py b/app/celery/nightly_tasks.py index 73086fb40..060834550 100644 --- a/app/celery/nightly_tasks.py +++ b/app/celery/nightly_tasks.py @@ -258,19 +258,27 @@ def raise_alert_if_letter_notifications_still_sending(): today = datetime.utcnow().date() # Do nothing on the weekend - if today.isoweekday() in [6, 7]: + if today.isoweekday() in {6, 7}: # sat, sun return - if today.isoweekday() in [1, 2]: + if today.isoweekday() in {1, 2}: # mon, tues. look for files from before the weekend offset_days = 4 else: offset_days = 2 - still_sending = Notification.query.filter( + + q = Notification.query.filter( Notification.notification_type == LETTER_TYPE, Notification.status == NOTIFICATION_SENDING, Notification.key_type == KEY_TYPE_NORMAL, func.date(Notification.sent_at) <= today - timedelta(days=offset_days) - ).count() + ) + + if today.isoweekday() in {2, 4}: # on tue, thu, we only care about first class letters + q = q.filter( + Notification.postage == 'first' + ) + + still_sending = q.count() if still_sending: message = "There are {} letters in the 'sending' state from {}".format( diff --git a/tests/app/celery/test_nightly_tasks.py b/tests/app/celery/test_nightly_tasks.py index 8eeedbe0b..e1c86c1ba 100644 --- a/tests/app/celery/test_nightly_tasks.py +++ b/tests/app/celery/test_nightly_tasks.py @@ -471,8 +471,8 @@ def test_alert_if_letter_notifications_still_sending_does_nothing_on_the_weekend def test_monday_alert_if_letter_notifications_still_sending_reports_thursday_letters(sample_letter_template, mocker): thursday = datetime(2018, 1, 11, 13, 30) yesterday = datetime(2018, 1, 14, 13, 30) - create_notification(template=sample_letter_template, status='sending', sent_at=thursday) - create_notification(template=sample_letter_template, status='sending', sent_at=yesterday) + create_notification(template=sample_letter_template, status='sending', sent_at=thursday, postage='second') + create_notification(template=sample_letter_template, status='sending', sent_at=yesterday, postage='second') mock_create_ticket = mocker.patch("app.celery.nightly_tasks.zendesk_client.create_ticket") @@ -489,8 +489,10 @@ def test_monday_alert_if_letter_notifications_still_sending_reports_thursday_let def test_tuesday_alert_if_letter_notifications_still_sending_reports_friday_letters(sample_letter_template, mocker): friday = datetime(2018, 1, 12, 13, 30) yesterday = datetime(2018, 1, 14, 13, 30) - create_notification(template=sample_letter_template, status='sending', sent_at=friday) - create_notification(template=sample_letter_template, status='sending', sent_at=yesterday) + create_notification(template=sample_letter_template, status='sending', sent_at=friday, postage='first') + create_notification(template=sample_letter_template, status='sending', sent_at=yesterday, postage='first') + # doesn't get reported because it's second class, and it's tuesday today + create_notification(template=sample_letter_template, status='sending', sent_at=friday, postage='second') mock_create_ticket = mocker.patch("app.celery.nightly_tasks.zendesk_client.create_ticket")