From 707950ad3075266d2aa7a31140c718d9de64ac82 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Sat, 16 Nov 2019 21:22:40 -0400 Subject: [PATCH] Cleanup test models when tests ends Signed-off-by: Roberto Rosario --- mayan/apps/common/tests/mixins.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/mayan/apps/common/tests/mixins.py b/mayan/apps/common/tests/mixins.py index 0c611a7639..4d1a318b21 100644 --- a/mayan/apps/common/tests/mixins.py +++ b/mayan/apps/common/tests/mixins.py @@ -21,6 +21,7 @@ from django.urls import clear_url_caches, reverse from django.utils.encoding import force_bytes from django.utils.six import PY3 +from mayan.apps.acls.classes import ModelPermission from mayan.apps.storage.settings import setting_temporary_directory from .literals import ( @@ -321,6 +322,21 @@ class TempfileCheckTestCasekMixin(object): class TestModelTestMixin(object): + _test_models = [] + + def tearDown(self): + # Delete the test models' content type entries and deregister the + # permissions, 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: + content_type = ContentType.objects.get_for_model(model=model) + if content_type.pk: + content_type.delete() + ModelPermission.deregister(model=model) + + super(TestModelTestMixin, self).tearDown() + def _get_test_model_meta(self): self.db_table = '{}_{}'.format( self.app_config.label, self.model_name.lower() @@ -395,6 +411,7 @@ class TestModelTestMixin(object): with connection.schema_editor() as schema_editor: schema_editor.create_model(model=model) + self._test_models.append(model) ContentType.objects.clear_cache() return model