Skip to content

Commit 1d9994a

Browse files
committed
Add tests
1 parent bd0ce27 commit 1d9994a

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""
2+
Tests for the selection data that reaches an on_selection callback.
3+
4+
Plotly.js keys box and lasso selection data by axis id, so a selection in
5+
the second subplot arrives as {"x2": [...], "y2": [...]}. The widget
6+
resolves that payload against each trace's own axes.
7+
"""
8+
9+
import pytest
10+
11+
import plotly.graph_objs as go
12+
from plotly.subplots import make_subplots
13+
14+
try:
15+
go.FigureWidget()
16+
except ImportError:
17+
pytest.skip("FigureWidget is unavailable", allow_module_level=True)
18+
19+
20+
def two_subplots():
21+
"""Return a widget with one trace on x/y and another on x2/y2."""
22+
fig = make_subplots(rows=1, cols=2)
23+
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[2, 1, 3]), row=1, col=1)
24+
fig.add_trace(go.Scatter(x=[10, 11, 20, 21], y=[1, 2, 3, 4]), row=1, col=2)
25+
return go.FigureWidget(fig)
26+
27+
28+
def select(widget, selector_state, selector_type="box"):
29+
"""
30+
Fire a selection and return the selector each trace received.
31+
32+
The selection carries no points, because a box can cover a region of
33+
the figure that holds no data. That is the case the ranges matter most.
34+
"""
35+
selectors = []
36+
for trace in widget.data:
37+
trace.on_selection(lambda t, p, selector: selectors.append(selector))
38+
39+
widget._js2py_pointsCallback = {
40+
"event_type": "plotly_selected",
41+
"points": {"xs": [], "ys": [], "point_indexes": [], "trace_indexes": []},
42+
"device_state": None,
43+
"selector": {"type": selector_type, "selector_state": selector_state},
44+
}
45+
return selectors
46+
47+
48+
def test_box_selection_in_the_first_subplot():
49+
first, second = select(two_subplots(), {"x": [1.1, 2.9], "y": [0.5, 3.5]})
50+
51+
assert first.xrange == [1.1, 2.9]
52+
assert first.yrange == [0.5, 3.5]
53+
# The other subplot was not selected, so it reports nothing.
54+
assert second.xrange is None
55+
assert second.yrange is None
56+
57+
58+
def test_box_selection_in_the_second_subplot():
59+
first, second = select(two_subplots(), {"x2": [12.0, 19.0], "y2": [0.0, 5.0]})
60+
61+
assert second.xrange == [12.0, 19.0]
62+
assert second.yrange == [0.0, 5.0]
63+
assert first.xrange is None
64+
assert first.yrange is None
65+
66+
67+
def test_box_selection_in_a_figure_without_subplots():
68+
widget = go.FigureWidget(go.Scatter(x=[1, 2, 3], y=[3, 2, 1]))
69+
70+
(only,) = select(widget, {"x": [1.0, 2.0], "y": [1.5, 3.0]})
71+
72+
assert only.xrange == [1.0, 2.0]
73+
assert only.yrange == [1.5, 3.0]
74+
75+
76+
def test_lasso_selection_in_the_second_subplot():
77+
first, second = select(two_subplots(), {"x2": [1, 2, 3], "y2": [4, 5, 6]}, "lasso")
78+
79+
assert second.xs == [1, 2, 3]
80+
assert second.ys == [4, 5, 6]
81+
assert first.xs is None
82+
assert first.ys is None
83+
84+
85+
def test_older_frontend_sends_xrange_and_yrange():
86+
first, _ = select(two_subplots(), {"xrange": [1.0, 2.0], "yrange": [1.5, 3.0]})
87+
88+
assert first.xrange == [1.0, 2.0]
89+
assert first.yrange == [1.5, 3.0]
90+
91+
92+
def test_older_frontend_sends_xs_and_ys():
93+
first, _ = select(two_subplots(), {"xs": [1, 2, 3], "ys": [4, 5, 6]}, "lasso")
94+
95+
assert first.xs == [1, 2, 3]
96+
assert first.ys == [4, 5, 6]

0 commit comments

Comments
 (0)