|
2 | 2 | Tests for various exceptions. |
3 | 3 | """ |
4 | 4 |
|
| 5 | +import io |
| 6 | +import random |
| 7 | + |
5 | 8 | import pytest |
| 9 | +from PIL import Image |
6 | 10 | from requests import codes |
7 | 11 |
|
8 | 12 | 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) |
10 | 65 |
|
| 66 | + assert exc.value.response.status_code == codes.UNPROCESSABLE_ENTITY |
11 | 67 |
|
12 | 68 | def test_invalid_given_id(client: VWS) -> None: |
13 | 69 | """ |
|
0 commit comments