From 1aa58f90a149d7de7813dcf45fe8c1fb04a63143 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Mon, 9 Jan 2017 13:30:23 +0100 Subject: tests: force managing of unmanage tables --- ishtar_common/tests.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'ishtar_common/tests.py') diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index a00f8a174..6db425cab 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -29,6 +29,7 @@ from django.core.urlresolvers import reverse from django.template.defaultfilters import slugify from django.test import TestCase from django.test.client import Client +from django.test.simple import DjangoTestSuiteRunner from ishtar_common import models @@ -101,6 +102,29 @@ class WizardTestFormData(object): test(test_object, final_step_response) +class ManagedModelTestRunner(DjangoTestSuiteRunner): + """ + Test runner that automatically makes all unmanaged models in your Django + project managed for the duration of the test run, so that one doesn't need + to execute the SQL manually to create them. + """ + def setup_test_environment(self, *args, **kwargs): + from django.db.models.loading import get_models + self.unmanaged_models = [m for m in get_models() + if not m._meta.managed] + for m in self.unmanaged_models: + m._meta.managed = True + super(ManagedModelTestRunner, self).setup_test_environment(*args, + **kwargs) + + def teardown_test_environment(self, *args, **kwargs): + super(ManagedModelTestRunner, self).teardown_test_environment(*args, + **kwargs) + # reset unmanaged models + for m in self.unmanaged_models: + m._meta.managed = False + + class WizardTest(object): url_name = None wizard_name = '' -- cgit v1.2.3