Belle II Software  release-08-01-10
module_setparameters.py
1 #!/usr/bin/env python3
2 
3 
10 
11 """Tests for :func:`basf2.set_module_parameters`"""
12 
13 import basf2
14 import unittest
15 
16 
17 class 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.m1m1 = basf2.register_module("EventInfoSetter")
24  self.m1m1.set_name("evt1")
25 
26  self.m2m2 = basf2.register_module("EventInfoSetter")
27  self.m2m2.set_name("evt2")
28 
29  self.m3m3 = basf2.register_module("EventInfoSetter")
30  self.m3m3.set_name("evt1")
31 
32  self.pathpath = basf2.create_path()
33 
34  self.subpathsubpath = basf2.create_path()
35  self.pathpath.add_module(self.m1m1)
36  self.pathpath.add_module(self.m2m2)
37  self.subpathsubpath.add_module(self.m3m3)
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_unsetcheck_unset(self.m1m1)
58  basf2.set_module_parameters(self.pathpath, "evt1", evtNumList=[1, 2, 3])
59  self.check_parameterscheck_parameters(self.m1m1, evtNumList=[1, 2, 3])
60  self.check_unsetcheck_unset(self.m2m2)
61  self.check_unsetcheck_unset(self.m3m3)
62  basf2.set_module_parameters(self.pathpath, "evt2", evtNumList=[4])
63  self.check_parameterscheck_parameters(self.m1m1, evtNumList=[1, 2, 3])
64  self.check_parameterscheck_parameters(self.m2m2, evtNumList=[4])
65  self.check_unsetcheck_unset(self.m3m3)
66 
67  def test_double(self):
68  """Check that it also works if more than one module with the name exists"""
69  self.pathpath.add_module(self.m3m3)
70  basf2.set_module_parameters(self.pathpath, "evt1", evtNumList=[1, 2, 3])
71  self.check_parameterscheck_parameters(self.m1m1, evtNumList=[1, 2, 3])
72  self.check_unsetcheck_unset(self.m2m2)
73  self.check_parameterscheck_parameters(self.m3m3, 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.pathpath, "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.pathpath, "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.pathpath, "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.m2m2.if_true(self.subpathsubpath)
93  basf2.set_module_parameters(self.pathpath, "evt1", evtNumList=[1, 2, 3])
94  self.check_parameterscheck_parameters(self.m1m1, evtNumList=[1, 2, 3])
95  self.check_unsetcheck_unset(self.m2m2)
96  self.check_unsetcheck_unset(self.m3m3)
97 
99  """check that modules in a condition sub path are affected in recursive mode"""
100  self.m2m2.if_true(self.subpathsubpath)
101  basf2.set_module_parameters(self.pathpath, "evt1", evtNumList=[1, 2, 3], recursive=True)
102  self.check_parameterscheck_parameters(self.m1m1, evtNumList=[1, 2, 3])
103  self.check_unsetcheck_unset(self.m2m2)
104  self.check_parameterscheck_parameters(self.m3m3, 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.pathpath.for_each("foo", "foos", self.subpathsubpath)
109  basf2.set_module_parameters(self.pathpath, "evt1", evtNumList=[1, 2, 3])
110  self.check_parameterscheck_parameters(self.m1m1, evtNumList=[1, 2, 3])
111  self.check_unsetcheck_unset(self.m2m2)
112  self.check_unsetcheck_unset(self.m3m3)
113 
115  """check that modules in a for_each sub path are affected in recursive mode"""
116  self.pathpath.for_each("foo", "foos", self.subpathsubpath)
117  basf2.set_module_parameters(self.pathpath, "evt1", evtNumList=[1, 2, 3], recursive=True)
118  self.check_parameterscheck_parameters(self.m1m1, evtNumList=[1, 2, 3])
119  self.check_unsetcheck_unset(self.m2m2)
120  self.check_parameterscheck_parameters(self.m3m3, 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.m2m2.if_true(self.subpathsubpath)
128  self.pathpath.for_each("foo", "foos", sub2)
129  basf2.set_module_parameters(self.pathpath, "evt1", evtNumList=[1, 2, 3], recursive=True)
130  self.check_parameterscheck_parameters(self.m1m1, evtNumList=[1, 2, 3])
131  self.check_unsetcheck_unset(self.m2m2)
132  self.check_parameterscheck_parameters(self.m3m3, evtNumList=[1, 2, 3])
133  self.check_parameterscheck_parameters(m4, evtNumList=[1, 2, 3])
134 
135 
136 if __name__ == '__main__':
137  unittest.main()
def check_parameters(self, module, **params)