From 28b03fe748a586f6687650223094a20a380e9930 Mon Sep 17 00:00:00 2001 From: Alexey Bezhan Date: Mon, 13 Aug 2018 17:04:40 +0100 Subject: [PATCH] Add limit_days argument to statistics API client method Allows getting notification counts for a given number of days to support services with custom data retention periods (admin dashboard page should still display counts for the last 7 days, while the notifications page displays all stored notifications). --- app/notify_client/service_api_client.py | 6 ++++-- tests/app/notify_client/test_service_api_client.py | 14 ++++++++++---- tests/conftest.py | 2 +- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/app/notify_client/service_api_client.py b/app/notify_client/service_api_client.py index b2d2f79b5..1f5e74d16 100644 --- a/app/notify_client/service_api_client.py +++ b/app/notify_client/service_api_client.py @@ -41,8 +41,10 @@ class ServiceAPIClient(NotifyAdminAPIClient): """ return self.get('/service/{0}'.format(service_id)) - def get_service_statistics(self, service_id, today_only): - return self.get('/service/{0}/statistics'.format(service_id), params={'today_only': today_only})['data'] + def get_service_statistics(self, service_id, today_only, limit_days=None): + return self.get('/service/{0}/statistics'.format(service_id), params={ + 'today_only': today_only, 'limit_days': limit_days + })['data'] def get_services(self, params_dict=None): """ diff --git a/tests/app/notify_client/test_service_api_client.py b/tests/app/notify_client/test_service_api_client.py index 7bc4afd4f..92dac3626 100644 --- a/tests/app/notify_client/test_service_api_client.py +++ b/tests/app/notify_client/test_service_api_client.py @@ -35,15 +35,21 @@ def test_client_gets_service(mocker): mock_get.assert_called_once_with('/service/foo') -@pytest.mark.parametrize('today_only', [True, False]) -def test_client_gets_service_statistics(mocker, today_only): +@pytest.mark.parametrize('today_only, limit_days', [ + (True, None), + (False, None), + (False, 30), +]) +def test_client_gets_service_statistics(mocker, today_only, limit_days): client = ServiceAPIClient() mock_get = mocker.patch.object(client, 'get', return_value={'data': {'a': 'b'}}) - ret = client.get_service_statistics('foo', today_only) + ret = client.get_service_statistics('foo', today_only, limit_days) assert ret == {'a': 'b'} - mock_get.assert_called_once_with('/service/foo/statistics', params={'today_only': today_only}) + mock_get.assert_called_once_with('/service/foo/statistics', params={ + 'today_only': today_only, 'limit_days': limit_days + }) def test_client_only_updates_allowed_attributes(mocker): diff --git a/tests/conftest.py b/tests/conftest.py index eba37a8c0..dc642a0b5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -523,7 +523,7 @@ def mock_get_international_service(mocker, api_user_active): @pytest.fixture(scope='function') def mock_get_service_statistics(mocker, api_user_active): - def _get(service_id, today_only): + def _get(service_id, today_only, limit_days=None): return { 'email': {'requested': 0, 'delivered': 0, 'failed': 0}, 'sms': {'requested': 0, 'delivered': 0, 'failed': 0},