Pieter
Pieter

Reputation: 923

Writing automated tests for data migrations

Is there a straightforward way to unit test data migrations in Django?

Let's say I have a CharField with fixed choices like London and Tokio, but I want to replace the enum with a foreign key to a dedicated model so that I can store additional information about each city (e.g. the country).

My approach to writing the migration would be:

But how would you test this with TestCase?

Upvotes: 2

Views: 77

Answers (1)

Alex Morozov
Alex Morozov

Reputation: 5993

It's surely doable. I think you could make use of the tools Django tests itself with: MigrationExecutor and MigrationTestBase. Here's a sample of how to invoke migrations in your tests.

Upvotes: 2

Related Questions