From a90a18541fdaaacfa19dd863f110348d462a274c Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Thu, 26 May 2016 16:46:16 +0100 Subject: [PATCH] notifications/statistics returns when no stats exist for today moved filtering from WHERE to JOIN ON so that when join suceeds but filter fails, we dont lose the service's data from the results set --- app/dao/notifications_dao.py | 14 +++++---- .../rest/test_notification_statistics.py | 29 +++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 72fc3a2f7..238c00c97 100644 --- a/app/dao/notifications_dao.py +++ b/app/dao/notifications_dao.py @@ -1,4 +1,4 @@ -from sqlalchemy import (desc, func, Integer, or_, asc) +from sqlalchemy import (desc, func, Integer, or_, and_, asc) from sqlalchemy.sql.expression import cast from datetime import ( @@ -62,11 +62,13 @@ def dao_get_potential_notification_statistics_for_day(day): Service.id, NotificationStatistics ).outerjoin( - Service.service_notification_stats - ).filter( - or_( - NotificationStatistics.day == day, - NotificationStatistics.day == None # noqa + NotificationStatistics, + and_( + Service.id == NotificationStatistics.service_id, + or_( + NotificationStatistics.day == day, + NotificationStatistics.day == None # noqa + ) ) ).order_by( asc(Service.created_at) diff --git a/tests/app/notifications/rest/test_notification_statistics.py b/tests/app/notifications/rest/test_notification_statistics.py index ec7a0ba0f..769fd7395 100644 --- a/tests/app/notifications/rest/test_notification_statistics.py +++ b/tests/app/notifications/rest/test_notification_statistics.py @@ -194,3 +194,32 @@ def test_get_notification_statistics_returns_both_existing_stats_and_generated_z assert generated_stats['service'] == str(service_without_stats.id) assert response.status_code == 200 + + +@freeze_time('1955-11-05T12:00:00') +def test_get_notification_statistics_returns_zeros_when_only_stats_for_different_date( + notify_api, + sample_notification_statistics +): + with notify_api.test_request_context(): + with notify_api.test_client() as client: + with freeze_time('1985-10-26T00:06:00'): + auth_header = create_authorization_header( + service_id=sample_notification_statistics.service_id + ) + response = client.get( + '/notifications/statistics?day={}'.format(date.today().isoformat()), + headers=[auth_header] + ) + + notifications = json.loads(response.get_data(as_text=True)) + + assert response.status_code == 200 + assert len(notifications['data']) == 1 + assert notifications['data'][0]['emails_requested'] == 0 + assert notifications['data'][0]['emails_delivered'] == 0 + assert notifications['data'][0]['emails_failed'] == 0 + assert notifications['data'][0]['sms_requested'] == 0 + assert notifications['data'][0]['sms_delivered'] == 0 + assert notifications['data'][0]['sms_failed'] == 0 + assert notifications['data'][0]['service'] == str(sample_notification_statistics.service_id)