Skip to content

Commit

Permalink
Add tests for model methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ababic committed Jul 3, 2024
1 parent dec7bf2 commit ce0701d
Showing 1 changed file with 66 additions and 5 deletions.
71 changes: 66 additions & 5 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from unittest import mock

from django.conf import settings
from django.test import SimpleTestCase
from django.core.files.uploadedfile import TemporaryUploadedFile
from django.test import SimpleTestCase, override_settings
from wagtail.documents import get_document_model
from wagtail.images import get_image_model

Expand All @@ -27,6 +28,9 @@ def setUp(self):
original_filesize=self.asset_data["fileSize"],
collection_id=1,
)
self.test_file = TemporaryUploadedFile(
name="unreal.pdf", content_type="application/pdf", size=1, charset="utf-8"
)
super().setUp()

def test_extract_file_source(self):
Expand Down Expand Up @@ -65,11 +69,28 @@ def test_update_file(self):
self.obj.original_filesize = 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=self.test_file,
) as download_asset_mock,
mock.patch(
"wagtail_bynder.models.utils.process_downloaded_file",
return_value=self.test_file,
) as process_downloaded_file_mock,
):
self.obj.update_file(self.asset_data)

# update_file() should have called 'download_asset' with the relevant URL
# from the asset data
download_asset_mock.assert_called_once_with(self.asset_data["original"])

# update_file() should always pass the result of 'download_asset' to the
# object's process_downloaded_file() method for additional processing
process_downloaded_file_mock.assert_called_once_with(
self.test_file, self.asset_data
)

self.assertTrue(self.obj._file_changed)
self.assertEqual(
self.obj.source_filename, filename_from_url(self.asset_data["original"])
Expand Down Expand Up @@ -122,6 +143,9 @@ def setUp(self):
original_width=self.asset_data["width"],
collection_id=1,
)
self.test_file = TemporaryUploadedFile(
name="imaginary.jpg", content_type="image/jpeg", size=1, charset="utf-8"
)
super().setUp()

def test_extract_file_source(self):
Expand Down Expand Up @@ -175,11 +199,29 @@ def test_update_file(self):
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=self.test_file,
) as mocked_download_asset,
mock.patch.object(
self.obj, "process_downloaded_file"
) as mocked_process_downloaded_file,
):
self.obj.update_file(self.asset_data)

# update_file() should have called 'download_asset' with the relevant URL
# from the asset data
mocked_download_asset.assert_called_once_with(
self.asset_data["thumbnails"]["WagtailSource"]
)

# update_file() should always pass the result of 'download_asset' to the
# object's process_downloaded_file() method for additional processing
mocked_process_downloaded_file.assert_called_once_with(
self.test_file, self.asset_data
)

self.assertTrue(self.obj._file_changed)
self.assertEqual(
self.obj.source_filename,
Expand Down Expand Up @@ -264,6 +306,25 @@ 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_reduces_image_size_by_default(self):
with mock.patch(
"wagtail_bynder.models.utils.reduce_image_size"
) as mocked_reduce_image_size:
result = self.obj.process_downloaded_file(self.test_file, {})
mocked_reduce_image_size.assert_called_once_with(self.test_file)
self.assertIsNone(result)

@override_settings(BYNDER_RESIZE_DOWNLOADED_IMAGES=False)
def test_process_downloaded_file_does_not_reduce_images_when_configured_accordingly(
self,
):
with mock.patch(
"wagtail_bynder.models.utils.reduce_image_size"
) as mocked_reduce_image_size:
result = self.obj.process_downloaded_file(self.test_file, {})
mocked_reduce_image_size.assert_not_called()
self.assertIs(result, self.test_file)


class BynderSyncedVideoTests(SimpleTestCase):
def setUp(self):
Expand Down

0 comments on commit ce0701d

Please sign in to comment.