首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django + solr.thumbnail: get_thumbnail()在测试环境中失败

Django + solr.thumbnail: get_thumbnail()在测试环境中失败
EN

Stack Overflow用户
提问于 2013-12-11 15:18:07
回答 1查看 949关注 0票数 4

在使用Django和sorl-thumbnailget_thumbnail()方法时,我试图找到一种测试缩略图生成的方法。

环境:

代码语言:javascript
复制
Django==1.5.5
Pillow==2.1.0
sorl-thumbnail==11.12

正在测试的简化代码(在测试环境中运行):

代码语言:javascript
复制
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

上面的内容在最后一行总是失败的:

代码语言:javascript
复制
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

我假设Django或sorl-缩略图在运行测试时退出了inmemorystorage。我已经做了很长一段时间了,但是除了直接在文件系统上测试东西(我想避免这种情况)之外,我没有找到任何可以工作的配置。

有人能让sorl的get_thumbnail()方法在测试中工作吗?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-11 17:04:56

11.12版是PyPI上的最新版本,现在已经2年了。回购版中有12.0版本,应该可以使用。它不在PyPI上,因为有维修人员的变动,我想他们还没有机会这么做。

我已经尝试过主版本,它与您的示例运行良好。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20522865

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档