Question

I'm trying to find a way how to test thumbnail generation when using Django and sorl-thumbnail's get_thumbnail() method.

Environment:

Django==1.5.5
Pillow==2.1.0
sorl-thumbnail==11.12

Simplified code under test (ran in test environment):

from StringIO import StringIO
from PIL import Image

from django.conf import settings
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.db import models
from sorl.thumbnail import get_thumbnail

# simple class with ImageField
class User(models.Model):
    avatar = models.ImageField(upload_to='avatars', default=None, null=True, blank=True)

    def get_thumbnail_uri(self):
        avatar_thumbnail = get_thumbnail(self.avatar, '100x100')

        return avatar_thumbnail.url

# make sure we're using in-memory test env.
assert settings.THUMBNAIL_STORAGE  == 'inmemorystorage.InMemoryStorage'
assert settings.DEFAULT_FILE_STORAGE == 'inmemorystorage.InMemoryStorage'

# prepare image
fake_file = StringIO()

picture = Image.new(mode='RGBA', size=(500, 500), color=(255, 0, 0, 0))
picture.save(fake_file, 'JPEG')

fake_file.name = 'test.jpg'
fake_file.seek(0)

uploaded_image = InMemoryUploadedFile(fake_file, field_name=None, name='test.jpg',
                                      content_type='image/jpeg', size=fake_file.len,
                                      charset=None)

# add image to user
u = User()
u.avatar = uploaded_image

assert u.get_thumbnail_uri() is not None

The above always fails on the last line:

Traceback (most recent call last):
  File "/vagrant/path/to/file.py", line 1440, in test_stackprep
    assert u.get_thumbnail_uri() is not None
  File "/vagrant/path/to/file.py", line 1413, in get_thumbnail_uri
    avatar_thumbnail = get_thumbnail(self.avatar, '100x100')
  File "/home/vagrant/.virtualenv/appname/local/lib/python2.7/site-packages/sorl/thumbnail/shortcuts.py", line 8, in get_thumbnail
    return default.backend.get_thumbnail(file_, geometry_string, **options)
  File "/home/vagrant/.virtualenv/appname/local/lib/python2.7/site-packages/sorl/thumbnail/base.py", line 56, in get_thumbnail
    source_image = default.engine.get_image(source)
  File "/home/vagrant/.virtualenv/appname/local/lib/python2.7/site-packages/sorl/thumbnail/engines/pil_engine.py", line 13, in get_image
    return Image.open(buf)
  File "/home/vagrant/.virtualenv/appname/local/lib/python2.7/site-packages/PIL/Image.py", line 2008, in open
    raise IOError("cannot identify image file")
IOError: cannot identify image file

I assume that either Django or sorl-thumbnail gets out of inmemorystorage while running the test. I've been at it for a long time, but I failed to find any configuration that works, with the exception of testing stuff directly on the filesystem (which I'd like to avoid).

Did anyone manage to get sorl's get_thumbnail() method working in tests, please?

Thanks.

Was it helpful?

Solution

Version 11.12, which is the latest one on PyPI today, is 2 years old. There's version 12.0 available in repo, which should work. It's not on PyPI, because there was change of maintainers and I guess they hadn't chance to do it yet.

I've tried master version and it works ok with your example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top