-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathtest_patch.py
422 lines (343 loc) · 11 KB
/
test_patch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import json
from selenium.webdriver.common.keys import Keys
from dash import Dash, html, dcc, Input, Output, State, ALL, Patch
def test_pch001_patch_operations(dash_duo):
app = Dash(__name__)
app.layout = html.Div(
[
html.Div(
[
dcc.Input(id="set-value"),
html.Button("Set", id="set-btn"),
]
),
html.Div(
[
dcc.Input(id="append-value"),
html.Button("Append", id="append-btn"),
]
),
html.Div(
[
dcc.Input(id="prepend-value"),
html.Button("prepend", id="prepend-btn"),
]
),
html.Div(
[
dcc.Input(id="insert-value"),
dcc.Input(id="insert-index", type="number", value=1),
html.Button("insert", id="insert-btn"),
]
),
html.Div(
[
dcc.Input(id="extend-value"),
html.Button("extend", id="extend-btn"),
]
),
html.Div(
[
dcc.Input(id="merge-value"),
html.Button("Merge", id="merge-btn"),
]
),
html.Button("Delete", id="delete-btn"),
html.Button("Delete index", id="delete-index"),
html.Button("Clear", id="clear-btn"),
html.Button("Reverse", id="reverse-btn"),
html.Button("Remove", id="remove-btn"),
dcc.Store(
data={
"value": "unset",
"n_clicks": 0,
"array": ["initial"],
"delete": "Delete me",
},
id="store",
),
html.Div(id="store-content"),
]
)
app.clientside_callback(
"function(value) {return JSON.stringify(value)}",
Output("store-content", "children"),
Input("store", "data"),
)
@app.callback(
Output("store", "data"),
Input("set-btn", "n_clicks"),
State("set-value", "value"),
prevent_initial_call=True,
)
def on_click(_, value):
p = Patch()
p.value = value
p.n_clicks += 1
return p
@app.callback(
Output("store", "data", allow_duplicate=True),
Input("append-btn", "n_clicks"),
State("append-value", "value"),
prevent_initial_call=True,
)
def on_click(_, value):
p = Patch()
p.array.append(value)
p.n_clicks += 1
return p
@app.callback(
Output("store", "data", allow_duplicate=True),
Input("prepend-btn", "n_clicks"),
State("prepend-value", "value"),
prevent_initial_call=True,
)
def on_click(_, value):
p = Patch()
p.array.prepend(value)
p.n_clicks += 1
return p
@app.callback(
Output("store", "data", allow_duplicate=True),
Input("extend-btn", "n_clicks"),
State("extend-value", "value"),
prevent_initial_call=True,
)
def on_click(_, value):
p = Patch()
p.array.extend([value])
p.n_clicks += 1
return p
@app.callback(
Output("store", "data", allow_duplicate=True),
Input("merge-btn", "n_clicks"),
State("merge-value", "value"),
prevent_initial_call=True,
)
def on_click(_, value):
p = Patch()
p.update({"merged": value})
p.n_clicks += 1
return p
@app.callback(
Output("store", "data", allow_duplicate=True),
Input("delete-btn", "n_clicks"),
prevent_initial_call=True,
)
def on_click(_):
p = Patch()
del p.delete
return p
@app.callback(
Output("store", "data", allow_duplicate=True),
Input("insert-btn", "n_clicks"),
State("insert-value", "value"),
State("insert-index", "value"),
prevent_initial_call=True,
)
def on_insert(_, value, index):
p = Patch()
p.array.insert(index, value)
return p
@app.callback(
Output("store", "data", allow_duplicate=True),
Input("delete-index", "n_clicks"),
prevent_initial_call=True,
)
def on_click(_):
p = Patch()
del p.array[1]
del p.array[-2]
return p
@app.callback(
Output("store", "data", allow_duplicate=True),
Input("clear-btn", "n_clicks"),
prevent_initial_call=True,
)
def on_clear(_):
p = Patch()
p.array.clear()
return p
@app.callback(
Output("store", "data", allow_duplicate=True),
Input("reverse-btn", "n_clicks"),
prevent_initial_call=True,
)
def on_reverse(_):
p = Patch()
p.array.reverse()
return p
@app.callback(
Output("store", "data", allow_duplicate=True),
Input("remove-btn", "n_clicks"),
prevent_initial_call=True,
)
def on_remove(_):
p = Patch()
p.array.remove("initial")
return p
dash_duo.start_server(app)
assert dash_duo.get_logs() == []
def get_output():
e = dash_duo.find_element("#store-content")
return json.loads(e.text)
_input = dash_duo.find_element("#set-value")
_input.send_keys("Set Value")
dash_duo.find_element("#set-btn").click()
assert get_output()["value"] == "Set Value"
_input = dash_duo.find_element("#append-value")
_input.send_keys("Append")
dash_duo.find_element("#append-btn").click()
assert get_output()["array"] == ["initial", "Append"]
_input = dash_duo.find_element("#prepend-value")
_input.send_keys("Prepend")
dash_duo.find_element("#prepend-btn").click()
assert get_output()["array"] == ["Prepend", "initial", "Append"]
_input = dash_duo.find_element("#extend-value")
_input.send_keys("Extend")
dash_duo.find_element("#extend-btn").click()
assert get_output()["array"] == ["Prepend", "initial", "Append", "Extend"]
undef = object()
assert get_output().get("merge", undef) is undef
_input = dash_duo.find_element("#merge-value")
_input.send_keys("Merged")
dash_duo.find_element("#merge-btn").click()
assert get_output()["merged"] == "Merged"
assert get_output()["delete"] == "Delete me"
dash_duo.find_element("#delete-btn").click()
assert get_output().get("delete", undef) is undef
_input = dash_duo.find_element("#insert-value")
_input.send_keys("Inserted")
dash_duo.find_element("#insert-btn").click()
assert get_output().get("array") == [
"Prepend",
"Inserted",
"initial",
"Append",
"Extend",
]
_input.send_keys(" with negative index")
_input = dash_duo.find_element("#insert-index")
_input.send_keys(Keys.BACKSPACE)
_input.send_keys("-1")
dash_duo.find_element("#insert-btn").click()
assert get_output().get("array") == [
"Prepend",
"Inserted",
"initial",
"Append",
"Inserted with negative index",
"Extend",
]
dash_duo.find_element("#delete-index").click()
assert get_output().get("array") == [
"Prepend",
"initial",
"Append",
"Extend",
]
dash_duo.find_element("#reverse-btn").click()
assert get_output().get("array") == [
"Extend",
"Append",
"initial",
"Prepend",
]
dash_duo.find_element("#remove-btn").click()
assert get_output().get("array") == [
"Extend",
"Append",
"Prepend",
]
dash_duo.find_element("#clear-btn").click()
assert get_output()["array"] == []
def test_pch002_patch_app_pmc_callbacks(dash_duo):
app = Dash(__name__)
app.layout = html.Div(
[
html.Button("Click", id="click"),
html.Div(id={"type": "output", "index": 0}, className="first"),
html.Div(id={"type": "output", "index": 1}, className="second"),
]
)
@app.callback(
Output({"type": "output", "index": ALL}, "children"), Input("click", "n_clicks")
)
def on_click(n_clicks):
if n_clicks is None:
return ["Foo", "Bar"]
p1 = Patch()
p2 = Patch()
p1.append("Bar")
p2.prepend("Foo")
return [p1, p2]
dash_duo.start_server(app)
dash_duo.find_element("#click").click()
dash_duo.wait_for_text_to_equal(".first", "FooBar")
dash_duo.wait_for_text_to_equal(".second", "FooBar")
def test_pch003_patch_children(dash_duo):
app = Dash(__name__)
app.layout = html.Div(
[
html.Div(
[
dcc.Input(value="", id="children-value"),
html.Button("Add", id="add-children"),
]
),
html.Div([html.Div("init", id="initial")], id="output"),
]
)
@app.callback(
Output("output", "children"),
Input("add-children", "n_clicks"),
State("children-value", "value"),
prevent_initial_call=True,
)
def on_click(_, value):
p = Patch()
p.append(html.Div(value, id=value))
return p
dash_duo.start_server(app)
_input = dash_duo.find_element("#children-value")
_input.send_keys("new-child")
dash_duo.find_element("#add-children").click()
dash_duo.wait_for_text_to_equal("#new-child", "new-child")
dash_duo.wait_for_text_to_equal("#initial", "init")
def test_pch004_duplicate_output_restart(dash_duo_mp):
# Duplicate output ids should be the same between restarts for the same ids
def create_app():
app = Dash(__name__)
app.layout = html.Div(
[
html.Button("Click 1", id="click1"),
html.Button("Click 2", id="click2"),
html.Div(id="output"),
]
)
@app.callback(
Output("output", "children", allow_duplicate=True),
Input("click1", "n_clicks"),
prevent_initial_call=True,
)
def on_click(_):
return "click 1"
@app.callback(
Output("output", "children", allow_duplicate=True),
Input("click2", "n_clicks"),
prevent_initial_call=True,
)
def on_click(_):
return "click 2"
return app
dash_duo_mp.start_server(create_app())
dash_duo_mp.wait_for_element("#click1").click()
dash_duo_mp.wait_for_text_to_equal("#output", "click 1")
dash_duo_mp.wait_for_element("#click2").click()
dash_duo_mp.wait_for_text_to_equal("#output", "click 2")
dash_duo_mp.server.stop()
dash_duo_mp.start_server(create_app(), navigate=False)
dash_duo_mp.wait_for_element("#click1").click()
dash_duo_mp.wait_for_text_to_equal("#output", "click 1")
dash_duo_mp.wait_for_element("#click2").click()
dash_duo_mp.wait_for_text_to_equal("#output", "click 2")