Belle II Software  release-05-02-19
test_validation.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 import unittest
5 import metaoptions
6 import tempfile
7 import validationscript
8 import validation
9 
10 call_iteration = 0
11 
12 
13 class ValidationTest(unittest.TestCase):
14 
15  """
16  Various test cases for the validation module
17  """
18 
20  """
21  Test if the interval selection works
22  """
23  interval_sel = validation.IntervalSelector(["release", " nightly"])
24 
25  with tempfile.NamedTemporaryFile() as tf:
26  tf.write(b'#!/usr/bin/env python3\n'
27  b'# -*- coding: utf-8 -*-\n'
28  b'"""\n'
29  b'<header>\n'
30  b'<interval>release</interval>'
31  b'<output>EvtGenSim.root</output>\n'
32  b'<contact>Kilian Lieret kilian.lieret@lmu.de</contact>\n'
33  b'<description>description_text</description>\n'
34  b'</header>\n'
35  b'"""\n')
36 
37  # flush file content, so it can be read by the Script class used
38  # below
39  tf.flush()
40 
41  script = validationscript.Script(tf.name, "package", None)
42  script.load_header()
43  self.assertTrue(interval_sel.in_interval(script))
44 
45  with tempfile.NamedTemporaryFile() as tf:
46  tf.write(b'#!/usr/bin/env python3\n'
47  b'# -*- coding: utf-8 -*-\n'
48  b'"""\n'
49  b'<header>\n'
50  b'<interval>nightly</interval>'
51  b'<output>EvtGenSim.root</output>\n'
52  b'<contact>Kilian Lieret kilian.lieret@lmu.de</contact>\n'
53  b'<description>description_text</description>\n'
54  b'</header>\n'
55  b'"""\n')
56 
57  # flush file content, so it can be read by the Script class used
58  # below
59  tf.flush()
60 
61  script = validationscript.Script(tf.name, "package", None)
62  script.load_header()
63  self.assertTrue(interval_sel.in_interval(script))
64 
66  """
67  Test if the interval selection works if there is no
68  interval setting in the validation header
69  """
70  with tempfile.NamedTemporaryFile() as tf:
71  tf.write(b'#!/usr/bin/env python3\n'
72  b'# -*- coding: utf-8 -*-\n'
73  b'"""\n'
74  b'<header>\n'
75  b'<output>EvtGenSim.root</output>\n'
76  b'<contact>Kilian Lieret kilian.lieret@lmu.de</contact>\n'
77  b'<description>description_text</description>\n'
78  b'</header>\n'
79  b'"""\n')
80 
81  # flush file content, so it can be read by the Script class used
82  # below
83  tf.flush()
84 
85  script = validationscript.Script(tf.name, "package", None)
86  script.load_header()
87 
88  interval_sel = validation.IntervalSelector(["release", " nightly"])
89  self.assertTrue(interval_sel.in_interval(script))
90 
92  """
93  Test if the package selection works and if the required dependecies are
94  properly honored
95  """
96 
97  val = validation.Validation()
98 
99  script1 = validation.Script("val1.py", "tracking", None)
100  script2 = validation.Script("val2.py", "tracking", None)
101  script3 = validation.Script("valOther.py", "other_package", None)
102  script4 = validation.Script("valOtherNotDepending.py", "other_package",
103  None)
104  script2.dependencies = [script3]
105 
106  val.add_script(script1)
107  val.add_script(script2)
108  val.add_script(script3)
109  val.add_script(script4)
110 
111  # test with honoring dependencies
112  val.apply_package_selection(["tracking"], False)
113 
114  self.assertEqual(3, len(val.scripts))
115  self.assertEqual(
116  1,
117  len([
118  s for s in val.scripts
119  if s.unique_name() == script3.unique_name()
120  ])
121  )
122  self.assertEqual(
123  0,
124  len([
125  s for s in val.scripts
126  if s.unique_name() == script4.unique_name()
127  ])
128  )
129 
130  val_no_deps = validation.Validation()
131 
132  val_no_deps.add_script(script1)
133  val_no_deps.add_script(script2)
134  val_no_deps.add_script(script3)
135  val_no_deps.add_script(script4)
136 
137  # test with honoring dependencies
138  val_no_deps.apply_package_selection(["tracking"], True)
139 
140  self.assertEqual(2, len(val_no_deps.scripts))
141  self.assertEqual(
142  0,
143  len([
144  s for s in val_no_deps.scripts
145  if s.unique_name() == script3.unique_name()
146  ])
147  )
148 
149  def test_parse_header(self):
150  """
151  Test if the interval selection works if there is no
152  interval setting in the validation header
153  """
154  with tempfile.NamedTemporaryFile() as tf:
155  tf.write(b'#!/usr/bin/env python3\n'
156  b'# -*- coding: utf-8 -*-\n'
157  b'"""\n'
158  b'<header>\n'
159  b'<input>SomeIn.root</input>\n'
160  b'<output>EvtGenSim.root</output>\n'
161  b'<cacheable/>\n'
162  b'<contact>Kilian Lieret kilian.lieret@lmu.de</contact>\n'
163  b'<description>description_text</description>\n'
164  b'</header>\n'
165  b'"""\n')
166 
167  # flush file content, so it can be read by the Script class used
168  # below
169  tf.flush()
170 
171  script = validationscript.Script(tf.name, "package", None)
172  script.load_header()
173  self.assertTrue(script.is_cacheable())
174  self.assertTrue('EvtGenSim.root' in script.get_output_files())
175  self.assertTrue('SomeIn.root' in script.get_input_files())
176 
178  """
179  Test if the meta options parsers behaves nice
180  """
182  ["shifter", "pvalue-warn=0.9", "pvalue-error=0.4"]
183  )
184 
185  self.assertEqual(0.9, p.pvalue_warn())
186  self.assertEqual(0.4, p.pvalue_error())
187  self.assertTrue(p.has_option("shifter"))
188  self.assertFalse(p.has_option("not is list"))
189 
190  p = metaoptions.MetaOptionParser(["expert", "pvalue-warn="])
191 
192  self.assertEqual(None, p.pvalue_warn())
193  self.assertEqual(None, p.pvalue_error())
194 
195 
196 if __name__ == "__main__":
197  unittest.main()
test_validation.ValidationTest.test_apply_package_selection
def test_apply_package_selection(self)
Definition: test_validation.py:91
test_validation.ValidationTest.test_interval_selection
def test_interval_selection(self)
Definition: test_validation.py:19
test_validation.ValidationTest
Definition: test_validation.py:13
test_validation.ValidationTest.test_parse_header
def test_parse_header(self)
Definition: test_validation.py:149
test_validation.ValidationTest.test_meta_option_parser
def test_meta_option_parser(self)
Definition: test_validation.py:177
validation.IntervalSelector
Definition: validation.py:390
test_validation.ValidationTest.test_interval_selection_default
def test_interval_selection_default(self)
Definition: test_validation.py:65
metaoptions.MetaOptionParser
Definition: metaoptions.py:8