Skip to content

Commit

Permalink
Test against actual scales and bigger images
Browse files Browse the repository at this point in the history
  • Loading branch information
agitator authored and jensens committed Oct 7, 2020
1 parent 68a00da commit e208a61
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 114 deletions.
2 changes: 2 additions & 0 deletions news/757.bugfix
@@ -0,0 +1,2 @@
Use scales from registry for tests.
[agitator]
Binary file added src/plone/restapi/tests/image.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/plone/restapi/tests/image.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 35 additions & 32 deletions src/plone/restapi/tests/test_atfield_serializer.py
Expand Up @@ -4,6 +4,7 @@
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from plone.restapi.interfaces import IFieldSerializer
from plone.restapi.imaging import get_scale_infos
from plone.restapi.testing import HAS_AT
from plone.restapi.testing import PLONE_RESTAPI_AT_INTEGRATION_TESTING
from plone.restapi.testing import PLONE_VERSION
Expand Down Expand Up @@ -112,13 +113,13 @@ def test_text_field_serialization_returns_dict(self):
)

def test_image_field_serialization_returns_dict(self):
image_file = os.path.join(os.path.dirname(__file__), u"1024x768.gif")
image_file = os.path.join(os.path.dirname(__file__), u"image.gif")
with open(image_file, "rb") as f:
image_data = f.read()
fn = "testImageField"
with patch.object(storage, "uuid4", return_value="uuid_1"):
value = self.serialize(
fn, image_data, filename="1024x768.gif", mimetype="image/gif"
fn, image_data, filename="image.gif", mimetype="image/gif"
)
self.assertTrue(isinstance(value, dict), "Not a <dict>")

Expand All @@ -128,28 +129,29 @@ def test_image_field_serialization_returns_dict(self):
download_url = u"{}/@@images/{}.{}".format(
obj_url, scale_url_uuid, GIF_SCALE_FORMAT
)
scales = {
u"listing": {u"download": download_url, u"width": 16, u"height": 12},
u"icon": {u"download": download_url, u"width": 32, u"height": 24},
u"tile": {u"download": download_url, u"width": 64, u"height": 48},
u"thumb": {u"download": download_url, u"width": 80, u"height": 60},
u"mini": {u"download": download_url, u"width": 200, u"height": 150},
u"preview": {u"download": download_url, u"width": 400, u"height": 300},
u"large": {u"download": download_url, u"width": 768, u"height": 576},
}
allowed_sizes = get_scale_infos()

scales = value["scales"]
del value["scales"]

self.assertEqual(
{
u"filename": u"1024x768.gif",
u"filename": u"image.gif",
u"content-type": u"image/gif",
u"size": 1514,
u"size": 3223,
u"download": download_url,
u"width": 1024,
u"height": 768,
u"scales": scales,
u"width": 2000,
u"height": 1500,
},
value,
)

for allowed_size in allowed_sizes:
name, width, height = allowed_size
self.assertIn(name, scales)
self.assertEqual(width, scales[name]["width"])
self.assertEqual(download_url, scales[name]["download"])

def test_blob_field_serialization_returns_dict(self):
value = self.serialize(
"testBlobField", "spam and eggs", filename="spam.txt", mimetype="text/plain"
Expand Down Expand Up @@ -187,42 +189,43 @@ def test_blobfile_field_serialization_returns_dict(self):
)

def test_blobimage_field_serialization_returns_dict(self):
image_file = os.path.join(os.path.dirname(__file__), u"1024x768.gif")
image_file = os.path.join(os.path.dirname(__file__), u"image.gif")
with open(image_file, "rb") as f:
image_data = f.read()
fn = "testBlobImageField"
with patch.object(storage, "uuid4", return_value="uuid_1"):
value = self.serialize(
fn, image_data, filename="1024x768.gif", mimetype="image/gif"
fn, image_data, filename="image.gif", mimetype="image/gif"
)
self.assertTrue(isinstance(value, dict), "Not a <dict>")
scale_url_uuid = "uuid_1"
obj_url = self.doc1.absolute_url()
download_url = u"{}/@@images/{}.{}".format(
obj_url, scale_url_uuid, GIF_SCALE_FORMAT
)
scales = {
u"listing": {u"download": download_url, u"width": 16, u"height": 12},
u"icon": {u"download": download_url, u"width": 32, u"height": 24},
u"tile": {u"download": download_url, u"width": 64, u"height": 48},
u"thumb": {u"download": download_url, u"width": 128, u"height": 96},
u"mini": {u"download": download_url, u"width": 200, u"height": 150},
u"preview": {u"download": download_url, u"width": 400, u"height": 300},
u"large": {u"download": download_url, u"width": 768, u"height": 576},
}
allowed_sizes = get_scale_infos()

