Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

4.1 KB
Raw Blame
from __future__ import annotations

from unittest.mock import MagicMock, patch

import pytest

from marimo._dependencies.dependencies import DependencyManager
from marimo._output.formatters.ipython_formatters import (
    IPythonFormatter,
    ReprMimeBundle,
)
from marimo._output.formatting import get_formatter

HAS_DEPS = DependencyManager.ipython.has()


@pytest.mark.skipif(not HAS_DEPS, reason="IPython not installed")
def test_register_html_display():
    from IPython.display import HTML

    IPythonFormatter().register()

    # Test HTML with direct content
    html = HTML("<div>Hello World</div>")
    formatter = get_formatter(html)
    assert formatter is not None
    result = formatter(html)
    assert result == ("text/html", "<div>Hello World</div>")


@pytest.mark.skipif(not HAS_DEPS, reason="IPython not installed")
@patch("marimo._runtime.output._output.append")
def test_display_patch(mock_append: MagicMock):
    from IPython.display import HTML, display

    unpatch = IPythonFormatter().register()
    try:
        # Test regular display
        obj = HTML("<div>Test</div>")
        display(obj)
        mock_append.assert_called_once_with(obj)
        mock_append.reset_mock()

        # Test raw mimebundle display
        mimebundle = {"text/html": "<div>Raw Test</div>"}
        display(mimebundle, raw=True)
        mock_append.assert_called_once()
        assert isinstance(mock_append.call_args[0][0], ReprMimeBundle)
        assert mock_append.call_args[0][0].data == mimebundle
    finally:
        unpatch()


@pytest.mark.skipif(not HAS_DEPS, reason="IPython not installed")
def test_repr_mimebundle():
    data = {"text/html": "<div>Test</div>", "text/plain": "Test"}
    bundle = ReprMimeBundle(data)
    assert bundle._repr_mimebundle_() == data
    # Test that include/exclude params are ignored
    assert bundle._repr_mimebundle_(include=["text/html"]) == data


@pytest.mark.skipif(not HAS_DEPS, reason="IPython not installed")
def test_format_image_gif_bytes():
    from IPython.display import Image

    IPythonFormatter().register()

    # Minimal valid GIF
    gif_bytes = (
        b"GIF89a\x01\x00\x01\x00\x80\x00\x00\xff\xff\xff\x00\x00\x00"
        b"!\xf9\x04\x00\x00\x00\x00\x00,\x00\x00\x00\x00\x01\x00"
        b"\x01\x00\x00\x02\x02D\x01\x00;"
    )
    img = Image(data=gif_bytes, format="gif")
    formatter = get_formatter(img)
    assert formatter is not None
    mime_type, data = formatter(img)
    assert mime_type == "image/gif"
    assert data.startswith("data:image/gif;base64,")


@pytest.mark.skipif(not HAS_DEPS, reason="IPython not installed")
def test_format_image_png_bytes():
    from IPython.display import Image

    IPythonFormatter().register()

    # Minimal valid PNG (1x1 white pixel)
    import base64

    png_b64 = (
        "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4"
        "2mP8/58BAwAI/AL+hc2rNAAAAABJRU5ErkJggg=="
    )
    png_bytes = base64.b64decode(png_b64)
    img = Image(data=png_bytes, format="png")
    formatter = get_formatter(img)
    assert formatter is not None
    mime_type, data = formatter(img)
    assert mime_type == "image/png"
    assert data.startswith("data:image/png;base64,")


@pytest.mark.skipif(not HAS_DEPS, reason="IPython not installed")
def test_format_image_url():
    from IPython.display import Image

    IPythonFormatter().register()

    img = Image(url="https://example.com/img.gif")
    formatter = get_formatter(img)
    assert formatter is not None
    mime_type, data = formatter(img)
    assert mime_type == "text/html"
    assert "https://example.com/img.gif" in data
    assert "<img" in data


@pytest.mark.skipif(not HAS_DEPS, reason="IPython not installed")
@patch("marimo._runtime.output._output.append")
def test_display_html(mock_append: MagicMock):
    from IPython.display import display_html

    unpatch = IPythonFormatter().register()

    try:
        display_html("<div>Test</div>", raw=True)
        mock_append.assert_called_once()
        assert isinstance(mock_append.call_args[0][0], ReprMimeBundle)
        assert mock_append.call_args[0][0].data == {
            "text/html": "<div>Test</div>"
        }
    finally:
        unpatch()