|
1 | 1 | import os |
| 2 | +import shutil |
| 3 | +import subprocess |
2 | 4 | import uuid |
| 5 | +from pathlib import Path |
3 | 6 |
|
4 | 7 | import pytest |
5 | 8 | import pytest_asyncio |
6 | 9 | from dotenv import load_dotenv |
7 | 10 |
|
8 | 11 | from resolver_athena_client.client.athena_options import AthenaOptions |
9 | 12 | from resolver_athena_client.client.channel import CredentialHelper |
| 13 | +from tests.utils.image_generation import create_test_image |
| 14 | + |
| 15 | +IMAGEMAGICK_FORMATS = [ |
| 16 | + "gif", |
| 17 | + "bmp", |
| 18 | + "dib", |
| 19 | + "png", |
| 20 | + "webp", |
| 21 | + "pbm", |
| 22 | + "pgm", |
| 23 | + "ppm", |
| 24 | + "pxm", |
| 25 | + "pnm", |
| 26 | + "sr", |
| 27 | + "ras", |
| 28 | + "tiff", |
| 29 | + "pic", |
| 30 | +] |
10 | 31 |
|
11 | 32 |
|
12 | 33 | def get_required_env_var(name: str) -> str: |
@@ -67,3 +88,58 @@ def athena_options() -> AthenaOptions: |
67 | 88 | keepalive_interval=30.0, # Longer intervals for persistent streams |
68 | 89 | affiliate="Crisp", |
69 | 90 | ) |
| 91 | + |
| 92 | + |
| 93 | +@pytest.fixture |
| 94 | +def formatted_images() -> list[tuple[bytes, str]]: |
| 95 | + if (magick_path := shutil.which("magick")) is None: |
| 96 | + msg = ( |
| 97 | + "ImageMagick 'magick' command not found - cannot run " |
| 98 | + "multi-format test" |
| 99 | + ) |
| 100 | + raise AssertionError(msg) |
| 101 | + |
| 102 | + images = [] |
| 103 | + this_dir = Path(__file__).resolve() |
| 104 | + image_dir = this_dir.parent / "../test_support/images/" |
| 105 | + |
| 106 | + if not image_dir.exists(): |
| 107 | + image_dir.mkdir(parents=True) |
| 108 | + if not image_dir.is_dir(): |
| 109 | + msg = f"Image directory {image_dir} is not a directory" |
| 110 | + raise AssertionError(msg) |
| 111 | + |
| 112 | + base_image_format = "png" |
| 113 | + base_image = create_test_image(448, 448, img_format=base_image_format) |
| 114 | + base_image_path = image_dir / "base_image.png" |
| 115 | + with base_image_path.open("wb") as f: |
| 116 | + f.write(base_image) |
| 117 | + |
| 118 | + for img_format in IMAGEMAGICK_FORMATS: |
| 119 | + if img_format == base_image_format: |
| 120 | + continue # base image is already generated. |
| 121 | + |
| 122 | + converted_image_path = image_dir / f"test_image.{img_format}" |
| 123 | + if converted_image_path.exists(): |
| 124 | + continue # already generated - probably from previous run. |
| 125 | + |
| 126 | + cmd = f'magick "{base_image_path}" "{converted_image_path}"' |
| 127 | + subprocess.run( # noqa: S603 - false positive :( |
| 128 | + [magick_path, str(base_image_path), str(converted_image_path)], |
| 129 | + check=True, |
| 130 | + shell=False, |
| 131 | + ) |
| 132 | + |
| 133 | + if not converted_image_path.exists(): |
| 134 | + msg = f"Failed to create {img_format} image with command: {cmd}" |
| 135 | + raise AssertionError(msg) |
| 136 | + |
| 137 | + for path in image_dir.iterdir(): |
| 138 | + if path.is_file(): |
| 139 | + with path.open("rb") as f: |
| 140 | + img_bytes = f.read() |
| 141 | + images.append((img_bytes, path.suffix.lstrip("."))) |
| 142 | + |
| 143 | + raw_uint8 = create_test_image(448, 448, img_format="raw_uint8") |
| 144 | + images.append((raw_uint8, "raw_uint8")) |
| 145 | + return images |
0 commit comments