Testing Southern Migrations in Django

Does anyone know how to test migration after writing it? So many times in my DataMigrations, I found stupid subtle errors like True instead of False for the default value, incorrect denormalization, etc.

The default convention is to start the migration using numbers, so you cannot import them without using __import__ . Has anyone come up with a similar problem? How do people solve them?

The most obvious approach would be to keep the migration logic in a separate imported module and verify this, but this is somewhat awkward.

+6
source share
2 answers

I came across the same problem. Since I did not find a way to do tests for datamigrations, I used assertions to detect corrupted 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 

Well, that’s a little awkward. But it seems to work.

[Edit] Once again thinking about this, I found a much better solution: put the tests in DataMigration itself. Since the transition is a one-time code, it does not need to be tested again and again.

 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 
+2
source

I am new to the South, but several times when I used it, I also used unit tests, and then. / manage.py test also performed migrations, this would already have found many errors.

However, this probably does not work in all cases (I think there is no data in the test database when these migrations are performed).

0
source

Source: https://habr.com/ru/post/905589/


All Articles