1 2
from __future__ import absolute_import, division, unicode_literals
2

3 2
import pytest
4 2
from datetime import datetime, date
5

6 2
from bokeh.models.widgets import FileInput as BkFileInput
7 2
from panel.widgets import (Checkbox, DatePicker, DatetimeInput, FileInput,
8
                           LiteralInput, TextInput, StaticText)
9

10

11 2
def test_checkbox(document, comm):
12

13 2
    checkbox = Checkbox(value=True, name='Checkbox')
14

15 2
    widget = checkbox.get_root(document, comm=comm)
16

17 2
    assert isinstance(widget, checkbox._widget_type)
18 2
    assert widget.labels == ['Checkbox']
19 2
    assert widget.active == [0]
20

21 2
    widget.active = []
22 2
    checkbox._process_events({'active': []})
23 2
    assert checkbox.value == False
24

25 2
    checkbox.value = True
26 2
    assert widget.active == [0]
27

28

29 2
def test_date_picker(document, comm):
30 2
    date_picker = DatePicker(name='DatePicker', value=date(2018, 9, 2),
31
                             start=date(2018, 9, 1), end=date(2018, 9, 10))
32

33 2
    widget = date_picker.get_root(document, comm=comm)
34

35 2
    assert isinstance(widget, date_picker._widget_type)
36 2
    assert widget.title == 'DatePicker'
37 2
    assert widget.value == '2018-09-02'
38 2
    assert widget.min_date == '2018-09-01'
39 2
    assert widget.max_date == '2018-09-10'
40

41 2
    widget.value = '2018-09-03'
42 2
    date_picker._process_events({'value': '2018-09-03'})
43 2
    assert date_picker.value == date(2018, 9, 3)
44

45 2
    date_picker._process_events({'value': date(2018, 9, 5)})
46 2
    assert date_picker.value == date(2018, 9, 5)
47

48 2
    date_picker._process_events({'value': date(2018, 9, 6)})
49 2
    assert date_picker.value == date(2018, 9, 6)
50

51 2
    date_picker.value = date(2018, 9, 4)
52 2
    assert widget.value == '2018-09-04'
53

54

55 2
def test_file_input(document, comm):
56 2
    file_input = FileInput(accept='.txt')
57

58 2
    widget = file_input.get_root(document, comm=comm)
59

60 2
    assert isinstance(widget, BkFileInput)
61

62 2
    file_input._process_events({'mime_type': 'text/plain', 'value': 'U29tZSB0ZXh0Cg==', 'filename': 'testfile'})
63 2
    assert file_input.value == b'Some text\n'
64 2
    assert file_input.mime_type == 'text/plain'
65 2
    assert file_input.accept == '.txt'
66 2
    assert file_input.filename == 'testfile'
67

68

69 2
def test_literal_input(document, comm):
70

71 2
    literal = LiteralInput(value={}, type=dict, name='Literal')
72

73 2
    widget = literal.get_root(document, comm=comm)
74

75 2
    assert isinstance(widget, literal._widget_type)
76 2
    assert widget.title == 'Literal'
77 2
    assert widget.value == '{}'
78

79 2
    literal._process_events({'value': "{'key': (0, 2)}"})
80 2
    assert literal.value == {'key': (0, 2)}
81 2
    assert widget.title == 'Literal'
82

83 2
    literal._process_events({'value': "(0, 2)"})
84 2
    assert literal.value == {'key': (0, 2)}
85 2
    assert widget.title == 'Literal (wrong type)'
86

87 2
    literal._process_events({'value': "invalid"})
88 2
    assert literal.value == {'key': (0, 2)}
89 2
    assert widget.title == 'Literal (invalid)'
90

91 2
    literal._process_events({'value': "{'key': (0, 3)}"})
92 2
    assert literal.value == {'key': (0, 3)}
93 2
    assert widget.title == 'Literal'
94

95 2
    with pytest.raises(ValueError):
96 2
        literal.value = []
97

98

99 2
def test_static_text(document, comm):
100

101 2
    text = StaticText(value='ABC', name='Text:')
102

103 2
    widget = text.get_root(document, comm=comm)
104

105 2
    assert isinstance(widget, text._widget_type)
106 2
    assert widget.text == '<b>Text:</b>: ABC'
107

108 2
    text.value = 'CBA'
109 2
    assert widget.text == '<b>Text:</b>: CBA'
110

111 2
    text.value = '<b>Text:</b>: ABC'
112 2
    assert widget.text == '<b>Text:</b>: ABC'
113

114

115 2
def test_text_input(document, comm):
116

117 2
    text = TextInput(value='ABC', name='Text:')
118

119 2
    widget = text.get_root(document, comm=comm)
120

121 2
    assert isinstance(widget, text._widget_type)
122 2
    assert widget.value == 'ABC'
123 2
    assert widget.title == 'Text:'
124

125 2
    text._process_events({'value': 'CBA'})
126 2
    assert text.value == 'CBA'
127

128 2
    text.value = 'A'
129 2
    assert widget.value == 'A'
130

131 2
def test_datetime_input(document, comm):
132 2
    dt_input = DatetimeInput(value=datetime(2018, 1, 1),
133
                             start=datetime(2017, 12, 31),
134
                             end=datetime(2018, 1, 10),
135
                             name='Datetime')
136

137 2
    widget = dt_input.get_root(document, comm=comm)
138

139 2
    assert isinstance(widget, dt_input._widget_type)
140 2
    assert widget.title == 'Datetime'
141 2
    assert widget.value == '2018-01-01 00:00:00'
142

143 2
    dt_input._process_events({'value': '2018-01-01 00:00:01'})
144 2
    assert dt_input.value == datetime(2018, 1, 1, 0, 0, 1)
145 2
    assert widget.title == 'Datetime'
146

147 2
    dt_input._process_events({'value': '2018-01-01 00:00:01a'})
148 2
    assert dt_input.value == datetime(2018, 1, 1, 0, 0, 1)
149 2
    assert widget.title == 'Datetime (invalid)'
150

151 2
    dt_input._process_events({'value': '2018-01-11 00:00:00'})
152 2
    assert dt_input.value == datetime(2018, 1, 1, 0, 0, 1)
153 2
    assert widget.title == 'Datetime (out of bounds)'
154

155 2
    dt_input._process_events({'value': '2018-01-02 00:00:01'})
156 2
    assert dt_input.value == datetime(2018, 1, 2, 0, 0, 1)
157 2
    assert widget.title == 'Datetime'
158

159 2
    with pytest.raises(ValueError):
160 2
        dt_input.value = datetime(2017, 12, 30)

Read our documentation on viewing source code .

Loading