Skip to content

Commit 139518e

Browse files
authored
Merge pull request #1330 from maykinmedia/disable-django-admin-index-fixture-loading-during-testing
Disable loading of admin-index fixture in testing
2 parents 056f8e8 + a33cc07 commit 139518e

File tree

4 files changed

+40
-34
lines changed

4 files changed

+40
-34
lines changed

src/open_inwoner/accounts/apps.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from io import StringIO
23

34
from django.apps import AppConfig, apps
@@ -6,10 +7,18 @@
67
from django.core.management import call_command
78
from django.db.models.signals import post_migrate
89

10+
logger = logging.getLogger(__name__)
11+
912

1013
def update_admin_index(sender=None, **kwargs) -> bool:
1114
if "django_admin_index" not in settings.INSTALLED_APPS:
12-
print("django_admin_index not installed, skipping update_admin_index()")
15+
logger.info("django_admin_index not installed, skipping update_admin_index()")
16+
return False
17+
18+
if getattr(settings, "SKIP_ADMIN_INDEX_FIXTURE", False):
19+
logger.info(
20+
"SKIP_ADMIN_INDEX_FIXTURE is set to True, skipping update_admin_index()"
21+
)
1322
return False
1423

1524
from django_admin_index.models import AppGroup
@@ -32,18 +41,21 @@ def update_admin_index(sender=None, **kwargs) -> bool:
3241
try:
3342
call_command("loaddata", "django-admin-index", verbosity=0, stdout=StringIO())
3443
except Exception as exc:
35-
print(f"Error: Unable to load django-admin-index fixture ({exc})")
44+
error_message = f"Error: Unable to load django-admin-index fixture ({exc})"
45+
3646
if ct_create_exceptions:
3747
ct_exc = ExceptionGroup(
3848
"Unable to create contenttypes", *ct_create_exceptions
3949
)
40-
print(
50+
error_message += (
4151
"NOTE: this may be a consequence of being unable to create the following "
4252
f"contenttypes: {ct_exc}"
4353
)
54+
55+
logger.error(error_message)
4456
return False
4557
else:
46-
print("Successfully loaded django-admin-index fixture")
58+
logger.info("Successfully loaded django-admin-index fixture")
4759
return True
4860

4961

src/open_inwoner/accounts/tests/test_admin_index_fixture.py

-28
This file was deleted.

src/open_inwoner/conf/ci.py

+4
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,7 @@
7373
PASSWORD_HASHERS = [
7474
"django.contrib.auth.hashers.MD5PasswordHasher",
7575
]
76+
77+
# Sip the auto-loading of the django-admin-index fixture on startup.
78+
# It doesn't add anything in CI, and just adds time to the run.
79+
SKIP_ADMIN_INDEX_FIXTURE = True

src/open_inwoner/conf/tests/test_fixtures.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from django.core.management import call_command
2-
from django.test import TestCase
2+
from django.db.models.signals import post_migrate
3+
from django.test import TestCase, override_settings
34

45
from open_inwoner.accounts.apps import update_admin_index
56

67

78
class FixtureTests(TestCase):
89
def test_admin_index(self):
9-
self.assertTrue(update_admin_index())
10+
call_command("loaddata", "django-admin-index")
1011

1112
def test_cms_pages(self):
1213
# pass if this doesn't raise anything
@@ -23,3 +24,20 @@ def test_mail_editor(self):
2324
def test_profile_apphook_config(self):
2425
# pass if this doesn't raise anything
2526
call_command("loaddata", "profile_apphook_config")
27+
28+
29+
class AutoLoadDjangoAdminIndexFixtureTests(TestCase):
30+
def test_update_admin_index_hook_is_not_registered_if_skip_flag_is_unset(self):
31+
self.assertTrue(update_admin_index())
32+
33+
@override_settings(SKIP_ADMIN_INDEX_FIXTURE=False)
34+
def test_update_admin_index_hook_is_not_registered_if_skip_flag_is_false(self):
35+
self.assertTrue(update_admin_index())
36+
37+
@override_settings(SKIP_ADMIN_INDEX_FIXTURE=True)
38+
def test_update_admin_index_hook_is_registered_if_skip_flag_is_true(self):
39+
self.assertFalse(update_admin_index())
40+
41+
def test_update_admin_index_hook_is_registered(self):
42+
connected_functions = [receiver[1]() for receiver in post_migrate.receivers]
43+
self.assertIn(update_admin_index, connected_functions)

0 commit comments

Comments
 (0)