Belle II Software  release-05-01-25
path_serialisation.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 import os
5 import shutil
6 import tempfile
7 from unittest import TestCase, main
8 
9 import basf2
10 from basf2 import pickle_path as b2pp
11 from ROOT import Belle2
12 
13 # @cond internal_test
14 
15 
16 class PathSerialisationTestCase(TestCase):
17 
18  def setUp(self):
19  # Create a temporary directory
20  self.test_dir = tempfile.mkdtemp()
21  self.pickle_file = os.path.join(self.test_dir, "output.pkl")
22 
23  def tearDown(self):
24  # Remove the directory after the test
25  shutil.rmtree(self.test_dir)
26 
27  def check_if_paths_equal(self, path1, path2):
28  self.assertEqual(len(path1.modules()), len(path2.modules()))
29 
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())
35 
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)
41 
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())
47 
48  self.check_if_paths_equal(cond1.get_path(), cond2.get_path())
49 
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)
53 
54  self.check_if_paths_equal(pickled_path, path)
55 
56  def test_simple_path(self):
57  path = basf2.create_path()
58  path.add_module("EventInfoSetter", evtNumList=[42])
59 
60  self.pickle_and_check(path)
61 
62  def test_condition_path(self):
63  path = basf2.create_path()
64  module = path.add_module("EventInfoSetter", evtNumList=[42])
65 
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"])
70 
71  module.if_value("<3", condition_path_1)
72  module.if_value(">5", condition_path_2)
73 
74  self.pickle_and_check(condition_path_1)
75  self.pickle_and_check(condition_path_2)
76  self.pickle_and_check(path)
77 
78  def test_high_level_things(self):
79  self.assertNotEqual(0, os.system('basf2 --execute-path /this/path/doesnot/exist'))
80 
81  pathFile = tempfile.NamedTemporaryFile(prefix='b2pathtest_')
82 
83  # uses Python modules, this should fail
84  steeringFile = Belle2.FileSystem.findFile('framework/tests/module_paths.py')
85  self.assertNotEqual(0, os.system('basf2 --dump-path ' + pathFile.name + ' ' + steeringFile))
86 
87  # test actual execution
88  outputFile = tempfile.NamedTemporaryFile(prefix='b2pathtest_')
89 
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)
93 
94  # equivalent to --dump-path
95  basf2.set_pickle_path(pathFile.name)
96  basf2.process(path)
97 
98  # path dumped, but not executed
99  pathSize = os.stat(pathFile.name).st_size
100  self.assertNotEqual(0, pathSize)
101  self.assertEqual(0, os.stat(outputFile.name).st_size)
102 
103  # equivalent to --execute-path
104  self.assertEqual(basf2.get_pickle_path(), pathFile.name)
105  basf2.process(None)
106 
107  # path unmodified, output file created
108  self.assertEqual(pathSize, os.stat(pathFile.name).st_size)
109  self.assertNotEqual(0, os.stat(outputFile.name).st_size)
110 
111 
112 if __name__ == '__main__':
113  main()
114 
115 # @endcond
basf2.process
def process(path, max_event=0)
Definition: __init__.py:25
main
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:77
TestCase
Definition: main.cc:1069
Belle2::FileSystem::findFile
static std::string findFile(const std::string &path, bool silent=false)
Search for given file or directory in local or central release directory, and return absolute path if...
Definition: FileSystem.cc:147