7 from unittest
import TestCase, main
10 from basf2
import pickle_path
as b2pp
11 from ROOT
import Belle2
16 class PathSerialisationTestCase(
TestCase):
20 self.test_dir = tempfile.mkdtemp()
21 self.pickle_file = os.path.join(self.test_dir,
"output.pkl")
25 shutil.rmtree(self.test_dir)
27 def check_if_paths_equal(self, path1, path2):
28 self.assertEqual(len(path1.modules()), len(path2.modules()))
30 for module1, module2
in zip(path1.modules(), path2.modules()):
31 self.assertEqual(module1.name(), module2.name())
32 self.assertEqual(module1.type(), module2.type())
33 self.assertEqual(module1.description(), module2.description())
34 self.assertEqual(module1.package(), module2.package())
36 self.assertEqual(len(module1.available_params()), len(module2.available_params()))
37 for param1, param2
in zip(module1.available_params(), module2.available_params()):
38 self.assertEqual(param1.name, param2.name)
39 self.assertEqual(param1.description, param2.description)
40 self.assertEqual(param1.values, param2.values)
42 self.assertEqual(len(module1.get_all_conditions()), len(module2.get_all_conditions()))
43 for cond1, cond2
in zip(module1.get_all_conditions(), module2.get_all_conditions()):
44 self.assertEqual(cond1.get_value(), cond2.get_value())
45 self.assertEqual(cond1.get_operator(), cond2.get_operator())
46 self.assertEqual(cond1.get_after_path(), cond2.get_after_path())
48 self.check_if_paths_equal(cond1.get_path(), cond2.get_path())
50 def pickle_and_check(self, path):
51 b2pp.write_path_to_file(path, self.pickle_file)
52 pickled_path = b2pp.get_path_from_file(self.pickle_file)
54 self.check_if_paths_equal(pickled_path, path)
56 def test_simple_path(self):
57 path = basf2.create_path()
58 path.add_module(
"EventInfoSetter", evtNumList=[42])
60 self.pickle_and_check(path)
62 def test_condition_path(self):
63 path = basf2.create_path()
64 module = path.add_module(
"EventInfoSetter", evtNumList=[42])
66 condition_path_1 = basf2.create_path()
67 condition_path_1.add_module(
"Geometry", components=[
"SVD"])
68 condition_path_2 = basf2.create_path()
69 condition_path_2.add_module(
"Geometry", components=[
"CDC"])
71 module.if_value(
"<3", condition_path_1)
72 module.if_value(
">5", condition_path_2)
74 self.pickle_and_check(condition_path_1)
75 self.pickle_and_check(condition_path_2)
76 self.pickle_and_check(path)
78 def test_high_level_things(self):
79 self.assertNotEqual(0, os.system(
'basf2 --execute-path /this/path/doesnot/exist'))
81 pathFile = tempfile.NamedTemporaryFile(prefix=
'b2pathtest_')
85 self.assertNotEqual(0, os.system(
'basf2 --dump-path ' + pathFile.name +
' ' + steeringFile))
88 outputFile = tempfile.NamedTemporaryFile(prefix=
'b2pathtest_')
90 path = basf2.create_path()
91 path.add_module(
'EventInfoSetter', evtNumList=[2, 1], expList=[1, 1], runList=[1, 2])
92 path.add_module(
'RootOutput', outputFileName=outputFile.name)
95 basf2.set_pickle_path(pathFile.name)
99 pathSize = os.stat(pathFile.name).st_size
100 self.assertNotEqual(0, pathSize)
101 self.assertEqual(0, os.stat(outputFile.name).st_size)
104 self.assertEqual(basf2.get_pickle_path(), pathFile.name)
108 self.assertEqual(pathSize, os.stat(pathFile.name).st_size)
109 self.assertNotEqual(0, os.stat(outputFile.name).st_size)
112 if __name__ ==
'__main__':