File: test_metadata.py

package info (click to toggle)
python-pymeasure 0.14.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,788 kB
  • sloc: python: 47,201; makefile: 155
file content (117 lines) | stat: -rw-r--r-- 3,719 bytes parent folder | download
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
#
# This file is part of the PyMeasure package.
#
# Copyright (c) 2013-2024 PyMeasure Developers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

import pytest

from pymeasure.experiment.parameters import Metadata
from pymeasure.experiment.procedure import Procedure


def test_metadata_default():
    p = Metadata('Test', default=5)
    assert p.value == 5


def test_metadata_units():
    p = Metadata('Test', units='tests')
    assert p.units == 'tests'


def test_metadata_formatting():
    p1 = Metadata('Test', default=5.157, units='tests')
    assert str(p1) == "5.157 tests"

    p2 = Metadata('Test', default=5.157, units='tests', fmt="%.1f")
    assert str(p2) == "5.2 tests"


def test_metadata_notset():
    p = Metadata('Test')
    with pytest.raises(ValueError):
        p.value


def test_metadata_object_replacement():
    class TestProcedure(Procedure):
        md = Metadata('Test callable', default=19)

    pr = TestProcedure()
    assert pr.md == 19
    assert pr._metadata["md"].value == 19

    # Manually set another value
    pr.md = 20

    pr.evaluate_metadata()

    # Check if the metadata has been correctly replaced with the new value
    assert pr.md == 20
    assert pr._metadata["md"].value == 20


def test_metadata_fget_evaluation():
    def test_method():
        return "teststring"

    class TestAttribute():
        def callable(self):
            return 84

        @property
        def property(self):
            return 48

    class TestProcedure(Procedure):
        md_callable = Metadata('Test callable', fget=test_method)
        md_str_call = Metadata('Test string callable', fget="callable")
        md_str_prop = Metadata('Test string property', fget="property")
        md_nest_call = Metadata('Test nested callable', fget="atr.callable")
        md_nest_prop = Metadata('Test nested property', fget="atr.property")

        def callable(self):
            return 42

        @property
        def property(self):
            return 24

        atr = TestAttribute()

    pr = TestProcedure()

    # Check if all Metadata have no value yet
    assert not pr._metadata["md_callable"].is_set()
    assert not pr._metadata["md_str_call"].is_set()
    assert not pr._metadata["md_str_prop"].is_set()
    assert not pr._metadata["md_nest_call"].is_set()
    assert not pr._metadata["md_nest_prop"].is_set()

    pr.evaluate_metadata()

    # Check if all Metadata has been correctly evaluated
    assert pr._metadata["md_callable"].value == "teststring"
    assert pr._metadata["md_str_call"].value == 42
    assert pr._metadata["md_str_prop"].value == 24
    assert pr._metadata["md_nest_call"].value == 84
    assert pr._metadata["md_nest_prop"].value == 48