llvm.org GIT mirror lnt / b17dc31
[LNT] Add unittests for test data creation library Add unit tests for existing code in the test data creation library ahead of a patch adding support for version 2 of the LNT JSON report file format to ensure no existing code is broken in the process. Differential Revision: https://reviews.llvm.org/D65747 git-svn-id: https://llvm.org/svn/llvm-project/lnt/trunk@368642 91177308-0d34-0410-b5e6-96231b3b80d8 Thomas Preud'homme 2 months ago
1 changed file(s) with 331 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 # RUN: python %s
1
2 import unittest
3 import logging
4 import copy
5 import re
6 import sys
7 from datetime import datetime
8 from lnt.testing import TestSamples, Run, Machine, Report
9
10 logging.basicConfig(level=logging.DEBUG)
11
12
13 class TestTestSamples(unittest.TestCase):
14 def setUp(self):
15 self.test_samples_int_list_noinfo = TestSamples('Test1', [1, 2])
16 self.test_samples_str_float_list_noinfo = TestSamples('Test3',
17 ['2.3', '5.8'])
18 self.test_samples_int_list_info = TestSamples('Test7', [1.7, 2.8],
19 {'nb_files': 2})
20
21 def test_constructor(self):
22 # Check implicit float conversion from integer.
23 self.assertEqual(self.test_samples_int_list_noinfo.name, 'Test1')
24 self.assertDictEqual(self.test_samples_int_list_noinfo.info, {})
25 self.assertIsInstance(self.test_samples_int_list_noinfo.data[0], float)
26 self.assertIsInstance(self.test_samples_int_list_noinfo.data[1], float)
27 self.assertListEqual(self.test_samples_int_list_noinfo.data,
28 [1.0, 2.0])
29
30 # Check explicit float conversion from integer.
31 float_test_samples_int_list_noinfo = TestSamples('Test2', [8, 9],
32 conv_f=float)
33 self.assertEqual(float_test_samples_int_list_noinfo.name, 'Test2')
34 self.assertDictEqual(float_test_samples_int_list_noinfo.info, {})
35 self.assertIsInstance(float_test_samples_int_list_noinfo.data[0],
36 float)
37 self.assertIsInstance(float_test_samples_int_list_noinfo.data[1],
38 float)
39 self.assertListEqual(float_test_samples_int_list_noinfo.data,
40 [8.0, 9.0])
41
42 # Check implicit float conversion from string.
43 self.assertEqual(self.test_samples_str_float_list_noinfo.name, 'Test3')
44 self.assertDictEqual(self.test_samples_str_float_list_noinfo.info, {})
45 self.assertListEqual(self.test_samples_str_float_list_noinfo.data,
46 [2.3, 5.8])
47
48 # Check implicit nop float conversion from float.
49 test_samples_float_list_noinfo = TestSamples('Test4', [6.4, 5.5])
50 self.assertEqual(test_samples_float_list_noinfo.name, 'Test4')
51 self.assertDictEqual(test_samples_float_list_noinfo.info, {})
52 self.assertListEqual(test_samples_float_list_noinfo.data, [6.4, 5.5])
53
54 # Check nop non-float conversion from input of same type.
55 int_test_samples_int_list_noinfo = TestSamples('Test5', [1, 2],
56 conv_f=int)
57 self.assertEqual(int_test_samples_int_list_noinfo.name, 'Test5')
58 self.assertDictEqual(int_test_samples_int_list_noinfo.info, {})
59 self.assertListEqual(int_test_samples_int_list_noinfo.data, [1, 2])
60
61 # Check non-float conversion from string.
62 int_test_samples_float_list_noinfo = TestSamples('Test6', [6.4, 5.5],
63 conv_f=int)
64 self.assertEqual(int_test_samples_float_list_noinfo.name, 'Test6')
65 self.assertDictEqual(int_test_samples_float_list_noinfo.info, {})
66 self.assertListEqual(int_test_samples_float_list_noinfo.data, [6, 5])
67
68 # Check non-float conversion from float.
69 self.assertEqual(self.test_samples_int_list_info.name, 'Test7')
70 self.assertDictEqual(self.test_samples_int_list_info.info,
71 {'nb_files': '2'})
72 self.assertListEqual(self.test_samples_int_list_info.data, [1.7, 2.8])
73
74 def test_render(self):
75 # Check rendering with no info.
76 d1 = {'Name': 'Test3',
77 'Info': {},
78 'Data': [2.3, 5.8]}
79 self.assertDictEqual(self.test_samples_str_float_list_noinfo.render(),
80 d1)
81
82 # Check rendering with info.
83 d2 = {'Name': 'Test7',
84 'Info': {'nb_files': '2'},
85 'Data': [1.7, 2.8]}
86 self.assertDictEqual(self.test_samples_int_list_info.render(), d2)
87
88
89 class TestRun(unittest.TestCase):
90 def setUp(self):
91 self.info = {'tag': 'nts', 'run_order': 18246}
92 self.run_float_start = Run(0.0, None, self.info)
93
94 def test_constructor(self):
95 info = {'__report_version__': '1',
96 'tag': 'nts',
97 'run_order': '18246'}
98
99 # Check time normalization of end time from float.
100 self.assertEqual(self.run_float_start.start_time,
101 '1970-01-01 00:00:00')
102 self.assertTrue(self.run_float_start.end_time)
103 self.assertNotEqual(self.run_float_start.end_time,
104 self.run_float_start.start_time)
105 self.assertTrue(datetime.strptime(self.run_float_start.end_time,
106 '%Y-%m-%d %H:%M:%S'))
107 self.assertDictEqual(self.run_float_start.info, info)
108
109 # Check time normalization of end time from datetime.
110 run_str_start = Run('2019-07-01 01:02:03', None, info=self.info)
111 self.assertEqual(run_str_start.start_time, '2019-07-01 01:02:03')
112 self.assertTrue(run_str_start.end_time)
113 self.assertNotEqual(run_str_start.end_time, run_str_start.start_time)
114 self.assertTrue(datetime.strptime(run_str_start.end_time,
115 '%Y-%m-%d %H:%M:%S'))
116 self.assertDictEqual(run_str_start.info, info)
117
118 # Check time normalization of end time from string.
119 run_datetime_start = Run(datetime(2019, 7, 2), None, info=self.info)
120 self.assertEqual(run_datetime_start.start_time, '2019-07-02 00:00:00')
121 self.assertTrue(run_datetime_start.end_time)
122 self.assertNotEqual(run_datetime_start.end_time,
123 run_datetime_start.start_time)
124 self.assertTrue(datetime.strptime(run_datetime_start.end_time,
125 '%Y-%m-%d %H:%M:%S'))
126 self.assertDictEqual(run_datetime_start.info, info)
127
128 # Check time normalization of start time from float.
129 run_float_end = Run(None, 0.0, self.info)
130 self.assertEqual(run_float_end.end_time, '1970-01-01 00:00:00')
131 self.assertTrue(run_float_end.start_time)
132 self.assertNotEqual(run_float_end.start_time, run_float_end.end_time)
133 self.assertTrue(datetime.strptime(run_float_end.start_time,
134 '%Y-%m-%d %H:%M:%S'))
135 self.assertDictEqual(run_float_end.info, info)
136
137 # Check time normalization of start time from datetime.
138 run_str_end = Run(None, '2019-07-01 01:02:03', self.info)
139 self.assertEqual(run_str_end.end_time, '2019-07-01 01:02:03')
140 self.assertTrue(run_str_end.start_time)
141 self.assertNotEqual(run_str_end.start_time, run_str_end.end_time)
142 self.assertTrue(datetime.strptime(run_str_end.start_time,
143 '%Y-%m-%d %H:%M:%S'))
144 self.assertDictEqual(run_str_end.info, info)
145
146 # Check time normalization of start time from string.
147 run_datetime_end = Run(None, datetime(2019, 7, 2), self.info)
148 self.assertEqual(run_datetime_end.end_time, '2019-07-02 00:00:00')
149 self.assertTrue(run_datetime_end.start_time)
150 self.assertNotEqual(run_datetime_end.start_time,
151 run_datetime_end.end_time)
152 self.assertTrue(datetime.strptime(run_datetime_end.start_time,
153 '%Y-%m-%d %H:%M:%S'))
154 self.assertDictEqual(run_datetime_end.info, info)
155
156 # Check failure when info contains __report_version__ key.
157 self.assertRaisesRegexp(ValueError, '__report_version__.*reserved',
158 Run, None, None, info)
159
160 def test_update(self):
161 # Check update with a supplied end time.
162 end_time_updated_run_float_start = copy.deepcopy(self.run_float_start)
163 end_time_updated_run_float_start.update_endtime(datetime(2019, 8, 2))
164 self.assertEqual(end_time_updated_run_float_start.end_time,
165 '2019-08-02 00:00:00')
166
167 # Check update with default (=now) end time.
168 updated_run_float_start = (
169 copy.deepcopy(end_time_updated_run_float_start))
170 updated_run_float_start.update_endtime()
171 self.assertTrue(updated_run_float_start.end_time)
172 self.assertNotEqual(updated_run_float_start.end_time,
173 updated_run_float_start.start_time)
174 self.assertNotEqual(updated_run_float_start.end_time,
175 end_time_updated_run_float_start.end_time)
176
177 def test_render(self):
178 d = {'Start Time': '1970-01-01 00:00:00',
179 'End Time': self.run_float_start.end_time,
180 'Info': {'__report_version__': '1',
181 'run_order': '18246',
182 'tag': 'nts'}}
183 self.assertDictEqual(self.run_float_start.render(), d)
184
185
186 class TestMachine(unittest.TestCase):
187 def setUp(self):
188 self.machine_noinfo = Machine('Machine1')
189 self.machine = Machine('Machine2', {'CPUs': 2})
190
191 def test_constructor(self):
192 # Check constructor with no info.
193 self.assertEqual(self.machine_noinfo.name, 'Machine1')
194 self.assertDictEqual(self.machine_noinfo.info, {})
195
196 # Check constructor with info.
197 self.assertEqual(self.machine.name, 'Machine2')
198 self.assertDictEqual(self.machine.info, {'CPUs': '2'})
199
200 def test_render(self):
201 # Check rendering with no info.
202 d1 = {'Name': 'Machine1',
203 'Info': {}}
204 self.assertDictEqual(self.machine_noinfo.render(), d1)
205
206 # Check rendering with info.
207 d2 = {'Name': 'Machine2',
208 'Info': {'CPUs': '2'}}
209 self.assertDictEqual(self.machine.render(), d2)
210
211
212 class TestReport(unittest.TestCase):
213 maxDiff = None
214
215 def setUp(self):
216 self.machine = Machine('Machine', info={'nb_cpus': 2})
217 self.run = Run(0.0, '1982-01-01 00:00:00', {'tag': 'nts',
218 'run_order': 18246})
219 self.tests_samples = [TestSamples('Test.exec', [1.7, 2.8],
220 {'nb_files': 2})]
221 self.report = Report(self.machine, self.run, self.tests_samples)
222
223 def test_constructor(self):
224 # Check successful constructor call.
225 self.assertEqual(self.report.machine, self.machine)
226 self.assertEqual(self.report.run, self.run)
227 self.assertListEqual(self.report.tests, self.tests_samples)
228
229 # Check call to check().
230 self.assertRaises(AssertionError, Report, [], self.run,
231 self.tests_samples)
232
233 def test_check(self):
234 # Check valid report.
235 self.report.check()
236
237 # Check type test for machine.
238 report_machine_list = copy.deepcopy(self.report)
239 report_machine_list.machine = []
240 self.assertRaises(AssertionError, report_machine_list.check)
241
242 # Check type test for run.
243 report_run_list = copy.deepcopy(self.report)
244 report_run_list.run = []
245 self.assertRaises(AssertionError, report_run_list.check)
246
247 # Check type test for all tests.
248 report_run_list = copy.deepcopy(self.report)
249 report_tests_int_list = copy.deepcopy(self.report)
250 report_tests_int_list.tests = [2]
251 self.assertRaises(AssertionError, report_tests_int_list.check)
252
253 def test_update_report(self):
254 orig_end_time = self.report.run.end_time
255 new_tests_samples = [TestSamples('Test2.exec', [56.5])]
256 self.report.update_report(new_tests_samples)
257 self.tests_samples.extend(new_tests_samples)
258 self.assertListEqual(self.report.tests, self.tests_samples)
259 self.assertNotEqual(self.report.run.end_time, orig_end_time)
260
261 def test_render(self):
262 # Check rendering with default indentation.
263 self.assertMultiLineEqual(re.sub(r' +\n', '\n', self.report.render()),
264 """\
265 {
266 "Machine": {
267 "Info": {
268 "nb_cpus": "2"
269 },
270 "Name": "Machine"
271 },
272 "Run": {
273 "End Time": "1982-01-01 00:00:00",
274 "Info": {
275 "__report_version__": "1",
276 "run_order": "18246",
277 "tag": "nts"
278 },
279 "Start Time": "1970-01-01 00:00:00"
280 },
281 "Tests": [
282 {
283 "Data": [
284 1.7,
285 2.8
286 ],
287 "Info": {
288 "nb_files": "2"
289 },
290 "Name": "Test.exec"
291 }
292 ]
293 }""")
294
295 # Check rendering with supplied indentation.
296 self.assertMultiLineEqual(re.sub(r' +\n', '\n',
297 self.report.render(indent=2)), """\
298 {
299 "Machine": {
300 "Info": {
301 "nb_cpus": "2"
302 },
303 "Name": "Machine"
304 },
305 "Run": {
306 "End Time": "1982-01-01 00:00:00",
307 "Info": {
308 "__report_version__": "1",
309 "run_order": "18246",
310 "tag": "nts"
311 },
312 "Start Time": "1970-01-01 00:00:00"
313 },
314 "Tests": [
315 {
316 "Data": [
317 1.7,
318 2.8
319 ],
320 "Info": {
321 "nb_files": "2"
322 },
323 "Name": "Test.exec"
324 }
325 ]
326 }""")
327
328
329 if __name__ == '__main__':
330 unittest.main(argv=[sys.argv[0], ])