wemake-services / django-test-migrations

Test django schema and data migrations, including migrations' order and best practices.
https://pypi.org/project/django-test-migrations/
MIT License
517 stars 31 forks source link

Apply only specific app migrations #435

Closed reeson46 closed 5 months ago

reeson46 commented 6 months ago

Hello,

This is not an issue, but I have a question regarding when you run the test. I noticed when I ran it, in my backend logs, every migration from every app in my project is being applied, although in my test I only want to test 'users' app migrations. This takes the migration test over 2 minutes to complete. I'm not sure if that's normal, but is it possible to specify which app migrations I want to include for my test? Also, I noticed that my local database was cleared after I ran the migration test.

sobolevn commented 6 months ago

This sounds like something is off: please, see this example https://github.com/wemake-services/django-test-migrations?tab=readme-ov-file#pytest

It clearly specifies what apps and migrations it should use.

reeson46 commented 6 months ago

I followed the example, but when I ran the test, I saw in my backend logs that all migrations had been applied, not just from my 'users' app. Also, this test took 2:20 minutes to complete. Is this normal? This is how I set up my test:


def test_0263_add_permission_roles_to_users(migrator):
    # apply the starting migration
    old_state = migrator.apply_initial_migration(("users", "0262_add_new_permission_groups"))
    GroupModel = old_state.apps.get_model("auth", "Group")

    company_manager_group = GroupModel.objects.get(name=USER_ROLE_COMPANY_MANAGER)

    # recreate data
    for _ in range(10):
        user = UserFactory.create()
        user.groups.add(company_manager_group.id)

    UserModel = old_state.apps.get_model("users", "User")
    company_manager_group.refresh_from_db()

    company_manager_user_count = company_manager_group.user_set.count()
    users_count = UserModel.objects.filter(is_system_user=False).count()
    assert company_manager_user_count == users_count

    # apply the migration you want to test
    new_state = migrator.apply_tested_migration(("users", "0263_add_permission_roles_to_users"))
    GroupModel = new_state.apps.get_model("auth", "Group")

    admin_group = GroupModel.objects.get(name=USER_ROLE_ADMINISTRATOR_ACCESS)
    blockchain_group = GroupModel.objects.get(name=USER_ROLE_BLOCKCHAIN_KEY_MANAGEMENT)

    assert admin_group.user_set.count() == company_manager_user_count
    assert blockchain_group.user_set.count() == company_manager_user_count

    migrator.reset()```