Debian Codegraph @

tests/experiment/test_metadata.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 Metadata
28 from pymeasure.experiment.procedure import Procedure
29
30
31 def test_metadata_default():
32 p = Metadata('Test', default=5)
33 assert p.value == 5
34
35
36 def test_metadata_units():
37 p = Metadata('Test', units='tests')
38 assert p.units == 'tests'
39
40
41 def test_metadata_formatting():
42 p1 = Metadata('Test', default=5.157, units='tests')
43 assert str(p1) == "5.157 tests"
44
45 p2 = Metadata('Test', default=5.157, units='tests', fmt="%.1f")
46 assert str(p2) == "5.2 tests"
47
48
49 def test_metadata_notset():
50 p = Metadata('Test')
51 with pytest.raises(ValueError):
52 p.value
53
54
55 def test_metadata_object_replacement():
56 class TestProcedure(Procedure):
57 md = Metadata('Test callable', default=19)
58
59 pr = TestProcedure()
60 assert pr.md == 19
61 assert pr._metadata["md"].value == 19
62
63 # Manually set another value
64 pr.md = 20
65
66 pr.evaluate_metadata()
67
68 # Check if the metadata has been correctly replaced with the new value
69 assert pr.md == 20
70 assert pr._metadata["md"].value == 20
71
72
73 def test_metadata_fget_evaluation():
74 def test_method():
75 return "teststring"
76
77 class TestAttribute():
78 def callable(self):
79 return 84
80
81 @property
82 def property(self):
83 return 48
84
85 class TestProcedure(Procedure):
86 md_callable = Metadata('Test callable', fget=test_method)
87 md_str_call = Metadata('Test string callable', fget="callable")
88 md_str_prop = Metadata('Test string property', fget="property")
89 md_nest_call = Metadata('Test nested callable', fget="atr.callable")
90 md_nest_prop = Metadata('Test nested property', fget="atr.property")
91
92 def callable(self):
93 return 42
94
95 @property
96 def property(self):
97 return 24
98
99 atr = TestAttribute()
100
101 pr = TestProcedure()
102
103 # Check if all Metadata have no value yet
104 assert not pr._metadata["md_callable"].is_set()
105 assert not pr._metadata["md_str_call"].is_set()
106 assert not pr._metadata["md_str_prop"].is_set()
107 assert not pr._metadata["md_nest_call"].is_set()
108 assert not pr._metadata["md_nest_prop"].is_set()
109
110 pr.evaluate_metadata()
111
112 # Check if all Metadata has been correctly evaluated
113 assert pr._metadata["md_callable"].value == "teststring"
114 assert pr._metadata["md_str_call"].value == 42
115 assert pr._metadata["md_str_prop"].value == 24
116 assert pr._metadata["md_nest_call"].value == 84
117 assert pr._metadata["md_nest_prop"].value == 48