Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: infra/tools/metric_tool/test/metric_tool_test.py

Issue 1368583005: Create metric_tool to deal with metric definitions (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Added tests, print undocumented metrics Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import os
6 import unittest
7
8 from infra.tools.metric_tool import metric_tool
9 from infra_libs import temporary_directory
10
11
12 DATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data')
13
14
15 class MainMetricToolTest(unittest.TestCase):
16 def test_smoke_main(self):
17 metric_tool.main(DATA_DIR)
18
19
20 class DescriptionExtractionTest(unittest.TestCase):
21 def test_extract_normal_case(self):
22 descriptions = metric_tool.extract_metrics_descriptions(
23 os.path.join(DATA_DIR, 'normal_case.py'))
24
25 self.assertEqual(len(descriptions), len(metric_tool.METRICS_NAMES))
26 # Cheap way of testing we're retrieving the correct strings.
27 for filename, _, name, desc in descriptions:
28 self.assertTrue(filename.endswith('normal_case.py'))
29 self.assertTrue(name.endswith(desc))
30
31 def test_extract_normal_case_2(self):
32 descriptions = metric_tool.extract_metrics_descriptions(
33 os.path.join(DATA_DIR, 'normal_case_2.py'))
34
35 self.assertEqual(len(descriptions), len(metric_tool.METRICS_NAMES))
36 # Cheap way of testing we're retrieving the correct strings.
37 for filename, _, name, desc in descriptions:
38 self.assertTrue(filename.endswith('normal_case_2.py'))
39 self.assertTrue(name.endswith(desc))
40
41 def test_missing_descriptions(self):
42 descriptions = metric_tool.extract_metrics_descriptions(
43 os.path.join(DATA_DIR, 'missing_descriptions.py'))
44
45 self.assertEqual(len(descriptions), 10)
46
47 # Cheap way of testing we're retrieving the correct strings.
48 for filename, _, name, desc in descriptions:
49 self.assertTrue(filename.endswith('missing_descriptions.py'))
50 self.assertTrue(desc is None or name.endswith(desc))
Sergey Berezin 2015/09/29 00:01:18 Technically, if the function drops all metrics wit
pgervais 2015/09/30 00:09:37 Good catch. Fixed.
51
52 def test_metric_name_not_a_string(self):
53 descriptions = metric_tool.extract_metrics_descriptions(
54 os.path.join(DATA_DIR, 'metric_name_not_a_string.py'))
55 self.assertEqual(len(descriptions), 1)
56 self.assertEqual(descriptions[0][2], 'DYNAMIC')
57 self.assertEqual(descriptions[0][3], 'metric1')
58
59 def test_missing_file(self):
60 descriptions = metric_tool.extract_metrics_descriptions(
61 os.path.join(DATA_DIR, 'should_not_exist.py'))
62 self.assertEqual(descriptions, [])
63
64 def test_invalid_syntax(self):
65 with temporary_directory(prefix='metric_tool-') as tempdir:
66 tempfile = os.path.join(tempdir, 'invalid_syntax.py')
67 with open(tempfile, 'w') as f:
68 f.write('=1\n')
69
70 descriptions = metric_tool.extract_metrics_descriptions(tempfile)
71
72 self.assertEqual(descriptions, [])
73
74 def test_other_tests(self):
75 # Sorry for the useless name...
Sergey Berezin 2015/09/29 00:01:18 nit: remove the comment, it's useless :-)
pgervais 2015/09/30 00:09:37 Done.
76 descriptions = metric_tool.extract_metrics_descriptions(
77 os.path.join(DATA_DIR, 'other_tests.py'))
78 self.assertEqual(len(descriptions), 1)
79 description = descriptions[0]
80 self.assertEqual(description[2], '/my/metric')
81 self.assertEqual(description[3], None)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698