Fixed tests: tests use a temporary settings.MEDIA_ROOT, so files are isolated for tests.

This commit is contained in:
Benoit Bryon 2012-08-28 10:13:41 +02:00
parent 24b15e0400
commit ab75aa6e08
6 changed files with 72 additions and 43 deletions

View file

@ -42,6 +42,8 @@ update: develop
clean:
find $(ROOT_DIR)/ -name "*.pyc" -delete
find $(ROOT_DIR)/ -name ".noseids" -delete
rm nosetests.xml
distclean: clean
@ -54,8 +56,7 @@ maintainer-clean: distclean
test:
#bin/nosetests --config=etc/nose.cfg
bin/demo test download
bin/demo test demo
documentation:

View file

@ -3,12 +3,6 @@
extensions =
buildout-versions
versions = versions
parts =
django-downloadview
testing
documentation-builder
documentation-directories
releaser
# Configure directories: put buildout generated files in lib/buildout instead
# of in current directory.
bin-directory = bin
@ -21,40 +15,34 @@ parts-directory = lib/buildout/parts
develop =
${buildout:directory}/
${buildout:directory}/demo/
[django-downloadview]
recipe = z3c.recipe.scripts
eggs =
parts =
django-downloadview
directories
releaser
eggs =
django-downloadview-demo
initialization =
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'demoproject.settings'
[testing]
recipe = z3c.recipe.scripts
eggs =
${django-downloadview:eggs}
bpython
nose
rednose
coverage
initialization = ${django-downloadview:initialization}
[documentation-builder]
recipe = z3c.recipe.scripts
eggs =
${django-downloadview:eggs}
sphinx
initialization = ${django-downloadview:initialization}
[documentation-directories]
[django-downloadview]
recipe = z3c.recipe.scripts
eggs = ${buildout:eggs}
initialization =
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'demoproject.settings'
[directories]
recipe = z3c.recipe.mkdir
paths =
var/docs
docs/_static
var/test
[releaser]
recipe = zc.recipe.egg
recipe = z3c.recipe.scripts
eggs = zest.releaser
[versions]

View file

@ -1,10 +1,14 @@
"""Test suite for django-downloadview."""
from os import listdir
from os.path import abspath, dirname, join
import shutil
import tempfile
from django.conf import settings
from django.core.files import File
from django.core.urlresolvers import reverse_lazy as reverse
from django.test import TestCase
from django.test.utils import override_settings
from demoproject.download.models import Document
@ -13,6 +17,41 @@ app_dir = dirname(abspath(__file__))
fixtures_dir = join(app_dir, 'fixtures')
class temporary_media_root(override_settings):
"""Context manager or decorator to override settings.MEDIA_ROOT.
>>> from django.conf import settings
>>> global_media_root = settings.MEDIA_ROOT
>>> with temporary_media_root():
... global_media_root == settings.MEDIA_ROOT
False
>>> global_media_root == settings.MEDIA_ROOT
True
>>> @temporary_media_root
... def use_temporary_media_root():
... return settings.MEDIA_ROOT
>>> tmp_media_root = use_temporary_media_root()
>>> global_media_root == tmp_media_root
False
>>> global_media_root == settings.MEDIA_ROOT
True
"""
def enable(self):
"""Create a temporary directory and use it to override
settings.MEDIA_ROOT."""
tmp_dir = tempfile.mkdtemp()
self.options['MEDIA_ROOT'] = tmp_dir
super(temporary_media_root, self).enable()
def disable(self):
"""Remove directory settings.MEDIA_ROOT then restore original
setting."""
shutil.rmtree(settings.MEDIA_ROOT)
super(temporary_media_root, self).disable()
class DownloadTestCase(TestCase):
"""Base class for download tests."""
def setUp(self):
@ -41,6 +80,7 @@ class DownloadViewTestCase(DownloadTestCase):
class ObjectDownloadViewTestCase(DownloadTestCase):
"""Test generic ObjectDownloadView."""
@temporary_media_root()
def test_download_hello_world(self):
"""Download_hello_world view returns hello-world.txt as attachement."""
document = Document.objects.create(

View file

@ -159,3 +159,16 @@ LOGGING = {
}
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
NOSE_ARGS = ['--verbose',
'--nocapture',
'--rednose',
'--with-id', # allows --failed which only reruns failed tests
'--id-file=%s' % join(data_dir, 'test', 'noseids'),
'--with-doctest',
'--with-xunit',
'--xunit-file=%s' % join(data_dir, 'test', 'nosetests.xml'),
'--with-coverage',
'--cover-erase',
'--cover-package=django_downloadview',
'--no-path-adjustment',
]

View file

@ -19,7 +19,7 @@ class DownloadMixin(object):
"""Returns a response with a file as attachment."""
mime_type = self.get_mime_type()
if isinstance(self.file, File):
absolute_filename = self.file.name
absolute_filename = self.file.path
else:
absolute_filename = abspath(self.file)
filename = basename(absolute_filename)

View file

@ -1,13 +0,0 @@
[nosetests]
verbosity = 2
with-doctest = True
with-xunit = True
rednose = 1
with-coverage = True
cover-erase = True
cover-package = django_downloadview
no-path-adjustment = True
nocapture = True
with-id = True
where = demo
#tests =