|
| 1 | +import unittest.mock |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +import solara |
| 6 | +import solara.widgets |
| 7 | + |
| 8 | + |
| 9 | +class MockChart: |
| 10 | + """Mock Altair chart that returns a configurable mimebundle.""" |
| 11 | + |
| 12 | + def __init__(self, bundle): |
| 13 | + self._bundle = bundle |
| 14 | + |
| 15 | + def _repr_mimebundle_(self): |
| 16 | + return (self._bundle, {}) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def mock_altair_renderers(): |
| 21 | + """Mock altair.renderers.enable to be a no-op context manager.""" |
| 22 | + import contextlib |
| 23 | + |
| 24 | + import altair as alt |
| 25 | + |
| 26 | + @contextlib.contextmanager |
| 27 | + def mock_enable(renderer): |
| 28 | + yield |
| 29 | + |
| 30 | + with unittest.mock.patch.object(alt.renderers, "enable", mock_enable): |
| 31 | + yield |
| 32 | + |
| 33 | + |
| 34 | +def test_figure_altair_v4(mock_altair_renderers): |
| 35 | + spec = {"$schema": "https://vega.github.io/schema/vega-lite/v4.json", "data": {"values": []}} |
| 36 | + chart = MockChart({"application/vnd.vegalite.v4+json": spec}) |
| 37 | + |
| 38 | + el = solara.FigureAltair(chart) |
| 39 | + _, rc = solara.render(el, handle_error=False) |
| 40 | + widget = rc.find(solara.widgets.VegaLite).widget |
| 41 | + assert widget.spec == spec |
| 42 | + rc.close() |
| 43 | + |
| 44 | + |
| 45 | +def test_figure_altair_v5(mock_altair_renderers): |
| 46 | + spec = {"$schema": "https://vega.github.io/schema/vega-lite/v5.json", "data": {"values": []}} |
| 47 | + chart = MockChart({"application/vnd.vegalite.v5+json": spec}) |
| 48 | + |
| 49 | + el = solara.FigureAltair(chart) |
| 50 | + _, rc = solara.render(el, handle_error=False) |
| 51 | + widget = rc.find(solara.widgets.VegaLite).widget |
| 52 | + assert widget.spec == spec |
| 53 | + rc.close() |
| 54 | + |
| 55 | + |
| 56 | +def test_figure_altair_v6(mock_altair_renderers): |
| 57 | + """Test that Altair 6+ with v6 MIME type works (uses .json suffix instead of +json).""" |
| 58 | + spec = {"$schema": "https://vega.github.io/schema/vega-lite/v6.json", "data": {"values": []}} |
| 59 | + # Altair 6 uses ".json" suffix instead of "+json" |
| 60 | + chart = MockChart({"application/vnd.vegalite.v6.json": spec}) |
| 61 | + |
| 62 | + el = solara.FigureAltair(chart) |
| 63 | + _, rc = solara.render(el, handle_error=False) |
| 64 | + widget = rc.find(solara.widgets.VegaLite).widget |
| 65 | + assert widget.spec == spec |
| 66 | + rc.close() |
| 67 | + |
| 68 | + |
| 69 | +def test_figure_altair_no_vegalite_mime(mock_altair_renderers): |
| 70 | + """Test that a KeyError is raised when no Vega-Lite MIME type is found.""" |
| 71 | + chart = MockChart({"text/plain": "some text"}) |
| 72 | + |
| 73 | + el = solara.FigureAltair(chart) |
| 74 | + with pytest.raises(KeyError, match="No Vega-Lite MIME type found"): |
| 75 | + solara.render(el, handle_error=False) |
0 commit comments