holoviz / panel
1 7
from collections import OrderedDict
2

3 7
import numpy as np
4 7
import pytest
5

6 7
from panel.widgets import (
7
    CrossSelector, MultiChoice, MultiSelect, Select, ToggleGroup
8
)
9 7
from panel.util import as_unicode
10

11

12 7
def test_select_list_constructor():
13 7
    select = Select(options=['A', 1], value=1)
14 7
    assert select.options == ['A', 1]
15

16

17 7
def test_select_float_option_with_equality():
18 7
    opts = {'A': 3.14, '1': 2.0}
19 7
    select = Select(options=opts, value=3.14, name='Select')
20 7
    assert select.value == 3.14
21

22 7
    select.value = 2
23 7
    assert select.value == 2.0
24

25 7
    select.value = 3.14
26 7
    assert select.value == 3.14
27

28

29 7
def test_select_text_option_with_equality():
30 7
    opts = {'A': 'ABC', '1': 'DEF'}
31 7
    select = Select(options=opts, value='DEF', name='Select')
32 7
    assert select.value == 'DEF'
33

34 7
    select.value = 'ABC'
35 7
    assert select.value == 'ABC'
36

37 7
    select.value = 'DEF'
38 7
    assert select.value == 'DEF'
39

40

41 7
def test_select(document, comm):
42 7
    opts = {'A': 'a', '1': 1}
43 7
    select = Select(options=opts, value=opts['1'], name='Select')
44

45 7
    widget = select.get_root(document, comm=comm)
46

47 7
    assert isinstance(widget, select._widget_type)
48 7
    assert widget.title == 'Select'
49 7
    assert widget.value == as_unicode(opts['1'])
50 7
    assert widget.options == [(as_unicode(v),k) for k,v in opts.items()]
51

52 7
    select._process_events({'value': as_unicode(opts['A'])})
53 7
    assert select.value == opts['A']
54

55 7
    widget.value = as_unicode(opts['1'])
56 7
    select.value = opts['1']
57 7
    assert select.value == opts['1']
58

59 7
    select.value = opts['A']
60 7
    assert widget.value == as_unicode(opts['A'])
61

62

63 7
def test_select_change_options(document, comm):
64 7
    opts = {'A': 'a', '1': 1}
65 7
    select = Select(options=opts, value=opts['1'], name='Select')
66

67 7
    widget = select.get_root(document, comm=comm)
68

69 7
    select.options = {'A': 'a'}
70 7
    assert select.value == opts['A']
71 7
    assert widget.value == as_unicode(opts['A'])
72

73 7
    select.options = {}
74 7
    assert select.value == None
75 7
    assert widget.value == ''
76

77

78 7
def test_select_non_hashable_options(document, comm):
79 7
    opts = {'A': np.array([1, 2, 3]), '1': np.array([3, 4, 5])}
80 7
    select = Select(options=opts, value=opts['1'], name='Select')
81

82 7
    widget = select.get_root(document, comm=comm)
83

84 7
    select.value = opts['A']
85 7
    assert select.value is opts['A']
86 7
    assert widget.value == as_unicode(opts['A'])
87

88 7
    opts.pop('A')
89 7
    select.options = opts
90 7
    assert select.value is opts['1']
91 7
    assert widget.value == as_unicode(opts['1'])
92

93

94 7
def test_select_mutables(document, comm):
95 7
    opts = OrderedDict([('A', [1,2,3]), ('B', [2,4,6]), ('C', dict(a=1,b=2))])
96 7
    select = Select(options=opts, value=opts['B'], name='Select')
97

98 7
    widget = select.get_root(document, comm=comm)
99

100 7
    assert isinstance(widget, select._widget_type)
101 7
    assert widget.title == 'Select'
102 7
    assert widget.value == as_unicode(opts['B'])
103 7
    assert widget.options == [(as_unicode(v),k) for k,v in opts.items()]
104

105 7
    widget.value = as_unicode(opts['B'])
106 7
    select._process_events({'value': as_unicode(opts['A'])})
107 7
    assert select.value == opts['A']
108

109 7
    widget.value = as_unicode(opts['B'])
110 7
    select._process_events({'value': as_unicode(opts['B'])})
111 7
    assert select.value == opts['B']
112

113 7
    select.value = opts['A']
114 7
    assert widget.value == as_unicode(opts['A'])
115

116

117 7
def test_select_change_options_on_watch(document, comm):
118 7
    select = Select(options=OrderedDict([('A', 'A'), ('1', 1), ('C', object)]),
119
                         value='A', name='Select')
120

121 7
    def set_options(event):
122 7
        if event.new == 1:
