Skip to content

Commit

Permalink
Fix create_perm in apps.py to use database alias given by the post_mi…
Browse files Browse the repository at this point in the history
…grate signal
  • Loading branch information
rlaszlo committed Feb 5, 2017
1 parent 61d32d2 commit 3ba4780
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
6 changes: 3 additions & 3 deletions constance/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def ready(self):
signals.post_migrate.connect(self.create_perm,
dispatch_uid='constance.create_perm')

def create_perm(self, *args, **kwargs):
def create_perm(self, using=None, *args, **kwargs):
"""
Creates a fake content type and permission
to be able to check for permissions
Expand All @@ -21,12 +21,12 @@ def create_perm(self, *args, **kwargs):
from django.contrib.contenttypes.models import ContentType

if ContentType._meta.installed and Permission._meta.installed:
content_type, created = ContentType.objects.get_or_create(
content_type, created = ContentType.objects.using(using).get_or_create(
app_label='constance',
model='config',
)

permission, created = Permission.objects.get_or_create(
permission, created = Permission.objects.using(using).get_or_create(
name='Can change config',
content_type=content_type,
codename='change_config')
4 changes: 4 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:',
},
'secondary': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:',
}
}

Expand Down
46 changes: 46 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from django.apps import apps
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.db.models import signals
from django.test import TestCase


class TestApp(TestCase):
def setUp(self):
self.app_config = apps.get_app_config('constance')

def test_post_migrate_signal_creates_content_type_and_permission_in_default_database(self):
self.assert_uses_correct_database('default')

def test_post_migrate_signal_creates_content_type_and_permission_in_secondary_database(self):
self.assert_uses_correct_database('secondary')

def test_uses_default_db_even_without_giving_using_keyword(self):
self.call_post_migrate(None)

self.assert_content_type_and_permission_created('default')

def assert_uses_correct_database(self, database_name):
self.call_post_migrate(database_name)

self.assert_content_type_and_permission_created(database_name)

def assert_content_type_and_permission_created(self, database_name):
content_type_queryset = ContentType.objects.filter(app_label=self.app_config.name) \
.using(database_name)

self.assertTrue(content_type_queryset.exists())

permission_queryset = Permission.objects.filter(content_type=content_type_queryset.get()) \
.using(database_name).exists()

self.assertTrue(permission_queryset)

def call_post_migrate(self, database_name):
signals.post_migrate.send(
sender=self.app_config,
app_config=self.app_config,
verbosity=None,
interactive=None,
using=database_name
)

0 comments on commit 3ba4780

Please sign in to comment.