moviewyrm/bookwyrm/tests/test_preview_images.py

123 lines
4.4 KiB
Python
Raw Normal View History

2021-05-28 19:24:45 +00:00
""" test generating preview images """
import pathlib
from unittest.mock import patch
2021-05-28 19:24:45 +00:00
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,
2021-05-29 13:32:04 +00:00
save_and_cleanup,
2021-05-28 19:24:45 +00:00
)
2021-05-28 19:26:13 +00:00
2021-06-18 22:08:36 +00:00
# pylint: disable=unused-argument
# pylint: disable=missing-function-docstring
2021-05-28 19:24:45 +00:00
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()
2021-08-02 23:05:40 +00:00
avatar_file = pathlib.Path(__file__).parent.joinpath(
"../static/images/no_cover.jpg"
)
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
"bookwyrm.activitystreams.populate_stream_task.delay"
):
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",
),
)
2021-08-02 23:05:40 +00:00
self.work = models.Work.objects.create(title="Test Work")
self.edition = models.Edition.objects.create(
title="Example Edition",
remote_id="https://example.com/book/1",
parent_work=self.work,
)
self.site = models.SiteSettings.objects.create()
settings.ENABLE_PREVIEW_IMAGES = True
2021-05-28 19:24:45 +00:00
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)
2021-05-28 19:26:13 +00:00
self.assertEqual(
result.size, (settings.PREVIEW_IMG_WIDTH, settings.PREVIEW_IMG_HEIGHT)
)
2021-05-28 19:24:45 +00:00
2021-05-29 13:32:04 +00:00
def test_store_preview_image(self, *args, **kwargs):
image = Image.new("RGB", (200, 200), color="#F00")
result = save_and_cleanup(image, instance=self.local_user)
self.assertTrue(result)
self.local_user.refresh_from_db()
self.assertIsInstance(self.local_user.preview_image, ImageFieldFile)
self.assertIsNotNone(self.local_user.preview_image)
self.assertEqual(self.local_user.preview_image.width, 200)
self.assertEqual(self.local_user.preview_image.height, 200)
2021-05-28 19:24:45 +00:00
def test_site_preview(self, *args, **kwargs):
2021-05-29 13:51:56 +00:00
"""generate site preview"""
2021-05-28 19:24:45 +00:00
generate_site_preview_image_task()
2021-05-29 09:17:59 +00:00
self.site.refresh_from_db()
2021-05-28 19:24:45 +00:00
2021-05-29 09:17:59 +00:00
self.assertIsInstance(self.site.preview_image, ImageFieldFile)
2021-05-29 10:33:04 +00:00
self.assertIsNotNone(self.site.preview_image)
self.assertEqual(self.site.preview_image.width, settings.PREVIEW_IMG_WIDTH)
self.assertEqual(self.site.preview_image.height, settings.PREVIEW_IMG_HEIGHT)
2021-05-28 21:41:41 +00:00
def test_edition_preview(self, *args, **kwargs):
2021-05-29 13:51:56 +00:00
"""generate edition preview"""
2021-05-29 10:33:04 +00:00
generate_edition_preview_image_task(self.edition.id)
2021-05-28 21:41:41 +00:00
2021-05-29 10:33:04 +00:00
self.edition.refresh_from_db()
2021-05-28 21:41:41 +00:00
2021-05-29 10:33:04 +00:00
self.assertIsInstance(self.edition.preview_image, ImageFieldFile)
self.assertIsNotNone(self.edition.preview_image)
self.assertEqual(self.edition.preview_image.width, settings.PREVIEW_IMG_WIDTH)
self.assertEqual(self.edition.preview_image.height, settings.PREVIEW_IMG_HEIGHT)
2021-05-28 19:24:45 +00:00
2021-05-28 21:41:41 +00:00
def test_user_preview(self, *args, **kwargs):
2021-05-29 13:51:56 +00:00
"""generate user preview"""
2021-05-28 21:41:41 +00:00
generate_user_preview_image_task(self.local_user.id)
2021-05-29 09:17:59 +00:00
self.local_user.refresh_from_db()
2021-05-28 21:41:41 +00:00
2021-05-29 09:17:59 +00:00
self.assertIsInstance(self.local_user.preview_image, ImageFieldFile)
2021-05-29 10:33:04 +00:00
self.assertIsNotNone(self.local_user.preview_image)
2021-05-29 13:51:56 +00:00
self.assertEqual(
self.local_user.preview_image.width, settings.PREVIEW_IMG_WIDTH
)
self.assertEqual(
self.local_user.preview_image.height, settings.PREVIEW_IMG_HEIGHT
)