python-Django solr.thumbnail:测试环境中的get_thumbnail()失败
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-Django solr.thumbnail:测试环境中的get_thumbnail()失败,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2676字,纯文字阅读大概需要4分钟。
内容图文
![python-Django solr.thumbnail:测试环境中的get_thumbnail()失败](/upload/InfoBanner/zyjiaocheng/659/784024a0163d46b897f6164f6115b56e.jpg)
我正在尝试找到一种方法,在使用Django和sorl-thumbnail的get_thumbnail()方法时如何测试缩略图的生成.
环境:
Django==1.5.5
Pillow==2.1.0
sorl-thumbnail==11.12
简化的测试代码(在测试环境中运行):
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
以上总是在最后一行失败:
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-thumbnail在运行测试时脱离了内存存储.我已经使用了很长时间,但是除了直接在文件系统上测试东西(我想避免这种情况)之外,我找不到任何有效的配置.
请问有人设法在测试中使用sorl的get_thumbnail()方法吗?
谢谢.
解决方法:
版本11.12是PyPI的最新版本,已有2年历史了.回购中有可用的版本12.0,该版本应该可以正常工作.它不在PyPI上,因为有change of maintainers,我想他们还没有机会这样做.
我尝试了主版本,并且可以在您的示例中正常使用.
内容总结
以上是互联网集市为您收集整理的python-Django solr.thumbnail:测试环境中的get_thumbnail()失败全部内容,希望文章能够帮你解决python-Django solr.thumbnail:测试环境中的get_thumbnail()失败所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。