Django Data Migration Test
Test utilities for easily testing Django data migrations
Installation
pip install django-test-data-migrations
Usage
Define the following functions in your migration file
data_forward(*args)data_backward(*args)(optional)
from django_test_data_migrations import DataMigrationsTestCaseBase
from app_a.models import Animal
class YourDataMigrationTestCase(DataMigrationsTestCaseBase):
def test__forward_migration__something_important(self):
# Prepare some data
# Run
self.data_forward(some_arg_0, some_arg_1, ...)
# Some assertions
def test__backward_migration__something_important(self):
# Prepare some data
# Run
self.data_backward(some_arg_0, some_arg_1, ...)
# Some assertions
Example
Say you have a simple Django project with following general structure
test_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.py
with the following model
from django.db import models
class Animal(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):
return f"Animal [name={self.name}, species={self.species}]"
along with the following migration
# app_a/migrations/0002_datafix_addsuffixtoname.py
from django.db import migrations
SUFFIX = " ZZ"
def data_forward(Animal):
for animal in Animal.objects.all():
animal.name += SUFFIX
animal.save()
def data_backward(Animal):
for animal in Animal.objects.filter(name__endswith=SUFFIX):
animal.name = animal.name.rstrip(SUFFIX)
animal.save()
def forward(apps, schema_editor):
Animal = apps.get_model("app_a", "Animal")
data_forward(Animal)
def backward(apps, schema_editor):
Animal = apps.get_model("app_a", "Animal")
data_backward(Animal)
class Migration(migrations.Migration):
dependencies = [
('app_a', '0001_initial'),
]
operations = [
migrations.RunPython(forward, backward),
]
You can test it as the following
from django_test_data_migrations import DataMigrationsTestCaseBase
from app_a.models import Animal
class DataMigrationsTestCase(DataMigrationsTestCaseBase):
app_name = "app_a"
migration_name = "0002_datafix_addsuffixtoname"
def test__data_forward__append_suffix_to_name(self):
# Prepare data before migration
dog = 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 migration
self.data_forward(Animal)
# Make your assertions
self.assertEqual(Animal.objects.get(id=dog.id).name, "Dog ZZ")
self.assertEqual(Animal.objects.get(id=cat.id).name, "Cat ZZ")
def test__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 migrations
- Writing 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 fast
Development
Setup
Check requirements
poetry --version
Clone source code repository
git clone git@github.com:imranariffin/django-test-data-migrations.git
Install dev dependencies
poetry install
Run tests
make test
You should be ready to start development
Links
Metadata
Release files for django-test-data-migrations 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-test-data-migrations-0.1.2.tar.gz | 4.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_test_data_migrations-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.2 kB
Release files / django-test-data-migrations-0.1.2.tar.gz
| Download URL | django-test-data-migrations-0.1.2.tar.gz |
|---|---|
| Size | 4.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4d02a14c8d44c9f111021b224d0e8463d06d8168db066ad3df6fc2d3eae6c47a
|
|
BLAKE2b-256 checksum How to use checksums |
81bb2bd7f23f28fa0a0fa88432aff3650819727cc722f78852a5d8fcaf920c8a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6
|
Release files / django_test_data_migrations-0.1.2-py3-none-any.whl
| Download URL | django_test_data_migrations-0.1.2-py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a1d8d270c26c2fb319877eb62646fd582e45a0bc6412729f45873a0fce371c76
|
|
BLAKE2b-256 checksum How to use checksums |
f41833062bbd18a62324bfdd59b472bc56eb95c714ac4ce80ca2ab6b24ed351d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6
|