Spaces:
Running
Running
Didier Durand commited on
Commit ·
0e53c18
1
Parent(s): 08ed45c
Adding a few tests to Image type
Browse files
tests/utilities/test_types.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from typing import Annotated, Any
|
| 2 |
|
| 3 |
import pytest
|
|
@@ -149,6 +150,56 @@ class TestImage:
|
|
| 149 |
):
|
| 150 |
Image(path="test.png", data=b"test")
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
class TestFindKwargByType:
|
| 154 |
def test_exact_type_match(self):
|
|
|
|
| 1 |
+
import base64
|
| 2 |
from typing import Annotated, Any
|
| 3 |
|
| 4 |
import pytest
|
|
|
|
| 150 |
):
|
| 151 |
Image(path="test.png", data=b"test")
|
| 152 |
|
| 153 |
+
def test_get_mime_type_from_path(self, tmp_path):
|
| 154 |
+
"""Test MIME type detection from file extension."""
|
| 155 |
+
extensions = {
|
| 156 |
+
".png": "image/png",
|
| 157 |
+
".jpg": "image/jpeg",
|
| 158 |
+
".jpeg": "image/jpeg",
|
| 159 |
+
".gif": "image/gif",
|
| 160 |
+
".webp": "image/webp",
|
| 161 |
+
".unknown": "application/octet-stream",
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
for ext, mime in extensions.items():
|
| 165 |
+
path = tmp_path / f"test{ext}"
|
| 166 |
+
path.write_bytes(b"fake image data")
|
| 167 |
+
img = Image(path=path)
|
| 168 |
+
assert img._mime_type == mime
|
| 169 |
+
|
| 170 |
+
def test_to_image_content(self, tmp_path, monkeypatch):
|
| 171 |
+
"""Test conversion to ImageContent."""
|
| 172 |
+
# Test with path
|
| 173 |
+
img_path = tmp_path / "test.png"
|
| 174 |
+
test_data = b"fake image data"
|
| 175 |
+
img_path.write_bytes(test_data)
|
| 176 |
+
|
| 177 |
+
img = Image(path=img_path)
|
| 178 |
+
content = img.to_image_content()
|
| 179 |
+
|
| 180 |
+
assert content.type == "image"
|
| 181 |
+
assert content.mimeType == "image/png"
|
| 182 |
+
assert content.data == base64.b64encode(test_data).decode()
|
| 183 |
+
|
| 184 |
+
# Test with data
|
| 185 |
+
img = Image(data=test_data, format="jpeg")
|
| 186 |
+
content = img.to_image_content()
|
| 187 |
+
|
| 188 |
+
assert content.type == "image"
|
| 189 |
+
assert content.mimeType == "image/jpeg"
|
| 190 |
+
assert content.data == base64.b64encode(test_data).decode()
|
| 191 |
+
|
| 192 |
+
def test_to_image_content_error(self, monkeypatch):
|
| 193 |
+
"""Test error case in to_image_content."""
|
| 194 |
+
# Create an Image with neither path nor data (shouldn't happen due to __init__ checks,
|
| 195 |
+
# but testing the method's own error handling)
|
| 196 |
+
img = Image(data=b"test")
|
| 197 |
+
monkeypatch.setattr(img, "path", None)
|
| 198 |
+
monkeypatch.setattr(img, "data", None)
|
| 199 |
+
|
| 200 |
+
with pytest.raises(ValueError, match="No image data available"):
|
| 201 |
+
img.to_image_content()
|
| 202 |
+
|
| 203 |
|
| 204 |
class TestFindKwargByType:
|
| 205 |
def test_exact_type_match(self):
|