Interesting!

I came up with the following today, but calling the 'migrate' command via 
'call_command' (like in Sean's code) is probably cleaner?!


"""
Test (data) migrations in Django.

This uses py.test/pytest-django (the `transactional_db` fixture comes from 
there),
but could be easily adopted for Django's testrunner:

    from django.test.testcases import TransactionTestCase

    class FooTestcase(TransactionTestCase):
        def test_with_django(self):
        …

This example tests that some fields are properly migrated from a `Profile` model
to `User`.
"""

from django.db import connection
from django.db.migrations.executor import MigrationExecutor


def test_migrate_profile_to_user(transactional_db):
    executor = MigrationExecutor(connection)
    app = "YOUR_APP"
    migrate_from = [(app, "000X_before")]
    migrate_to = [(app, "000X_after")]

    executor.migrate(migrate_from)
    old_apps = executor.loader.project_state(migrate_from).apps

    # Create some old data.
    Profile = old_apps.get_model(app, "Profile")
    old_profile = Profile.objects.create(email="email",
                                         firstname="firstname",
                                         lastname="lastname")
    # Migrate forwards.
    executor.loader.build_graph()  # reload.
    executor.migrate(migrate_to)
    new_apps = executor.loader.project_state(migrate_to).apps

    # Test the new data.
    Profile = new_apps.get_model(app, "Profile")
    User = new_apps.get_model(app, "UserEntry")
    assert 'firstname' not in Profile._meta.get_all_field_names()

    user = User.objects.get(email='email')
    profile = Profile.objects.get(user__email='email')
    assert user.profile.pk == old_profile.pk == profile.pk
    assert profile.user.email == 'email'
    assert profile.user.first_name == 'firstname'
    assert profile.user.last_name == 'lastname'
 


https://gist.github.com/blueyed/4fb0a807104551f103e6


Cheers,
Daniel.

Am Donnerstag, 7. Mai 2015 12:02:12 UTC+2 schrieb Tom Linford:
>
> At Robinhood, we've been using a custom in-house MigrationTestCase for 
> testing migrations that we'd like to contribute, but want to check the API 
> of it before contributing it. Here's the definition of the class:
>
> class MigrationTestCase(TransactionTestCase):
>     """
>     app_label: name of app (ie. "users" or "polls")
>     (start|dest)_migration_name: name of migration in app
>         (e.g. "0001_initial")
>     additional_dependencies: list of tuples of `(app_label, 
> migration_name)`.
>         Add any additional migrations here that need to be included in the
>         generation of the model states.
>
>     Usage:
>
>     class TestCase(MigrationTestCase):
>         app_label = ...
>         start_migration_name = ...
>         dest_migration_name = ...
>         additional_dependencies = ...
>
>         def setUp(self):
>             # Create models with regular orm
>             super(TestCase, self).setUp()
>             # Create models with start orm. Access model with:
>             # self.start_models["<app_label>"]["<model_name>"]
>             # Note that model_name must be all lower case, you can just do:
>             # <instance>._meta.model_name to get the model_name
>
>         def test(self):
>             # Still using start orm
>             self.migrate_to_dest()
>             # Now, you can access dest models with:
>             # self.dest_models["<app_label>"]["<model_name>"]
>     """
>     app_label = None
>     start_migration_name = None
>     dest_migration_name = None
>     additional_dependencies = []
>
>
> Let me know if this API is agreeable and I can make a PR for this feature.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/10e51c46-3aff-4ee5-bbb0-889112c6a2b5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to