Skip to content

Commit 5e17caf

Browse files
committed
rearrange callback tests
get some more of them out of the old test_integration
1 parent 3006905 commit 5e17caf

File tree

4 files changed

+332
-355
lines changed

4 files changed

+332
-355
lines changed

tests/integration/callbacks/test_basic_callback.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,53 @@ def set_path(n):
342342
if not refresh:
343343
dash_duo.find_element("#btn").click()
344344
dash_duo.wait_for_text_to_equal("#out", '[{"a": "/2:a"}] - /2')
345+
346+
347+
def test_cbsc008_wildcard_prop_callbacka(dash_duo):
348+
app = dash.Dash(__name__)
349+
app.layout = html.Div(
350+
[
351+
dcc.Input(id="input", value="initial value"),
352+
html.Div(
353+
html.Div(
354+
[
355+
1.5,
356+
None,
357+
"string",
358+
html.Div(
359+
id="output-1",
360+
**{"data-cb": "initial value", "aria-cb": "initial value"}
361+
),
362+
]
363+
)
364+
),
365+
]
366+
)
367+
368+
input_call_count = Value("i", 0)
369+
370+
@app.callback(Output("output-1", "data-cb"), [Input("input", "value")])
371+
def update_data(value):
372+
input_call_count.value += 1
373+
return value
374+
375+
@app.callback(Output("output-1", "children"), [Input("output-1", "data-cb")])
376+
def update_text(data):
377+
return data
378+
379+
dash_duo.start_server(app)
380+
dash_duo.wait_for_text_to_equal("#output-1", "initial value")
381+
dash_duo.percy_snapshot(name="wildcard-callback-1")
382+
383+
input1 = dash_duo.find_element("#input")
384+
dash_duo.clear_input(input1)
385+
input1.send_keys("hello world")
386+
387+
dash_duo.wait_for_text_to_equal("#output-1", "hello world")
388+
dash_duo.percy_snapshot(name="wildcard-callback-2")
389+
390+
# an initial call, one for clearing the input
391+
# and one for each hello world character
392+
assert input_call_count.value == 2 + len("hello world")
393+
394+
assert not dash_duo.get_logs()
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
from multiprocessing import Value
2+
3+
from copy import copy
4+
from selenium.webdriver.common.keys import Keys
5+
6+
import dash_core_components as dcc
7+
import dash_html_components as html
8+
from dash import Dash, no_update
9+
from dash.dependencies import Input, Output, State
10+
from dash.exceptions import PreventUpdate
11+
12+
from dash.testing.wait import until
13+
14+
15+
def test_cbpu001_aborted_callback(dash_duo):
16+
"""Raising PreventUpdate OR returning no_update prevents update and
17+
triggering dependencies."""
18+
19+
initial_input = "initial input"
20+
initial_output = "initial output"
21+
22+
app = Dash(__name__)
23+
app.layout = html.Div(
24+
[
25+
dcc.Input(id="input", value=initial_input),
26+
html.Div(initial_output, id="output1"),
27+
html.Div(initial_output, id="output2"),
28+
]
29+
)
30+
31+
callback1_count = Value("i", 0)
32+
callback2_count = Value("i", 0)
33+
34+
@app.callback(Output("output1", "children"), [Input("input", "value")])
35+
def callback1(value):
36+
callback1_count.value += 1
37+
if callback1_count.value > 2:
38+
return no_update
39+
raise PreventUpdate("testing callback does not update")
40+
return value
41+
42+
@app.callback(Output("output2", "children"), [Input("output1", "children")])
43+
def callback2(value):
44+
callback2_count.value += 1
45+
return value
46+
47+
dash_duo.start_server(app)
48+
49+
input_ = dash_duo.find_element("#input")
50+
input_.send_keys("xyz")
51+
dash_duo.wait_for_text_to_equal("#input", "initial inputxyz")
52+
53+
until(
54+
lambda: callback1_count.value == 4,
55+
timeout=3,
56+
msg="callback1 runs 4x (initial page load and 3x through send_keys)",
57+
)
58+
59+
assert (
60+
callback2_count.value == 0
61+
), "callback2 is never triggered, even on initial load"
62+
63+
# double check that output1 and output2 children were not updated
64+
assert dash_duo.find_element("#output1").text == initial_output
65+
assert dash_duo.find_element("#output2").text == initial_output
66+
67+
assert not dash_duo.get_logs()
68+
69+
dash_duo.percy_snapshot(name="aborted")
70+
71+
72+
def test_cbpu002_multi_output_no_update(dash_duo):
73+
app = Dash(__name__)
74+
75+
app.layout = html.Div(
76+
[
77+
html.Button("B", "btn"),
78+
html.P("initial1", "n1"),
79+
html.P("initial2", "n2"),
80+
html.P("initial3", "n3"),
81+
]
82+
)
83+
84+
@app.callback(
85+
[Output("n1", "children"), Output("n2", "children"), Output("n3", "children")],
86+
[Input("btn", "n_clicks")],
87+
)
88+
def show_clicks(n):
89+
# partial or complete cancelation of updates via no_update
90+
return [
91+
no_update if n and n > 4 else n,
92+
no_update if n and n > 2 else n,
93+
# make a new instance, to mock up caching and restoring no_update
94+
copy(no_update),
95+
]
96+
97+
dash_duo.start_server(app)
98+
99+
dash_duo.multiple_click("#btn", 10)
100+
101+
dash_duo.wait_for_text_to_equal("#n1", "4")
102+
dash_duo.wait_for_text_to_equal("#n2", "2")
103+
dash_duo.wait_for_text_to_equal("#n3", "initial3")
104+
105+
106+
def test_cbpu003_no_update_chains(dash_duo):
107+
app = Dash(__name__)
108+
109+
app.layout = html.Div(
110+
[
111+
dcc.Input(id="a_in", value="a"),
112+
dcc.Input(id="b_in", value="b"),
113+
html.P("", id="a_out"),
114+
html.P("", id="a_out_short"),
115+
html.P("", id="b_out"),
116+
html.P("", id="ab_out"),
117+
]
118+
)
119+
120+
@app.callback(
121+
[Output("a_out", "children"), Output("a_out_short", "children")],
122+
[Input("a_in", "value")],
123+
)
124+
def a_out(a):
125+
return a, a if len(a) < 3 else no_update
126+
127+
@app.callback(Output("b_out", "children"), [Input("b_in", "value")])
128+
def b_out(b):
129+
return b
130+
131+
@app.callback(
132+
Output("ab_out", "children"),
133+
[Input("a_out_short", "children")],
134+
[State("b_out", "children")],
135+
)
136+
def ab_out(a, b):
137+
return a + " " + b
138+
139+
dash_duo.start_server(app)
140+
141+
a_in = dash_duo.find_element("#a_in")
142+
b_in = dash_duo.find_element("#b_in")
143+
144+
b_in.send_keys("b")
145+
a_in.send_keys("a")
146+
dash_duo.wait_for_text_to_equal("#a_out", "aa")
147+
dash_duo.wait_for_text_to_equal("#b_out", "bb")
148+
dash_duo.wait_for_text_to_equal("#a_out_short", "aa")
149+
dash_duo.wait_for_text_to_equal("#ab_out", "aa bb")
150+
151+
b_in.send_keys("b")
152+
a_in.send_keys("a")
153+
dash_duo.wait_for_text_to_equal("#a_out", "aaa")
154+
dash_duo.wait_for_text_to_equal("#b_out", "bbb")
155+
dash_duo.wait_for_text_to_equal("#a_out_short", "aa")
156+
# ab_out has not been triggered because a_out_short received no_update
157+
dash_duo.wait_for_text_to_equal("#ab_out", "aa bb")
158+
159+
b_in.send_keys("b")
160+
a_in.send_keys(Keys.END)
161+
a_in.send_keys(Keys.BACKSPACE)
162+
dash_duo.wait_for_text_to_equal("#a_out", "aa")
163+
dash_duo.wait_for_text_to_equal("#b_out", "bbbb")
164+
dash_duo.wait_for_text_to_equal("#a_out_short", "aa")
165+
# now ab_out *is* triggered - a_out_short got a new value
166+
# even though that value is the same as the last value it got
167+
dash_duo.wait_for_text_to_equal("#ab_out", "aa bbbb")
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import pytest
2+
3+
import dash_html_components as html
4+
5+
from dash import Dash
6+
7+
from dash.dependencies import Input, Output, State
8+
from dash.exceptions import InvalidCallbackReturnValue, IncorrectTypeException
9+
10+
11+
def test_cbva001_callback_dep_types():
12+
app = Dash(__name__)
13+
app.layout = html.Div(
14+
[
15+
html.Div("child", id="in1"),
16+
html.Div("state", id="state1"),
17+
html.Div(id="out1"),
18+
html.Div("child", id="in2"),
19+
html.Div("state", id="state2"),
20+
html.Div(id="out2"),
21+
html.Div("child", id="in3"),
22+
html.Div("state", id="state3"),
23+
html.Div(id="out3"),
24+
]
25+
)
26+
27+
with pytest.raises(IncorrectTypeException):
28+
29+
@app.callback([[Output("out1", "children")]], [Input("in1", "children")])
30+
def f(i):
31+
return i
32+
33+
pytest.fail("extra output nesting")
34+
35+
# all OK with tuples
36+
@app.callback(
37+
(Output("out1", "children"),),
38+
(Input("in1", "children"),),
39+
(State("state1", "children"),),
40+
)
41+
def f1(i):
42+
return i
43+
44+
# all OK with all args in single list
45+
@app.callback(
46+
Output("out2", "children"),
47+
Input("in2", "children"),
48+
State("state2", "children"),
49+
)
50+
def f2(i):
51+
return i
52+
53+
# all OK with lists
54+
@app.callback(
55+
[Output("out3", "children")],
56+
[Input("in3", "children")],
57+
[State("state3", "children")],
58+
)
59+
def f3(i):
60+
return i
61+
62+
63+
def test_cbva002_callback_return_validation():
64+
app = Dash(__name__)
65+
app.layout = html.Div(
66+
[
67+
html.Div(id="a"),
68+
html.Div(id="b"),
69+
html.Div(id="c"),
70+
html.Div(id="d"),
71+
html.Div(id="e"),
72+
html.Div(id="f"),
73+
]
74+
)
75+
76+
@app.callback(Output("b", "children"), [Input("a", "children")])
77+
def single(a):
78+
return set([1])
79+
80+
with pytest.raises(InvalidCallbackReturnValue):
81+
# outputs_list (normally callback_context.outputs_list) is provided
82+
# by the dispatcher from the request.
83+
single("aaa", outputs_list={"id": "b", "property": "children"})
84+
pytest.fail("not serializable")
85+
86+
@app.callback(
87+
[Output("c", "children"), Output("d", "children")], [Input("a", "children")]
88+
)
89+
def multi(a):
90+
return [1, set([2])]
91+
92+
with pytest.raises(InvalidCallbackReturnValue):
93+
outputs_list = [
94+
{"id": "c", "property": "children"},
95+
{"id": "d", "property": "children"},
96+
]
97+
multi("aaa", outputs_list=outputs_list)
98+
pytest.fail("nested non-serializable")
99+
100+
@app.callback(
101+
[Output("e", "children"), Output("f", "children")], [Input("a", "children")]
102+
)
103+
def multi2(a):
104+
return ["abc"]
105+
106+
with pytest.raises(InvalidCallbackReturnValue):
107+
outputs_list = [
108+
{"id": "e", "property": "children"},
109+
{"id": "f", "property": "children"},
110+
]
111+
multi2("aaa", outputs_list=outputs_list)
112+
pytest.fail("wrong-length list")

0 commit comments

Comments
 (0)