Try another way

This commit is contained in:
Joachim 2021-05-29 12:33:04 +02:00
parent 022182e114
commit cc0881b11c
2 changed files with 39 additions and 42 deletions

View file

@ -309,7 +309,7 @@ def save_and_cleanup(image, instance=None):
file_name = "%s-%s.jpg" % (str(instance.id), str(uuid4())) file_name = "%s-%s.jpg" % (str(instance.id), str(uuid4()))
image_buffer = BytesIO() image_buffer = BytesIO()
# try: try:
try: try:
old_path = instance.preview_image.path old_path = instance.preview_image.path
except ValueError: except ValueError:
@ -335,7 +335,7 @@ def save_and_cleanup(image, instance=None):
# Clean up old file after saving # Clean up old file after saving
if os.path.exists(old_path): if os.path.exists(old_path):
os.remove(old_path) os.remove(old_path)
# finally: finally:
image_buffer.close() image_buffer.close()

View file

@ -46,7 +46,7 @@ class PreviewImages(TestCase):
) )
with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"): with patch("bookwyrm.preview_images.generate_edition_preview_image_task.delay"):
self.work = models.Work.objects.create(title="Test Work") self.work = models.Work.objects.create(title="Test Work")
self.book = models.Edition.objects.create( self.edition = models.Edition.objects.create(
title="Example Edition", title="Example Edition",
remote_id="https://example.com/book/1", remote_id="https://example.com/book/1",
parent_work=self.work, parent_work=self.work,
@ -77,22 +77,20 @@ class PreviewImages(TestCase):
self.site.refresh_from_db() self.site.refresh_from_db()
self.assertIsInstance(self.site.preview_image, ImageFieldFile) self.assertIsInstance(self.site.preview_image, ImageFieldFile)
result = Image.open(self.site.preview_image) self.assertIsNotNone(self.site.preview_image)
self.assertEqual( self.assertEqual(self.site.preview_image.width, settings.PREVIEW_IMG_WIDTH)
result.size, (settings.PREVIEW_IMG_WIDTH, settings.PREVIEW_IMG_HEIGHT) self.assertEqual(self.site.preview_image.height, settings.PREVIEW_IMG_HEIGHT)
)
def test_edition_preview(self, *args, **kwargs): def test_edition_preview(self, *args, **kwargs):
"""generate user preview""" """generate edition preview"""
generate_edition_preview_image_task(self.book.id) generate_edition_preview_image_task(self.edition.id)
self.book.refresh_from_db() self.edition.refresh_from_db()
self.assertIsInstance(self.book.preview_image, ImageFieldFile) self.assertIsInstance(self.edition.preview_image, ImageFieldFile)
result = Image.open(self.book.preview_image) self.assertIsNotNone(self.edition.preview_image)
self.assertEqual( self.assertEqual(self.edition.preview_image.width, settings.PREVIEW_IMG_WIDTH)
result.size, (settings.PREVIEW_IMG_WIDTH, settings.PREVIEW_IMG_HEIGHT) self.assertEqual(self.edition.preview_image.height, settings.PREVIEW_IMG_HEIGHT)
)
def test_user_preview(self, *args, **kwargs): def test_user_preview(self, *args, **kwargs):
"""generate user preview""" """generate user preview"""
@ -101,7 +99,6 @@ class PreviewImages(TestCase):
self.local_user.refresh_from_db() self.local_user.refresh_from_db()
self.assertIsInstance(self.local_user.preview_image, ImageFieldFile) self.assertIsInstance(self.local_user.preview_image, ImageFieldFile)
result = Image.open(self.local_user.preview_image) self.assertIsNotNone(self.local_user.preview_image)
self.assertEqual( self.assertEqual(self.local_user.preview_image.width, settings.PREVIEW_IMG_WIDTH)
result.size, (settings.PREVIEW_IMG_WIDTH, settings.PREVIEW_IMG_HEIGHT) self.assertEqual(self.local_user.preview_image.height, settings.PREVIEW_IMG_HEIGHT)
)