|
1 |
| -from base64 import b64decode, b64encode |
2 |
| - |
| 1 | +from base64 import b64encode |
| 2 | +from pathlib import Path |
3 | 3 |
|
| 4 | +import pytest |
4 | 5 | from ollama._types import Image
|
| 6 | +import tempfile |
5 | 7 |
|
6 | 8 |
|
7 |
| -def test_image_serialization(): |
8 |
| - # Test bytes serialization |
| 9 | +def test_image_serialization_bytes(): |
9 | 10 | image_bytes = b'test image bytes'
|
| 11 | + encoded_string = b64encode(image_bytes).decode() |
10 | 12 | img = Image(value=image_bytes)
|
11 |
| - assert img.model_dump() == b64encode(image_bytes).decode() |
| 13 | + assert img.model_dump() == encoded_string |
| 14 | + |
12 | 15 |
|
13 |
| - # Test base64 string serialization |
| 16 | +def test_image_serialization_base64_string(): |
14 | 17 | b64_str = 'dGVzdCBiYXNlNjQgc3RyaW5n'
|
15 | 18 | img = Image(value=b64_str)
|
16 |
| - assert img.model_dump() == b64decode(b64_str).decode() |
| 19 | + assert img.model_dump() == b64_str # Should return as-is if valid base64 |
| 20 | + |
| 21 | + |
| 22 | +def test_image_serialization_plain_string(): |
| 23 | + img = Image(value='not a path or base64') |
| 24 | + assert img.model_dump() == 'not a path or base64' # Should return as-is |
| 25 | + |
| 26 | + |
| 27 | +def test_image_serialization_path(): |
| 28 | + with tempfile.NamedTemporaryFile() as temp_file: |
| 29 | + temp_file.write(b'test file content') |
| 30 | + temp_file.flush() |
| 31 | + img = Image(value=Path(temp_file.name)) |
| 32 | + assert img.model_dump() == b64encode(b'test file content').decode() |
| 33 | + |
| 34 | + |
| 35 | +def test_image_serialization_string_path(): |
| 36 | + with tempfile.NamedTemporaryFile() as temp_file: |
| 37 | + temp_file.write(b'test file content') |
| 38 | + temp_file.flush() |
| 39 | + img = Image(value=temp_file.name) |
| 40 | + assert img.model_dump() == b64encode(b'test file content').decode() |
| 41 | + |
| 42 | + with pytest.raises(ValueError): |
| 43 | + img = Image(value='some_path/that/does/not/exist.png') |
| 44 | + img.model_dump() |
| 45 | + |
| 46 | + with pytest.raises(ValueError): |
| 47 | + img = Image(value='not an image') |
| 48 | + img.model_dump() |
0 commit comments