From ec159ebcb0bc1742aeb90af83edbe5524af22feb Mon Sep 17 00:00:00 2001 From: Joachim Date: Fri, 28 May 2021 21:24:45 +0200 Subject: [PATCH] Test generating preview images --- bookwyrm/preview_images.py | 15 ++-- bookwyrm/tests/test_preview_images.py | 99 +++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 bookwyrm/tests/test_preview_images.py diff --git a/bookwyrm/preview_images.py b/bookwyrm/preview_images.py index 7a23353c8..17511ddee 100644 --- a/bookwyrm/preview_images.py +++ b/bookwyrm/preview_images.py @@ -57,7 +57,7 @@ def generate_texts_layer(texts, content_width): text_y = 0 - if "text_zero" in texts: + if "text_zero" in texts and texts["text_zero"]: # Text one (Book title) text_zero = textwrap.fill(texts["text_zero"], width=72) text_layer_draw.multiline_text( @@ -66,7 +66,7 @@ def generate_texts_layer(texts, content_width): text_y = text_y + font_text_zero.getsize_multiline(text_zero)[1] + 16 - if "text_one" in texts: + if "text_one" in texts and texts["text_one"]: # Text one (Book title) text_one = textwrap.fill(texts["text_one"], width=28) text_layer_draw.multiline_text( @@ -75,7 +75,7 @@ def generate_texts_layer(texts, content_width): text_y = text_y + font_text_one.getsize_multiline(text_one)[1] + 16 - if "text_two" in texts: + if "text_two" in texts and texts["text_two"]: # Text one (Book subtitle) text_two = textwrap.fill(texts["text_two"], width=36) text_layer_draw.multiline_text( @@ -84,7 +84,7 @@ def generate_texts_layer(texts, content_width): text_y = text_y + font_text_one.getsize_multiline(text_two)[1] + 16 - if "text_three" in texts: + if "text_three" in texts and texts["text_three"]: # Text three (Book authors) text_three = textwrap.fill(texts["text_three"], width=36) text_layer_draw.multiline_text( @@ -299,7 +299,12 @@ def save_and_cleanup(image, instance=None): image_buffer.tell(), None, ) - instance.save(update_fields=["preview_image"]) + + save_without_broadcast = isinstance(instance, (models.Book, models.User)) + if save_without_broadcast: + instance.save(update_fields=["preview_image"], broadcast=False) + else: + instance.save(update_fields=["preview_image"]) # Clean up old file after saving if os.path.exists(old_path): diff --git a/bookwyrm/tests/test_preview_images.py b/bookwyrm/tests/test_preview_images.py new file mode 100644 index 000000000..6edf2e791 --- /dev/null +++ b/bookwyrm/tests/test_preview_images.py @@ -0,0 +1,99 @@ +""" test generating preview images """ +import sys +import pathlib +from unittest.mock import patch +from PIL import Image + +from django.test import TestCase +from django.test.client import RequestFactory +from django.core.files.uploadedfile import SimpleUploadedFile +from django.db.models.fields.files import ImageFieldFile + +from bookwyrm import models, settings + +from bookwyrm.preview_images import ( + generate_site_preview_image_task, + generate_edition_preview_image_task, + generate_user_preview_image_task, + generate_preview_image, +) + +import logging + +@patch("bookwyrm.emailing.send_email.delay") +class PreviewImages(TestCase): + """every response to a get request, html or json""" + + def setUp(self): + """we need basic test data and mocks""" + self.factory = RequestFactory() + with patch("bookwyrm.preview_images.generate_user_preview_image_task.delay"): + avatar_file = pathlib.Path(__file__).parent.joinpath( + "../static/images/no_cover.jpg" + ) + self.local_user = models.User.objects.create_user( + "possum@local.com", + "possum@possum.possum", + "password", + local=True, + localname="possum", + avatar=SimpleUploadedFile( + avatar_file, open(avatar_file, "rb").read(), content_type="image/jpeg" + ) + ) + with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"): + self.work = models.Work.objects.create(title="Test Work") + self.book = models.Edition.objects.create( + title="Example Edition", + remote_id="https://example.com/book/1", + parent_work=self.work, + ) + with patch("bookwyrm.preview_images.generate_site_preview_image_task.delay"): + models.SiteSettings.objects.create() + + def test_generate_preview_image(self, *args, **kwargs): + image_file = pathlib.Path(__file__).parent.joinpath( + "../static/images/no_cover.jpg" + ) + + texts = { + "text_one": "Awesome Possum", + "text_three": "@possum@local.com", + } + + result = generate_preview_image(texts=texts, picture=image_file, rating=5) + self.assertIsInstance(result, Image.Image) + self.assertEqual(result.size, (settings.PREVIEW_IMG_WIDTH, settings.PREVIEW_IMG_HEIGHT)) + + def test_site_preview(self, *args, **kwargs): + """generate site preview""" + generate_site_preview_image_task() + + updated_site = models.SiteSettings.objects.get() + site_preview_image = updated_site.preview_image + + self.assertIsInstance(site_preview_image, ImageFieldFile) + result = Image.open(site_preview_image) + self.assertEqual(result.size, (settings.PREVIEW_IMG_WIDTH, settings.PREVIEW_IMG_HEIGHT)) + + def test_edition_preview(self, *args, **kwargs): + """generate user preview""" + generate_edition_preview_image_task(self.book.id) + + updated_book = models.Book.objects.get(id=self.book.id) + book_preview_image = updated_book.preview_image + + self.assertIsInstance(book_preview_image, ImageFieldFile) + result = Image.open(book_preview_image) + self.assertEqual(result.size, (settings.PREVIEW_IMG_WIDTH, settings.PREVIEW_IMG_HEIGHT)) + + def test_user_preview(self, *args, **kwargs): + """generate user preview""" + generate_user_preview_image_task(self.local_user.id) + + updated_user = models.User.objects.get(id=self.local_user.id) + user_preview_image = updated_user.preview_image + + self.assertIsInstance(user_preview_image, ImageFieldFile) + result = Image.open(user_preview_image) + self.assertEqual(result.size, (settings.PREVIEW_IMG_WIDTH, settings.PREVIEW_IMG_HEIGHT))