|
1 | 1 | """Unit tests for _anthropic_utils.py.""" |
2 | 2 |
|
| 3 | +import base64 |
| 4 | +from unittest.mock import patch |
3 | 5 | import pytest |
4 | 6 | from anthropic.types import ( |
5 | 7 | RawContentBlockDeltaEvent, |
|
23 | 25 | _thinking_in_params, |
24 | 26 | ) |
25 | 27 |
|
| 28 | +from langchain_google_vertexai._anthropic_utils import _format_image |
| 29 | + |
26 | 30 |
|
27 | 31 | def test_format_message_anthropic_with_cache_control_in_kwargs() -> None: |
28 | 32 | """Test formatting a message with cache control in additional_kwargs.""" |
@@ -1309,3 +1313,34 @@ def test_tool_message_preserves_cache_control() -> None: |
1309 | 1313 | "tool_use_id": "call_1", |
1310 | 1314 | "cache_control": {"type": "ephemeral"}, |
1311 | 1315 | } |
| 1316 | + |
| 1317 | + |
| 1318 | +@pytest.mark.parametrize( |
| 1319 | + ("image_url", "expected_media_type"), |
| 1320 | + [ |
| 1321 | + ("https://example.com/image.png?token=123", "image/png"), |
| 1322 | + ("https://example.com/image.jpg", "image/jpeg"), |
| 1323 | + ("https://example.com/document.pdf", "application/pdf"), |
| 1324 | + ], |
| 1325 | +) |
| 1326 | +def test_format_image(image_url: str, expected_media_type: str) -> None: |
| 1327 | + """Test that _format_image correctly handles various URLs.""" |
| 1328 | + project = "test-project" |
| 1329 | + |
| 1330 | + with patch( |
| 1331 | + "langchain_google_vertexai._anthropic_utils.ImageBytesLoader" |
| 1332 | + ) as MockLoader: |
| 1333 | + mock_loader_instance = MockLoader.return_value |
| 1334 | + mock_loader_instance.load_bytes.return_value = b"fake_image_data" |
| 1335 | + |
| 1336 | + result = _format_image(image_url, project) |
| 1337 | + |
| 1338 | + expected_data = base64.b64encode(b"fake_image_data").decode("ascii") |
| 1339 | + |
| 1340 | + assert result == { |
| 1341 | + "type": "base64", |
| 1342 | + "media_type": expected_media_type, |
| 1343 | + "data": expected_data, |
| 1344 | + } |
| 1345 | + |
| 1346 | + mock_loader_instance.load_bytes.assert_called_once_with(image_url) |
0 commit comments