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