Debian Codegraph @

tests/experiment/test_parameters.py

raw
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
27 from pymeasure.experiment.parameters import Parameter
28 from pymeasure.experiment.parameters import IntegerParameter
29 from pymeasure.experiment.parameters import BooleanParameter
30 from pymeasure.experiment.parameters import FloatParameter
31 from pymeasure.experiment.parameters import ListParameter
32 from pymeasure.experiment.parameters import VectorParameter
33
34
35 def test_parameter_default():
36 p = Parameter('Test', default=5)
37 assert p.value == 5
38 assert p.cli_args[0] == 5
39 assert p.cli_args[1] == [('units are', 'units'), 'default']
40
41
42 def test_integer_units():
43 p = IntegerParameter('Test', units='V')
44 assert p.units == 'V'
45 assert p.cli_args[0] is None
46 assert p.cli_args[1] == [('units are', 'units'), 'default', 'minimum', 'maximum']
47
48
49 def test_integer_value():
50 p = IntegerParameter('Test', units='tests')
51 with pytest.raises(ValueError):
52 _ = p.value # not set
53 with pytest.raises(ValueError):
54 p.value = 'a' # not an integer
55 p.value = 0.5 # a float
56 assert p.value == 0
57 p.value = False # a boolean
58 assert p.value == 0
59 p.value = 10
60 assert p.value == 10
61 p.value = '5'
62 assert p.value == 5
63 p.value = '11 tests'
64 assert p.value == 11
65 assert p.units == 'tests'
66 with pytest.raises(ValueError):
67 p.value = '31 incorrect units' # not the correct units
68
69
70 def test_integer_bounds():
71 p = IntegerParameter('Test', minimum=0, maximum=10)
72 p.value = 10
73 assert p.value == 10
74 with pytest.raises(ValueError):
75 p.value = 100 # above maximum
76 with pytest.raises(ValueError):
77 p.value = -100 # below minimum
78
79
80 def test_boolean_value():
81 p = BooleanParameter('Test')
82 with pytest.raises(ValueError):
83 _ = p.value # not set
84 with pytest.raises(ValueError):
85 p.value = 'a' # a string
86 with pytest.raises(ValueError):
87 p.value = 10 # a number other than 0 or 1
88 p.value = "True"
89 assert p.value is True
90 p.value = "False"
91 assert p.value is False
92 p.value = "true"
93 assert p.value is True
94 p.value = "false"
95 assert p.value is False
96 p.value = 1 # a number
97 assert p.value is True
98 p.value = 0 # zero
99 assert p.value is False
100 p.value = True
101 assert p.value is True
102 assert p.cli_args[0] is None
103 assert p.cli_args[1] == [('units are', 'units'), 'default']
104
105
106 def test_float_value():
107 p = FloatParameter('Test', units='tests')
108 with pytest.raises(ValueError):
109 _ = p.value # not set
110 with pytest.raises(ValueError):
111 p.value = 'a' # not a float
112 p.value = False # boolean
113 assert p.value == 0.0
114 p.value = 100
115 assert p.value == 100.0
116 p.value = '1.06'
117 assert p.value == 1.06
118 p.value = '11.3 tests'
119 assert p.value == 11.3
120 assert p.units == 'tests'
121 with pytest.raises(ValueError):
122 p.value = '31.3 incorrect units' # not the correct units
123 assert p.cli_args[0] is None
124 assert p.cli_args[1] == [('units are', 'units'), 'default', 'decimals']
125
126
127 def test_float_bounds():
128 p = FloatParameter('Test', minimum=0.1, maximum=0.5)
129 p.value = 0.3
130 assert p.value == 0.3
131 with pytest.raises(ValueError):
132 p.value = 10 # above maximum
133 with pytest.raises(ValueError):
134 p.value = -10 # below minimum
135
136
137 def test_list_string():
138 # make sure string representation of choices is unique
139 with pytest.raises(ValueError):
140 _ = ListParameter('Test', choices=[1, '1'])
141
142
143 def test_list_value():
144 p = ListParameter('Test', choices=[1, 2.2, 'three', 'and four'])
145 p.value = 1
146 assert p.value == 1
147 p.value = 2.2
148 assert p.value == 2.2
149 p.value = '1' # reading from file
150 assert p.value == 1
151 p.value = '2.2' # reading from file
152 assert p.value == 2.2
153 p.value = 'three'
154 assert p.value == 'three'
155 p.value = 'and four'
156 assert p.value == 'and four'
157 with pytest.raises(ValueError):
158 p.value = 5
159 assert p.cli_args[0] is None
160 assert p.cli_args[1] == [('units are', 'units'), 'default', ('choices are', 'choices')]
161
162
163 def test_list_value_with_units():
164 p = ListParameter(
165 'Test', choices=[1, 2.2, 'three', 'and four'],
166 units='tests')
167 p.value = '1 tests'
168 assert p.value == 1
169 p.value = '2.2 tests'
170 assert p.value == 2.2
171 p.value = 'three tests'
172 assert p.value == 'three'
173 p.value = 'and four tests'
174 assert p.value == 'and four'
175 assert p.cli_args[0] is None
176 assert p.cli_args[1] == [('units are', 'units'), 'default', ('choices are', 'choices')]
177
178
179 def test_list_order():
180 p = ListParameter('Test', choices=[1, 2.2, 'three', 'and four'])
181 # check if order is preserved, choices are internally stored as dict
182 assert p.choices == (1, 2.2, 'three', 'and four')
183 assert p.cli_args[0] is None
184 assert p.cli_args[1] == [('units are', 'units'), 'default', ('choices are', 'choices')]
185
186
187 def test_vector():
188 p = VectorParameter('test', length=3, units='tests')
189 p.value = [1, 2, 3]
190 assert p.value == [1, 2, 3]
191 p.value = '[4, 5, 6]'
192 assert p.value == [4, 5, 6]
193 p.value = '[7, 8, 9] tests'
194 assert p.value == [7, 8, 9]
195 with pytest.raises(ValueError):
196 p.value = '[0, 1, 2] wrong unit'
197 with pytest.raises(ValueError):
198 p.value = [1, 2]
199 with pytest.raises(ValueError):
200 p.value = ['a', 'b']
201 with pytest.raises(ValueError):
202 p.value = '0, 1, 2'
203
204 assert p.cli_args[0] is None
205 assert p.cli_args[1] == [('units are', 'units'), 'default', '_length']
206
207 # TODO: Add tests for Measurable