Fix image url tests

This commit is contained in:
Iacopo Spalletti 2014-06-20 18:28:06 +02:00
parent cc92affb8d
commit aa4a4e87d5
4 changed files with 43 additions and 5 deletions

View file

@ -143,7 +143,9 @@ class Post(ModelMeta, TranslatableModel):
return description.strip()
def get_image_full_url(self):
return self.make_full_url(self.main_image.url)
if self.main_image:
return self.make_full_url(self.main_image.url)
return ''
def get_tags(self):
taglist = [tag.name for tag in self.tags.all()]

View file

@ -1,5 +1,6 @@
import sys
from optparse import OptionParser
import sys
from tempfile import mkdtemp
gettext = lambda s: s
try:
@ -108,7 +109,8 @@ try:
'easy_thumbnails.processors.autocrop',
'filer.thumbnail_processors.scale_and_crop_with_subject_location',
'easy_thumbnails.processors.filters',
)
),
FILE_UPLOAD_TEMP_DIR=mkdtemp()
)
from django_nose import NoseTestSuiteRunner
@ -133,4 +135,4 @@ def run_tests(*test_args):
if __name__ == '__main__':
parser = OptionParser()
(options, args) = parser.parse_args()
run_tests(*args)
run_tests(*args)

View file

@ -2,12 +2,18 @@
"""
Tests for `djangocms_blog` module.
"""
import os
from cms.utils.i18n import get_language_list
from cmsplugin_filer_image.models import ThumbnailOption
from django.conf import settings
from django.contrib.auth.models import User
from django.core.files import File as DjangoFile
from django.http import SimpleCookie
from django.test import TestCase, RequestFactory
from django.utils.translation import activate
from filer.models import File, Image
from PIL import Image as PilImage, ImageChops, ImageDraw
from six import StringIO
from djangocms_blog.models import BlogCategory, Post
@ -66,6 +72,15 @@ class BaseTest(TestCase):
self.thumb_2 = ThumbnailOption.objects.create(
name='main', width=200, height=200, crop=False, upscale=False
)
img = create_image()
self.image_name = 'test_file.jpg'
self.filename = os.path.join(settings.FILE_UPLOAD_TEMP_DIR,
self.image_name)
img.save(self.filename, 'JPEG')
file_obj = DjangoFile(open(self.filename, 'rb'), name=self.image_name)
self.img = Image.objects.create(owner=self.user,
original_filename=self.image_name,
file=file_obj)
def _get_post(self, data, post=None, lang='en'):
if not post:
@ -84,6 +99,11 @@ class BaseTest(TestCase):
@classmethod
def tearDownClass(cls):
User.objects.all().delete()
def tearDown(self):
os.remove(self.filename)
for f in File.objects.all():
f.delete()
def get_pages(self):
from cms.api import create_page, create_title
@ -127,9 +147,20 @@ class BaseTest(TestCase):
post1 = self._get_post(self.data['en'][0])
post1 = self._get_post(self.data['it'][0], post1, 'it')
post1.publish = True
post1.main_image = self.img
post1.save()
post1.set_current_language('en')
post2 = self._get_post(self.data['en'][1])
post2 = self._get_post(self.data['it'][1], post2, 'it')
post2.set_current_language('en')
return post1, post2
post2.main_image = self.img
return post1, post2
def create_image(mode='RGB', size=(800, 600)):
image = PilImage.new(mode, size)
draw = ImageDraw.Draw(image)
x_bit, y_bit = size[0] // 10, size[1] // 10
draw.rectangle((x_bit, y_bit * 2, x_bit * 7, y_bit * 3), 'red')
draw.rectangle((x_bit * 2, y_bit, x_bit * 3, y_bit * 8), 'red')
return image

View file

@ -19,6 +19,8 @@ class ModelsTest(BaseTest):
def test_model_attributes(self):
post = self._get_post(self.data['en'][0])
post = self._get_post(self.data['it'][0], post, 'it')
post.main_image = self.img
post.save()
post.set_current_language('en')
meta_en = post.as_meta()
self.assertEqual(meta_en.og_type, settings.BLOG_FB_TYPE)
@ -51,6 +53,7 @@ class ModelsTest(BaseTest):
self.assertNotEqual(url_it, url_en)
self.assertEqual(post.get_full_url(), 'http://example.com%s' % url_it)
self.assertEqual(post.get_image_full_url(), 'http://example.com%s' % post.main_image.url)
self.assertEqual(post.thumbnail_options(), settings.BLOG_IMAGE_THUMBNAIL_SIZE)
self.assertEqual(post.full_image_options(), settings.BLOG_IMAGE_FULL_SIZE)