Skip to content

Commit 13196bc

Browse files
committed
Start of consolidating tests for exceptions
1 parent 7cf624f commit 13196bc

1 file changed

Lines changed: 82 additions & 0 deletions

File tree

tests/test_exceptions.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"""
2+
Tests for various exceptions.
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, 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)
65+
66+
assert exc.value.response.status_code == codes.UNPROCESSABLE_ENTITY
67+
68+
def test_invalid_given_id(client: VWS) -> None:
69+
"""
70+
Giving an invalid ID to a helper which requires a target ID to be given
71+
causes an ``UnknownTarget`` exception to be raised.
72+
"""
73+
with pytest.raises(UnknownTarget) as exc:
74+
client.delete_target(target_id='x')
75+
assert exc.value.response.status_code == codes.NOT_FOUND
76+
77+
78+
def test_request_quota_reached() -> None:
79+
"""
80+
See https://github.com/adamtheturtle/vws-python/issues/822 for writing
81+
this test.
82+
"""

0 commit comments

Comments
 (0)