Be prepared to test with various storages or just with the usual FileSystemStorage, but with an overridden location (e.g. when the storage is created in another apps test before MEDIA_ROOT defined for tests gets set).

It would be nice to also find a way to ensure files are cleaned up with non-file-system storages in case test_translated_models_instance fails in the middle.
This commit is contained in:
wrwrwr 2013-02-20 19:59:01 +01:00
parent 46afcd0e5e
commit 670f378ae5
3 changed files with 12 additions and 17 deletions

View file

@ -16,6 +16,7 @@ from django.contrib.admin.sites import AdminSite
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError, ImproperlyConfigured
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
from django.core.management import call_command
from django.db.models import Q, F
from django.db.models.loading import AppCache
@ -518,14 +519,14 @@ class FallbackTests(ModeltranslationTestBase):
class FileFieldsTest(ModeltranslationTestBase):
test_media_root = TEST_SETTINGS['MEDIA_ROOT']
def tearDown(self):
# File tests create a temporary media directory structure. While the
# files are automatically deleted by the storage, the directories will
# stay. So we clean up a bit...
if os.path.isdir(self.test_media_root):
shutil.rmtree(self.test_media_root)
if default_storage.exists('modeltranslation_tests'):
# With FileSystemStorage uploading files creates a new directory,
# that's not automatically removed upon their deletion.
tests_dir = default_storage.path('modeltranslation_tests')
if os.path.isdir(tests_dir):
shutil.rmtree(tests_dir)
super(FileFieldsTest, self).tearDown()
def test_translated_models(self):
@ -573,10 +574,10 @@ class FileFieldsTest(ModeltranslationTestBase):
self.failUnless(inst.image.name.count('i_en') > 0)
self.failUnlessEqual(inst.image.read(), 'image in english')
# Check if file was actually saved on disc
self.failUnless(os.path.exists(os.path.join(self.test_media_root, inst.file.name)))
# Check if file was actually created in the global storage.
self.failUnless(default_storage.exists(inst.file))
self.failUnless(inst.file.size > 0)
self.failUnless(os.path.exists(os.path.join(self.test_media_root, inst.image.name)))
self.failUnless(default_storage.exists(inst.image))
self.failUnless(inst.image.size > 0)
trans_real.activate("de")

View file

@ -31,8 +31,8 @@ class FallbackModel2(models.Model):
class FileFieldsModel(models.Model):
title = models.CharField(ugettext_lazy('title'), max_length=255)
file = models.FileField(upload_to='test', null=True, blank=True)
image = models.ImageField(upload_to='test', null=True, blank=True)
file = models.FileField(upload_to='modeltranslation_tests', null=True, blank=True)
image = models.ImageField(upload_to='modeltranslation_tests', null=True, blank=True)
########## Custom fields testing

View file

@ -2,12 +2,9 @@
"""
Settings overrided for test time
"""
import os
from django.conf import settings
DIRNAME = os.path.dirname(__file__)
INSTALLED_APPS = tuple(settings.INSTALLED_APPS) + (
'modeltranslation.tests',
)
@ -17,9 +14,6 @@ INSTALLED_APPS = tuple(settings.INSTALLED_APPS) + (
#STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(DIRNAME, 'media/')
LANGUAGES = (('de', 'Deutsch'),
('en', 'English'))
LANGUAGE_CODE = 'de'