Reputation: 923
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:
ForeignKey
fieldForeignKey
field that reflects the value of the CharField
ForeignKey
non-nullableCharField
But how would you test this with TestCase
?
Upvotes: 2
Views: 77
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