From 2a1e060907af4435ee6ae3ba97ee5b7220d0386f Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Mon, 4 Mar 2019 16:18:39 -0400 Subject: [PATCH] TestModelMixin: Perform stateless model creation Don't delete test models at the end of the test case. Failed test cases don't execute the tearDown() method. Instead perform model registry cleanup before creating any new test model. Signed-off-by: Roberto Rosario --- mayan/apps/common/tests/mixins.py | 46 ++++++++++++------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/mayan/apps/common/tests/mixins.py b/mayan/apps/common/tests/mixins.py index 9a83f423e0..9898d38d63 100644 --- a/mayan/apps/common/tests/mixins.py +++ b/mayan/apps/common/tests/mixins.py @@ -234,13 +234,24 @@ class TempfileCheckTestCaseMixin(object): class TestModelTestMixin(object): - _test_models = [] - def _create_test_model(self, fields=None, model_name='TestModel', options=None): + # Obtain the app_config and app_label from the test's module path + app_config = apps.get_containing_app_config( + object_name=self.__class__.__module__ + ) + app_label = app_config.label + class Meta: pass + setattr(Meta, 'app_label', app_label) + + if options is not None: + for key, value in options.items(): + setattr(Meta, key, value) + def save(instance, *args, **kwargs): + # Custom .save() method to use random primary key values. if instance.pk: return models.Model.self(instance, *args, **kwargs) else: @@ -251,10 +262,6 @@ class TestModelTestMixin(object): return instance.save_base(force_insert=True) - if options is not None: - for key, value in options.items(): - setattr(Meta, key, value) - attrs = { '__module__': self.__class__.__module__, 'save': save, 'Meta': Meta } @@ -262,6 +269,11 @@ class TestModelTestMixin(object): if fields: attrs.update(fields) + # Clear previous model registration before re-registering it again to + # avoid conflict with test models with the same name, in the same app + # but from another test module. + apps.all_models[app_label].pop(model_name.lower(), None) + TestModel = type( force_bytes(model_name), (models.Model,), attrs ) @@ -271,8 +283,6 @@ class TestModelTestMixin(object): with connection.schema_editor() as schema_editor: schema_editor.create_model(model=TestModel) - self.__class__._test_models.append(model_name) - apps.clear_cache() ContentType.objects.clear_cache() def _create_test_object(self, model_name='TestModel', **kwargs): @@ -280,26 +290,6 @@ class TestModelTestMixin(object): self.test_object = TestModel.objects.create(**kwargs) - def _delete_test_model(self, model_name='TestModel'): - TestModel = getattr(self, model_name) - - with connection.schema_editor() as schema_editor: - schema_editor.delete_model(model=TestModel) - - del TestModel._meta.app_config.models[model_name.lower()] - apps.clear_cache() - ContentType.objects.clear_cache() - - def _delete_test_models(self): - for test_model in self.__class__._test_models: - self._delete_test_model(model_name=test_model) - - self.__class__._test_models = [] - - def tearDown(self): - self._delete_test_models() - super(TestModelTestMixin, self).tearDown() - class TestViewTestCaseMixin(object): has_test_view = False