|
| 1 | +import pytest |
| 2 | +import copy |
| 3 | +import pickle |
| 4 | + |
| 5 | +from plotly.tools import make_subplots |
| 6 | +import plotly.graph_objs as go |
| 7 | +import plotly.io as pio |
| 8 | + |
| 9 | + |
| 10 | +# fixtures |
| 11 | +# -------- |
| 12 | +@pytest.fixture |
| 13 | +def fig1(request): |
| 14 | + return go.Figure(data=[{'type': 'scattergl', |
| 15 | + 'marker': {'color': 'green'}}, |
| 16 | + {'type': 'parcoords', |
| 17 | + 'dimensions': [{'values': [1, 2, 3]}, |
| 18 | + {'values': [3, 2, 1]}], |
| 19 | + 'line': {'color': 'blue'}}], |
| 20 | + layout={'title': 'Figure title'}) |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def fig_subplots(request): |
| 25 | + fig = make_subplots(3, 2) |
| 26 | + fig.add_scatter(y=[2, 1, 3], row=1, col=1) |
| 27 | + fig.add_scatter(y=[1, 3, 3], row=2, col=2) |
| 28 | + return fig |
| 29 | + |
| 30 | + |
| 31 | +# Deep copy |
| 32 | +# --------- |
| 33 | +def test_deepcopy_figure(fig1): |
| 34 | + fig_copied = copy.deepcopy(fig1) |
| 35 | + |
| 36 | + # Contents should be equal |
| 37 | + assert pio.to_json(fig_copied) == pio.to_json(fig1) |
| 38 | + |
| 39 | + # Identities should be distinct |
| 40 | + assert fig_copied is not fig1 |
| 41 | + assert fig_copied.layout is not fig1.layout |
| 42 | + assert fig_copied.data is not fig1.data |
| 43 | + |
| 44 | + |
| 45 | +def test_deepcopy_figure_subplots(fig_subplots): |
| 46 | + fig_copied = copy.deepcopy(fig_subplots) |
| 47 | + |
| 48 | + # Contents should be equal |
| 49 | + assert pio.to_json(fig_copied) == pio.to_json(fig_subplots) |
| 50 | + |
| 51 | + # Subplot metadata should be equal |
| 52 | + assert fig_subplots._grid_ref == fig_copied._grid_ref |
| 53 | + assert fig_subplots._grid_str == fig_copied._grid_str |
| 54 | + |
| 55 | + # Identities should be distinct |
| 56 | + assert fig_copied is not fig_subplots |
| 57 | + assert fig_copied.layout is not fig_subplots.layout |
| 58 | + assert fig_copied.data is not fig_subplots.data |
| 59 | + |
| 60 | + # Should be possible to add new trace to subplot location |
| 61 | + fig_subplots.add_bar(y=[0, 0, 1], row=1, col=2) |
| 62 | + fig_copied.add_bar(y=[0, 0, 1], row=1, col=2) |
| 63 | + |
| 64 | + # And contents should be still equal |
| 65 | + assert pio.to_json(fig_copied) == pio.to_json(fig_subplots) |
| 66 | + |
| 67 | + |
| 68 | +def test_deepcopy_layout(fig1): |
| 69 | + copied_layout = copy.deepcopy(fig1.layout) |
| 70 | + |
| 71 | + # Contents should be equal |
| 72 | + assert copied_layout == fig1.layout |
| 73 | + |
| 74 | + # Identities should not |
| 75 | + assert copied_layout is not fig1.layout |
| 76 | + |
| 77 | + # Original layout should still have fig1 as parent |
| 78 | + assert fig1.layout.parent is fig1 |
| 79 | + |
| 80 | + # Copied layout should have no parent |
| 81 | + assert copied_layout.parent is None |
| 82 | + |
| 83 | + |
| 84 | +# Pickling |
| 85 | +# -------- |
| 86 | +def test_pickle_figure_round_trip(fig1): |
| 87 | + fig_copied = pickle.loads(pickle.dumps(fig1)) |
| 88 | + |
| 89 | + # Contents should be equal |
| 90 | + assert pio.to_json(fig_copied) == pio.to_json(fig1) |
| 91 | + |
| 92 | + |
| 93 | +def test_pickle_figure_subplots_round_trip(fig_subplots): |
| 94 | + fig_copied = pickle.loads(pickle.dumps(fig_subplots)) |
| 95 | + |
| 96 | + # Contents should be equal |
| 97 | + assert pio.to_json(fig_copied) == pio.to_json(fig_subplots) |
| 98 | + |
| 99 | + # Should be possible to add new trace to subplot location |
| 100 | + fig_subplots.add_bar(y=[0, 0, 1], row=1, col=2) |
| 101 | + fig_copied.add_bar(y=[0, 0, 1], row=1, col=2) |
| 102 | + |
| 103 | + # And contents should be still equal |
| 104 | + assert pio.to_json(fig_copied) == pio.to_json(fig_subplots) |
| 105 | + |
| 106 | + |
| 107 | +def test_pickle_layout(fig1): |
| 108 | + copied_layout = pickle.loads(pickle.dumps(fig1.layout)) |
| 109 | + |
| 110 | + # Contents should be equal |
| 111 | + assert copied_layout == fig1.layout |
| 112 | + |
| 113 | + # Identities should not |
| 114 | + assert copied_layout is not fig1.layout |
| 115 | + |
| 116 | + # Original layout should still have fig1 as parent |
| 117 | + assert fig1.layout.parent is fig1 |
| 118 | + |
| 119 | + # Copied layout should have no parent |
| 120 | + assert copied_layout.parent is None |
0 commit comments