Skip to content

Add pytest fixture for testing migrations (TS-2289) #1093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions django/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from django.contrib.sites.models import Site
from django.core.cache import cache
from django.core.files.base import File
from django.db import connection
from django.db.migrations.executor import MigrationExecutor
from PIL import Image
from rest_framework.test import APIClient

Expand Down Expand Up @@ -110,6 +112,16 @@ def prime_testing_database(django_db_setup, django_db_blocker):
[x.delete() for x in Site.objects.all()]


@pytest.fixture
def migrate_db_state():
def _run_migration(app_name: str, migration: str):
executor = MigrationExecutor(connection)
executor.migrate([(app_name, migration)])
executor.loader.build_graph()

return _run_migration


@pytest.fixture()
def user(django_user_model):
return django_user_model.objects.create_user(
Expand Down
23 changes: 23 additions & 0 deletions django/thunderstore/core/tests/test_migrations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest

from conftest import migrate_db_state
from thunderstore.repository.factories import NamespaceFactory, TeamFactory
from thunderstore.repository.models import Namespace


@pytest.mark.django_db
def test_0055_delete_namespaces_without_team(migrate_db_state):
migrate_db_state("repository", "0054_alter_chunked_package_cache_index")

team = TeamFactory.create()
namespace_with_team = NamespaceFactory.create(team=team)
namespace_without_team = NamespaceFactory.create(team=None)

assert namespace_without_team.team is None
assert Namespace.objects.count() == 2

migrate_db_state("repository", "0055_delete_namespaces_without_team")

assert Namespace.objects.count() == 1
assert namespace_with_team in Namespace.objects.all()
assert namespace_without_team not in Namespace.objects.all()
Loading