Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ababic committed Jul 4, 2024
1 parent 5b22005 commit bf90110
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 4 deletions.
93 changes: 89 additions & 4 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import io

from unittest import mock

from django.conf import settings
from django.test import SimpleTestCase
from django.test import SimpleTestCase, override_settings
from wagtail.documents import get_document_model
from wagtail.images import get_image_model

from wagtail_bynder import get_video_model
from wagtail_bynder.exceptions import BynderAssetDataError
from wagtail_bynder.utils import filename_from_url

from .utils import get_test_asset_data
from .utils import get_fake_downloaded_image, get_test_asset_data


class BynderSyncedDocumentTests(SimpleTestCase):
Expand Down Expand Up @@ -169,17 +171,32 @@ def test_asset_file_has_changed(self):
self.assertTrue(self.obj.asset_file_has_changed(data))

def test_update_file(self):
fake_download = get_fake_downloaded_image()

self.obj.source_filename = None
self.obj.original_filesize = None
self.obj.original_height = None
self.obj.original_width = None
self.assertFalse(hasattr(self.obj, "_file_changed"))

with mock.patch(
"wagtail_bynder.models.utils.download_asset", return_value=None
with (
mock.patch(
"wagtail_bynder.models.utils.download_asset",
return_value=fake_download,
) as download_asset_mock,
mock.patch.object(
self.obj, "process_downloaded_file"
) as process_downloaded_file_mock,
):
self.obj.update_file(self.asset_data)

download_asset_mock.assert_called_once_with(
self.asset_data["thumbnails"]["WagtailSource"]
)
process_downloaded_file_mock.assert_called_once_with(
fake_download, self.asset_data
)

self.assertTrue(self.obj._file_changed)
self.assertEqual(
self.obj.source_filename,
Expand Down Expand Up @@ -264,6 +281,74 @@ def test_update_from_asset_data_without_focal_point_change(self):
self.assertEqual(new_focal_point, current_focal_point)
self.assertFalse(self.obj._focal_point_changed)

def test_process_downloaded_file(self):
fake_image = get_fake_downloaded_image("example.jpg", 500, 200)
state_before = self.obj.__dict__

# The original image data should be available via the `file` attribute
self.assertTrue(fake_image.file)

result = self.obj.process_downloaded_file(fake_image, self.asset_data)

# Wagtail doesn't convert JPEGs to a differet format by default, so the
# resulting name and content type should be the same as what was provided
self.assertEqual(result.name, fake_image.name)
self.assertEqual(result.content_type, fake_image.content_type)

# The original image data should have been deleted to create headroom
# for the converted image
self.assertFalse(hasattr(fake_image, "file"))

# No attribute values should on the object itself should have changed
self.assertEqual(state_before, self.obj.__dict__)

@override_settings(
BYNDER_IMAGE_MAX_WIDTH=100,
BYNDER_IMAGE_MAX_HEIGHT=100,
WAGTAILIMAGES_FORMAT_CONVERSIONS={"gif": "png", "bmp": "png", "tiff": "jpeg"},
)
def test_convert_image(self):
for original_details, expected_details in (
(
("tall.gif", "gif", "image/gif", 240, 400),
("tall.png", "png", "image/png", 60, 100),
),
(
("wide.bmp", "bmp", "image/bmp", 400, 100),
("wide.png", "png", "image/png", 100, 25),
),
(
("big-square.tif", "tiff", "image/tiff", 400, 400),
("big-square.jpg", "jpeg", "image/jpeg", 100, 100),
),
(
("small-square.tiff", "tiff", "image/tiff", 80, 80),
("small-square.jpg", "jpeg", "image/jpeg", 80, 80),
),
):
with self.subTest(f"{original_details[0]} becomes {expected_details[0]}"):
original = get_fake_downloaded_image(
name=original_details[0],
width=original_details[3],
height=original_details[4],
)
self.assertEqual(original.content_type, original_details[2])
result = self.obj.convert_image(original, io.BytesIO())
self.assertEqual(
(
result.image_format,
result.mime_type,
result.width,
result.height,
),
(
expected_details[1],
expected_details[2],
expected_details[3],
expected_details[4],
),
)


class BynderSyncedVideoTests(SimpleTestCase):
def setUp(self):
Expand Down
56 changes: 56 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
import io
import os

from datetime import datetime
from enum import StrEnum

from django.core.files.uploadedfile import InMemoryUploadedFile
from django.utils.text import slugify
from PIL import Image


TEST_ASSET_ID = "1A7BA172-97B9-44A4-8C0AA41D9E8AE6A2"


class ImageFormat(StrEnum):
JPEG = "JPEG"
GIF = "GIF"
PNG = "PNG"
BMP = "BMP"
WEBP = "WebP"
TIFF = "TIFF"


EXTENSION_TO_IMAGE_FORMAT = {
".jpg": (ImageFormat.JPEG, "image/jpeg"),
".jpeg": (ImageFormat.JPEG, "image/jpeg"),
".gif": (ImageFormat.GIF, "image/gif"),
".png": (ImageFormat.PNG, "image/png"),
".webp": (ImageFormat.WEBP, "image/webp"),
".bmp": (ImageFormat.BMP, "image/bmp"),
".tif": (ImageFormat.TIFF, "image/tiff"),
".tiff": (ImageFormat.TIFF, "image/tiff"),
}


def get_test_asset_data(
id: str = TEST_ASSET_ID,
name: str = "Test asset",
Expand Down Expand Up @@ -86,3 +113,32 @@ def get_test_asset_data(
]

return data


def get_fake_image(
width: int = 100, height: int = 100, image_format: ImageFormat = ImageFormat.JPEG
) -> io.BytesIO:
thumb_io = io.BytesIO()
with Image.new("RGB", (width, height), "blue") as thumb:
thumb.save(thumb_io, format=image_format)
return thumb_io


def get_fake_downloaded_image(
name: str = "fake.jpg", width: int = 100, height: int = 100
) -> InMemoryUploadedFile:
_, ext = os.path.splitext(name.lower())
if ext not in EXTENSION_TO_IMAGE_FORMAT:
raise ValueError(
f"{ext} is not supported image file extension. Try one of the following: {list(EXTENSION_TO_IMAGE_FORMAT.keys())}"
)
image_format, content_type = EXTENSION_TO_IMAGE_FORMAT[ext]

return InMemoryUploadedFile(
get_fake_image(width, height, image_format),
field_name="file",
name=name,
content_type=content_type,
size=1048576,
charset="utf-8",
)

0 comments on commit bf90110

Please sign in to comment.