diff --git a/app/dao/notifications_dao.py b/app/dao/notifications_dao.py index 2c9dca604..7df225561 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)