14from unittest
import TestCase, main
17from basf2
import pickle_path
as b2pp
22class PathSerialisationTestCase(TestCase):
26 self.test_dir = tempfile.mkdtemp()
27 self.pickle_file = os.path.join(self.test_dir,
"output.pkl")
31 shutil.rmtree(self.test_dir)
33 def check_if_paths_equal(self, path1, path2):
34 self.assertEqual(len(path1.modules()), len(path2.modules()))
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())
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)
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())
54 self.check_if_paths_equal(cond1.get_path(), cond2.get_path())
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)
60 self.check_if_paths_equal(pickled_path, path)
62 def test_simple_path(self):
63 path = basf2.create_path()
64 path.add_module(
"EventInfoSetter", evtNumList=[42])
66 self.pickle_and_check(path)
68 def test_condition_path(self):
69 path = basf2.create_path()
70 module = path.add_module(
"EventInfoSetter", evtNumList=[42])
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"])
77 module.if_value(
"<3", condition_path_1)
78 module.if_value(
">5", condition_path_2)
80 self.pickle_and_check(condition_path_1)
81 self.pickle_and_check(condition_path_2)
82 self.pickle_and_check(path)
84 def test_high_level_things(self):
85 self.assertNotEqual(0, os.system(
'basf2 --execute-path /this/path/doesnot/exist'))
87 pathFile = tempfile.NamedTemporaryFile(prefix=
'b2pathtest_')
90 steeringFile = basf2.find_file(
'framework/tests/module_paths.py')
91 self.assertNotEqual(0, os.system(
'basf2 --dump-path ' + pathFile.name +
' ' + steeringFile))
94 outputFile = tempfile.NamedTemporaryFile(prefix=
'b2pathtest_')
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)
101 basf2.set_pickle_path(pathFile.name)
105 pathSize = os.stat(pathFile.name).st_size
106 self.assertNotEqual(0, pathSize)
107 self.assertEqual(0, os.stat(outputFile.name).st_size)
110 self.assertEqual(basf2.get_pickle_path(), pathFile.name)
114 self.assertEqual(pathSize, os.stat(pathFile.name).st_size)
115 self.assertNotEqual(0, os.stat(outputFile.name).st_size)
118if __name__ ==
'__main__':