refactor migration test case

This commit is contained in:
jonaswinkler 2021-02-08 13:18:39 +01:00
parent 816c95a4ae
commit 302ebf737e
2 changed files with 41 additions and 44 deletions

View File

@ -1,52 +1,11 @@
import os import os
import shutil import shutil
from pathlib import Path
from django.apps import apps
from django.conf import settings from django.conf import settings
from django.db import connection from django.test import override_settings
from django.db.migrations.executor import MigrationExecutor
from django.test import TestCase, TransactionTestCase, override_settings
from documents.models import Document
from documents.parsers import get_default_file_extension from documents.parsers import get_default_file_extension
from documents.tests.utils import DirectoriesMixin from documents.tests.utils import DirectoriesMixin, TestMigrations
class TestMigrations(TransactionTestCase):
@property
def app(self):
return apps.get_containing_app_config(type(self).__module__).name
migrate_from = None
migrate_to = None
def setUp(self):
super(TestMigrations, self).setUp()
assert self.migrate_from and self.migrate_to, \
"TestCase '{}' must define migrate_from and migrate_to properties".format(type(self).__name__)
self.migrate_from = [(self.app, self.migrate_from)]
self.migrate_to = [(self.app, self.migrate_to)]
executor = MigrationExecutor(connection)
old_apps = executor.loader.project_state(self.migrate_from).apps
# Reverse to the original migration
executor.migrate(self.migrate_from)
self.setUpBeforeMigration(old_apps)
# Run the migration to test
executor = MigrationExecutor(connection)
executor.loader.build_graph() # reload.
executor.migrate(self.migrate_to)
self.apps = executor.loader.project_state(self.migrate_to).apps
def setUpBeforeMigration(self, apps):
pass
STORAGE_TYPE_UNENCRYPTED = "unencrypted" STORAGE_TYPE_UNENCRYPTED = "unencrypted"
STORAGE_TYPE_GPG = "gpg" STORAGE_TYPE_GPG = "gpg"

View File

@ -4,7 +4,10 @@ import tempfile
from collections import namedtuple from collections import namedtuple
from contextlib import contextmanager from contextlib import contextmanager
from django.test import override_settings from django.apps import apps
from django.db import connection
from django.db.migrations.executor import MigrationExecutor
from django.test import override_settings, TransactionTestCase
def setup_directories(): def setup_directories():
@ -79,3 +82,38 @@ class DirectoriesMixin:
def tearDown(self) -> None: def tearDown(self) -> None:
super(DirectoriesMixin, self).tearDown() super(DirectoriesMixin, self).tearDown()
remove_dirs(self.dirs) remove_dirs(self.dirs)
class TestMigrations(TransactionTestCase):
@property
def app(self):
return apps.get_containing_app_config(type(self).__module__).name
migrate_from = None
migrate_to = None
def setUp(self):
super(TestMigrations, self).setUp()
assert self.migrate_from and self.migrate_to, \
"TestCase '{}' must define migrate_from and migrate_to properties".format(type(self).__name__)
self.migrate_from = [(self.app, self.migrate_from)]
self.migrate_to = [(self.app, self.migrate_to)]
executor = MigrationExecutor(connection)
old_apps = executor.loader.project_state(self.migrate_from).apps
# Reverse to the original migration
executor.migrate(self.migrate_from)
self.setUpBeforeMigration(old_apps)
# Run the migration to test
executor = MigrationExecutor(connection)
executor.loader.build_graph() # reload.
executor.migrate(self.migrate_to)
self.apps = executor.loader.project_state(self.migrate_to).apps
def setUpBeforeMigration(self, apps):
pass