|
| 1 | +"""Test the display method on BaseChart.""" |
| 2 | + |
| 3 | +from unittest.mock import MagicMock, patch |
| 4 | + |
| 5 | +import pytest |
| 6 | +from IPython.display import IFrame |
| 7 | + |
| 8 | +from datawrapper.charts.base import BaseChart |
| 9 | + |
| 10 | + |
| 11 | +def test_base_chart_display_method_exists(): |
| 12 | + """Test that the display method exists on BaseChart.""" |
| 13 | + chart = BaseChart(chart_type="d3-lines", title="Test Chart") |
| 14 | + assert hasattr(chart, "display") |
| 15 | + assert callable(chart.display) |
| 16 | + |
| 17 | + |
| 18 | +def test_base_chart_display_requires_chart_id(): |
| 19 | + """Test that display raises ValueError when chart_id is not set.""" |
| 20 | + chart = BaseChart(chart_type="d3-lines", title="Test Chart") |
| 21 | + |
| 22 | + with pytest.raises(ValueError, match="No chart_id set"): |
| 23 | + chart.display() |
| 24 | + |
| 25 | + |
| 26 | +def test_base_chart_display_with_chart_id(): |
| 27 | + """Test that display works when chart_id is set.""" |
| 28 | + # Create a chart with a chart_id |
| 29 | + chart = BaseChart( |
| 30 | + chart_type="d3-lines", |
| 31 | + title="Test Display Chart", |
| 32 | + data=[{"x": 1, "y": 2}, {"x": 2, "y": 4}], |
| 33 | + ) |
| 34 | + chart.chart_id = "test123" |
| 35 | + |
| 36 | + # Mock the client and its display_chart method |
| 37 | + mock_client = MagicMock() |
| 38 | + mock_iframe = IFrame("https://example.com", width=600, height=400) |
| 39 | + mock_client.display_chart.return_value = mock_iframe |
| 40 | + |
| 41 | + with patch.object(chart, "_get_client", return_value=mock_client): |
| 42 | + # Call the display method |
| 43 | + result = chart.display() |
| 44 | + |
| 45 | + # Verify the client method was called |
| 46 | + mock_client.display_chart.assert_called_once() |
| 47 | + |
| 48 | + # Verify the chart_id was passed |
| 49 | + call_kwargs = mock_client.display_chart.call_args.kwargs |
| 50 | + assert call_kwargs["chart_id"] == "test123" |
| 51 | + |
| 52 | + # Verify the result is an IFrame |
| 53 | + assert result == mock_iframe |
| 54 | + assert isinstance(result, IFrame) |
| 55 | + |
| 56 | + |
| 57 | +def test_base_chart_display_with_access_token(): |
| 58 | + """Test that display passes access_token to the client.""" |
| 59 | + # Create a chart with a chart_id |
| 60 | + chart = BaseChart( |
| 61 | + chart_type="d3-lines", |
| 62 | + title="Test Display Chart with Token", |
| 63 | + data=[{"x": 1, "y": 2}, {"x": 2, "y": 4}], |
| 64 | + ) |
| 65 | + chart.chart_id = "test123" |
| 66 | + |
| 67 | + # Mock the client and its display_chart method |
| 68 | + mock_client = MagicMock() |
| 69 | + mock_iframe = IFrame("https://example.com", width=600, height=400) |
| 70 | + mock_client.display_chart.return_value = mock_iframe |
| 71 | + |
| 72 | + with patch.object( |
| 73 | + chart, "_get_client", return_value=mock_client |
| 74 | + ) as mock_get_client: |
| 75 | + # Call the display method with an access token |
| 76 | + test_token = "test_access_token" |
| 77 | + result = chart.display(access_token=test_token) |
| 78 | + |
| 79 | + # Verify _get_client was called with the access token |
| 80 | + mock_get_client.assert_called_once_with(test_token) |
| 81 | + |
| 82 | + # Verify the client method was called |
| 83 | + mock_client.display_chart.assert_called_once() |
| 84 | + |
| 85 | + # Verify the chart_id was passed |
| 86 | + call_kwargs = mock_client.display_chart.call_args.kwargs |
| 87 | + assert call_kwargs["chart_id"] == "test123" |
| 88 | + |
| 89 | + # Verify the result is an IFrame |
| 90 | + assert result == mock_iframe |
| 91 | + assert isinstance(result, IFrame) |
0 commit comments