Package: django-test-data-migrations
Django Data Migration TestTest utilities for easily testing Django data migrationsInstallationpip install django-test-data-migrationsUsageDefine the following functions in your migration filedata_forward(*args)data_backward(*args)(optional)fromdjango_test_data_migrationsimportDataMigrationsTestCaseBasefromapp_a.modelsimportAnimalclassYourDataMigrationTestCase(DataMigrationsTestCaseBase):deftest__forward_migration__something_important(self):# Prepare some data# Runself.data_forward(some_arg_0,some_arg_1,...)# Some assertionsdeftest__backward_migration__something_important(self):# Prepare some data# Runself.data_backward(some_arg_0,some_arg_1,...)# Some assertionsExampleSay you have a simple Django project with following general structuretest_project/
└──app_a├──apps.py├──__init__.py├──migrations│├──0001_initial.py│├──0002_datafix_addsuffixtoname.py│└──__init__.py├──models.py└──tests├──__init__.py└──test_0002_datafix_addsuffixtoname.pywith the following modelfromdjango.dbimportmodelsclassAnimal(models.Model):species=models.CharField(blank=False,null=False,max_length=50)name=models.CharField(blank=False,null=False,max_length=100)def__str__(self):returnf"Animal [name={self.name}, species={self.species}]"along with the following migration# app_a/migrations/0002_datafix_addsuffixtoname.pyfromdjango.dbimportmigrationsSUFFIX=" ZZ"defdata_forward(Animal):foranimalinAnimal.objects.all():animal.name+=SUFFIXanimal.save()defdata_backward(Animal):foranimalinAnimal.objects.filter(name__endswith=SUFFIX):animal.name=animal.name.rstrip(SUFFIX)animal.save()defforward(apps,schema_editor):Animal=apps.get_model("app_a","Animal")data_forward(Animal)defbackward(apps,schema_editor):Animal=apps.get_model("app_a","Animal")data_backward(Animal)classMigration(migrations.Migration):dependencies=[('app_a','0001_initial'),]operations=[migrations.RunPython(forward,backward),]You can test it as the followingfromdjango_test_data_migrationsimportDataMigrationsTestCaseBasefromapp_a.modelsimportAnimalclassDataMigrationsTestCase(DataMigrationsTestCaseBase):app_name="app_a"migration_name="0002_datafix_addsuffixtoname"deftest__data_forward__append_suffix_to_name(self):# Prepare data before migrationdog=Animal.objects.create(name="Dog",species="dog")cat=Animal.objects.create(name="Cat",species="cat")# Run `data_forward` aka the entry point to your data migrationself.data_forward(Animal)# Make your assertionsself.assertEqual(Animal.objects.get(id=dog.id).name,"Dog ZZ")self.assertEqual(Animal.objects.get(id=cat.id).name,"Cat ZZ")deftest__data_backward__append_suffix_to_name(self):dog=Animal.objects.create(name="Dog ZZ",species="dog")cat=Animal.objects.create(name="Cat zz",species="cat")self.data_backward(Animal)self.assertEqual(Animal.objects.get(id=dog.id).name,"Dog")self.assertEqual(Animal.objects.get(id=cat.id).name,"Cat zz")Why would you need this library?It runs your data migration test very fast.It encourages developers to write data-related Django migrations separately from model definition related Django migrationsWriting tests for data related migrations is extremely important, but it's either tricky to do or very run-time consuming. This library intends to enable testing data migrations easy and fastDevelopmentSetupCheck requirementspoetry --versionClone source code repositorygitclonegit@github.com:imranariffin/django-test-data-migrations.gitInstall dev dependenciespoetryinstallRun testsmaketestYou should be ready to start developmentLinksPyPi