16from ROOT.Belle2
import TestCalibrationAlgorithm
as TestAlgo
18from caf.framework
import Calibration, CAF
21from unittest
import TestCase
22from pathlib
import Path
25b2.set_log_level(b2.LogLevel.ERROR)
28class TestCalibrationClass_Configure(TestCase):
30 UnitTest for configuration of Calibration
class
35 Create useful objects for each test
38 self.alg1 = TestAlgo()
40 self.alg2 = TestAlgo()
42 self.col1 = b2.register_module('CaTest')
44 self.example_file1 = Path(
"example1.root")
45 self.example_file2 = Path(
"example2.root")
46 self.example_file1.touch()
47 self.example_file2.touch()
51 Test whether or not calibration
is valid
with incorrect setup.
53 cal = Calibration('TestCalibrationClass_Configure_test1')
54 self.assertFalse(cal.is_valid())
58 Test whether or not calibration
is valid
with incorrect setup.
60 cal = Calibration('TestCalibrationClass_Configure_test2')
61 cal.collector =
'CaTest'
62 self.assertFalse(cal.is_valid())
66 Test whether or not calibration
is valid
with incorrect setup.
68 cal = Calibration('TestCalibrationClass_Configure_test3')
69 cal.collector = self.col1
70 self.assertFalse(cal.is_valid())
74 Test whether or not calibration
is valid
with incorrect setup.
76 cal = Calibration('TestCalibrationClass_Configure_test4')
77 cal.collector = self.col1
78 cal.algorithms = [self.alg1, self.alg2]
79 self.assertFalse(cal.is_valid())
83 Test whether or not calibration
is valid
with correct setup.
85 cal = Calibration('TestCalibrationClass_Configure_test5')
86 cal.collector = self.col1
87 cal.algorithms = [self.alg1, self.alg2]
88 cal.input_files = self.example_file1.as_posix()
89 self.assertTrue(cal.is_valid())
93 Test whether or not calibration
is valid
with alternative correct setup.
95 cal = Calibration('TestCalibrationClass_Configure_test6')
96 cal.collector = self.col1
97 cal.algorithms = [self.alg1, self.alg2]
98 cal.input_files = [self.example_file1.as_posix(), self.example_file2.as_posix()]
99 self.assertTrue(cal.is_valid())
102 self.example_file1.unlink()
103 self.example_file2.unlink()
106class TestCalibrationClass_Args(TestCase):
108 UnitTest for validity of Calibration
class when given arguments of different types
"""
112 Create useful objects for each test
115 self.alg1 = TestAlgo()
117 self.alg2 = TestAlgo()
119 self.col1 = b2.register_module('CaTest')
121 self.name =
'TestCalibration'
123 self.example_file1 = Path(
"example1.root")
124 self.example_file2 = Path(
"example2.root")
125 self.example_file1.touch()
126 self.example_file2.touch()
130 Test whether or not calibration
is valid
with correct setup
and if name
is stored correctly
138 input_files=self.example_file2.as_posix())
139 self.assertTrue(cal.is_valid() and cal.name == self.name)
143 Test whether or not calibration
is valid
with alternative correct setup
and if name
is stored correctly
145 cal = Calibration(self.name, 'CaTest', self.alg1, input_files=self.example_file1.as_posix())
146 self.assertTrue(cal.is_valid()
and cal.name == self.name)
150 Test that the default options are correctly applied to a calibration
152 defaults = {"max_iterations": 4}
153 cal1 =
Calibration(self.name, collector=self.col1, algorithms=[self.alg1], input_files=self.example_file1.as_posix())
154 cal1._apply_calibration_defaults(defaults)
155 cal2 =
Calibration(self.name, collector=self.col1, algorithms=[self.alg1], input_files=self.example_file2.as_posix())
156 self.assertTrue(cal1.max_iterations == 4
and not cal2.max_iterations)
159 self.example_file1.unlink()
160 self.example_file2.unlink()
163class TestCAF(TestCase):
165 UnitTest for configuration
and simple running of CAF
170 Create useful objects for each test
and the teardown
173 self.name1 = 'TestCalibration1'
175 self.name2 =
'TestCalibration2'
177 self.name3 =
'TestCalibration3'
179 col = b2.register_module(
'CaTest')
181 self.example_file1 = Path(
"example1.root")
182 self.example_file1.touch()
184 self.cal1 =
Calibration(self.name1, col, alg, self.example_file1.as_posix())
186 self.cal2 =
Calibration(self.name2, col, alg, self.example_file1.as_posix())
188 self.cal3 =
Calibration(self.name3, col, alg, self.example_file1.as_posix())
190 def test_add_calibration(self):
192 Test that add_calibration function results in correct output
195 fw.add_calibration(self.cal1)
196 fw.add_calibration(self.cal2)
197 self.assertTrue(fw.calibrations[self.name1].name == self.name1 and fw.calibrations[self.name2].name == self.name2)
199 def test_add_dependency_on_itself(self):
201 Test that add_dependency function cannot add itself
203 self.cal1.depends_on(self.cal1)
204 self.assertFalse(self.cal1.dependencies)
206 def test_make_output_dir(self):
208 Test that output_dir directory is created correctly
211 fw.output_dir = 'testCAF_outputdir'
212 fw._make_output_dir()
213 self.assertTrue(os.path.isdir(
'testCAF_outputdir'))
215 def test_config_output_dir(self):
217 Test that config is correctly setting the default output path.
220 self.assertTrue(fw.output_dir == 'calibration_results')
224 Removes files that were created during these tests
226 self.example_file1.unlink()
227 dirs = glob.glob('*testCAF_outputdir')
228 for directory
in dirs:
229 shutil.rmtree(directory)
236if __name__ ==
'__main__':