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