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