Skip to content

Commit 06ec651

Browse files
Merge pull request #824 from adamtheturtle/test-image-too-large
Test image too large
2 parents be0bb55 + b7cd36e commit 06ec651

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

src/vws/exceptions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,18 @@ def __init__(self, response: Response) -> None:
6363
"""
6464
super().__init__()
6565
self.response = response
66+
67+
68+
class ImageTooLarge(Exception):
69+
"""
70+
Exception raised when Vuforia returns a response with a result code
71+
'ImageTooLarge'.
72+
"""
73+
74+
def __init__(self, response: Response) -> None:
75+
"""
76+
Args:
77+
response: The response to a request to Vuforia.
78+
"""
79+
super().__init__()
80+
self.response = response

src/vws/vws.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from vws._authorization import authorization_header, rfc_1123_date
1818
from vws.exceptions import (
19+
ImageTooLarge,
1920
MetadataTooLarge,
2021
TargetNameExist,
2122
TargetStatusProcessing,
@@ -110,6 +111,7 @@ class _ResultCodes(Enum):
110111

111112

112113
_EXCEPTIONS = {
114+
_ResultCodes.IMAGE_TOO_LARGE: ImageTooLarge,
113115
_ResultCodes.METADATA_TOO_LARGE: MetadataTooLarge,
114116
_ResultCodes.TARGET_NAME_EXIST: TargetNameExist,
115117
_ResultCodes.TARGET_STATUS_PROCESSING: TargetStatusProcessing,
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
Tests for giving an image which is too large.
3+
"""
4+
5+
import io
6+
import random
7+
8+
import pytest
9+
from PIL import Image
10+
from requests import codes
11+
12+
from vws import VWS
13+
from vws.exceptions import ImageTooLarge
14+
15+
16+
def make_image_file(
17+
file_format: str,
18+
color_space: str,
19+
width: int,
20+
height: int,
21+
) -> io.BytesIO:
22+
"""
23+
Return an image file in the given format and color space.
24+
25+
The image file is filled with randomly colored pixels.
26+
27+
Args:
28+
file_format: See
29+
http://pillow.readthedocs.io/en/3.1.x/handbook/image-file-formats.html
30+
color_space: One of "L", "RGB", or "CMYK".
31+
width: The width, in pixels of the image.
32+
height: The width, in pixels of the image.
33+
34+
Returns:
35+
An image file in the given format and color space.
36+
"""
37+
image_buffer = io.BytesIO()
38+
reds = random.choices(population=range(0, 255), k=width * height)
39+
greens = random.choices(population=range(0, 255), k=width * height)
40+
blues = random.choices(population=range(0, 255), k=width * height)
41+
pixels = list(zip(reds, greens, blues))
42+
image = Image.new(color_space, (width, height))
43+
image.putdata(pixels)
44+
image.save(image_buffer, file_format)
45+
image_buffer.seek(0)
46+
return image_buffer
47+
48+
49+
def test_image_too_large(client: VWS) -> None:
50+
"""
51+
When giving an image which is too large, an ``ImageTooLarge`` exception is
52+
raised.
53+
"""
54+
width = height = 890
55+
56+
png_too_large = make_image_file(
57+
file_format='PNG',
58+
color_space='RGB',
59+
width=width,
60+
height=height,
61+
)
62+
63+
with pytest.raises(ImageTooLarge) as exc:
64+
client.add_target(name='x', width=1, image=png_too_large)
65+
66+
assert exc.value.response.status_code == codes.UNPROCESSABLE_ENTITY

0 commit comments

Comments
 (0)