From 3cd8605e111a05d23349ff42b3170d0c59cfded2 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Tue, 31 Jan 2017 11:32:53 +0000 Subject: [PATCH] fix api statistics to account for letters --- app/service/statistics.py | 4 +-- tests/app/service/test_statistics.py | 52 +++++++++++++++++++--------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/app/service/statistics.py b/app/service/statistics.py index 3f06fcacb..626335784 100644 --- a/app/service/statistics.py +++ b/app/service/statistics.py @@ -1,7 +1,7 @@ import itertools from datetime import datetime, timedelta -from app.models import EMAIL_TYPE, SMS_TYPE +from app.models import TEMPLATE_TYPES def format_statistics(statistics): @@ -33,7 +33,7 @@ def create_zeroed_stats_dicts(): return { template_type: { status: 0 for status in ('requested', 'delivered', 'failed') - } for template_type in (EMAIL_TYPE, SMS_TYPE) + } for template_type in TEMPLATE_TYPES } diff --git a/tests/app/service/test_statistics.py b/tests/app/service/test_statistics.py index fcf854da8..c5adad55d 100644 --- a/tests/app/service/test_statistics.py +++ b/tests/app/service/test_statistics.py @@ -16,24 +16,25 @@ WeeklyStatsRow = collections.namedtuple('row', ('notification_type', 'status', ' # email_counts and sms_counts are 3-tuple of requested, delivered, failed -@pytest.mark.idparametrize('stats, email_counts, sms_counts', { - 'empty': ([], [0, 0, 0], [0, 0, 0]), +@pytest.mark.idparametrize('stats, email_counts, sms_counts, letter_counts', { + 'empty': ([], [0, 0, 0], [0, 0, 0], [0, 0, 0]), 'always_increment_requested': ([ StatsRow('email', 'delivered', 1), StatsRow('email', 'failed', 1) - ], [2, 1, 1], [0, 0, 0]), - 'dont_mix_email_and_sms': ([ + ], [2, 1, 1], [0, 0, 0], [0, 0, 0]), + 'dont_mix_template_types': ([ StatsRow('email', 'delivered', 1), - StatsRow('sms', 'delivered', 1) - ], [1, 1, 0], [1, 1, 0]), + StatsRow('sms', 'delivered', 1), + StatsRow('letter', 'delivered', 1) + ], [1, 1, 0], [1, 1, 0], [1, 1, 0]), 'convert_fail_statuses_to_failed': ([ StatsRow('email', 'failed', 1), StatsRow('email', 'technical-failure', 1), StatsRow('email', 'temporary-failure', 1), StatsRow('email', 'permanent-failure', 1), - ], [4, 0, 4], [0, 0, 0]), + ], [4, 0, 4], [0, 0, 0], [0, 0, 0]), }) -def test_format_statistics(stats, email_counts, sms_counts): +def test_format_statistics(stats, email_counts, sms_counts, letter_counts): ret = format_statistics(stats) @@ -49,6 +50,12 @@ def test_format_statistics(stats, email_counts, sms_counts): in zip(['requested', 'delivered', 'failed'], sms_counts) } + assert ret['letter'] == { + status: count + for status, count + in zip(['requested', 'delivered', 'failed'], letter_counts) + } + @pytest.mark.parametrize('start,end,dates', [ (datetime(2016, 7, 25), datetime(2016, 7, 25), [datetime(2016, 7, 25)]), @@ -66,6 +73,7 @@ def test_create_zeroed_stats_dicts(): assert create_zeroed_stats_dicts() == { 'sms': {'requested': 0, 'delivered': 0, 'failed': 0}, 'email': {'requested': 0, 'delivered': 0, 'failed': 0}, + 'letter': {'requested': 0, 'delivered': 0, 'failed': 0}, } @@ -79,43 +87,51 @@ def _stats(requested, delivered, failed): (datetime(2016, 7, 28), [], { datetime(2016, 7, 25): { 'sms': _stats(0, 0, 0), - 'email': _stats(0, 0, 0) + 'email': _stats(0, 0, 0), + 'letter': _stats(0, 0, 0) } }), # with a random created time, still create the dict for midnight (datetime(2016, 7, 28, 12, 13, 14), [], { datetime(2016, 7, 25, 0, 0, 0): { 'sms': _stats(0, 0, 0), - 'email': _stats(0, 0, 0) + 'email': _stats(0, 0, 0), + 'letter': _stats(0, 0, 0) } }), # with no stats but a service (datetime(2016, 7, 14), [], { datetime(2016, 7, 11): { 'sms': _stats(0, 0, 0), - 'email': _stats(0, 0, 0) + 'email': _stats(0, 0, 0), + 'letter': _stats(0, 0, 0) }, datetime(2016, 7, 18): { 'sms': _stats(0, 0, 0), - 'email': _stats(0, 0, 0) + 'email': _stats(0, 0, 0), + 'letter': _stats(0, 0, 0) }, datetime(2016, 7, 25): { 'sms': _stats(0, 0, 0), - 'email': _stats(0, 0, 0) + 'email': _stats(0, 0, 0), + 'letter': _stats(0, 0, 0) } }), # two stats for same week dont re-zero each other (datetime(2016, 7, 21), [ WeeklyStatsRow('email', 'created', datetime(2016, 7, 18), 1), WeeklyStatsRow('sms', 'created', datetime(2016, 7, 18), 1), + WeeklyStatsRow('letter', 'created', datetime(2016, 7, 18), 1), ], { datetime(2016, 7, 18): { 'sms': _stats(1, 0, 0), - 'email': _stats(1, 0, 0) + 'email': _stats(1, 0, 0), + 'letter': _stats(1, 0, 0) }, datetime(2016, 7, 25): { 'sms': _stats(0, 0, 0), - 'email': _stats(0, 0, 0) + 'email': _stats(0, 0, 0), + 'letter': _stats(0, 0, 0) } }), # two stats for same type are added together @@ -126,11 +142,13 @@ def _stats(requested, delivered, failed): ], { datetime(2016, 7, 18): { 'sms': _stats(2, 1, 0), - 'email': _stats(0, 0, 0) + 'email': _stats(0, 0, 0), + 'letter': _stats(0, 0, 0) }, datetime(2016, 7, 25): { 'sms': _stats(1, 0, 0), - 'email': _stats(0, 0, 0) + 'email': _stats(0, 0, 0), + 'letter': _stats(0, 0, 0) } }) ])