From bb0fb73bc82a4e83712185fe7eb5ae8ffb7205b8 Mon Sep 17 00:00:00 2001 From: Ben Thorner Date: Tue, 10 May 2022 16:48:03 +0100 Subject: [PATCH] Minor tweaks in response to PR comments In response to [^1] and [^2]. [^1]: https://github.com/alphagov/notifications-admin/pull/4229#discussion_r869181152 [^2]: https://github.com/alphagov/notifications-admin/pull/4229#discussion_r869186063 --- app/main/views/dashboard.py | 4 ++++ tests/app/main/views/test_dashboard.py | 10 ++++++---- tests/conftest.py | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index 46520f8b5..e58eba8a7 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -423,6 +423,10 @@ def get_monthly_usage_breakdown_for_letters(monthly_letters): postage_order[get_monthly_usage_postage_description(row)], row['rate'] ) + # First sort letter rows by postage and then by rate, clumping "europe" and + # "rest-of-world" postage together as "international". Group the sorted rows + # together using the same fields - "group_key" is used for both operations. + # Note that "groupby" preserves the sort order in the groups it returns. rate_groups = groupby(sorted(monthly_letters, key=group_key), key=group_key) for _key, rate_group in rate_groups: diff --git a/tests/app/main/views/test_dashboard.py b/tests/app/main/views/test_dashboard.py index a0968c673..bb3738ff6 100644 --- a/tests/app/main/views/test_dashboard.py +++ b/tests/app/main/views/test_dashboard.py @@ -1079,9 +1079,10 @@ def test_usage_page_monthly_breakdown( assert '140 free text messages' in monthly_breakdown assert '960 text messages at 1.65p' in monthly_breakdown assert '33 text messages at 1.70p' in monthly_breakdown - assert '10 second class letters at 31p' in monthly_breakdown assert '5 first class letters at 33p' in monthly_breakdown - assert '10 international letters at 84p' in monthly_breakdown + assert '10 second class letters at 31p' in monthly_breakdown + assert '3 international letters at 55p' in monthly_breakdown + assert '7 international letters at 84p' in monthly_breakdown assert 'March' in monthly_breakdown assert '£20.91' in monthly_breakdown @@ -1110,7 +1111,7 @@ def test_usage_page_monthly_breakdown_shows_months_so_far( @freeze_time("2012-03-31 12:12:12") -def test_usage_page_letter_breakdown_ordering( +def test_usage_page_letter_breakdown_ordered_by_postage_and_rate( client_request, service_one, mock_get_monthly_usage_for_service, @@ -1123,7 +1124,8 @@ def test_usage_page_letter_breakdown_ordering( assert normalize_spaces(postage_details[3].text) == '5 first class letters at 33p' assert normalize_spaces(postage_details[4].text) == '10 second class letters at 31p' - assert normalize_spaces(postage_details[5].text) == '10 international letters at 84p' + assert normalize_spaces(postage_details[5].text) == '3 international letters at 55p' + assert normalize_spaces(postage_details[6].text) == '7 international letters at 84p' def test_usage_page_with_0_free_allowance( diff --git a/tests/conftest.py b/tests/conftest.py index 569fcc7cc..6e0e4f51d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2421,7 +2421,7 @@ def mock_get_monthly_usage_for_service(mocker): { 'month': 'February', 'notification_type': 'letter', - 'rate': 0.84, + 'rate': 0.55, 'chargeable_units': 3, 'notifications_sent': 3, 'postage': 'europe',