Deregister ephimeral models from ModelPermissions
Explictly delete the ephimeral models from ModelPermission registry to avoid them being lookedup in a subsequent tests where they don't exist. Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
@@ -12,6 +12,12 @@ class ModelPermission(object):
|
||||
_inheritances = {}
|
||||
_registry = {}
|
||||
|
||||
@classmethod
|
||||
def deregister(cls, model):
|
||||
cls._registry.pop(model, None)
|
||||
# TODO: Find method to revert the add_to_class('acls'...)
|
||||
# delattr doesn't work.
|
||||
|
||||
@classmethod
|
||||
def register(cls, model, permissions):
|
||||
from django.contrib.contenttypes.fields import GenericRelation
|
||||
|
||||
@@ -36,10 +36,6 @@ class ACLTestMixin(PermissionTestMixin, RoleTestMixin, TestModelTestMixin):
|
||||
auto_create_test_role = True
|
||||
auto_create_test_object = False
|
||||
|
||||
def _create_test_acl(self):
|
||||
self.test_acl = AccessControlList.objects.create(
|
||||
content_object=self.test_object, role=self.test_role
|
||||
)
|
||||
|
||||
def setUp(self):
|
||||
super(ACLTestMixin, self).setUp()
|
||||
@@ -49,6 +45,22 @@ class ACLTestMixin(PermissionTestMixin, RoleTestMixin, TestModelTestMixin):
|
||||
if self.auto_create_test_object:
|
||||
self._setup_test_object()
|
||||
|
||||
def tearDown(self):
|
||||
# Deregister the permissions of the ephimeral test models
|
||||
# this avoids their Content Type from being looked up
|
||||
# in subsequent tests where they don't exists due to the database
|
||||
# transaction rollback.
|
||||
for model in self._test_models:
|
||||
ModelPermission.deregister(model=model)
|
||||
self._test_models.remove(model)
|
||||
|
||||
super(ACLTestMixin, self).tearDown()
|
||||
|
||||
def _create_test_acl(self):
|
||||
self.test_acl = AccessControlList.objects.create(
|
||||
content_object=self.test_object, role=self.test_role
|
||||
)
|
||||
|
||||
def _inject_test_object_content_type(self):
|
||||
self.test_object_content_type = ContentType.objects.get_for_model(
|
||||
model=self.test_object
|
||||
|
||||
@@ -5,7 +5,7 @@ from mayan.apps.common.tests import BaseTestCase
|
||||
from ..classes import ModelPermission
|
||||
|
||||
|
||||
class ModelpermissionTestCase(BaseTestCase):
|
||||
class ModelPermissionTestCase(BaseTestCase):
|
||||
def test_model_permission_get_classes_as_content_type(self):
|
||||
self.assertNotEqual(
|
||||
ModelPermission.get_classes(as_content_type=True).count(), 0
|
||||
|
||||
@@ -283,11 +283,13 @@ class TempfileCheckTestCasekMixin(object):
|
||||
|
||||
|
||||
class TestModelTestMixin(object):
|
||||
_test_models = []
|
||||
|
||||
def _create_test_model(self, fields=None, model_name='TestModel', options=None):
|
||||
|
||||
if connection.vendor == 'mysql':
|
||||
self.skipTest(
|
||||
reason='MySQL doesn\t support schema changes inside an '
|
||||
reason='MySQL doesn\'t support schema changes inside an '
|
||||
'atomic block.'
|
||||
)
|
||||
|
||||
@@ -340,6 +342,7 @@ class TestModelTestMixin(object):
|
||||
)
|
||||
|
||||
setattr(self, model_name, TestModel)
|
||||
self._test_models.append(TestModel)
|
||||
|
||||
with connection.schema_editor() as schema_editor:
|
||||
schema_editor.create_model(model=TestModel)
|
||||
|
||||
Reference in New Issue
Block a user