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.
Here is how I solved my problem for now.
Migrations that are created with managed=False
option look like this:
# migrations/0001_initial.py
migrations.CreateModel(
name='MyModel',
fields=[
('field_id', models.IntegerField(primary_key=True, serialize=False)),
('slug', models.CharField(max_length=20, unique=True)),
('name', models.CharField(max_length=64)),
],
options={
'db_table': 'MyModel',
'managed': False,
},
),
One needs to comment out 'managed': False
to allow migrations to be applied. In order not to mess with actual migrations, I have created folder test_migrations
and copied there my migrations with 'managed': False
commented out:
# test_migrations/0001_initial.py
migrations.CreateModel(
name='MyModel',
fields=[
('field_id', models.IntegerField(primary_key=True, serialize=False)),
('slug', models.CharField(max_length=20, unique=True)),
('name', models.CharField(max_length=64)),
],
options={
'db_table': 'MyModel',
# 'managed': False,
},
),
Then we need to refer to these migrations during test run. To do that, I have created settings file test.py
and put there necessary references. Like this:
from web_services.settings.dev import *
MIGRATION_MODULES = {
'myapp': 'web_services.apps.myapp.test_migrations',
}
And when running tests, you need to refer to that settings:
python manage.py test --settings=web_services.settings.test