Reputation: 2124
Does anyone know a way to test the migration itself after writing it? Very many times in my DataMigrations I've found stupid subtle bugs, like True
instead of False
for the default value, wrong denormalizations, etc.
The default South convention is to start the migrations with numbers, so you can't even import them without using __import__
. Has anyone came up upon a similar problem? How do people solve them?
The most obvious approach would be to hold the migration logic in a separate imported module and test that, but that's somewhat clunky.
Upvotes: 6
Views: 545
Reputation: 3464
I stumbled into the same problem. Since I didn't find a way to do tests for datamigrations, I used assertions to detect corrupt data:
from django.conf import settings
class MyModel(models.Model):
stupid_error = models.BooleanField(default=False)
def __init__(self, *args, **kwargs):
super(MyModel, self).__init__(*args, **kwargs)
if settings.DEBUG:
assert not self.stupid_error
Ok, it's a bit clunky. But it seems to work.
[Edit] Thinking about it again, I found a much better solution: put the tests into the DataMigration itself. Since a migration is one-off code, it doesn't have to be tested over and over.
class Migration(DataMigration):
def forwards(self, orm):
# lots of awesome migration code here
# ...
for m in orm.MyModel.objects.all():
assert not m.stupid_error
Upvotes: 2
Reputation: 13053
I am fairly new to South, but the few times I used it, I used unit tests as well and then, ./manage.py test
executed the migrations as well, this would already find many bugs.
However, this probably does not work in all cases (I think there is no data in the test database when these migrations are executed).
Upvotes: 0