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