holoviz / panel
1 7
import param
2 7
import pytest
3

4 7
from panel.io import block_comm
5 7
from panel.layout import Row
6 7
from panel.links import CallbackGenerator
7 7
from panel.widgets import (
8
    CompositeWidget, Dial, FileDownload, FloatSlider, TextInput,
9
    Terminal, ToggleGroup, Widget
10
)
11 7
from panel.widgets.tables import BaseTable
12 7
from panel.tests.util import check_layoutable_properties
13

14 7
excluded = (
15
    BaseTable, CompositeWidget, Dial, FileDownload, ToggleGroup, Terminal
16
)
17

18 7
all_widgets = [
19
    w for w in param.concrete_descendents(Widget).values()
20
    if not w.__name__.startswith('_') and not issubclass(w, excluded)
21
]
22

23 7
@pytest.mark.parametrize('widget', all_widgets)
24 2
def test_widget_signature(widget):
25 7
    from inspect import signature
26 7
    parameters = signature(widget).parameters
27 7
    assert len(parameters) == 1
28

29

30 7
@pytest.mark.parametrize('widget', all_widgets)
31 2
def test_widget_linkable_params(widget):
32 7
    w = widget()
33 7
    controls = w.controls(jslink=True)
34 7
    layout = Row(w, controls)
35

36 7
    try:
37 7
        CallbackGenerator.error = True
38 7
        layout.get_root()
39
    finally:
40 7
        CallbackGenerator.error = False
41

42

43 7
@pytest.mark.parametrize('widget', all_widgets)
44 2
def test_widget_layout_properties(widget, document, comm):
45 7
    w = widget()
46 7
    model = w.get_root(document, comm)
47 7
    check_layoutable_properties(w, model)
48

49

50 7
@pytest.mark.parametrize('widget', all_widgets)
51 2
def test_widget_disabled_properties(widget, document, comm):
52 7
    w = widget(disabled=True)
53

54 7
    model = w.get_root(document, comm)
55

56 7
    assert model.disabled == True
57 7
    model.disabled = False
58 7
    assert model.disabled == False
59

60

61 7
@pytest.mark.parametrize('widget', all_widgets)
62 2
def test_widget_clone(widget):
63 7
    w = widget()
64 7
    clone = w.clone()
65

66 7
    assert ([(k, v) for k, v in sorted(w.param.get_param_values()) if k != 'name'] ==
67
            [(k, v) for k, v in sorted(clone.param.get_param_values()) if k != 'name'])
68

69

70 7
@pytest.mark.parametrize('widget', all_widgets)
71 2
def test_widget_clone_override(widget):
72 7
    w = widget()
73 7
    clone = w.clone(width=50)
74

75 7
    assert ([(k, v) for k, v in sorted(w.param.get_param_values()) if k not in ['name', 'width']] ==
76
            [(k, v) for k, v in sorted(clone.param.get_param_values()) if k not in ['name', 'width']])
77 7
    assert clone.width == 50
78 7
    assert w.width is widget.width
79

80

81 7
@pytest.mark.parametrize('widget', all_widgets)
82 2
def test_widget_model_cache_cleanup(widget, document, comm):
83 7
    w = widget()
84

85 7
    model = w.get_root(document, comm)
86

87 7
    assert model.ref['id'] in w._models
88 7
    assert w._models[model.ref['id']] == (model, None)
89

90 7
    w._cleanup(model)
91 7
    assert w._models == {}
92

93

94 7
def test_widget_triggers_events(document, comm):
95
    """
96
    Ensure widget events don't get swallowed in comm mode
97
    """
98 7
    text = TextInput(value='ABC', name='Text:')
99

100 7
    widget = text.get_root(document, comm=comm)
101 7
    document.add_root(widget)
102 7
    document.hold()
103

104
    # Simulate client side change
105 7
    document._held_events = document._held_events[:-1]
106

107
    # Set new value
108 7
    with block_comm():
109 7
        text.value = '123'
110

111 7
    assert len(document._held_events) == 1
112 7
    event = document._held_events[0]
113 7
    assert event.attr == 'value'
114 7
    assert event.model is widget
115 7
    assert event.new == '123'
116

117

118 7
def test_widget_from_param_cls():
119 7
    class Test(param.Parameterized):
120

121 7
        a = param.Parameter()
122

123 7
    widget = TextInput.from_param(Test.param.a)
124 7
    assert isinstance(widget, TextInput)
125 7
    assert widget.name == 'A'
126

127 7
    Test.a = 'abc'
128 7
    assert widget.value == 'abc'
129

130 7
    widget.value = 'def'
131 7
    assert Test.a == 'def'
132

133

134 7
def test_widget_from_param_instance():
135 7
    class Test(param.Parameterized):
136

137 7
        a = param.Parameter()
138

139 7
    test = Test()
140 7
    widget = TextInput.from_param(test.param.a)
141 7
    assert isinstance(widget, TextInput)
142 7
    assert widget.name == 'A'
143

144 7
    test.a = 'abc'
145 7
    assert widget.value == 'abc'
146

147 7
    widget.value = 'def'
148 7
    assert test.a == 'def'
149

150

151 7
def test_widget_from_param_instance_with_kwargs():
152 7
    class Test(param.Parameterized):
153

154 7
        a = param.Number(default=3.14)
155

156 7
    test = Test()
157 7
    widget = FloatSlider.from_param(test.param.a, start=0.3, end=5.2)
158 7
    assert isinstance(widget, FloatSlider)
159 7
    assert widget.name == 'A'
160 7
    assert widget.start == 0.3
161 7
    assert widget.end == 5.2
162 7
    assert widget.value == 3.14
163

164 7
    test.a = 1.57
165 7
    assert widget.value == 1.57
166

167 7
    widget.value = 4.3
168 7
    assert test.a == 4.3
169

Read our documentation on viewing source code .

Loading