Belle II Software light-2406-ragdoll
path_serialisation.py
1#!/usr/bin/env python3
2
3
10
11import os
12import shutil
13import tempfile
14from unittest import TestCase, main
15
16import basf2
17from basf2 import pickle_path as b2pp
18
19# @cond internal_test
20
21
22class PathSerialisationTestCase(TestCase):
23
24 def setUp(self):
25 # Create a temporary directory
26 self.test_dir = tempfile.mkdtemp()
27 self.pickle_file = os.path.join(self.test_dir, "output.pkl")
28
29 def tearDown(self):
30 # Remove the directory after the test
31 shutil.rmtree(self.test_dir)
32
33 def check_if_paths_equal(self, path1, path2):
34 self.assertEqual(len(path1.modules()), len(path2.modules()))
35
36 for module1, module2 in zip(path1.modules(), path2.modules()):
37 self.assertEqual(module1.name(), module2.name())
38 self.assertEqual(module1.type(), module2.type())
39 self.assertEqual(module1.description(), module2.description())
40 self.assertEqual(module1.package(), module2.package())
41
42 self.assertEqual(len(module1.available_params()), len(module2.available_params()))
43 for param1, param2 in zip(module1.available_params(), module2.available_params()):
44 self.assertEqual(param1.name, param2.name)
45 self.assertEqual(param1.description, param2.description)
46 self.assertEqual(param1.values, param2.values)
47
48 self.assertEqual(len(module1.get_all_conditions()), len(module2.get_all_conditions()))
49 for cond1, cond2 in zip(module1.get_all_conditions(), module2.get_all_conditions()):
50 self.assertEqual(cond1.get_value(), cond2.get_value())
51 self.assertEqual(cond1.get_operator(), cond2.get_operator())
52 self.assertEqual(cond1.get_after_path(), cond2.get_after_path())
53
54 self.check_if_paths_equal(cond1.get_path(), cond2.get_path())
55
56 def pickle_and_check(self, path):
57 b2pp.write_path_to_file(path, self.pickle_file)
58 pickled_path = b2pp.get_path_from_file(self.pickle_file)
59
60 self.check_if_paths_equal(pickled_path, path)
61
62 def test_simple_path(self):
63 path = basf2.create_path()
64 path.add_module("EventInfoSetter", evtNumList=[42])
65
66 self.pickle_and_check(path)
67
68 def test_condition_path(self):
69 path = basf2.create_path()
70 module = path.add_module("EventInfoSetter", evtNumList=[42])
71
72 condition_path_1 = basf2.create_path()
73 condition_path_1.add_module("Geometry", components=["SVD"])
74 condition_path_2 = basf2.create_path()
75 condition_path_2.add_module("Geometry", components=["CDC"])
76
77 module.if_value("<3", condition_path_1)
78 module.if_value(">5", condition_path_2)
79
80 self.pickle_and_check(condition_path_1)
81 self.pickle_and_check(condition_path_2)
82 self.pickle_and_check(path)
83
84 def test_high_level_things(self):
85 self.assertNotEqual(0, os.system('basf2 --execute-path /this/path/doesnot/exist'))
86
87 pathFile = tempfile.NamedTemporaryFile(prefix='b2pathtest_')
88
89 # uses Python modules, this should fail
90 steeringFile = basf2.find_file('framework/tests/module_paths.py')
91 self.assertNotEqual(0, os.system('basf2 --dump-path ' + pathFile.name + ' ' + steeringFile))
92
93 # test actual execution
94 outputFile = tempfile.NamedTemporaryFile(prefix='b2pathtest_')
95
96 path = basf2.create_path()
97 path.add_module('EventInfoSetter', evtNumList=[2, 1], expList=[1, 1], runList=[1, 2])
98 path.add_module('RootOutput', outputFileName=outputFile.name)
99
100 # equivalent to --dump-path
101 basf2.set_pickle_path(pathFile.name)
102 basf2.process(path)
103
104 # path dumped, but not executed
105 pathSize = os.stat(pathFile.name).st_size
106 self.assertNotEqual(0, pathSize)
107 self.assertEqual(0, os.stat(outputFile.name).st_size)
108
109 # equivalent to --execute-path
110 self.assertEqual(basf2.get_pickle_path(), pathFile.name)
111 basf2.process(None)
112
113 # path unmodified, output file created
114 self.assertEqual(pathSize, os.stat(pathFile.name).st_size)
115 self.assertNotEqual(0, os.stat(outputFile.name).st_size)
116
117
118if __name__ == '__main__':
119 main()
120
121# @endcond
Definition: main.py:1