From 33eaa60e5bc905fa399fe427c7d6cd64597945e3 Mon Sep 17 00:00:00 2001 From: stvnrlly Date: Fri, 2 Dec 2022 11:27:41 -0500 Subject: [PATCH] mock the api call --- app/main/views/dashboard.py | 26 +++++++++++--------------- tests/app/main/views/test_dashboard.py | 17 +++++++++++++---- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index 1c5705e48..24b853f0f 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -131,22 +131,18 @@ def usage(service_id): free_sms_allowance = billing_api_client.get_free_sms_fragment_limit_for_year(service_id, year) units = billing_api_client.get_monthly_usage_for_service(service_id, year) yearly_usage = billing_api_client.get_annual_usage_for_service(service_id, year) - more_stats = [] - try: - more_stats = format_monthly_stats_to_list( - service_api_client.get_monthly_notification_stats(service_id, year)['data'] - ) - if year == current_financial_year: - # This includes Oct, Nov, Dec - # but we don't need next year's data yet - more_stats = [month for month in more_stats if month['name'] in ['October', 'November', 'December']] - elif year == (current_financial_year + 1): - # This is all the other months - # and we need last year's data - more_stats = [month for month in more_stats if month['name'] not in ['October', 'November', 'December']] - except Exception: - pass + more_stats = format_monthly_stats_to_list( + service_api_client.get_monthly_notification_stats(service_id, year)['data'] + ) + if year == current_financial_year: + # This includes Oct, Nov, Dec + # but we don't need next year's data yet + more_stats = [month for month in more_stats if month['name'] in ['October', 'November', 'December']] + elif year == (current_financial_year + 1): + # This is all the other months + # and we need last year's data + more_stats = [month for month in more_stats if month['name'] not in ['October', 'November', 'December']] return render_template( 'views/usage.html', diff --git a/tests/app/main/views/test_dashboard.py b/tests/app/main/views/test_dashboard.py index c3b7873ca..6bc5881c9 100644 --- a/tests/app/main/views/test_dashboard.py +++ b/tests/app/main/views/test_dashboard.py @@ -844,7 +844,8 @@ def test_usage_page( client_request, mock_get_annual_usage_for_service, mock_get_monthly_usage_for_service, - mock_get_free_sms_fragment_limit + mock_get_free_sms_fragment_limit, + mock_get_monthly_notification_stats, ): page = client_request.get( 'main.usage', @@ -884,7 +885,8 @@ def test_usage_page_no_sms_spend( mocker, client_request, mock_get_monthly_usage_for_service, - mock_get_free_sms_fragment_limit + mock_get_free_sms_fragment_limit, + mock_get_monthly_notification_stats, ): mocker.patch('app.billing_api_client.get_annual_usage_for_service', return_value=[ { @@ -916,7 +918,8 @@ def test_usage_page_monthly_breakdown( service_one, mock_get_annual_usage_for_service, mock_get_monthly_usage_for_service, - mock_get_free_sms_fragment_limit + mock_get_free_sms_fragment_limit, + mock_get_monthly_notification_stats, ): page = client_request.get('main.usage', service_id=SERVICE_ONE_ID) monthly_breakdown = normalize_spaces(page.find('table').text) @@ -947,6 +950,7 @@ def test_usage_page_monthly_breakdown_shows_months_so_far( mock_get_annual_usage_for_service, mock_get_monthly_usage_for_service, mock_get_free_sms_fragment_limit, + mock_get_monthly_notification_stats, now, expected_number_of_months ): @@ -961,6 +965,7 @@ def test_usage_page_with_0_free_allowance( client_request, mock_get_annual_usage_for_service, mock_get_monthly_usage_for_service, + mock_get_monthly_notification_stats, ): mocker.patch( 'app.billing_api_client.get_free_sms_fragment_limit_for_year', @@ -984,6 +989,7 @@ def test_usage_page_with_year_argument( mock_get_annual_usage_for_service, mock_get_monthly_usage_for_service, mock_get_free_sms_fragment_limit, + mock_get_monthly_notification_stats, ): client_request.get( 'main.usage', @@ -993,6 +999,7 @@ def test_usage_page_with_year_argument( mock_get_monthly_usage_for_service.assert_called_once_with(SERVICE_ONE_ID, 2000) mock_get_annual_usage_for_service.assert_called_once_with(SERVICE_ONE_ID, 2000) mock_get_free_sms_fragment_limit.assert_called_with(SERVICE_ONE_ID, 2000) + mock_get_monthly_notification_stats.assert_called_with(SERVICE_ONE_ID, 2000) def test_usage_page_for_invalid_year( @@ -1011,7 +1018,8 @@ def test_future_usage_page( client_request, mock_get_annual_usage_for_service_in_future, mock_get_monthly_usage_for_service_in_future, - mock_get_free_sms_fragment_limit + mock_get_free_sms_fragment_limit, + mock_get_monthly_notification_stats ): client_request.get( 'main.usage', @@ -1022,6 +1030,7 @@ def test_future_usage_page( mock_get_monthly_usage_for_service_in_future.assert_called_once_with(SERVICE_ONE_ID, 2014) mock_get_annual_usage_for_service_in_future.assert_called_once_with(SERVICE_ONE_ID, 2014) mock_get_free_sms_fragment_limit.assert_called_with(SERVICE_ONE_ID, 2014) + mock_get_monthly_notification_stats.assert_called_with(SERVICE_ONE_ID, 2014) def _test_dashboard_menu(client_request, mocker, usr, service, permissions):