-
-
Notifications
You must be signed in to change notification settings - Fork 484
/
test_base.py
647 lines (467 loc) · 18.3 KB
/
test_base.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
import param
import pytest
from bokeh.models import Column as BkColumn, Div, Row as BkRow
from panel.chat import ChatInterface
from panel.layout import (
Accordion, Card, Column, FlexBox, Row, Spacer, Tabs, WidgetBox,
)
from panel.layout.base import ListPanel, NamedListPanel
from panel.pane import Bokeh, Markdown
from panel.param import Param
from panel.tests.util import check_layoutable_properties
from panel.widgets import Debugger, MultiSelect
excluded = (NamedListPanel, Debugger, ChatInterface)
all_panels = [w for w in param.concrete_descendents(ListPanel).values()
if not w.__name__.startswith('_') and not issubclass(w, excluded)]
@pytest.mark.parametrize('panel', all_panels)
def test_layout_signature(panel):
from inspect import signature
parameters = signature(panel).parameters
assert len(parameters) == 2, 'Found following parameters %r on %s' % (parameters, panel)
assert 'objects' in parameters
@pytest.mark.parametrize('layout', [Column, Row, Tabs, Spacer, Card, Accordion])
def test_layout_properties(layout, document, comm):
l = layout()
model = l.get_root(document, comm)
check_layoutable_properties(l, model)
@pytest.mark.parametrize('layout', [Card, Column, Row, Tabs, Spacer])
def test_layout_model_cache_cleanup(layout, document, comm):
l = layout()
model = l.get_root(document, comm)
assert model.ref['id'] in l._models
assert l._models[model.ref['id']] == (model, None)
l._cleanup(model)
assert l._models == {}
@pytest.mark.parametrize('panel', [Card, Column, Row])
def test_layout_constructor(panel):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
assert all(isinstance(p, Bokeh) for p in layout.objects)
@pytest.mark.parametrize('panel', [Card, Column, Row])
def test_layout_constructor_with_objects_param(panel):
div1 = Div()
div2 = Div()
layout = panel(objects=[div1, div2])
assert all(isinstance(p, Bokeh) for p in layout.objects)
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_add(panel, document, comm):
div1 = Div()
div2 = Div()
layout1 = panel(div1, div2)
div3 = Div()
div4 = Div()
layout2 = panel(div3, div4)
combined = layout1 + layout2
model = combined.get_root(document, comm=comm)
assert model.children == [div1, div2, div3, div4]
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_add_list(panel, document, comm):
div1 = Div()
div2 = Div()
layout1 = panel(div1, div2)
div3 = Div()
div4 = Div()
combined = layout1 + [div3, div4]
model = combined.get_root(document, comm=comm)
assert model.children == [div1, div2, div3, div4]
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_radd_list(panel, document, comm):
div1 = Div()
div2 = Div()
layout1 = panel(div1, div2)
div3 = Div()
div4 = Div()
combined = [div3, div4] + layout1
model = combined.get_root(document, comm=comm)
assert model.children == [div3, div4, div1, div2]
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_add_error(panel, document, comm):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
with pytest.raises(TypeError):
layout + 1
@pytest.mark.parametrize('panel', [Card, Column, Row])
def test_layout_getitem(panel):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
assert layout[0].object is div1
assert layout[1].object is div2
@pytest.mark.parametrize('panel', [Card, Column, Row])
def test_layout_repr(panel):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
name = panel.__name__
assert repr(layout) == '%s\n [0] Bokeh(Div)\n [1] Bokeh(Div)' % name
@pytest.mark.parametrize('panel', [Card, Column, Row])
def test_layout_select_by_type(panel):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
panes = layout.select(Bokeh)
assert len(panes) == 2
assert all(isinstance(p, Bokeh) for p in panes)
assert panes[0].object is div1
assert panes[1].object is div2
@pytest.mark.parametrize('panel', [Card, Column, Row])
def test_layout_select_by_function(panel):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
panes = layout.select(lambda x: getattr(x, 'object', None) is div2)
assert len(panes) == 1
assert panes[0].object is div2
@pytest.mark.parametrize(['panel', 'model_type'], [(Column, BkColumn), (Row, BkRow)])
def test_layout_get_root(panel, model_type, document, comm):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
model = layout.get_root(document, comm=comm)
assert isinstance(model, model_type)
assert model.children == [div1, div2]
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_reverse(panel, document, comm):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
model = layout.get_root(document, comm=comm)
layout.reverse()
assert model.children == [div2, div1]
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_append(panel, document, comm):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
model = layout.get_root(document, comm=comm)
div3 = Div()
layout.append(div3)
assert model.children == [div1, div2, div3]
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_extend(panel, document, comm):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
model = layout.get_root(document, comm=comm)
div3 = Div()
div4 = Div()
layout.extend([div4, div3])
assert model.children == [div1, div2, div4, div3]
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_iadd(panel, document, comm):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
model = layout.get_root(document, comm=comm)
div3 = Div()
div4 = Div()
layout += [div4, div3]
assert model.children == [div1, div2, div4, div3]
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_insert(panel, document, comm):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
model = layout.get_root(document, comm=comm)
div3 = Div()
layout.insert(1, div3)
assert model.children == [div1, div3, div2]
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_setitem(panel, document, comm):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
p1, p2 = layout.objects
model = layout.get_root(document, comm=comm)
assert p1._models[model.ref['id']][0] is model.children[0]
div3 = Div()
layout[0] = div3
assert model.children == [div3, div2]
assert p1._models == {}
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_setitem_out_of_bounds(panel, document, comm):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
layout.get_root(document, comm=comm)
div3 = Div()
with pytest.raises(IndexError):
layout[2] = div3
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_setitem_replace_all(panel, document, comm):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
p1, p2 = layout.objects
model = layout.get_root(document, comm=comm)
assert p1._models[model.ref['id']][0] is model.children[0]
div3 = Div()
div4 = Div()
layout[:] = [div3, div4]
assert model.children == [div3, div4]
assert p1._models == {}
assert p2._models == {}
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_setitem_replace_all_error(panel, document, comm):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
layout.get_root(document, comm=comm)
div3 = Div()
with pytest.raises(IndexError):
layout[:] = div3
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_setitem_replace_slice(panel, document, comm):
div1 = Div()
div2 = Div()
div3 = Div()
layout = panel(div1, div2, div3)
p1, p2, p3 = layout.objects
model = layout.get_root(document, comm=comm)
assert p1._models[model.ref['id']][0] is model.children[0]
div3 = Div()
div4 = Div()
layout[1:] = [div3, div4]
assert model.children == [div1, div3, div4]
assert p2._models == {}
assert p3._models == {}
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_setitem_replace_slice_error(panel, document, comm):
div1 = Div()
div2 = Div()
div3 = Div()
layout = panel(div1, div2, div3)
layout.get_root(document, comm=comm)
div3 = Div()
with pytest.raises(IndexError):
layout[1:] = [div3]
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_setitem_replace_slice_out_of_bounds(panel, document, comm):
div1 = Div()
div2 = Div()
div3 = Div()
layout = panel(div1, div2, div3)
layout.get_root(document, comm=comm)
div3 = Div()
with pytest.raises(IndexError):
layout[3:4] = [div3]
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_pop(panel, document, comm):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
p1, p2 = layout.objects
model = layout.get_root(document, comm=comm)
assert p1._models[model.ref['id']][0] is model.children[0]
layout.pop(0)
assert model.children == [div2]
assert p1._models == {}
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_remove(panel, document, comm):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
p1, p2 = layout.objects
model = layout.get_root(document, comm=comm)
assert p1._models[model.ref['id']][0] is model.children[0]
layout.remove(p1)
assert model.children == [div2]
assert p1._models == {}
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_clear(panel, document, comm):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
p1, p2 = layout.objects
model = layout.get_root(document, comm=comm)
assert p1._models[model.ref['id']][0] is model.children[0]
layout.clear()
assert model.children == []
assert p1._models == p2._models == {}
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_clone_args(panel):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
clone = layout.clone(div2, div1)
assert layout.objects[0].object is clone.objects[1].object
assert layout.objects[1].object is clone.objects[0].object
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_clone_kwargs(panel):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
clone = layout.clone(width=400, sizing_mode='stretch_height')
assert clone.width == 400
assert clone.sizing_mode == 'stretch_height'
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_clone_no_args_no_kwargs(panel):
div1 = Div()
div2 = Div()
layout = panel(div1, div2, width=400, sizing_mode='stretch_height')
clone = layout.clone()
assert layout.objects[0].object is clone.objects[0].object
assert layout.objects[1].object is clone.objects[1].object
assert clone.width == 400
assert clone.sizing_mode == 'stretch_height'
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_clone_objects_in_kwargs(panel):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
clone = layout.clone(
objects=(div2, div1),
width=400, sizing_mode='stretch_height'
)
assert layout.objects[0].object is clone.objects[1].object
assert layout.objects[1].object is clone.objects[0].object
assert clone.width == 400
assert clone.sizing_mode == 'stretch_height'
@pytest.mark.parametrize('panel', [Column, Row])
def test_layout_clone_objects_in_args_and_kwargs(panel):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
with pytest.raises(ValueError):
layout.clone(div1, objects=div1)
def test_widgetbox(document, comm):
widget_box = WidgetBox("WidgetBox")
model = widget_box.get_root(document, comm=comm)
assert isinstance(model, widget_box._bokeh_model)
# Test the horizontal param.
assert not widget_box.horizontal
widget_box.horizontal = True
assert widget_box.horizontal
def test_layout_with_param_setitem(document, comm):
import param
class TestClass(param.Parameterized):
select = param.ObjectSelector(default=0, objects=[0,1])
def __init__(self, **params):
super().__init__(**params)
self._layout = Row(Param(self.param, parameters=['select']),
self.select)
@param.depends('select', watch=True)
def _load(self):
self._layout[-1] = self.select
test = TestClass()
model = test._layout.get_root(document, comm=comm)
test.select = 1
assert model.children[1].text == '<pre>1</pre>'
@pytest.mark.parametrize('panel', [Card, Column, Tabs, Accordion])
@pytest.mark.parametrize('sizing_mode', ['stretch_width', 'stretch_height', 'stretch_both'])
def test_expand_sizing_mode_to_match_child(panel, sizing_mode, document, comm):
div1 = Div()
div2 = Div(sizing_mode=sizing_mode)
layout = panel(div1, div2)
model = layout.get_root(document, comm)
assert model.sizing_mode == sizing_mode
def test_expand_row_sizing_mode_stretch_both(document, comm):
div1 = Div(sizing_mode='stretch_both')
div2 = Div(sizing_mode='stretch_both')
layout = Row(div1, div2)
model = layout.get_root(document, comm)
assert model.sizing_mode == 'stretch_both'
@pytest.mark.parametrize('panel', [Accordion, Card, Column, Row, Tabs])
def test_expand_both_axes(panel, document, comm):
div1 = Div(sizing_mode='stretch_width')
div2 = Div(sizing_mode='stretch_height')
layout = panel(div1, div2)
model = layout.get_root(document, comm)
assert model.sizing_mode == 'stretch_both'
def test_expand_row_both_axes(document, comm):
div1 = Div(sizing_mode='stretch_both')
div2 = Div(sizing_mode='stretch_both')
layout = Row(div1, div2)
model = layout.get_root(document, comm)
assert model.sizing_mode == 'stretch_both'
@pytest.mark.parametrize('panel', [Card, Column, Tabs, Accordion])
def test_expand_only_non_fixed_axis_width(panel, document, comm):
div1 = Div(sizing_mode='stretch_width')
div2 = Div(sizing_mode='stretch_height')
layout = panel(div1, div2, width=500)
model = layout.get_root(document, comm)
assert model.sizing_mode == 'stretch_height'
def test_not_expand_row_only_non_fixed_axis_width(document, comm):
div1 = Div(sizing_mode='stretch_width')
div2 = Div(sizing_mode='stretch_height')
layout = Row(div1, div2, width=500)
model = layout.get_root(document, comm)
assert model.sizing_mode == 'stretch_height'
def test_expand_row_all_only_non_fixed_axis_width(document, comm):
div1 = Div(sizing_mode='stretch_height')
div2 = Div(sizing_mode='stretch_height')
layout = Row(div1, div2, width=500)
model = layout.get_root(document, comm)
assert model.sizing_mode == 'stretch_height'
@pytest.mark.parametrize('panel', [Card, Column, Row, Tabs, Accordion])
def test_expand_only_non_fixed_axis_height(panel, document, comm):
div1 = Div(sizing_mode='stretch_width')
div2 = Div(sizing_mode='stretch_height')
layout = panel(div1, div2, height=500)
model = layout.get_root(document, comm)
assert model.sizing_mode == 'stretch_width'
@pytest.mark.parametrize('panel', [Card, Column, Row, Tabs, Accordion])
def test_no_expand_fixed(panel, document, comm):
div1 = Div(sizing_mode='stretch_width')
div2 = Div(sizing_mode='stretch_height')
layout = panel(div1, div2, height=500, width=500)
model = layout.get_root(document, comm)
assert model.sizing_mode == 'fixed'
@pytest.mark.parametrize('scroll_param', ["auto_scroll_limit", "scroll", "scroll_button_threshold", "view_latest"])
def test_column_scroll_params_sets_scroll(scroll_param, document, comm):
if scroll_param not in ["auto_scroll_limit", "scroll_button_threshold"]:
params = {scroll_param: True}
else:
params = {scroll_param: 1}
col = Column(**params)
assert getattr(col, scroll_param)
assert col.scroll
@pytest.mark.parametrize('layout', [Row, Column, FlexBox])
def test_pass_objects_ref(document, comm, layout):
multi_select = MultiSelect(options=['foo', 'bar', 'baz'], value=['bar', 'baz'])
col = layout(objects=multi_select)
col.get_root(document, comm=comm)
assert len(col.objects) == 2
md1, md2 = col.objects
assert isinstance(md1, Markdown)
assert md1.object == 'bar'
assert isinstance(md2, Markdown)
assert md2.object == 'baz'
multi_select.value = ['foo']
assert len(col.objects) == 1
md3 = col.objects[0]
assert isinstance(md3, Markdown)
assert md3.object == 'foo'
@pytest.mark.parametrize('dim', ["width", "height"])
def test_compute_sizing_mode_stretch_margin_none(dim, document, comm):
md = Markdown(**{dim: 100})
col = Column(md, margin=None, sizing_mode=f'stretch_{dim}')
root = col.get_root(document, comm=comm)
new_props = col._compute_sizing_mode(root.children, {'margin': None})
assert new_props == {f'min_{dim}': 100, 'sizing_mode': f'stretch_{dim}'}
@pytest.mark.parametrize('dim', ["width", "height"])
def test_compute_sizing_mode_stretch_margin_int(dim, document, comm):
margin = 10
md = Markdown(**{dim: 100})
col = Column(md, margin=margin, sizing_mode=f'stretch_{dim}')
root = col.get_root(document, comm=comm)
new_props = col._compute_sizing_mode(root.children, {'margin': margin})
assert new_props == {f'min_{dim}': 120, 'sizing_mode': f'stretch_{dim}'}
@pytest.mark.parametrize('dim', ["width", "height"])
def test_compute_sizing_mode_stretch_margin_two_tuple(dim, document, comm):
margin = (0, 10) if dim == 'width' else (10, 0)
md = Markdown(**{dim: 100})
col = Column(md, margin=margin, sizing_mode=f'stretch_{dim}')
root = col.get_root(document, comm=comm)
new_props = col._compute_sizing_mode(root.children, {'margin': margin})
assert new_props == {f'min_{dim}': 120, 'sizing_mode': f'stretch_{dim}'}
@pytest.mark.parametrize('dim', ["width", "height"])
def test_compute_sizing_mode_stretch_margin_four_tuple(dim, document, comm):
margin = (0, 10, 0, 5) if dim == 'width' else (10, 0, 5, 0)
md = Markdown(**{dim: 100})
col = Column(md, margin=margin, sizing_mode=f'stretch_{dim}')
root = col.get_root(document, comm=comm)
new_props = col._compute_sizing_mode(root.children, {'margin': margin})
assert new_props == {f'min_{dim}': 115, 'sizing_mode': f'stretch_{dim}'}