forked from plotly/dash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_react.py
497 lines (447 loc) · 14.2 KB
/
test_react.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
import unittest
import dash
import json
import plotly
import dash_core_components as dcc
from dash_html_components import Div
import dash_renderer
import pkgutil
from dash.dependencies import Event, Input, Output, State
from dash import exceptions
def generate_css(css_links):
return '\n'.join([
'<link rel="stylesheet" href="{}">'.format(l)
for l in css_links
])
def generate_js(js_links):
return '\n'.join([
'<script type="text/JavaScript" src="{}"></script>'.format(l)
for l in js_links
])
class IntegrationTest(unittest.TestCase):
def setUp(self):
self.app = dash.Dash('my-app')
self.app.layout = Div([
Div('Hello World', id='header', style={'color': 'red'}),
dcc.Input(id='id1', placeholder='Type a value'),
dcc.Input(id='id2', placeholder='Type a value')
])
self.client = self.app.server.test_client()
self.maxDiff = 100*1000
@unittest.skip("")
def test_route_list(self):
urls = [rule.rule for rule in self.app.server.url_map.iter_rules()]
self.assertEqual(
sorted(urls),
sorted([
'/interceptor',
'/initialize',
'/dependencies',
'/',
'/component-suites/<path:path>',
'/static/<path:filename>'
])
)
@unittest.skip("")
def test_initialize_route(self):
response = self.client.get('/initialize')
self.assertEqual(response.status_code, 200)
self.assertEqual(
json.loads(response.data),
json.loads(
json.dumps(self.app.layout, cls=plotly.utils.PlotlyJSONEncoder)
)
)
@unittest.skip("")
def test_dependencies_route(self):
self.app.callback('header', ['id1'])
response = self.client.get('/dependencies')
self.assertEqual(response.status_code, 200)
self.assertEqual(
json.loads(response.data), {
'header': {
'state': [{'id': 'id1'}],
'events': [{'id': 'id1'}]
}
}
)
self.app.callback(
'header',
state=[{'id': 'id1'}],
events=[{'id': 'id1'}])
response = self.client.get('/dependencies')
self.assertEqual(response.status_code, 200)
self.assertEqual(
json.loads(response.data), {
'header': {
'state': [{'id': 'id1'}],
'events': [{'id': 'id1'}]
}
}
)
state = [
{'id': 'id1', 'prop': 'value'},
# Multiple properties from a single component
{'id': 'id1', 'prop': 'className'},
# Nested state
{'id': 'id1', 'prop': ['style', 'color']}
]
events = [
{'id': 'id1', 'event': 'click'},
{'id': 'id1', 'event': 'submit'}
]
self.app.callback('header', state=state, events=events)
response = self.client.get('/dependencies')
self.assertEqual(response.status_code, 200)
self.assertEqual(
json.loads(response.data), {
'header': {
'state': state,
'events': events
}
}
)
@unittest.skip("")
def test_index_html(self):
response = self.client.get('/')
self.assertEqual(response.status_code, 200)
@unittest.skip("")
def test_single_observer_returning_a_dict(self):
@self.app.callback('header', ['id1'])
def update_header(input1):
self.assertEqual({'value': 'New Value'}, input1)
new_value = input1['value']
return {
'children': new_value,
'style.color': 'red',
'className': 'active',
'width': None
}
response = self.client.post(
'/interceptor',
headers={
'Content-Type': 'application/json'
},
data=json.dumps({
# TODO - Why not just `target: id`?
'target': {
'props': {
'id': 'header'
}
},
'parents': {
'id1': {
'props': {
'value': 'New Value'
}
}
}
})
)
self.assertEqual(
json.loads(response.data),
{
'response': {
'children': 'New Value',
'props': {
'id': 'header',
'style.color': 'red',
'className': 'active',
'width': None
}
}
}
)
@unittest.skip("")
def test_single_observer_returning_a_component(self):
@self.app.callback('header', ['id1'])
def update_header(input1):
self.assertEqual({'value': 'New Value'}, input1)
return {
'children': Div('New Component')
}
response = self.client.post(
'/interceptor',
headers={
'Content-Type': 'application/json'
},
data=json.dumps({
'target': {
'props': {
'id': 'header'
}
},
'parents': {
'id1': {
'props': {
'value': 'New Value'
}
}
}
})
)
self.assertEqual(
json.loads(response.data),
{
'response': {
'props': {
'id': 'header'
},
'children': {
'type': 'Div',
'namespace': 'html_components',
'props': {
'children': 'New Component'
}
}
}
}
)
@unittest.skip("")
def test_single_observer_updating_component_that_doesnt_exist(self):
# It's possible to register callbacks for components that don't
# exist in the initial layout because users could add them as
# children in response to another callback
@self.app.callback('doesnt-exist-yet', ['id1'])
def update_header(input1):
self.assertEqual({
'value': 'New Value'
}, input1)
new_value = input1['value']
return {
'value': new_value
}
response = self.client.post(
'/interceptor',
headers={
'Content-Type': 'application/json'
},
data=json.dumps({
'target': {
'props': {
'id': 'header'
}
},
'parents': {
'id1': {
'props': {
'value': 'New Value'
}
}
}
})
)
self.assertEqual(
json.loads(response.data),
{
'response': {
'props': {
'id': 'doesnt-exit-yet',
'value': 'New Value'
},
}
}
)
@unittest.skip("")
def test_single_observer_with_multiple_controllers(self):
@self.app.callback('header', ['id1', 'id2'])
def update_header(input1, input2):
self.assertEqual({
'value': 'New Value'
}, input1)
self.assertEqual({}, input2)
new_value = input1['value']
return {
'value': new_value
}
response = self.client.post(
'/interceptor',
headers={
'Content-Type': 'application/json'
},
data=json.dumps({
'target': {
'props': {
'id': 'header'
}
},
'parents': {
'id1': {
'props': {
'value': 'New Value'
}
},
'id2': {
'props': {}
}
}
})
)
self.assertEqual(
json.loads(response.data),
{
'response': {
'props': {
'id': 'header',
'value': 'New Value'
},
}
}
)
def test_serving_scripts(self):
self.app.scripts.config.serve_locally = True
self.app._setup_server()
response = self.client.get(
('/_dash-component-suites/'
'dash_renderer/bundle.js?v={}').format(dash_renderer.__version__)
)
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.data,
pkgutil.get_data('dash_renderer', 'bundle.js')
)
class TestCallbacks(unittest.TestCase):
def test_callback_registry(self):
app = dash.Dash('')
input = dcc.Input(id='input')
input._events = ['blur', 'change']
app.layout = Div([
input,
Div(id='output-1'),
Div(id='output-2'),
Div(id='output-3')
], id='body')
app.callback(
Output('body', 'children'),
[Input('input', 'value')]
)
app.callback(
Output('output-1', 'children'),
[Input('input', 'value')]
)
app.callback(
Output('output-2', 'children'),
[Input('input', 'value')],
state=[State('input', 'value')],
)
app.callback(
Output('output-3', 'children'),
[Input('input', 'value')],
state=[State('input', 'value')],
events=[Event('input', 'blur')],
)
def test_no_layout_exception(self):
app = dash.Dash('')
self.assertRaises(
exceptions.LayoutIsNotDefined,
app.callback,
Output('body', 'children'),
[Input('input', 'value')]
)
def test_exception_id_not_in_layout(self):
app = dash.Dash('')
app.layout = Div('', id='test')
self.assertRaises(
exceptions.NonExistantIdException,
app.callback,
Output('output', 'children'),
[Input('input', 'value')]
)
def test_exception_prop_not_in_component(self):
app = dash.Dash('')
app.layout = Div([
dcc.Input(id='input'),
Div(id='output')
], id='body')
self.assertRaises(
exceptions.NonExistantPropException,
app.callback,
Output('output', 'non-there'),
[Input('input', 'value')]
)
self.assertRaises(
exceptions.NonExistantPropException,
app.callback,
Output('output', 'children'),
[Input('input', 'valuez')]
)
self.assertRaises(
exceptions.NonExistantPropException,
app.callback,
Output('body', 'childrenz'),
[Input('input', 'value')]
)
def test_exception_event_not_in_component(self):
app = dash.Dash('')
app.layout = Div([
Div(id='button'),
Div(id='output'),
Div(id='graph-output'),
dcc.Graph(id='graph')
], id='body')
for id in ['output', 'body']:
self.assertRaises(
exceptions.NonExistantEventException,
app.callback,
Output(id, 'children'),
events=[Event(id, 'style')]
)
app.callback(
Output(id, 'children'),
events=[Event(id, 'click')]
)
self.assertRaises(
exceptions.NonExistantEventException,
app.callback,
Output('output', 'children'),
events=[Event('graph', 'zoom')]
)
app.callback(
Output('graph-output', 'children'),
events=[Event('graph', 'click')]
)
def test_exception_component_is_not_right_type(self):
app = dash.Dash('')
app.layout = Div([
dcc.Input(id='input'),
Div(id='output')
], id='body')
test_args = [
['asdf', ['asdf'], [], []],
[Output('output', 'children'), Input('input', 'value'), [], []],
[Output('output', 'children'), [], State('input', 'value'), []],
[Output('output', 'children'), [], [], Event('input', 'click')],
]
for args in test_args:
self.assertRaises(
exceptions.IncorrectTypeException,
app.callback,
*args
)
def test_suppress_callback_exception(self):
app = dash.Dash('')
app.layout = Div([
dcc.Input(id='input'),
Div(id='output')
], id='body')
self.assertRaises(
exceptions.NonExistantIdException,
app.callback,
Output('id-not-there', 'children'),
[Input('input', 'value')]
)
app.config.supress_callback_exceptions = True
app.callback(Output('id-not-there', 'children'),
[Input('input', 'value')])
def test_missing_input_and_events(self):
app = dash.Dash('')
app.layout = Div([
dcc.Input(id='input')
], id='body')
self.assertRaises(
exceptions.MissingEventsException,
app.callback,
Output('body', 'children'),
[],
[State('input', 'value')]
)