From aa4a4e87d5dfbc9901ebaf16b742e78f7f8ef4bc Mon Sep 17 00:00:00 2001 From: Iacopo Spalletti Date: Fri, 20 Jun 2014 18:28:06 +0200 Subject: [PATCH] Fix image url tests --- djangocms_blog/models.py | 4 +++- runtests.py | 8 +++++--- tests/__init__.py | 33 ++++++++++++++++++++++++++++++++- tests/test_models.py | 3 +++ 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/djangocms_blog/models.py b/djangocms_blog/models.py index f32990e..1146537 100644 --- a/djangocms_blog/models.py +++ b/djangocms_blog/models.py @@ -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()] diff --git a/runtests.py b/runtests.py index 1a952a6..4ab27b3 100644 --- a/runtests.py +++ b/runtests.py @@ -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) \ No newline at end of file + run_tests(*args) diff --git a/tests/__init__.py b/tests/__init__.py index 5e5082b..e3d5d12 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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 \ No newline at end of file + 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 diff --git a/tests/test_models.py b/tests/test_models.py index ec0d115..a44bbd8 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -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)