Files
notifications-admin/tests/app/notify_client/test_status_api_client.py
David McDonald 20cc1e230f Reduce TTL to 1 hour for get_count_of_live_services_and_organisations
This stat is shown on a few of our pages, such as our homepage,
the performance page and also a platform admin page
and is currently catched for the
default TTL of 1 week. I think there is no reason we can't make
this only cache once an hour and give slightly more up to date stats
which will update more regularly.

This mimics the approach and also the TTL choice of 1 hour that has
been added for the performance page (although there is no
particular bug here to fix, it is just nice to have slightly more up
up to date data).

Note, the API call only takes about 0.3 seconds at the moment
so it is not particularly intensive on the DB to run this more
regularly.
2021-12-08 15:39:10 +00:00

60 lines
1.9 KiB
Python

from app.notify_client.status_api_client import StatusApiClient
def test_get_count_of_live_services_and_organisations(mocker):
mocker.patch('app.extensions.RedisClient.get', return_value=None)
client = StatusApiClient()
mock = mocker.patch.object(client, 'get', return_value={})
client.get_count_of_live_services_and_organisations()
mock.assert_called_once_with(url='/_status/live-service-and-organisation-counts')
def test_sets_value_in_cache(mocker):
client = StatusApiClient()
mock_redis_get = mocker.patch(
'app.extensions.RedisClient.get',
return_value=None
)
mock_api_get = mocker.patch(
'app.notify_client.NotifyAdminAPIClient.get',
return_value={'data_from': 'api'},
)
mock_redis_set = mocker.patch(
'app.extensions.RedisClient.set',
)
assert client.get_count_of_live_services_and_organisations() == {'data_from': 'api'}
mock_redis_get.assert_called_once_with('live-service-and-organisation-counts')
mock_api_get.assert_called_once_with(url='/_status/live-service-and-organisation-counts')
mock_redis_set.assert_called_once_with(
'live-service-and-organisation-counts',
'{"data_from": "api"}',
ex=3600
)
def test_returns_value_from_cache(mocker):
client = StatusApiClient()
mock_redis_get = mocker.patch(
'app.extensions.RedisClient.get',
return_value=b'{"data_from": "cache"}',
)
mock_api_get = mocker.patch(
'app.notify_client.NotifyAdminAPIClient.get',
)
mock_redis_set = mocker.patch(
'app.extensions.RedisClient.set',
)
assert client.get_count_of_live_services_and_organisations() == {'data_from': 'cache'}
mock_redis_get.assert_called_once_with('live-service-and-organisation-counts')
assert mock_api_get.called is False
assert mock_redis_set.called is False