From c122a5b3d7a07fc93d1c3a90cba3959341634071 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Thu, 16 Mar 2017 14:46:27 +0000 Subject: [PATCH] Add unit tests for dashboard template functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Although we integration test the pages that depend on these, it’s probably a good idea to unit test them as well, to make sure, for example, that all the different statuses are counted when working out how many messages have been sent from a template. --- tests/app/main/views/test_dashboard.py | 60 ++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/tests/app/main/views/test_dashboard.py b/tests/app/main/views/test_dashboard.py index b7143a7d3..2d964e2c5 100644 --- a/tests/app/main/views/test_dashboard.py +++ b/tests/app/main/views/test_dashboard.py @@ -12,6 +12,8 @@ from app.main.views.dashboard import ( format_monthly_stats_to_list, get_free_paid_breakdown_for_billable_units, aggregate_status_types, + format_template_stats_to_list, + get_tuples_of_financial_years, ) from tests import validate_route_permission @@ -613,3 +615,61 @@ def test_get_free_paid_breakdown_for_billable_units(now, expected_number_of_mont {'name': 'February', 'free': 0, 'paid': 1234}, {'name': 'March', 'free': 0, 'paid': 0} ][:expected_number_of_months] + + +def test_format_template_stats_to_list_with_no_stats(): + assert list(format_template_stats_to_list({})) == [] + + +def test_format_template_stats_to_list(): + counts = { + 'created': 1, + 'pending': 1, + 'delivered': 1, + 'failed': 1, + 'temporary-failure': 1, + 'permanent-failure': 1, + 'technical-failure': 1, + 'do-not-count': 999, + } + stats_list = list(format_template_stats_to_list({ + 'template_2_id': { + 'counts': {}, + 'name': 'bar', + }, + 'template_1_id': { + 'counts': counts, + 'name': 'foo', + }, + })) + + # we don’t care about the order of this function’s output + assert len(stats_list) == 2 + assert { + 'counts': counts, + 'name': 'foo', + 'requested_count': 7, + } in stats_list + assert { + 'counts': {}, + 'name': 'bar', + 'requested_count': 0, + } in stats_list + + +def test_get_tuples_of_financial_years(): + assert list(get_tuples_of_financial_years( + lambda year: 'http://example.com?year={}'.format(year), + start=2040, + end=2041, + )) == [ + ('financial year', 2040, 'http://example.com?year=2040', '2040 to 2041'), + ('financial year', 2041, 'http://example.com?year=2041', '2041 to 2042'), + ] + + +def test_get_tuples_of_financial_years_defaults_to_2015(): + assert 2015 in list(get_tuples_of_financial_years( + lambda year: 'http://example.com?year={}'.format(year), + end=2040, + ))[0]