Skip to content

Commit

Permalink
disable loading of admin-index fixture in testing
Browse files Browse the repository at this point in the history
  • Loading branch information
swrichards committed Aug 1, 2024
1 parent 451e3b4 commit 16a6221
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
20 changes: 16 additions & 4 deletions src/open_inwoner/accounts/apps.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from io import StringIO

from django.apps import AppConfig, apps
Expand All @@ -6,10 +7,18 @@
from django.core.management import call_command
from django.db.models.signals import post_migrate

logger = logging.getLogger(__name__)


def update_admin_index(sender=None, **kwargs) -> bool:
if "django_admin_index" not in settings.INSTALLED_APPS:
print("django_admin_index not installed, skipping update_admin_index()")
logger.info("django_admin_index not installed, skipping update_admin_index()")
return False

if not settings.LOAD_ADMIN_INDEX_FIXTURE_ON_STARTUP:
logger.info(
"LOAD_ADMIN_INDEX_FIXTURE_ON_STARTUP is set to False, skipping update_admin_index()"
)
return False

from django_admin_index.models import AppGroup
Expand All @@ -32,18 +41,21 @@ def update_admin_index(sender=None, **kwargs) -> bool:
try:
call_command("loaddata", "django-admin-index", verbosity=0, stdout=StringIO())
except Exception as exc:
print(f"Error: Unable to load django-admin-index fixture ({exc})")
error_message = f"Error: Unable to load django-admin-index fixture ({exc})"

if ct_create_exceptions:
ct_exc = ExceptionGroup(
"Unable to create contenttypes", *ct_create_exceptions
)
print(
error_message += (
"NOTE: this may be a consequence of being unable to create the following "
f"contenttypes: {ct_exc}"
)

logger.error(error_message)
return False
else:
print("Successfully loaded django-admin-index fixture")
logger.info("Successfully loaded django-admin-index fixture")
return True


Expand Down
28 changes: 0 additions & 28 deletions src/open_inwoner/accounts/tests/test_admin_index_fixture.py

This file was deleted.

3 changes: 3 additions & 0 deletions src/open_inwoner/conf/ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,6 @@
PASSWORD_HASHERS = [
"django.contrib.auth.hashers.MD5PasswordHasher",
]

# No need for this in CI, it just slows things down
LOAD_ADMIN_INDEX_FIXTURE_ON_STARTUP = False
19 changes: 17 additions & 2 deletions src/open_inwoner/conf/tests/test_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from django.core.management import call_command
from django.test import TestCase
from django.db.models.signals import post_migrate
from django.test import TestCase, override_settings

from open_inwoner.accounts.apps import update_admin_index


class FixtureTests(TestCase):
def test_admin_index(self):
self.assertTrue(update_admin_index())
call_command("loaddata", "django-admin-index")

def test_cms_pages(self):
# pass if this doesn't raise anything
Expand All @@ -23,3 +24,17 @@ def test_mail_editor(self):
def test_profile_apphook_config(self):
# pass if this doesn't raise anything
call_command("loaddata", "profile_apphook_config")


class AutoLoadDjangoAdminIndexFixtureTests(TestCase):
@override_settings(LOAD_ADMIN_INDEX_FIXTURE_ON_STARTUP=False)
def test_update_admin_index_hook_is_not_registered_if_flag_is_unset(self):
self.assertFalse(update_admin_index())

@override_settings(LOAD_ADMIN_INDEX_FIXTURE_ON_STARTUP=True)
def test_update_admin_index_hook_is_registered_if_flag_is_set(self):
self.assertTrue(update_admin_index())

def test_update_admin_index_hook_is_registered(self):
connected_functions = [receiver[1]() for receiver in post_migrate.receivers]
self.assertIn(update_admin_index, connected_functions)

0 comments on commit 16a6221

Please sign in to comment.