Make function-level setup fixture clear cache

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.
This commit is contained in:
Chris Hill-Scott
2020-12-31 09:36:55 +00:00
parent 55afc9a401
commit 624bd1d12e

View File

@@ -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(