I have django application with 5 legacy databases. Almost all models are set with the meta attribute managed=False
. Since managed=False
is set, migrations for each model have been created with the option managed=False
. And since, django test runner picks existing migrations for each model to create test tables in test_databases, it simply doesn't create anything.
I tried creating test.py
settings file with the following workarounds:
from web_services.settings.dev import *
from django.test.runner import DiscoverRunner
class UnManagedModelTestRunner(DiscoverRunner):
def setup_test_environment(self, *args, **kwargs):
from django.apps import apps
self.unmanaged_models = [m for m in apps.get_models() if not m._meta.managed]
for m in self.unmanaged_models:
m._meta.managed = True
super(UnManagedModelTestRunner, self).setup_test_environment(*args, **kwargs)
def teardown_test_environment(self, *args, **kwargs):
super(UnManagedModelTestRunner, self).teardown_test_environment(*args, **kwargs)
# reset unmanaged models
for m in self.unmanaged_models:
m._meta.managed = False
TEST_RUNNER = 'web_services.settings.test.UnManagedModelTestRunner'
and running python manage.py test --settings=web_services.settings.test
. However, it didn't help, since anyway - existing migrations had been already created with managed=False
option. Tests seem to be working only after I comment out in my models managed=False
, deleted old migrations, created new ones (without managed=False
option).
With this, I am quite lost - what is actually a good practice to write tests in my case (multiple legacy databases)? It seems wrong to deal with that hassle of adjusting migrations.