Skip to content

Commit

Permalink
Merge pull request #528 from keuko/master
Browse files Browse the repository at this point in the history
Fix failing tests when local migrations are used (Fixes: #527).
  • Loading branch information
last-partizan committed Nov 13, 2019
2 parents 7bb8194 + 7e6f912 commit 15f39ac
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
7 changes: 1 addition & 6 deletions modeltranslation/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"""
Settings overrided for test time
"""
import django
from django.conf import settings


Expand All @@ -24,8 +23,4 @@

ROOT_URLCONF = 'modeltranslation.tests.urls'

if django.VERSION < (1, 11):
# TODO: Check what this was about
MIGRATION_MODULES = {'auth': 'modeltranslation.tests.auth_migrations'}
else:
MIGRATION_MODULES = {}
MIGRATION_MODULES = {'auth': 'modeltranslation.tests.auth_migrations'}
44 changes: 39 additions & 5 deletions modeltranslation/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from decimal import Decimal
from unittest import skipUnless
import datetime
import fnmatch
import imp
import os
import shutil
Expand Down Expand Up @@ -80,6 +81,40 @@ class ModeltranslationTransactionTestBase(TransactionTestCase):
cache = django_apps
synced = False

@classmethod
def _get_migrations_path(cls):
return os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"auth_migrations"
)

@classmethod
def _copy_migrations(cls):
# Locate the original contrib.auth migrations files
import django.contrib.auth.migrations as auth_migrations
source_dir_path = os.path.dirname(auth_migrations.__file__)

# Copy them to the local auth_migrations directory
target_dir_path = cls._get_migrations_path()
for f in os.listdir(source_dir_path):
source_path = os.path.join(source_dir_path, f)
target_path = os.path.join(target_dir_path, f)

# Only migration files get copied
if os.path.isfile(source_path) and not fnmatch.fnmatch(
f, "__init__.py"
):
shutil.copyfile(source_path, target_path)

@classmethod
def _clean_migrations(cls):
target_dir_path = cls._get_migrations_path()
for f in os.listdir(target_dir_path):
target_path = os.path.join(target_dir_path, f)
if os.path.isfile(target_path) and not fnmatch.fnmatch(
f, "__init__.py"):
os.remove(target_path)

@classmethod
def setUpClass(cls):
"""
Expand All @@ -93,6 +128,9 @@ def setUpClass(cls):
ModeltranslationTransactionTestBase.synced = True
# 0. Render initial migration of auth
if MIGRATIONS:
# Make copies of auth migrations in the (local) auth_migrations
# module to setup test database
cls._copy_migrations()
call_command('makemigrations', 'auth', verbosity=2, interactive=False)

# 1. Reload translation in case USE_I18N was False
Expand Down Expand Up @@ -132,11 +170,7 @@ def setUpClass(cls):

# 7. clean migrations
if MIGRATIONS:
import glob
dir = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"auth_migrations")
for f in glob.glob(dir + "/000?_*.py*"):
os.unlink(f)
cls._clean_migrations()

# A rather dirty trick to import models into module namespace, but not before
# tests app has been added into INSTALLED_APPS and loaded
Expand Down

0 comments on commit 15f39ac

Please sign in to comment.