Skip to content

Commit

Permalink
Add tests for webp format conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
gasman committed Oct 16, 2019
1 parent 01e0cd9 commit e496d18
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
32 changes: 31 additions & 1 deletion wagtail/images/tests/test_image_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from wagtail.images import image_operations
from wagtail.images.exceptions import InvalidFilterSpecError
from wagtail.images.models import Filter, Image
from wagtail.images.tests.utils import get_test_image_file, get_test_image_file_jpeg
from wagtail.images.tests.utils import (
get_test_image_file, get_test_image_file_jpeg, get_test_image_file_webp)


class WillowOperationRecorder:
Expand Down Expand Up @@ -671,3 +672,32 @@ def test_invalid_length(self):
file=get_test_image_file(),
)
self.assertRaises(ValueError, fil.run, image, BytesIO())


class TestWebpFormatConversion(TestCase):
def test_webp_convert_to_png(self):
"""by default, webp images will be converted to png"""

fil = Filter(spec='width-400')
image = Image.objects.create(
title="Test image",
file=get_test_image_file_webp(),
)
out = fil.run(image, BytesIO())

self.assertEqual(out.format_name, 'png')

@override_settings(
WAGTAILIMAGES_FORMAT_CONVERSIONS={'webp': 'webp'}
)
def test_override_webp_convert_to_png(self):
"""WAGTAILIMAGES_FORMAT_CONVERSIONS can be overridden to disable webp conversion"""

fil = Filter(spec='width-400')
image = Image.objects.create(
title="Test image",
file=get_test_image_file_webp(),
)
out = fil.run(image, BytesIO())

self.assertEqual(out.format_name, 'webp')
7 changes: 7 additions & 0 deletions wagtail/images/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ def get_test_image_file_jpeg(filename='test.jpg', colour='white', size=(640, 480
image = PIL.Image.new('RGB', size, colour)
image.save(f, 'JPEG')
return ImageFile(f, name=filename)


def get_test_image_file_webp(filename='test.webp', colour='white', size=(640, 480)):
f = BytesIO()
image = PIL.Image.new('RGB', size, colour)
image.save(f, 'WEBP')
return ImageFile(f, name=filename)

0 comments on commit e496d18

Please sign in to comment.