123 7
            select.options = OrderedDict([('D', 2), ('E', 'a')])
124 7
    select.param.watch(set_options, 'value')
125

126 7
    model = select.get_root(document, comm=comm)
127

128 7
    select.value = 1
129 7
    assert model.value == as_unicode(list(select.options.values())[0])
130 7
    assert model.options == [(as_unicode(v),k) for k,v in select.options.items()]
131

132

133 7
def test_multi_select(document, comm):
134 7
    select = MultiSelect(options=OrderedDict([('A', 'A'), ('1', 1), ('C', object)]),
135
                         value=[object, 1], name='Select')
136

137 7
    widget = select.get_root(document, comm=comm)
138

139 7
    assert isinstance(widget, select._widget_type)
140 7
    assert widget.title == 'Select'
141 7
    assert widget.value == ['C', '1']
142 7
    assert widget.options == ['A', '1', 'C']
143

144 7
    widget.value = ['1']
145 7
    select._process_events({'value': ['1']})
146 7
    assert select.value == [1]
147

148 7
    widget.value = ['A', 'C']
149 7
    select._process_events({'value': ['A', 'C']})
150 7
    assert select.value == ['A', object]
151

152 7
    select.value = [object, 'A']
153 7
    assert widget.value == ['C', 'A']
154

155

156 7
def test_multi_choice(document, comm):
157 7
    choice = MultiChoice(options=OrderedDict([('A', 'A'), ('1', 1), ('C', object)]),
158
                         value=[object, 1], name='MultiChoice')
159

160 7
    widget = choice.get_root(document, comm=comm)
161

162 7
    assert isinstance(widget, choice._widget_type)
163 7
    assert widget.title == 'MultiChoice'
164 7
    assert widget.value == ['C', '1']
165 7
    assert widget.options == ['A', '1', 'C']
166

167 7
    widget.value = ['1']
168 7
    choice._process_events({'value': ['1']})
169 7
    assert choice.value == [1]
170

171 7
    widget.value = ['A', 'C']
172 7
    choice._process_events({'value': ['A', 'C']})
173 7
    assert choice.value == ['A', object]
174

175 7
    choice.value = [object, 'A']
176 7
    assert widget.value == ['C', 'A']
177

178

179 7
def test_multi_select_change_options(document, comm):
180 7
    select = MultiSelect(options=OrderedDict([('A', 'A'), ('1', 1), ('C', object)]),
181
                         value=[object, 1], name='Select')
182

183 7
    def set_options(event):
184 7
        if event.new == [1]:
185 7
            select.options = OrderedDict([('D', 2), ('E', 'a')])
186 7
    select.param.watch(set_options, 'value')
187

188 7
    model = select.get_root(document, comm=comm)
189

190 7
    select.value = [1]
191 7
    assert model.value == []
192 7
    assert model.options == ['D', 'E']
193

194

195 7
def test_toggle_group_error_init(document, comm):
196 7
    with pytest.raises(ValueError):
197 7
        ToggleGroup(options=OrderedDict([('A', 'A'), ('1', 1), ('C', object)]),
198
                    value=1, name='RadioButtonGroup',
199
                    widget_type='button', behavior='check')
200

201 7
    with pytest.raises(ValueError):
202 7
        ToggleGroup(options=OrderedDict([('A', 'A'), ('1', 1), ('C', object)]),
203
                    value=[1, object], name='RadioButtonGroup',
204
                    widget_type='button', behavior='radio')
205

206 7
    with pytest.raises(ValueError):
207 7
        ToggleGroup(options=OrderedDict([('A', 'A'), ('1', 1), ('C', object)]),
208
                    value=[1, object], name='RadioButtonGroup',
209
                    widget_type='buttons')
210

211 7
    with pytest.raises(ValueError):
212 7
        ToggleGroup(options=OrderedDict([('A', 'A'), ('1', 1), ('C', object)]),
213
                    value=[1, object], name='RadioButtonGroup',
214
                    behavior='checks')
215

216

217 7
def test_toggle_group_check(document, comm):
218

219 7
    for widget_type in ToggleGroup._widgets_type:
220 7
        select = ToggleGroup(options=OrderedDict([('A', 'A'), ('1', 1), ('C', object)]),
221
                               value=[1, object], name='CheckButtonGroup',
222
                               widget_type=widget_type, behavior='check')
223

224 7
        widget = select.get_root(document, comm=comm)
225

226 7
        assert isinstance(widget, select._widget_type)
227 7
        assert widget.active == [1, 2]
228 7
        assert widget.labels == ['A', '1', 'C']
229

230 7
        widget.active = [2]