scales = value["scales"]
del value["scales"]

self.assertEqual(
{
u"filename": u"1024x768.gif",
u"filename": u"image.gif",
u"content-type": u"image/gif",
u"size": 1514,
u"size": 3223,
u"download": download_url,
u"width": 1024,
u"height": 768,
u"scales": scales,
u"width": 2000,
u"height": 1500,
},
value,
)

for allowed_size in allowed_sizes:
name, width, height = allowed_size
self.assertIn(name, scales)
self.assertEqual(width, scales[name]["width"])
self.assertEqual(download_url, scales[name]["download"])

def test_query_field_serialization_returns_list(self):
query_data = [
{
Expand Down
97 changes: 29 additions & 68 deletions src/plone/restapi/tests/test_dxfield_serializer.py
Expand Up @@ -11,6 +11,7 @@
from plone.namedfile.file import NamedBlobImage
from plone.namedfile.file import NamedFile
from plone.namedfile.file import NamedImage
from plone.restapi.imaging import get_scale_infos
from plone.restapi.interfaces import IFieldSerializer
from plone.restapi.serializer.dxfields import DefaultFieldSerializer
from plone.restapi.testing import PLONE_RESTAPI_DX_INTEGRATION_TESTING
Expand Down Expand Up @@ -350,15 +351,15 @@ def test_namedimage_field_serialization_returns_dict(self):
"""In Plone < 5.1 the image returned when requesting an image
scale with the same width and height of the original image is
a Pillow-generated image scale in JPEG format"""
image_file = os.path.join(os.path.dirname(__file__), u"1024x768.gif")
image_file = os.path.join(os.path.dirname(__file__), u"image.gif")
with open(image_file, "rb") as f:
image_data = f.read()
fn = "test_namedimage_field"
with patch.object(storage, "uuid4", return_value="uuid_1"):
value = self.serialize(
fn,
NamedImage(
data=image_data, contentType=u"image/gif", filename=u"1024x768.gif"
data=image_data, contentType=u"image/gif", filename=u"image.gif"
),
)
self.assertTrue(isinstance(value, dict), "Not a <dict>")
Expand All @@ -375,47 +376,27 @@ def test_namedimage_field_serialization_returns_dict(self):
scale_download_url = u"{}/@@images/{}.{}".format(
obj_url, scale_url_uuid, "jpeg"
)
scales = {
u"listing": {
u"download": scale_download_url,
u"width": 16,
u"height": 12,
},
u"icon": {u"download": scale_download_url, u"width": 32, u"height": 24},
u"tile": {u"download": scale_download_url, u"width": 64, u"height": 48},
u"thumb": {
u"download": scale_download_url,
u"width": 128,
u"height": 96,
},
u"mini": {
u"download": scale_download_url,
u"width": 200,
u"height": 150,
},
u"preview": {
u"download": scale_download_url,
u"width": 400,
u"height": 300,
},
u"large": {
u"download": scale_download_url,
u"width": 768,
u"height": 576,
},
}
allowed_sizes = get_scale_infos()

scales = value["scales"]
del value["scales"]

self.assertEqual(
{
u"filename": u"1024x768.gif",
u"filename": u"image.gif",
u"content-type": u"image/gif",
u"size": 1514,
u"size": 3223,
u"download": original_download_url,
u"width": 1024,
u"height": 768,
u"scales": scales,
},
value,
)
for allowed_size in allowed_sizes:
name, width, height = allowed_size
self.assertIn(name, scales)
self.assertEqual(width, scales[name]["width"])
self.assertEqual(scale_download_url, scales[name]["download"])

def test_namedimage_field_serialization_doesnt_choke_on_corrupt_image(self):
"""Original image url will be None because the original image is corrupted
Expand Down Expand Up @@ -447,15 +428,15 @@ def test_namedblobimage_field_serialization_returns_dict(self):
"""In Plone < 5.1 the image returned when requesting an image
scale with the same width and height of the original image is
a Pillow-generated image scale in JPEG format"""
image_file = os.path.join(os.path.dirname(__file__), u"1024x768.gif")
image_file = os.path.join(os.path.dirname(__file__), u"image.gif")
with open(image_file, "rb") as f:
image_data = f.read()
fn = "test_namedblobimage_field"
with patch.object(storage, "uuid4", return_value="uuid_1"):
value = self.serialize(
fn,
NamedBlobImage(
data=image_data, contentType=u"image/gif", filename=u"1024x768.gif"
data=image_data, contentType=u"image/gif", filename=u"image.gif"
),
)
self.assertTrue(isinstance(value, dict), "Not a <dict>")
Expand All @@ -472,47 +453,27 @@ def test_namedblobimage_field_serialization_returns_dict(self):
scale_download_url = u"{}/@@images/{}.{}".format(
obj_url, scale_url_uuid, "jpeg"
)
scales = {
u"listing": {
u"download": scale_download_url,
u"width": 16,
u"height": 12,
},
u"icon": {u"download": scale_download_url, u"width": 32, u"height": 24},
u"tile": {u"download": scale_download_url, u"width": 64, u"height": 48},
u"thumb": {
u"download": scale_download_url,
u"width": 128,
u"height": 96,
},
u"mini": {
u"download": scale_download_url,
u"width": 200,
u"height": 150,
},
u"preview": {
u"download": scale_download_url,
u"width": 400,
u"height": 300,
},
u"large": {
u"download": scale_download_url,
u"width": 768,
u"height": 576,
},
}
allowed_sizes = get_scale_infos()

scales = value["scales"]
del value["scales"]

self.assertEqual(
{
u"filename": u"1024x768.gif",
u"filename": u"image.gif",
u"content-type": u"image/gif",
u"size": 1514,
u"size": 3223,
u"download": original_download_url,
u"width": 1024,
u"height": 768,
u"scales": scales,
},
value,
)
for allowed_size in allowed_sizes:
name, width, height = allowed_size
self.assertIn(name, scales)
self.assertEqual(width, scales[name]["width"])
self.assertEqual(scale_download_url, scales[name]["download"])

def test_namedblobimage_field_serialization_doesnt_choke_on_corrupt_image(self):
"""Original image url will be None because the original image is corrupted
Expand Down
32 changes: 18 additions & 14 deletions src/plone/restapi/tests/test_serializer.py
Expand Up @@ -6,6 +6,7 @@
from plone.app.textfield.value import RichTextValue
from plone.namedfile.file import NamedBlobImage
from plone.namedfile.file import NamedFile
from plone.restapi.imaging import get_scale_infos
from plone.restapi.interfaces import ISerializeToJson
from plone.restapi.testing import PLONE_RESTAPI_DX_INTEGRATION_TESTING
from plone.scale import storage
Expand Down Expand Up @@ -262,28 +263,31 @@ def test_serialize_image(self):
obj_url = self.portal.image1.absolute_url()
scale_url_uuid = "uuid_1"
download_url = u"{}/@@images/{}.png".format(obj_url, scale_url_uuid)
scales = {
u"listing": {u"download": download_url, u"width": 16, u"height": 4},
u"icon": {u"download": download_url, u"width": 32, u"height": 8},
u"tile": {u"download": download_url, u"width": 64, u"height": 16},
u"thumb": {u"download": download_url, u"width": 128, u"height": 33},
u"mini": {u"download": download_url, u"width": 200, u"height": 52},
u"preview": {u"download": download_url, u"width": 215, u"height": 56},
u"large": {u"download": download_url, u"width": 215, u"height": 56},
}
obj = self.serialize(self.portal.image1)["image"]

allowed_sizes = get_scale_infos()

scales = obj["scales"]
del obj["scales"]

self.assertEqual(
{
u"filename": u"image.png",
u"content-type": u"image/png",
u"size": 1185,
u"size": 7534,
u"download": download_url,
u"width": 215,
u"height": 56,
u"scales": scales,
u"width": 982,
u"height": 256,
},
self.serialize(self.portal.image1)["image"],
obj,
)

for allowed_size in allowed_sizes:
name, width, height = allowed_size
self.assertIn(name, scales)
self.assertEqual(width, scales[name]["width"])
self.assertEqual(download_url, scales[name]["download"])

def test_serialize_empty_image_returns_none(self):
self.portal.invokeFactory("Image", id="image1", title="Image 1")
self.assertEqual(None, self.serialize(self.portal.image1)["image"])
Expand Down

0 comments on commit e208a61

Please sign in to comment.