Belle II Software development
test_framework.py
1
8import basf2 as b2
9import os
10import shutil
11import glob
12
13from caf.framework import Calibration, CAF
14
15import unittest
16from unittest import TestCase
17from pathlib import Path
18
19# show only Errors as we'll be setting off a lot of ugly deliberate warnings
20b2.set_log_level(b2.LogLevel.ERROR)
21
22
24 """
25 UnitTest for configuration of Calibration class
26 """
27
28 def setUp(self):
29 """
30 Create useful objects for each test
31 """
32 from ROOT import Belle2 # noqa: make the Belle2 namespace available
33 from ROOT.Belle2 import TestCalibrationAlgorithm as TestAlgo
34
35 self.alg1 = TestAlgo()
36
37 self.alg2 = TestAlgo()
38
39 self.col1 = b2.register_module('CaTest')
40
41 self.example_file1 = Path("example1.root")
42
43 self.example_file2 = Path("example2.root")
44 self.example_file1.touch()
45 self.example_file2.touch()
46
47 def test_1(self):
48 """
49 Test whether or not calibration is valid with incorrect setup.
50 """
51 cal = Calibration('TestCalibrationClass_Configure_test1')
52 self.assertFalse(cal.is_valid())
53
54 def test_2(self):
55 """
56 Test whether or not calibration is valid with incorrect setup.
57 """
58 cal = Calibration('TestCalibrationClass_Configure_test2')
59 cal.collector = 'CaTest'
60 self.assertFalse(cal.is_valid())
61
62 def test_3(self):
63 """
64 Test whether or not calibration is valid with incorrect setup.
65 """
66 cal = Calibration('TestCalibrationClass_Configure_test3')
67 cal.collector = self.col1
68 self.assertFalse(cal.is_valid())
69
70 def test_4(self):
71 """
72 Test whether or not calibration is valid with incorrect setup.
73 """
74 cal = Calibration('TestCalibrationClass_Configure_test4')
75 cal.collector = self.col1
76 cal.algorithms = [self.alg1, self.alg2]
77 self.assertFalse(cal.is_valid())
78
79 def test_5(self):
80 """
81 Test whether or not calibration is valid with correct setup.
82 """
83 cal = Calibration('TestCalibrationClass_Configure_test5')
84 cal.collector = self.col1
85 cal.algorithms = [self.alg1, self.alg2]
86 cal.input_files = self.example_file1.as_posix()
87 self.assertTrue(cal.is_valid())
88
89 def test_6(self):
90 """
91 Test whether or not calibration is valid with alternative correct setup.
92 """
93 cal = Calibration('TestCalibrationClass_Configure_test6')
94 cal.collector = self.col1
95 cal.algorithms = [self.alg1, self.alg2]
96 cal.input_files = [self.example_file1.as_posix(), self.example_file2.as_posix()]
97 self.assertTrue(cal.is_valid())
98
99 def tearDown(self):
100 """
101 Unlink example files
102 """
103 self.example_file1.unlink()
104 self.example_file2.unlink()
105
106
108 """
109 UnitTest for validity of Calibration class when given arguments of different types
110 """
111
112 def setUp(self):
113 """
114 Create useful objects for each test
115 """
116 from ROOT import Belle2 # noqa: make the Belle2 namespace available
117 from ROOT.Belle2 import TestCalibrationAlgorithm as TestAlgo
118
119
120 self.alg1 = TestAlgo()
121
122 self.alg2 = TestAlgo()
123
124 self.col1 = b2.register_module('CaTest')
125
126 self.name = 'TestCalibration'
127
128 self.example_file1 = Path("example1.root")
129
130 self.example_file2 = Path("example2.root")
131 self.example_file1.touch()
132 self.example_file2.touch()
133
134 def test_1(self):
135 """
136 Test whether or not calibration is valid with correct setup and if name is stored correctly
137 """
138 cal = Calibration(
139 self.name,
140 collector=self.col1,
141 algorithms=[
142 self.alg1,
143 self.alg2],
144 input_files=self.example_file2.as_posix())
145 self.assertTrue(cal.is_valid() and cal.name == self.name)
146
147 def test_2(self):
148 """
149 Test whether or not calibration is valid with alternative correct setup and if name is stored correctly
150 """
151 cal = Calibration(self.name, 'CaTest', self.alg1, input_files=self.example_file1.as_posix())
152 self.assertTrue(cal.is_valid() and cal.name == self.name)
153
154 def test_3(self):
155 """
156 Test that the default options are correctly applied to a calibration
157 """
158 defaults = {"max_iterations": 4}
159 cal1 = Calibration(self.name, collector=self.col1, algorithms=[self.alg1], input_files=self.example_file1.as_posix())
160 cal1._apply_calibration_defaults(defaults)
161 cal2 = Calibration(self.name, collector=self.col1, algorithms=[self.alg1], input_files=self.example_file2.as_posix())
162 self.assertTrue(cal1.max_iterations == 4 and not cal2.max_iterations)
163
164 def tearDown(self):
165 """
166 Unlink example files
167 """
168 self.example_file1.unlink()
169 self.example_file2.unlink()
170
171
172class TestCAF(TestCase):
173 """
174 UnitTest for configuration and simple running of CAF
175 """
176
177 def setUp(self):
178 """
179 Create useful objects for each test and the teardown
180 """
181 from ROOT import Belle2 # noqa: make the Belle2 namespace available
182 from ROOT.Belle2 import TestCalibrationAlgorithm as TestAlgo
183
184
185 self.name1 = 'TestCalibration1'
186
187 self.name2 = 'TestCalibration2'
188
189 self.name3 = 'TestCalibration3'
190 alg = TestAlgo()
191 col = b2.register_module('CaTest')
192
193 self.example_file1 = Path("example1.root")
194 self.example_file1.touch()
195
196 self.cal1 = Calibration(self.name1, col, alg, self.example_file1.as_posix())
197
198 self.cal2 = Calibration(self.name2, col, alg, self.example_file1.as_posix())
199
200 self.cal3 = Calibration(self.name3, col, alg, self.example_file1.as_posix())
201
203 """
204 Test that add_calibration function results in correct output
205 """
206 fw = CAF()
207 fw.add_calibration(self.cal1)
208 fw.add_calibration(self.cal2)
209 self.assertTrue(fw.calibrations[self.name1].name == self.name1 and fw.calibrations[self.name2].name == self.name2)
210
212 """
213 Test that add_dependency function cannot add itself
214 """
215 self.cal1.depends_on(self.cal1)
216 self.assertFalse(self.cal1.dependencies)
217
219 """
220 Test that output_dir directory is created correctly
221 """
222 fw = CAF()
223 fw.output_dir = 'testCAF_outputdir'
224 fw._make_output_dir()
225 self.assertTrue(os.path.isdir('testCAF_outputdir'))
226
228 """
229 Test that config is correctly setting the default output path.
230 """
231 fw = CAF()
232 self.assertTrue(fw.output_dir == 'calibration_results')
233
234 def tearDown(self):
235 """
236 Removes files that were created during these tests
237 """
238 self.example_file1.unlink()
239 dirs = glob.glob('*testCAF_outputdir')
240 for directory in dirs:
241 shutil.rmtree(directory)
242
243
244def main():
245 unittest.main()
246
247
248if __name__ == '__main__':
249 main()
str name3
Calibration name for use in unittests.
cal1
Calibration attribute for use in unittests.
str name2
Calibration name for use in unittests.
cal3
Calibration attribute for use in unittests.
cal2
Calibration attribute for use in unittests.
str name1
Calibration name for use in unittests.
example_file1
Path to an example file.
col1
Collector module attribute for use in unittests.
example_file2
Path to a second example file.
alg2
Calibration algorithm for use in unittests.
str name
Calibration name for use in unittests.
alg1
Calibration algorithm for use in unittests.
col1
Collector module attribute for use in unittests.
example_file2
Path to a second example file.
alg2
Calibration algorithm attribute for use in unittests.
alg1
Calibration algorithm for use in unittests.
Definition main.py:1