File: test_instrument_checker.py

package info (click to toggle)
python-mcstasscript 0.0.46%2Bgit20250402111921.bfa5a26-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,440 kB
  • sloc: python: 13,421; makefile: 14
file content (152 lines) | stat: -rw-r--r-- 5,247 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import unittest
import os
import mcstasscript as ms
from mcstasscript.tests.helpers_for_tests import WorkInTestDir

from mcstasscript.tools.instrument_checker import has_component, has_parameter, all_parameters_set


def setup_instr_with_path():
    """
    Sets up an instrument with a valid package_path, but it points to
    the dummy installation in the test folder.
    """

    with WorkInTestDir() as handler:
        THIS_DIR = os.path.dirname(os.path.abspath(__file__))
        dummy_path = os.path.join(THIS_DIR, "dummy_mcstas")
        instrument = ms.McStas_instr("test_instrument",
                                     package_path=dummy_path,
                                     executable_path=dummy_path)

    return instrument


def setup_populated_instr():
    """
    Sets up a neutron instrument with some features used and three components
    """
    instr = setup_instr_with_path()

    instr.add_parameter("double", "theta")
    instr.add_parameter("double", "has_default", value=37)
    instr.add_parameter("int", "n_pulses")

    instr.add_declare_var("double", "two_theta")

    instr.append_initialize("two_theta = 2.0*theta;")

    instr.add_component("first_component", "test_for_reading")
    instr.add_component("second_component", "test_for_reading")
    instr.add_component("third_component", "test_for_reading")

    return instr


class TestToolInstrumentChecker(unittest.TestCase):
    """
    Various test of instrument_checker functions
    """

    def test_simple_case_name(self):
        """
        check has_component of given name
        """
        instr = setup_populated_instr()
        self.assertTrue(has_component(instr, component_name="second_component"))

    def test_simple_case_name_not_found(self):
        """
        check has_component returns false when name is not there
        """
        instr = setup_populated_instr()
        self.assertFalse(has_component(instr, component_name="fourth_component"))

    def test_simple_case_type(self):
        """
        check has_component can find component of given type
        """
        instr = setup_populated_instr()
        self.assertTrue(has_component(instr, component_type="test_for_reading"))

    def test_simple_case_type_not_found(self):
        """
        check has_component returns false when component type is not found
        """
        instr = setup_populated_instr()
        self.assertFalse(has_component(instr, component_type="Arm"))

    def test_case_both(self):
        """
        check has_component when given both name and component type
        """
        instr = setup_populated_instr()
        self.assertTrue(has_component(instr, component_name="first_component",
                                      component_type="test_for_reading"))

    def test_case_both_not_found_name(self):
        """
        check has_component returns false when name not found but type is
        """
        instr = setup_populated_instr()
        self.assertFalse(has_component(instr, component_name="",
                                       component_type="test_for_reading"))

    def test_case_both_not_found_type(self):
        """
        check has_component returns false when type not found but name is
        """
        instr = setup_populated_instr()
        self.assertFalse(has_component(instr, component_name="first_component",
                                       component_type="Arm"))

    def test_parameter_found(self):
        """
        check has_parameter of given name
        """
        instr = setup_populated_instr()
        self.assertTrue(has_parameter(instr, parameter_name="theta"))

    def test_parameter_not_found(self):
        """
        check has_parameter returns false when of given name
        """
        instr = setup_populated_instr()
        self.assertFalse(has_parameter(instr, parameter_name="bogus"))

    def test_parameter_found_with_type(self):
        """
        check has_parameter of given name and type
        """
        instr = setup_populated_instr()
        self.assertTrue(has_parameter(instr, parameter_name="n_pulses", parameter_type="int"))

    def test_parameter_found_with_type_default(self):
        """
        check has_parameter of given name and type (using default double)
        """
        instr = setup_populated_instr()
        self.assertTrue(has_parameter(instr, parameter_name="theta", parameter_type="double"))

    def test_parameter_found_with_wrong_type(self):
        """
        check has_parameter of given name
        """
        instr = setup_populated_instr()
        self.assertFalse(has_parameter(instr, parameter_name="theta", parameter_type="int"))

    def test_all_parameters_set_not_set(self):
        """
        check all_parameters_set returns false when not all parameters are set of given name
        """
        instr = setup_populated_instr()
        self.assertFalse(all_parameters_set(instr))

    def test_all_parameters_set_actually_set(self):
        """
        check all_parameters_set returns false when all parameters are set of given name
        """
        instr = setup_populated_instr()

        instr.set_parameters(theta=37, n_pulses=3)
        self.assertTrue(all_parameters_set(instr))