Skip to content

Commit c24e182

Browse files
committed
Start of consolidating tests for exceptions
1 parent 5596153 commit c24e182

2 files changed

Lines changed: 57 additions & 67 deletions

File tree

tests/test_exceptions/test_image_too_large.py

Lines changed: 0 additions & 66 deletions
This file was deleted.

tests/test_exceptions/test_unknown_target.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,68 @@
22
Tests for various exceptions.
33
"""
44

5+
import io
6+
import random
7+
58
import pytest
9+
from PIL import Image
610
from requests import codes
711

812
from vws import VWS
9-
from vws.exceptions import UnknownTarget
13+
from vws.exceptions import ImageTooLarge, UnknownTarget
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)
1065

66+
assert exc.value.response.status_code == codes.UNPROCESSABLE_ENTITY
1167

1268
def test_invalid_given_id(client: VWS) -> None:
1369
"""

0 commit comments

Comments
 (0)