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