Fixes #382: Tests no longer leave junk

This commit is contained in:
Venelin Stoykov 2016-07-17 04:43:04 +03:00
parent 5061679b17
commit 07d29b3bf7
7 changed files with 45 additions and 9 deletions

View file

@ -52,8 +52,7 @@ class CachedFileBackend(object):
@property
def cache(self):
if not getattr(self, '_cache', None):
from django.conf import settings
self._cache = get_cache(settings.IMAGEKIT_CACHE_BACKEND)
self._cache = get_cache()
return self._cache
def get_key(self, file):

View file

@ -148,12 +148,12 @@ def call_strategy_method(file, method_name):
fn(file)
def get_cache(backend, **kwargs):
def get_cache(backend=settings.IMAGEKIT_CACHE_BACKEND):
try:
from django.core.cache import caches
except ImportError:
from django.core.cache import get_cache
return get_cache(backend, **kwargs)
return get_cache(backend)
return caches[backend]

View file

@ -16,4 +16,7 @@ def run_tests():
cls = get_runner(settings)
runner = cls()
failures = runner.run_tests(['tests'])
# Clean autogenerated junk before exit
from tests.utils import clear_imagekit_test_files
clear_imagekit_test_files()
sys.exit(failures)

View file

@ -1,11 +1,12 @@
from django.template import TemplateSyntaxError
from nose.tools import eq_, assert_false, raises, assert_not_equal
from . import imagegenerators # noqa
from .utils import render_tag, get_html_attrs
from .utils import render_tag, get_html_attrs, clear_imagekit_cache
def test_img_tag():
ttag = r"""{% generateimage 'testspec' source=img %}"""
clear_imagekit_cache()
attrs = get_html_attrs(ttag)
expected_attrs = set(['src', 'width', 'height'])
eq_(set(attrs.keys()), expected_attrs)
@ -15,6 +16,7 @@ def test_img_tag():
def test_img_tag_attrs():
ttag = r"""{% generateimage 'testspec' source=img -- alt="Hello" %}"""
clear_imagekit_cache()
attrs = get_html_attrs(ttag)
eq_(attrs.get('alt'), 'Hello')
@ -42,11 +44,13 @@ def test_single_dimension_attr():
"""
ttag = r"""{% generateimage 'testspec' source=img -- width="50" %}"""
clear_imagekit_cache()
attrs = get_html_attrs(ttag)
assert_false('height' in attrs)
def test_assignment_tag():
ttag = r"""{% generateimage 'testspec' source=img as th %}{{ th.url }}"""
clear_imagekit_cache()
html = render_tag(ttag)
assert_not_equal(html.strip(), '')

View file

@ -6,10 +6,11 @@ deserialized. This is important when using IK with Celery.
from imagekit.cachefiles import ImageCacheFile
from .imagegenerators import TestSpec
from .utils import create_photo, pickleback, get_unique_image_file
from .utils import create_photo, pickleback, get_unique_image_file, clear_imagekit_cache
def test_imagespecfield():
clear_imagekit_cache()
instance = create_photo('pickletest2.jpg')
thumbnail = pickleback(instance.thumbnail)
thumbnail.generate()
@ -22,12 +23,14 @@ def test_circular_ref():
This corresponds to #234
"""
clear_imagekit_cache()
instance = create_photo('pickletest3.jpg')
instance.thumbnail # Cause thumbnail to be added to instance's __dict__
pickleback(instance)
def test_cachefiles():
clear_imagekit_cache()
spec = TestSpec(source=get_unique_image_file())
file = ImageCacheFile(spec)
file.url

View file

@ -1,11 +1,12 @@
from django.template import TemplateSyntaxError
from nose.tools import eq_, raises, assert_not_equal
from . import imagegenerators # noqa
from .utils import render_tag, get_html_attrs
from .utils import render_tag, get_html_attrs, clear_imagekit_cache
def test_img_tag():
ttag = r"""{% thumbnail '100x100' img %}"""
clear_imagekit_cache()
attrs = get_html_attrs(ttag)
expected_attrs = set(['src', 'width', 'height'])
eq_(set(attrs.keys()), expected_attrs)
@ -15,6 +16,7 @@ def test_img_tag():
def test_img_tag_attrs():
ttag = r"""{% thumbnail '100x100' img -- alt="Hello" %}"""
clear_imagekit_cache()
attrs = get_html_attrs(ttag)
eq_(attrs.get('alt'), 'Hello')
@ -50,17 +52,20 @@ def test_html_attrs_assignment():
def test_assignment_tag():
ttag = r"""{% thumbnail '100x100' img as th %}{{ th.url }}"""
clear_imagekit_cache()
html = render_tag(ttag)
assert_not_equal(html, '')
def test_single_dimension():
ttag = r"""{% thumbnail '100x' img as th %}{{ th.width }}"""
clear_imagekit_cache()
html = render_tag(ttag)
eq_(html, '100')
def test_alternate_generator():
ttag = r"""{% thumbnail '1pxsq' '100x' img as th %}{{ th.width }}"""
clear_imagekit_cache()
html = render_tag(ttag)
eq_(html, '1')

View file

@ -1,10 +1,12 @@
from bs4 import BeautifulSoup
import os
from django.conf import settings
import shutil
from django.core.files import File
from django.template import Context, Template
from imagekit.cachefiles.backends import Simple, CacheFileState
from imagekit.conf import settings
from imagekit.lib import Image, StringIO
from imagekit.utils import get_cache
from nose.tools import assert_true, assert_false
import pickle
from tempfile import NamedTemporaryFile
@ -82,3 +84,23 @@ class DummyAsyncCacheFileBackend(Simple):
def generate(self, file, force=False):
pass
def clear_imagekit_cache():
cache = get_cache()
cache.clear()
# Clear IMAGEKIT_CACHEFILE_DIR
cache_dir = os.path.join(settings.MEDIA_ROOT, settings.IMAGEKIT_CACHEFILE_DIR)
if os.path.exists(cache_dir):
shutil.rmtree(cache_dir)
def clear_imagekit_test_files():
clear_imagekit_cache()
for fname in os.listdir(settings.MEDIA_ROOT):
if fname != 'reference.png':
path = os.path.join(settings.MEDIA_ROOT, fname)
if os.path.isdir(path):
shutil.rmtree(path)
else:
os.remove(path)