From 624bd1d12e27ca98e74fe492bada7285c1f07002 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Thu, 31 Dec 2020 09:36:55 +0000 Subject: [PATCH] Make function-level setup fixture clear cache MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This means that anyone adding a new test to this file doesn’t have to remember to clear the cache in their test, or forget to and have a hard-to-debug test failure. Using `setup_function` means we don’t have to convert this module into using class-based tests. --- tests/app/delivery/test_send_to_providers.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/app/delivery/test_send_to_providers.py b/tests/app/delivery/test_send_to_providers.py index ffde4d39b..4ddfa0563 100644 --- a/tests/app/delivery/test_send_to_providers.py +++ b/tests/app/delivery/test_send_to_providers.py @@ -34,6 +34,12 @@ from tests.app.db import ( ) +def setup_function(_function): + # pytest will run this function before each test. It makes sure the + # state of the cache is not shared between tests. + send_to_providers.provider_cache.clear() + + def test_provider_to_use_should_return_random_provider(mocker, notify_db_session): mmg = get_provider_details_by_identifier('mmg') firetext = get_provider_details_by_identifier('firetext') @@ -53,8 +59,6 @@ def test_provider_to_use_should_cache_repeated_calls(mocker, notify_db_session): wraps=send_to_providers.random.choices, ) - send_to_providers.provider_cache.clear() - results = [ send_to_providers.provider_to_use('sms', international=False) for _ in range(10) @@ -90,8 +94,6 @@ def test_provider_to_use_raises_if_no_active_providers(mocker, restore_provider_ mmg = get_provider_details_by_identifier('mmg') mmg.active = False - send_to_providers.provider_cache.clear() - with pytest.raises(Exception): send_to_providers.provider_to_use('sms', international=True) @@ -643,7 +645,6 @@ def test_should_send_sms_to_international_providers( international=True, reply_to_text=sample_template.service.get_default_sms_sender() ) - send_to_providers.provider_cache.clear() send_to_providers.send_sms_to_provider( notification_uk ) @@ -694,7 +695,6 @@ def test_should_handle_sms_sender_and_prefix_message( template = create_template(service, content='bar') notification = create_notification(template, reply_to_text=sms_sender) - send_to_providers.provider_cache.clear() send_to_providers.send_sms_to_provider(notification) mmg_client.send_sms.assert_called_once_with(