Belle II Software light-2406-ragdoll
module_setparameters.py
1#!/usr/bin/env python3
2
3
10
11"""Tests for :func:`basf2.set_module_parameters`"""
12
13import basf2
14import unittest
15
16
17class SetModuleParameters(unittest.TestCase):
18 """Test fixture to check setting of parameters for modules in a path by name"""
19
20 def setUp(self):
21 """Setup a path with a few EventInfoSetter modules with special names"""
22
23 self.m1 = basf2.register_module("EventInfoSetter")
24 self.m1.set_name("evt1")
25
26 self.m2 = basf2.register_module("EventInfoSetter")
27 self.m2.set_name("evt2")
28
29 self.m3 = basf2.register_module("EventInfoSetter")
30 self.m3.set_name("evt1")
31
32 self.path = basf2.create_path()
33
34 self.subpath = basf2.create_path()
35 self.path.add_module(self.m1)
36 self.path.add_module(self.m2)
37 self.subpath.add_module(self.m3)
38
39 def check_parameters(self, module, **params):
40 """Check if the parameters of a module are set explicitly to a given value
41
42 module: basf2.Module instance
43 params: named parameters of to check for
44 """
45 for p in module.available_params():
46 if p.name in params:
47 self.assertTrue(p.setInSteering)
48 self.assertEqual(p.values, params[p.name])
49
50 def check_unset(self, module):
51 """Check that all parameters of a given module are not set explicitely"""
52 for p in module.available_params():
53 self.assertFalse(p.setInSteering)
54
55 def test_simple(self):
56 """Check setting the parameters for one module and that it does not affect the other modules"""
57 self.check_unset(self.m1)
58 basf2.set_module_parameters(self.path, "evt1", evtNumList=[1, 2, 3])
59 self.check_parameters(self.m1, evtNumList=[1, 2, 3])
60 self.check_unset(self.m2)
61 self.check_unset(self.m3)
62 basf2.set_module_parameters(self.path, "evt2", evtNumList=[4])
63 self.check_parameters(self.m1, evtNumList=[1, 2, 3])
64 self.check_parameters(self.m2, evtNumList=[4])
65 self.check_unset(self.m3)
66
67 def test_double(self):
68 """Check that it also works if more than one module with the name exists"""
69 self.path.add_module(self.m3)
70 basf2.set_module_parameters(self.path, "evt1", evtNumList=[1, 2, 3])
71 self.check_parameters(self.m1, evtNumList=[1, 2, 3])
72 self.check_unset(self.m2)
73 self.check_parameters(self.m3, evtNumList=[1, 2, 3])
74
75 def test_empty(self):
76 """Make sure that not supplying any parameters gives a ValueError"""
77 with self.assertRaises(ValueError):
78 basf2.set_module_parameters(self.path, "evt1")
79
80 def test_missing(self):
81 """Make sure that not finding any module of the given name raises a KeyError"""
82 with self.assertRaises(KeyError):
83 basf2.set_module_parameters(self.path, "evt3", evtNumList=[0])
84
86 """Make sure that being unable to set the parameter raises a RuntimeError"""
87 with self.assertRaises(RuntimeError):
88 basf2.set_module_parameters(self.path, "evt1", foo="bar")
89
90 def test_condition(self):
91 """check that modules in a condition sub path are not affected by default"""
92 self.m2.if_true(self.subpath)
93 basf2.set_module_parameters(self.path, "evt1", evtNumList=[1, 2, 3])
94 self.check_parameters(self.m1, evtNumList=[1, 2, 3])
95 self.check_unset(self.m2)
96 self.check_unset(self.m3)
97
99 """check that modules in a condition sub path are affected in recursive mode"""
100 self.m2.if_true(self.subpath)
101 basf2.set_module_parameters(self.path, "evt1", evtNumList=[1, 2, 3], recursive=True)
102 self.check_parameters(self.m1, evtNumList=[1, 2, 3])
103 self.check_unset(self.m2)
104 self.check_parameters(self.m3, evtNumList=[1, 2, 3])
105
106 def test_subevent(self):
107 """check that modules in a for_each sub path are not affected by default"""
108 self.path.for_each("foo", "foos", self.subpath)
109 basf2.set_module_parameters(self.path, "evt1", evtNumList=[1, 2, 3])
110 self.check_parameters(self.m1, evtNumList=[1, 2, 3])
111 self.check_unset(self.m2)
112 self.check_unset(self.m3)
113
115 """check that modules in a for_each sub path are affected in recursive mode"""
116 self.path.for_each("foo", "foos", self.subpath)
117 basf2.set_module_parameters(self.path, "evt1", evtNumList=[1, 2, 3], recursive=True)
118 self.check_parameters(self.m1, evtNumList=[1, 2, 3])
119 self.check_unset(self.m2)
120 self.check_parameters(self.m3, evtNumList=[1, 2, 3])
121
123 """check that it also works with a conditions and a for_each sub path in recursive mode"""
124 sub2 = basf2.create_path()
125 m4 = sub2.add_module("EventInfoSetter")
126 m4.set_name("evt1")
127 self.m2.if_true(self.subpath)
128 self.path.for_each("foo", "foos", sub2)
129 basf2.set_module_parameters(self.path, "evt1", evtNumList=[1, 2, 3], recursive=True)
130 self.check_parameters(self.m1, evtNumList=[1, 2, 3])
131 self.check_unset(self.m2)
132 self.check_parameters(self.m3, evtNumList=[1, 2, 3])
133 self.check_parameters(m4, evtNumList=[1, 2, 3])
134
135
136if __name__ == '__main__':
137 unittest.main()
def check_parameters(self, module, **params)