Skip to content

Commit 78946de

Browse files
committed
test: fix flaky tests
- disable parler cache - clear notifications registry in teardown - move autouse settings to settings - add missing settings fixture - mark django_db explicitly Refs: KEH-282
1 parent c24087b commit 78946de

4 files changed

Lines changed: 17 additions & 11 deletions

File tree

tests/conftest.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@
1010

1111

1212
@pytest.fixture(autouse=True)
13-
def autouse_django_db(db):
14-
pass
15-
16-
17-
@pytest.fixture(autouse=True)
18-
def force_settings(settings):
19-
settings.LANGUAGES = (("fi", "Finnish"), ("en", "English"))
20-
settings.LANGUAGE_CODE = "en"
13+
def clear_notification_registry():
14+
yield
15+
notifications.registry = {}
2116

2217

2318
@pytest.fixture

tests/settings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,8 @@
4242
USE_TZ = True
4343
SECRET_KEY = "sekret"
4444
LANGUAGES = (("fi", "Finnish"), ("en", "English"), ("sv", "Swedish"))
45+
LANGUAGE_CODE = "en"
46+
47+
# This one is very important, otherwise you get flaky tests because the cache
48+
# leaks between tests.
49+
PARLER_ENABLE_CACHING = False

tests/test_notification_queries.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django_ilmoitin.utils import render_preview
77

88

9+
@pytest.mark.django_db
910
def test_query_notification_templates(graphql_client, notification_template):
1011
query = """
1112
{

tests/test_notifications.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pytest
2-
from django.conf import settings
32
from django.core import mail
43
from mailer.engine import send_all
54
from mailer.models import Message
@@ -8,6 +7,7 @@
87
from django_ilmoitin.utils import render_notification_template, send_notification
98

109

10+
@pytest.mark.django_db
1111
def test_notification_template_rendering(notification_template):
1212
context = {
1313
"extra_var": "foo",
@@ -31,6 +31,7 @@ def test_notification_template_rendering(notification_template):
3131
assert rendered.body_text == "testitekstiruumis, muuttujan arvo: text_baz!"
3232

3333

34+
@pytest.mark.django_db
3435
def test_notification_template_rendering_no_body_text_provided(notification_template):
3536
context = {
3637
"extra_var": "foo",
@@ -60,6 +61,7 @@ def test_notification_template_rendering_no_body_text_provided(notification_temp
6061
assert rendered.body_text == "testihötömölöruumis, muuttujan arvo: html_baz!"
6162

6263

64+
@pytest.mark.django_db
6365
def test_undefined_rendering_context_variable(notification_template):
6466
context = {"extra_var": "foo", "subject_var": "bar", "body_text_var": "baz"}
6567

@@ -70,7 +72,8 @@ def test_undefined_rendering_context_variable(notification_template):
7072
assert "'body_html_var' is undefined" in str(e.value)
7173

7274

73-
def test_notification_sending(notification_template):
75+
@pytest.mark.django_db
76+
def test_notification_sending(notification_template, settings):
7477
context = {
7578
"extra_var": "foo",
7679
"subject_var": "bar",
@@ -97,6 +100,7 @@ def test_notification_sending(notification_template):
97100

98101

99102
@pytest.mark.parametrize("language", ["fi", "en"])
103+
@pytest.mark.django_db
100104
def test_translated_from_email(notification_template, settings, language):
101105
context = {
102106
"extra_var": "foo",
@@ -115,7 +119,8 @@ def test_translated_from_email(notification_template, settings, language):
115119
)
116120

117121

118-
def test_notification_delayed_sending(notification_template):
122+
@pytest.mark.django_db
123+
def test_notification_delayed_sending(notification_template, settings):
119124
context = {
120125
"extra_var": "foo",
121126
"subject_var": "bar",

0 commit comments

Comments
 (0)