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 @property
def cache(self): def cache(self):
if not getattr(self, '_cache', None): if not getattr(self, '_cache', None):
from django.conf import settings self._cache = get_cache()
self._cache = get_cache(settings.IMAGEKIT_CACHE_BACKEND)
return self._cache return self._cache
def get_key(self, file): def get_key(self, file):

View file

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

View file

@ -16,4 +16,7 @@ def run_tests():
cls = get_runner(settings) cls = get_runner(settings)
runner = cls() runner = cls()
failures = runner.run_tests(['tests']) 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) sys.exit(failures)

View file

@ -1,11 +1,12 @@
from django.template import TemplateSyntaxError from django.template import TemplateSyntaxError
from nose.tools import eq_, assert_false, raises, assert_not_equal from nose.tools import eq_, assert_false, raises, assert_not_equal
from . import imagegenerators # noqa 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(): def test_img_tag():
ttag = r"""{% generateimage 'testspec' source=img %}""" ttag = r"""{% generateimage 'testspec' source=img %}"""
clear_imagekit_cache()
attrs = get_html_attrs(ttag) attrs = get_html_attrs(ttag)
expected_attrs = set(['src', 'width', 'height']) expected_attrs = set(['src', 'width', 'height'])
eq_(set(attrs.keys()), expected_attrs) eq_(set(attrs.keys()), expected_attrs)
@ -15,6 +16,7 @@ def test_img_tag():
def test_img_tag_attrs(): def test_img_tag_attrs():
ttag = r"""{% generateimage 'testspec' source=img -- alt="Hello" %}""" ttag = r"""{% generateimage 'testspec' source=img -- alt="Hello" %}"""
clear_imagekit_cache()
attrs = get_html_attrs(ttag) attrs = get_html_attrs(ttag)
eq_(attrs.get('alt'), 'Hello') eq_(attrs.get('alt'), 'Hello')
@ -42,11 +44,13 @@ def test_single_dimension_attr():
""" """
ttag = r"""{% generateimage 'testspec' source=img -- width="50" %}""" ttag = r"""{% generateimage 'testspec' source=img -- width="50" %}"""
clear_imagekit_cache()
attrs = get_html_attrs(ttag) attrs = get_html_attrs(ttag)
assert_false('height' in attrs) assert_false('height' in attrs)
def test_assignment_tag(): def test_assignment_tag():
ttag = r"""{% generateimage 'testspec' source=img as th %}{{ th.url }}""" ttag = r"""{% generateimage 'testspec' source=img as th %}{{ th.url }}"""
clear_imagekit_cache()
html = render_tag(ttag) html = render_tag(ttag)
assert_not_equal(html.strip(), '') 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 imagekit.cachefiles import ImageCacheFile
from .imagegenerators import TestSpec 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(): def test_imagespecfield():
clear_imagekit_cache()
instance = create_photo('pickletest2.jpg') instance = create_photo('pickletest2.jpg')
thumbnail = pickleback(instance.thumbnail) thumbnail = pickleback(instance.thumbnail)
thumbnail.generate() thumbnail.generate()
@ -22,12 +23,14 @@ def test_circular_ref():
This corresponds to #234 This corresponds to #234
""" """
clear_imagekit_cache()
instance = create_photo('pickletest3.jpg') instance = create_photo('pickletest3.jpg')
instance.thumbnail # Cause thumbnail to be added to instance's __dict__ instance.thumbnail # Cause thumbnail to be added to instance's __dict__
pickleback(instance) pickleback(instance)
def test_cachefiles(): def test_cachefiles():
clear_imagekit_cache()
spec = TestSpec(source=get_unique_image_file()) spec = TestSpec(source=get_unique_image_file())
file = ImageCacheFile(spec) file = ImageCacheFile(spec)
file.url file.url

View file

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

View file

@ -1,10 +1,12 @@
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import os import os
from django.conf import settings import shutil
from django.core.files import File from django.core.files import File
from django.template import Context, Template from django.template import Context, Template
from imagekit.cachefiles.backends import Simple, CacheFileState from imagekit.cachefiles.backends import Simple, CacheFileState
from imagekit.conf import settings
from imagekit.lib import Image, StringIO from imagekit.lib import Image, StringIO
from imagekit.utils import get_cache
from nose.tools import assert_true, assert_false from nose.tools import assert_true, assert_false
import pickle import pickle
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
@ -82,3 +84,23 @@ class DummyAsyncCacheFileBackend(Simple):
def generate(self, file, force=False): def generate(self, file, force=False):
pass 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)