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