django-imagekit/tests/test_cachefiles.py

78 lines
2.1 KiB
Python
Raw Normal View History

2013-04-30 13:32:13 +00:00
from django.conf import settings
from hashlib import md5
from imagekit.cachefiles import ImageCacheFile
2013-04-30 13:32:13 +00:00
from imagekit.cachefiles.backends import Simple
from nose.tools import raises, eq_
import random
import string
from .imagegenerators import TestSpec
2013-03-15 04:49:44 +00:00
from .utils import (assert_file_is_truthy, assert_file_is_falsy,
DummyAsyncCacheFileBackend, get_unique_image_file)
2013-03-15 04:49:44 +00:00
def test_no_source_falsiness():
"""
2013-03-15 04:49:44 +00:00
Ensure cache files generated from sourceless specs are falsy.
"""
spec = TestSpec(source=None)
file = ImageCacheFile(spec)
2013-03-15 02:23:47 +00:00
assert_file_is_falsy(file)
2013-03-15 04:49:44 +00:00
def test_sync_backend_truthiness():
"""
Ensure that a cachefile with a synchronous cache file backend (the default)
is truthy.
"""
spec = TestSpec(source=get_unique_image_file())
file = ImageCacheFile(spec)
assert_file_is_truthy(file)
def test_async_backend_falsiness():
"""
Ensure that a cachefile with an asynchronous cache file backend is falsy.
"""
spec = TestSpec(source=get_unique_image_file())
file = ImageCacheFile(spec, cachefile_backend=DummyAsyncCacheFileBackend())
assert_file_is_falsy(file)
@raises(TestSpec.MissingSource)
def test_no_source_error():
spec = TestSpec(source=None)
file = ImageCacheFile(spec)
file.generate()
2013-04-30 13:32:13 +00:00
def test_memcached_cache_key():
"""
Ensure the default cachefile backend is sanitizing its cache key for
memcached by default.
"""
class MockFile(object):
def __init__(self, name):
self.name = name
backend = Simple()
extra_char_count = len('state-') + len(settings.IMAGEKIT_CACHE_PREFIX)
length = 199 - extra_char_count
filename = '1' * length
file = MockFile(filename)
eq_(backend.get_key(file), '%s%s-state' %
(settings.IMAGEKIT_CACHE_PREFIX, file.name))
length = 200 - extra_char_count
filename = '1' * length
file = MockFile(filename)
eq_(backend.get_key(file), '%s%s:%s' % (
settings.IMAGEKIT_CACHE_PREFIX,
'1' * (200 - len(':') - 32 - len(settings.IMAGEKIT_CACHE_PREFIX)),
md5('%s%s-state' % (settings.IMAGEKIT_CACHE_PREFIX, filename)).hexdigest()))