1 7
from __future__ import absolute_import, division, unicode_literals
2

3 7
from collections import OrderedDict
4

5 7
import numpy as np
6 7
import pytest
7

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

13

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

18

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

24 7
    select.value = 2
25 7
    assert select.value == 2.0
26

27 7
    select.value = 3.14
28 7
    assert select.value == 3.14
29

30

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

36 7
    select.value = 'ABC'
37 7
    assert select.value == 'ABC'
38

39 7
    select.value = 'DEF'
40 7
    assert select.value == 'DEF'
41

42

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

47 7
    widget = select.get_root(document, comm=comm)
48

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

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

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

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

64

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

69 7
    widget = select.get_root(document, comm=comm)
70

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

75 7
    select.options = {}
76 7
    assert select.value == None
77 7
    assert widget.value == None
78

79

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

84 7
    widget = select.get_root(document, comm=comm)
85

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

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

95

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

100 7
    widget = select.get_root(document, comm=comm)
101

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

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

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

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

118

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

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

128 7
    model = select.get_root(document, comm=comm)
129

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

134

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

139 7
    widget = select.get_root(document, comm=comm)
140

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

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

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

154 7
    select.value = [object, 'A']
155 7
    assert widget.value == ['C', 'A']
156

157

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

162 7
    widget = choice.get_root(document, comm=comm)
163

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

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

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

177 7
    choice.value = [object, 'A']
178 7
    assert widget.value == ['C', 'A']
179

180

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

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

190 7
    model = select.get_root(document, comm=comm)
191

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

196

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

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

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

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

218

219 7
def test_toggle_group_check(document, comm):
220

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

226 7
        widget = select.get_root(document, comm=comm)
227

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

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

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

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

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

247

248 7
def test_toggle_group_radio(document, comm):
249

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

255 7
        widget = select.get_root(document, comm=comm)
256

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

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

265 7
        select.value = 'A'
266 7
        assert widget.active == 0
267

268

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

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

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

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

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

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

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

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

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

312

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

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

319 7
    assert cross_select.value == []
320 7
    assert cross_select._lists[True].options == []
321

322

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

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

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

332

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

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

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

Read our documentation on viewing source code .

Loading