231 7
        select._process_events({'active': [2]})
232 7
        assert select.value == [object]
233

234 7
        widget.active = [0, 2]
235 7
        select._process_events({'active': [0, 2]})
236 7
        assert select.value == ['A', object]
237

238 7
        select.value = [object, 'A']
239 7
        assert widget.active == [2, 0]
240

241 7
        widget.active = []
242 7
        select._process_events({'active': []})
243 7
        assert select.value == []
244

245

246 7
def test_toggle_group_radio(document, comm):
247

248 7
    for widget_type in ToggleGroup._widgets_type:
249 7
        select = ToggleGroup(options=OrderedDict([('A', 'A'), ('1', 1), ('C', object)]),
250
                               value=1, name='RadioButtonGroup',
251
                               widget_type=widget_type, behavior='radio')
252

253 7
        widget = select.get_root(document, comm=comm)
254

255 7
        assert isinstance(widget, select._widget_type)
256 7
        assert widget.active == 1
257 7
        assert widget.labels == ['A', '1', 'C']
258

259 7
        widget.active = 2
260 7
        select._process_events({'active': 2})
261 7
        assert select.value == object
262

263 7
        select.value = 'A'
264 7
        assert widget.active == 0
265

266

267 7
def test_cross_select_constructor():
268 7
    cross_select = CrossSelector(options=['A', 'B', 'C', 1, 2, 3], value=['A', 1], size=5)
269

270 7
    assert cross_select._lists[True].options == ['A', '1']
271 7
    assert cross_select._lists[False].options == ['B', 'C', '2', '3']
272

273
    # Change selection
274 7
    cross_select.value = ['B', 2]
275 7
    assert cross_select._lists[True].options == ['B', '2']
276 7
    assert cross_select._lists[False].options == ['A', 'C', '1', '3']
277

278
    # Change options
279 7
    cross_select.options = {'D': 'D', '4': 4}
280 7
    assert cross_select._lists[True].options == []
281 7
    assert cross_select._lists[False].options == ['D', '4']
282

283
    # Change size
284 7
    cross_select.size = 5
285 7
    assert cross_select._lists[True].size == 5
286 7
    assert cross_select._lists[False].size == 5
287

288
    # Query unselected item
289 7
    cross_select._search[False].value = 'D'
290 7
    assert cross_select._lists[False].value == ['D']
291

292
    # Move queried item
293 7
    cross_select._buttons[True].param.trigger('clicks')
294 7
    assert cross_select._lists[False].options == ['4']
295 7
    assert cross_select._lists[False].value == []
296 7
    assert cross_select._lists[True].options == ['D']
297 7
    assert cross_select._lists[False].value == []
298

299
    # Query selected item
300 7
    cross_select._search[True].value = 'D'
301 7
    cross_select._buttons[False].param.trigger('clicks')
302 7
    assert cross_select._lists[False].options == ['D', '4']
303 7
    assert cross_select._lists[False].value == ['D']
304 7
    assert cross_select._lists[True].options == []
305

306
    # Clear query
307 7
    cross_select._search[False].value = ''
308 7
    assert cross_select._lists[False].value == []
309

310

311 7
def test_cross_select_move_selected_to_unselected():
312 7
    cross_select = CrossSelector(options=['A', 'B', 'C', 1, 2, 3], value=['A', 1], size=5)
313

314 7
    cross_select._lists[True].value = ['A', '1']
315 7
    cross_select._buttons[False].clicks = 1
316

317 7
    assert cross_select.value == []
318 7
    assert cross_select._lists[True].options == []
319

320

321 7
def test_cross_select_move_unselected_to_selected():
322 7
    cross_select = CrossSelector(options=['A', 'B', 'C', 1, 2, 3], value=['A', 1], size=5)
323

324 7
    cross_select._lists[False].value = ['B', '3']
325 7
    cross_select._buttons[True].clicks = 1
326

327 7
    assert cross_select.value == ['A', 1, 'B', 3]
328 7
    assert cross_select._lists[True].options == ['A', 'B', '1', '3']
329

330

331 7
def test_cross_select_move_unselected_to_selected_not_definition_order():
332 7
    cross_select = CrossSelector(options=['B', 'A', 'C', 1, 2, 3], value=['A', 1], size=5, definition_order=False)
333

334 7
    cross_select._lists[False].value = ['B', '3']
335 7
    cross_select._buttons[True].clicks = 1
336

337 7
    assert cross_select.value == ['A', 1, 'B', 3]
338 7
    assert cross_select._lists[True].options == ['A', '1', 'B', '3']

Read our documentation on viewing source code .

Loading