diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index fcb2a0fdf..74072aa2b 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -154,7 +154,7 @@ def get_dashboard_partials(service_id): 'totals': render_template( 'views/dashboard/_totals.html', service_id=service_id, - statistics=get_dashboard_totals(service) + statistics=get_dashboard_totals(service['data']['statistics']) ), 'template-statistics': render_template( 'views/dashboard/template-statistics.html', @@ -176,11 +176,11 @@ def get_dashboard_partials(service_id): } -def get_dashboard_totals(service): - for msg_type in service['data']['statistics'].values(): +def get_dashboard_totals(statistics): + for msg_type in statistics.values(): msg_type['failed_percentage'] = get_formatted_percentage(msg_type['failed'], msg_type['requested']) msg_type['show_warning'] = float(msg_type['failed_percentage']) > 3 - return service['data']['statistics'] + return statistics def calculate_usage(usage): diff --git a/app/statistics_utils.py b/app/statistics_utils.py index dc3a803a0..4581e069d 100644 --- a/app/statistics_utils.py +++ b/app/statistics_utils.py @@ -48,7 +48,7 @@ def get_formatted_percentage(x, tot): """ Return a percentage to one decimal place (respecting ) """ - return "{0:.1f}".format((float(x) / tot * 100)) if tot else 0 + return "{0:.1f}".format((float(x) / tot * 100)) if tot else '0' def statistics_by_state(statistics): diff --git a/tests/app/main/views/test_dashboard.py b/tests/app/main/views/test_dashboard.py index b9a23afda..f93f70d7c 100644 --- a/tests/app/main/views/test_dashboard.py +++ b/tests/app/main/views/test_dashboard.py @@ -1,9 +1,12 @@ import copy from flask import url_for +import pytest from bs4 import BeautifulSoup from freezegun import freeze_time +from app.main.views.dashboard import get_dashboard_totals + from tests import validate_route_permission from tests.conftest import SERVICE_ONE_ID @@ -435,8 +438,8 @@ def test_service_dashboard_updates_gets_dashboard_totals(mocker, mock_get_service_statistics, mock_get_usage): dashboard_totals = mocker.patch('app.main.views.dashboard.get_dashboard_totals', return_value={ - 'email': {'requested': 0, 'delivered': 0, 'failed': 0}, - 'sms': {'requested': 0, 'delivered': 0, 'failed': 0} + 'email': {'requested': 123, 'delivered': 0, 'failed': 0}, + 'sms': {'requested': 456, 'delivered': 0, 'failed': 0} }) with app_.test_request_context(), app_.test_client() as client: @@ -444,3 +447,45 @@ def test_service_dashboard_updates_gets_dashboard_totals(mocker, response = client.get(url_for('main.service_dashboard', service_id=SERVICE_ONE_ID)) assert response.status_code == 200 + + page = BeautifulSoup(response.data.decode('utf-8'), 'html.parser') + numbers = [number.text.strip() for number in page.find_all('div', class_='big-number-number')] + assert '123' in numbers + assert '456' in numbers + + table_rows = page.find_all('tbody')[0].find_all('tr') + + +def test_get_dashboard_totals_adds_percentages(): + stats = { + 'sms': { + 'requested': 3, + 'delivered': 0, + 'failed': 2 + }, + 'email': { + 'requested': 0, + 'delivered': 0, + 'failed': 0 + } + } + assert get_dashboard_totals(stats)['sms']['failed_percentage'] == '66.7' + assert get_dashboard_totals(stats)['email']['failed_percentage'] == '0' + + +@pytest.mark.parametrize( + 'failures,expected', [ + (2, False), + (3, False), + (4, True) + ] +) +def test_get_dashboard_totals_adds_warning(failures, expected): + stats = { + 'sms': { + 'requested': 100, + 'delivered': 0, + 'failed': failures + } + } + assert get_dashboard_totals(stats)['sms']['show_warning'] == expected diff --git a/tests/app/test_statistics_utils.py b/tests/app/test_statistics_utils.py index 1ea0d6542..cb6b27db8 100644 --- a/tests/app/test_statistics_utils.py +++ b/tests/app/test_statistics_utils.py @@ -53,7 +53,7 @@ def test_sum_of_statistics_sums_inputs(): @pytest.mark.parametrize('emails_failed,emails_requested,expected_failure_rate', [ - (0, 0, 0), + (0, 0, '0'), (0, 1, '0.0'), (1, 3, '33.3') ]) @@ -69,7 +69,7 @@ def test_add_rates_sets_email_failure_rate(emails_failed, emails_requested, expe @pytest.mark.parametrize('sms_failed,sms_requested,expected_failure_rate', [ - (0, 0, 0), + (0, 0, '0'), (0, 1, '0.0'), (1, 3, '33.3') ])