Debian Codegraph @

tests/display/test_inputs.py

raw ◀ ▶
1 Debian patch touch this file
1 #
2 # This file is part of the PyMeasure package.
3 #
4 # Copyright (c) 2013-2024 PyMeasure Developers
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24
25 import pytest
26 from unittest import mock
27
28 from pymeasure.display.Qt import QtCore
29 from pymeasure.display.inputs import ScientificInput, BooleanInput, ListInput
30 from pymeasure.experiment.parameters import BooleanParameter, ListParameter, FloatParameter
31
32
33 @pytest.mark.parametrize("default_value", [True, False])
34 class TestBooleanInput:
35 @pytest.mark.parametrize("value_remains_default", [True, False])
36 def test_init_from_param(self, qtbot, default_value, value_remains_default):
37 # set up BooleanInput
38 bool_param = BooleanParameter('potato', default=default_value)
39
40 if (value_remains_default):
41 # Enable check that the value is initialized to default_value
42 check_value = default_value
43 else:
44 # Set to a non default value
45 bool_param.value = not default_value
46 # Enable check that the value is changed after initialization to a non default value
47 check_value = not default_value
48
49 bool_input = BooleanInput(bool_param)
50 qtbot.addWidget(bool_input)
51
52 # test
53 assert bool_input.text() == bool_param.name
54 assert bool_input.value() == check_value
55
56 def test_setValue_should_update_value(self, qtbot, default_value):
57
58 # set up BooleanInput
59 bool_param = BooleanParameter('potato', default=default_value)
60 bool_input = BooleanInput(bool_param)
61 qtbot.addWidget(bool_input)
62
63 bool_input.setValue(not default_value)
64 assert bool_input.value() == (not default_value)
65
66 def test_leftclick_should_update_parameter(self, qtbot, default_value):
67 # set up BooleanInput
68 bool_param = BooleanParameter('potato', default=default_value)
69
70 with mock.patch('test_inputs.BooleanParameter.value',
71 new_callable=mock.PropertyMock,
72 return_value=default_value) as p:
73 bool_input = BooleanInput(bool_param)
74
75 # Clear any call to property 'value' during initialization
76 p.reset_mock()
77
78 qtbot.addWidget(bool_input)
79 bool_input.show()
80
81 # TODO: fix: fails to toggle on Windows
82 # qtbot.mouseClick(bool_input, QtCore.Qt.LeftButton)
83 bool_input.setValue(not default_value)
84
85 assert bool_input.value() == (not default_value)
86 bool_input.parameter # lazy update
87 p.assert_called_once_with(not default_value)
88
89
90 class TestListInput:
91 @pytest.mark.parametrize("choices,default_value", [
92 (["abc", "def", "ghi"], "abc"), # strings
93 ([123, 456, 789], 123), # numbers
94 (["abc", "def", "ghi"], "def") # default not first value
95 ])
96 @pytest.mark.parametrize("value_remains_default", [True, False])
97 def test_init_from_param(self, qtbot, choices, default_value, value_remains_default):
98 list_param = ListParameter('potato',
99 choices=choices,
100 default=default_value,
101 units='m')
102
103 if (value_remains_default):
104 # Enable check that the value is initialized to default_value
105 check_value = default_value
106 else:
107 # Set to a non default value
108 list_param.value = choices[2]
109 # Enable check that the value is changed after initialization to a non default_value
110 check_value = choices[2]
111
112 list_input = ListInput(list_param)
113 qtbot.addWidget(list_input)
114
115 assert list_input.isEditable() is False
116 assert list_input.value() == check_value
117
118 def test_setValue_should_update_value(self, qtbot):
119 # Test write-read loop: verify value -> index -> value conversion
120 choices = [123, 'abc', 0]
121 list_param = ListParameter('potato', choices=choices, default=123)
122 list_input = ListInput(list_param)
123 qtbot.addWidget(list_input)
124
125 for choice in choices:
126 list_input.setValue(choice)
127 assert list_input.currentText() == str(choice)
128 assert list_input.value() == choice
129
130 def test_setValue_should_update_parameter(self, qtbot):
131 choices = [123, 'abc', 0]
132 list_param = ListParameter('potato', choices=choices, default=123)
133 list_input = ListInput(list_param)
134 qtbot.addWidget(list_input)
135
136 with mock.patch('test_inputs.ListParameter.value',
137 new_callable=mock.PropertyMock,
138 return_value=123) as p:
139 for choice in choices:
140 list_input.setValue(choice)
141 list_input.parameter # lazy update
142 p.assert_has_calls((mock.call(123), mock.call('abc'), mock.call(0)))
143
144 def test_unit_should_append_to_strings(self, qtbot):
145 list_param = ListParameter('potato', choices=[123, 456], default=123, units='m')
146 list_input = ListInput(list_param)
147 qtbot.addWidget(list_input)
148
149 assert list_input.currentText() == '123 m'
150
151 def test_set_invalid_value_should_raise(self, qtbot):
152 list_param = ListParameter('potato', choices=[123, 456], default=123, units='m')
153 list_input = ListInput(list_param)
154 qtbot.addWidget(list_input)
155 with pytest.raises(ValueError):
156 list_input.setValue(789)
157
158
159 class TestScientificInput:
160 @pytest.mark.parametrize("min_,max_,default_value", [
161 [0, 20, 12],
162 [0, 1000, 200], # regression #118: default above default max 99.99
163 [-1000, 1000, -10], # regression #118: default below default min 0
164 [0.004, 5.5, 3.3], # minimum #225: 0 < minimum < 0.005
165 [0, 0.01, 0.002] # default #233: default <0.01 changes to 0
166 ])
167 @pytest.mark.parametrize("value_remains_default", [True, False])
168 def test_init_from_param(self, qtbot, min_, max_, default_value, value_remains_default):
169 float_param = FloatParameter('potato',
170 minimum=min_,
171 maximum=max_,
172 default=default_value,
173 units='m')
174
175 if (value_remains_default):
176 # Enable check that the value is initialized to default_value
177 check_value = default_value
178 else:
179 # Set to a non default value
180 float_param.value = min_
181 # Enable check that the value is changed after initialization to a non default value
182 check_value = min_
183
184 sci_input = ScientificInput(float_param)
185 qtbot.addWidget(sci_input)
186
187 assert sci_input.minimum() == min_
188 assert sci_input.maximum() == max_
189 assert sci_input.value() == check_value
190 assert sci_input.suffix() == ' m'
191
192 def test_setValue_within_range_should_set(self, qtbot):
193 float_param = FloatParameter('potato',
194 minimum=-10, maximum=10, default=0)
195 sci_input = ScientificInput(float_param)
196 qtbot.addWidget(sci_input)
197
198 # test
199 sci_input.setValue(5)
200 assert sci_input.value() == 5
201
202 def test_setValue_within_range_should_set_regression_118(self, qtbot):
203 float_param = FloatParameter('potato',
204 minimum=-1000, maximum=1000, default=0)
205 sci_input = ScientificInput(float_param)
206 qtbot.addWidget(sci_input)
207
208 # test - validate min/max beyond QDoubleSpinBox defaults
209 # QDoubleSpinBox defaults are 0 to 99.9 - so test value >= 100
210 sci_input.setValue(999)
211 assert sci_input.value() == 999
212
213 sci_input.setValue(-999)
214 assert sci_input.value() == -999
215
216 def test_setValue_out_of_range_should_constrain(self, qtbot):
217 float_param = FloatParameter('potato',
218 minimum=-1000, maximum=1000, default=0)
219 sci_input = ScientificInput(float_param)
220 qtbot.addWidget(sci_input)
221
222 # test
223 sci_input.setValue(1024)
224 assert sci_input.value() == 1000
225
226 sci_input.setValue(-1024)
227 assert sci_input.value() == -1000
228
229 def test_setValue_should_update_param(self, qtbot):
230 float_param = FloatParameter('potato',
231 minimum=-1000, maximum=1000, default=10.0)
232 sci_input = ScientificInput(float_param)
233 qtbot.addWidget(sci_input)
234
235 with mock.patch('test_inputs.FloatParameter.value',
236 new_callable=mock.PropertyMock,
237 return_value=10.0) as p:
238 # test
239 sci_input.setValue(5.0)
240 sci_input.parameter # lazy update
241 p.assert_called_once_with(5.0)
242
243 @pytest.mark.parametrize("locale, decimalSep", [
•244 [QtCore.QLocale(QtCore.QLocale.Language.English, QtCore.QLocale.Script.LatinScript, QtCore.QLocale.Country.UnitedKingdom), "."], # UK locale for period
245 [QtCore.QLocale(QtCore.QLocale.Language.Dutch, QtCore.QLocale.Script.LatinScript, QtCore.QLocale.Country.Netherlands), ","], # NL locale for comma
246 ])
247 def test_locale_settings(self, qtbot, locale, decimalSep):
248 assert locale.decimalPoint() == decimalSep
249 QtCore.QLocale.setDefault(locale)
250
251 float_param = FloatParameter('potato',
252 minimum=-1000, maximum=1000, default=1.3)
253 sci_input = ScientificInput(float_param)
254
255 # Check if the modified locale is set
256 assert sci_input.locale().decimalPoint() == decimalSep
257 assert sci_input.validator.locale().decimalPoint() == decimalSep
258
259 # Check if conversion from double to text works correctly
260 assert sci_input.valueFromText(f"2{decimalSep}6") == 2.6
261 # Check if conversion from text to double works correctly
262 assert sci_input.textFromValue(2.6) == f"2{decimalSep}6"
263
264 # Reset the locale settings
265 QtCore.QLocale.setDefault(QtCore.QLocale.system())