-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathtest_integration.py
346 lines (283 loc) · 10.9 KB
/
test_integration.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
from multiprocessing import Value
import datetime
import itertools
import re
import dash_html_components as html
import dash_core_components as dcc
import dash_flow_example
import dash
import time
from dash.dependencies import Input, Output
from dash.exceptions import PreventUpdate
from .IntegrationTests import IntegrationTests
from .utils import assert_clean_console, invincible, wait_for
class Tests(IntegrationTests):
def setUp(self):
def wait_for_element_by_id(id):
wait_for(lambda: None is not invincible(
lambda: self.driver.find_element_by_id(id)
))
return self.driver.find_element_by_id(id)
self.wait_for_element_by_id = wait_for_element_by_id
def test_simple_callback(self):
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Input(
id='input',
value='initial value'
),
html.Div(
html.Div([
1.5,
None,
'string',
html.Div(id='output-1')
])
)
])
call_count = Value('i', 0)
@app.callback(Output('output-1', 'children'), [Input('input', 'value')])
def update_output(value):
call_count.value = call_count.value + 1
return value
self.startServer(app)
output1 = self.wait_for_element_by_id('output-1')
wait_for(lambda: output1.text == 'initial value')
self.percy_snapshot(name='simple-callback-1')
input1 = self.wait_for_element_by_id('input')
input1.clear()
input1.send_keys('hello world')
output1 = lambda: self.wait_for_element_by_id('output-1')
wait_for(lambda: output1().text == 'hello world')
self.percy_snapshot(name='simple-callback-2')
self.assertEqual(
call_count.value,
# an initial call to retrieve the first value
1 +
# one for each hello world character
len('hello world')
)
assert_clean_console(self)
def test_wildcard_callback(self):
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Input(
id='input',
value='initial value'
),
html.Div(
html.Div([
1.5,
None,
'string',
html.Div(id='output-1', **{'data-cb': 'initial value',
'aria-cb': 'initial value'})
])
)
])
input_call_count = Value('i', 0)
@app.callback(Output('output-1', 'data-cb'), [Input('input', 'value')])
def update_data(value):
input_call_count.value = input_call_count.value + 1
return value
@app.callback(Output('output-1', 'children'),
[Input('output-1', 'data-cb')])
def update_text(data):
return data
self.startServer(app)
output1 = self.wait_for_element_by_id('output-1')
wait_for(lambda: output1.text == 'initial value')
self.percy_snapshot(name='wildcard-callback-1')
input1 = self.wait_for_element_by_id('input')
input1.clear()
input1.send_keys('hello world')
output1 = lambda: self.wait_for_element_by_id('output-1')
wait_for(lambda: output1().text == 'hello world')
self.percy_snapshot(name='wildcard-callback-2')
self.assertEqual(
input_call_count.value,
# an initial call
1 +
# one for each hello world character
len('hello world')
)
assert_clean_console(self)
def test_aborted_callback(self):
"""Raising PreventUpdate prevents update and triggering dependencies"""
initial_input = 'initial input'
initial_output = 'initial output'
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Input(id='input', value=initial_input),
html.Div(initial_output, id='output1'),
html.Div(initial_output, id='output2'),
])
callback1_count = Value('i', 0)
callback2_count = Value('i', 0)
@app.callback(Output('output1', 'children'), [Input('input', 'value')])
def callback1(value):
callback1_count.value = callback1_count.value + 1
raise PreventUpdate("testing callback does not update")
return value
@app.callback(Output('output2', 'children'), [Input('output1', 'children')])
def callback2(value):
callback2_count.value = callback2_count.value + 1
return value
self.startServer(app)
input_ = self.wait_for_element_by_id('input')
input_.clear()
input_.send_keys('x')
output1 = self.wait_for_element_by_id('output1')
output2 = self.wait_for_element_by_id('output2')
# callback1 runs twice (initial page load and through send_keys)
self.assertEqual(callback1_count.value, 2)
# callback2 is never triggered, even on initial load
self.assertEqual(callback2_count.value, 0)
# double check that output1 and output2 children were not updated
self.assertEqual(output1.text, initial_output)
self.assertEqual(output2.text, initial_output)
assert_clean_console(self)
self.percy_snapshot(name='aborted')
def test_wildcard_data_attributes(self):
app = dash.Dash()
test_time = datetime.datetime(2012, 1, 10, 2, 3)
test_date = datetime.date(test_time.year, test_time.month,
test_time.day)
app.layout = html.Div([
html.Div(
id="inner-element",
**{
'data-string': 'multiple words',
'data-number': 512,
'data-none': None,
'data-date': test_date,
'aria-progress': 5
}
)
], id='data-element')
self.startServer(app)
div = self.wait_for_element_by_id('data-element')
# React wraps text and numbers with e.g. <!-- react-text: 20 -->
# Remove those
comment_regex = '<!--[^\[](.*?)-->'
# Somehow the html attributes are unordered.
# Try different combinations (they're all valid html)
permutations = itertools.permutations([
'id="inner-element"',
'data-string="multiple words"',
'data-number="512"',
'data-date="%s"' % (test_date),
'aria-progress="5"'
], 5)
passed = False
for i, permutation in enumerate(permutations):
actual_cleaned = re.sub(comment_regex, '',
div.get_attribute('innerHTML'))
expected_cleaned = re.sub(
comment_regex,
'',
"<div PERMUTE></div>"
.replace('PERMUTE', ' '.join(list(permutation)))
)
passed = passed or (actual_cleaned == expected_cleaned)
if passed:
break
if not passed:
raise Exception(
'HTML does not match\nActual:\n{}\n\nExpected:\n{}'.format(
actual_cleaned,
expected_cleaned
)
)
assert_clean_console(self)
def test_flow_component(self):
app = dash.Dash()
app.layout = html.Div([
dash_flow_example.ExampleReactComponent(
id='react',
value='my-value',
label='react component'
),
dash_flow_example.ExampleFlowComponent(
id='flow',
value='my-value',
label='flow component'
),
html.Hr(),
html.Div(id='output')
])
@app.callback(Output('output', 'children'),
[Input('react', 'value'), Input('flow', 'value')])
def display_output(react_value, flow_value):
return html.Div([
'You have entered {} and {}'.format(react_value, flow_value),
html.Hr(),
html.Label('Flow Component Docstring'),
html.Pre(dash_flow_example.ExampleFlowComponent.__doc__),
html.Hr(),
html.Label('React PropTypes Component Docstring'),
html.Pre(dash_flow_example.ExampleReactComponent.__doc__),
html.Div(id='waitfor')
])
self.startServer(app)
self.wait_for_element_by_id('waitfor')
self.percy_snapshot(name='flowtype')
def test_meta_tags(self):
metas = (
{'name': 'description', 'content': 'my dash app'},
{'name': 'custom', 'content': 'customized'}
)
app = dash.Dash(meta_tags=metas)
app.layout = html.Div(id='content')
self.startServer(app)
meta = self.driver.find_elements_by_tag_name('meta')
# -1 for the meta charset.
self.assertEqual(len(metas), len(meta) - 1, 'Not enough meta tags')
for i in range(1, len(meta)):
meta_tag = meta[i]
meta_info = metas[i - 1]
name = meta_tag.get_attribute('name')
content = meta_tag.get_attribute('content')
self.assertEqual(name, meta_info['name'])
self.assertEqual(content, meta_info['content'])
def test_index_customization(self):
app = dash.Dash()
app.index_string = '''
<!DOCTYPE html>
<html>
<head>
{metas}
<title>{title}</title>
{favicon}
{css}
</head>
<body>
<div id="custom-header">My custom header</div>
<div id="add"></div>
{app_entry}
<footer>
{config}
{scripts}
</footer>
<div id="custom-footer">My custom footer</div>
<script>
// Test the formatting doesn't mess up script tags.
var elem = document.getElementById('add');
if (!elem) {
throw Error('could not find container to add');
}
elem.innerHTML = 'Got added';
</script>
</body>
</html>
'''
app.layout = html.Div('Dash app', id='app')
self.startServer(app)
time.sleep(0.5)
header = self.wait_for_element_by_id('custom-header')
footer = self.wait_for_element_by_id('custom-footer')
self.assertEqual('My custom header', header.text)
self.assertEqual('My custom footer', footer.text)
add = self.wait_for_element_by_id('add')
self.assertEqual('Got added', add.text)
self.percy_snapshot('custom-index')