def test_set_base64_and_mimetype_for_image_docs(tmp_path: Path):
    """Test setting base64 and mimetype fields for ImageDocument objects."""
    content = httpx.get(EXP_IMAGE_URLS[0]).content
    expected_b64 = base64.b64encode(content).decode("utf-8")
    fl_path = tmp_path / "test_image.png"
    fl_path.write_bytes(content)
    image_docs = [
        ImageDocument(image=expected_b64),
        ImageDocument(image_path=fl_path.__str__()),
    ]

    with patch("requests.get") as mock_get:
        mock_get.return_value.content = EXP_BINARY
        # patch os.path.isfile
        with patch("os.path.isfile", return_value=True):
            with patch("builtins.open", mock_open(read_data=EXP_BINARY)):
                results = set_base64_and_mimetype_for_image_docs(image_docs)

    assert len(results) == 2
    assert results[0].image == expected_b64
    assert results[0].image_mimetype == "image/jpeg"
    assert results[1].image_mimetype == "image/jpeg"
Test setting base64 and mimetype fields for ImageDocument objects.
llama-index-core/tests/multi_modal_llms/test_generic_utils.py