Debian Codegraph @

tests/experiment/test_procedure.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 import pickle
27
28 from pymeasure.experiment.procedure import Procedure, ProcedureWrapper
29 from pymeasure.experiment.parameters import Parameter
30 from pymeasure.units import ureg
31
32 from data.procedure_for_testing import RandomProcedure
33
34
35 def test_parameters():
36 class TestProcedure(Procedure):
37 x = Parameter('X', default=5)
38
39 p = TestProcedure()
40 assert p.x == 5
41 p.x = 10
42 assert p.x == 10
43 assert p.parameters_are_set()
44 objs = p.parameter_objects()
45 assert 'x' in objs
46 assert objs['x'].value == p.x
47
48
49 # TODO: Add tests for measureables
50
51
52 def test_procedure_wrapper():
53 assert RandomProcedure.iterations.value == 100
54 procedure = RandomProcedure()
55 procedure.iterations = 101
56 wrapper = ProcedureWrapper(procedure)
57
58 new_wrapper = pickle.loads(pickle.dumps(wrapper))
59 assert hasattr(new_wrapper, 'procedure')
60 assert new_wrapper.procedure.iterations == 101
61 assert RandomProcedure.iterations.value == 100
62
63
64 # This test checks that user can define properties using the parameters inside the procedure
65 # The test ensure that property is evaluated only when the Parameter has been processed during
66 # class initialization.
67
68
69 def test_procedure_properties():
70 class TestProcedure(Procedure):
71 @property
72 def a(self):
73 assert isinstance(self.x, int)
74 return self.x
75
76 @property
77 def z(self):
78 assert isinstance(self.x, int)
79 return self.x
80
81 x = Parameter('X', default=5)
82
83 p = TestProcedure()
84 assert p.x == 5
85
86
87 # Make sure that a procedure can be initialized even though some properties are raising
88 # errors at initialization time
89
90
91 def test_procedure_init_with_invalid_property():
92 class TestProcedure(Procedure):
93 @property
94 def prop(self):
95 return self.x
96
97 p = TestProcedure()
98 with pytest.raises(AttributeError):
99 _ = p.prop # AttributeError
100 p.x = 5
101 assert p.prop == 5
102
103
104 @pytest.mark.parametrize("header, units", (
105 ("x (m)", ureg.m),
106 ("x (m/s)", ureg.m / ureg.s),
107 ("x (V/(m*s))", ureg.V / ureg.m / ureg.s),
108 ("x (1)", ureg.dimensionless)
109 ))
110 def test_procedure_parse_columns(header, units):
111 assert Procedure.parse_columns([header])[header] == ureg.Quantity(1, units)
112
113
114 @pytest.mark.parametrize("valid_header_no_unit", (
115 ["x"], ["x ( x + y )"], ["x ( notes )"], ["x [V]"]
116 ))
117 def test_procedure_no_parsed_units(valid_header_no_unit):
118 assert Procedure.parse_columns(valid_header_no_unit) == {}
119
120
121 @pytest.mark.parametrize("invalid_header_unit", (
122 ["x (sqrt)"], ["x (x)"], ["x (y)"],
123 ))
124 def test_procedure_invalid_parsed_unit(invalid_header_unit):
125 with pytest.raises(ValueError):
126 Procedure.parse_columns(invalid_header_unit)