-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
number_input_test.py
196 lines (159 loc) · 7.17 KB
/
number_input_test.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
# Copyright 2018-2020 Streamlit Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""number_input unit test."""
import pytest
import streamlit as st
from streamlit.errors import StreamlitAPIException
from streamlit.js_number import JSNumber
from streamlit.proto.Alert_pb2 import Alert as AlertProto
from streamlit.proto.NumberInput_pb2 import NumberInput
from tests import testutil
class NumberInputTest(testutil.DeltaGeneratorTestCase):
def test_data_type(self):
"""Test that NumberInput.type is set to the proper
NumberInput.DataType value
"""
st.number_input("Label", value=0)
c = self.get_delta_from_queue().new_element.number_input
self.assertEqual(NumberInput.INT, c.data_type)
st.number_input("Label", value=0.5)
c = self.get_delta_from_queue().new_element.number_input
self.assertEqual(NumberInput.FLOAT, c.data_type)
def test_min_value_zero_sets_default_value(self):
st.number_input("Label", 0, 10)
c = self.get_delta_from_queue().new_element.number_input
self.assertEqual(c.default, 0) # the 0 we provided, not 0.0!
def test_just_label(self):
"""Test that it can be called with no value."""
st.number_input("the label")
c = self.get_delta_from_queue().new_element.number_input
self.assertEqual(c.label, "the label")
self.assertEqual(c.default, 0.0)
self.assertEqual(c.has_min, False)
self.assertEqual(c.has_max, False)
def test_default_value_when_min_is_passed(self):
st.number_input("the label", min_value=1, max_value=10)
c = self.get_delta_from_queue().new_element.number_input
self.assertEqual(c.label, "the label")
self.assertEqual(c.default, 1)
def test_value_between_range(self):
st.number_input("the label", 0, 11, 10)
c = self.get_delta_from_queue().new_element.number_input
self.assertEqual(c.label, "the label")
self.assertEqual(c.default, 10)
self.assertEqual(c.min, 0)
self.assertEqual(c.max, 11)
self.assertEqual(c.has_min, True)
self.assertEqual(c.has_max, True)
def test_default_step_when_a_value_is_int(self):
st.number_input("the label", value=10)
c = self.get_delta_from_queue().new_element.number_input
self.assertEqual(c.step, 1.0)
def test_default_step_when_a_value_is_float(self):
st.number_input("the label", value=10.5)
c = self.get_delta_from_queue().new_element.number_input
self.assertEqual("%0.2f" % c.step, "0.01")
def test_default_format_int(self):
st.number_input("the label", value=10)
c = self.get_delta_from_queue().new_element.number_input
self.assertEqual(c.format, "%d")
def test_default_format_float(self):
st.number_input("the label", value=10.5)
c = self.get_delta_from_queue().new_element.number_input
self.assertEqual(c.format, "%0.2f")
def test_format_int_and_default_step(self):
st.number_input("the label", value=10, format="%d")
c = self.get_delta_from_queue().new_element.number_input
self.assertEqual(c.format, "%d")
self.assertEqual(c.step, 1)
def test_format_float_and_default_step(self):
st.number_input("the label", value=10.0, format="%f")
c = self.get_delta_from_queue().new_element.number_input
self.assertEqual(c.format, "%f")
self.assertEqual("%0.2f" % c.step, "0.01")
def test_value_outrange(self):
with pytest.raises(StreamlitAPIException) as exc_message:
st.number_input("the label", 11, 0, 10)
assert (
"The default `value` of 10 must lie between the `min_value` of "
"11 and the `max_value` of 0, inclusively." == str(exc_message.value)
)
def test_accept_valid_formats(self):
# note: We decided to accept %u even though it is slightly problematic.
# See https://github.com/streamlit/streamlit/pull/943
SUPPORTED = "adifFeEgGuXxo"
for char in SUPPORTED:
st.number_input("any label", format="%" + char)
c = self.get_delta_from_queue().new_element.number_input
self.assertEqual(c.format, "%" + char)
def test_warns_on_float_type_with_int_format(self):
st.number_input("the label", value=5.0, format="%d")
c = self.get_delta_from_queue(-2).new_element.alert
self.assertEqual(c.format, AlertProto.WARNING)
self.assertEqual(
c.body,
"Warning: NumberInput value below has type float, but format %d displays as integer.",
)
def test_warns_on_int_type_with_float_format(self):
st.number_input("the label", value=5, format="%0.2f")
c = self.get_delta_from_queue(-2).new_element.alert
self.assertEqual(c.format, AlertProto.WARNING)
self.assertEqual(
c.body,
"Warning: NumberInput value below has type int so is displayed as int despite format string %0.2f.",
)
def test_error_on_unsupported_formatters(self):
UNSUPPORTED = "pAn"
for char in UNSUPPORTED:
with pytest.raises(StreamlitAPIException) as exc_message:
st.number_input("any label", value=3.14, format="%" + char)
def test_error_on_invalid_formats(self):
BAD_FORMATS = [
"blah",
"a%f",
"a%.3f",
"%d%d",
]
for fmt in BAD_FORMATS:
with pytest.raises(StreamlitAPIException) as exc_message:
st.number_input("any label", value=3.14, format=fmt)
def test_value_out_of_bounds(self):
# Max int
with pytest.raises(StreamlitAPIException) as exc:
value = JSNumber.MAX_SAFE_INTEGER + 1
st.number_input("Label", value=value)
self.assertEqual(
"`value` (%s) must be <= (1 << 53) - 1" % str(value), str(exc.value)
)
# Min int
with pytest.raises(StreamlitAPIException) as exc:
value = JSNumber.MIN_SAFE_INTEGER - 1
st.number_input("Label", value=value)
self.assertEqual(
"`value` (%s) must be >= -((1 << 53) - 1)" % str(value), str(exc.value)
)
# Max float
with pytest.raises(StreamlitAPIException) as exc:
value = 2e308
st.number_input("Label", value=value)
self.assertEqual(
"`value` (%s) must be <= 1.797e+308" % str(value), str(exc.value)
)
# Min float
with pytest.raises(StreamlitAPIException) as exc:
value = -2e308
st.number_input("Label", value=value)
self.assertEqual(
"`value` (%s) must be >= -1.797e+308" % str(value), str(exc.value)
)