|
| 1 | +from io import BytesIO |
1 | 2 | from pathlib import Path
|
2 | 3 |
|
| 4 | +import pytest |
| 5 | +from PIL import Image |
| 6 | + |
3 | 7 | from sketch_map_tool import helpers
|
4 | 8 |
|
5 | 9 |
|
6 | 10 | def test_get_project_root():
|
7 | 11 | expected = Path(__file__).resolve().parent.parent.parent.resolve()
|
8 | 12 | result = helpers.get_project_root()
|
9 | 13 | assert expected == result
|
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.parametrize( |
| 17 | + ("max_length", "width_exp", "height_exp"), |
| 18 | + [ |
| 19 | + (1450, 1450, 1232), # One dimension too big |
| 20 | + (1000, 1000, 850), # Both dimensions too big |
| 21 | + (1456, 1456, 1238), # Not too big, exactly limit |
| 22 | + (1600, 1456, 1238), # Not too big, smaller than limit |
| 23 | + ], |
| 24 | +) |
| 25 | +def test_resize_png(map_frame_buffer, max_length, width_exp, height_exp): |
| 26 | + img = Image.open(map_frame_buffer) |
| 27 | + img_rotated = img.rotate(90, expand=1) |
| 28 | + width, height = img.width, img.height |
| 29 | + width_rotated, height_rotated = img_rotated.width, img_rotated.height |
| 30 | + assert width == 1456 and height == 1238 |
| 31 | + assert width_rotated == 1238 and height_rotated == 1456 |
| 32 | + |
| 33 | + img_rotated_buffer = BytesIO() |
| 34 | + img_rotated.save(img_rotated_buffer, format="png") |
| 35 | + img_rotated_buffer.seek(0) |
| 36 | + |
| 37 | + img_resized_buffer = helpers.resize_png(map_frame_buffer, max_length=max_length) |
| 38 | + img_rotated_resized_buffer = helpers.resize_png( |
| 39 | + img_rotated_buffer, max_length=max_length |
| 40 | + ) |
| 41 | + img_resized = Image.open(img_resized_buffer) |
| 42 | + img_rotated_resized = Image.open(img_rotated_resized_buffer) |
| 43 | + |
| 44 | + width_resized, height_resized = img_resized.width, img_resized.height |
| 45 | + width_rotated_resized, height_rotated_resized = ( |
| 46 | + img_rotated_resized.width, |
| 47 | + img_rotated_resized.height, |
| 48 | + ) |
| 49 | + |
| 50 | + assert width_resized == width_exp and height_resized == height_exp |
| 51 | + assert width_rotated_resized == height_exp and height_rotated_resized == width_exp |
0 commit comments