OLD | NEW |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
| 5 import datetime |
5 import unittest | 6 import unittest |
6 | 7 |
7 from infra.tools.antibody import compute_stats | 8 from infra.tools.antibody import compute_stats |
8 | 9 |
9 | 10 |
| 11 class Cursor(object): |
| 12 def execute(self, arg): |
| 13 pass |
| 14 |
| 15 def fetchall(self): |
| 16 results = (('https://codereview.chromium.org/1148323006', |
| 17 datetime.datetime(2015, 5, 28, 16, 8, 33), |
| 18 'suppress-uninit-error-from-sessions-SessionBackend-' |
| 19 'AppendCommandsToFile', |
| 20 'bf1cf11bb721eb52bf46868cb831afd1f53567af'), |
| 21 ('https://codereview.chromium.org/1159593004', |
| 22 datetime.datetime(2015, 6, 1, 3, 37, 20), |
| 23 'Revert-of-Converted-some-extension-browser-tests-into-using-' |
| 24 'event-pages-patchset-1-id-60001-of-https-codereview.chromium.' |
| 25 'org-1108133002', |
| 26 'cda8c938f06f9955ac895099d05a9db3b61f3ab5'), |
| 27 ('https://codereview.chromium.org/1156073004', |
| 28 datetime.datetime(2015, 5, 26, 20, 52, 41), |
| 29 'MemSheriff-Expand-suppressions-for-sqlite3-uninitialized-' |
| 30 'reads', |
| 31 'f48757cfe41e83e770095253b90775eb70f024b3'), |
| 32 ('https://codereview.chromium.org/1124083006', |
| 33 datetime.datetime(2015, 5, 20, 0, 26, 31), |
| 34 'Revert-of-Temporarily-disable-a-webgl-conformance-test-on-' |
| 35 'D3D9-only.-patchset-1-id-1-of-https-codereview.chromium.org-' |
| 36 '1135333004', |
| 37 '0b0b636093a7dbb56cc8712e2263b1c9a1ad8079')) |
| 38 return results |
| 39 |
10 class TestComputeStats(unittest.TestCase): | 40 class TestComputeStats(unittest.TestCase): |
| 41 def setUp(self): |
| 42 self.cc = Cursor() |
| 43 |
11 def test_ratio_calculator(self): | 44 def test_ratio_calculator(self): |
12 reg_num = [['2014-01', 1], ['2014-02', 3], ['2014-07', 5]] | 45 reg_num = [['2014-01', 1], ['2014-02', 3], ['2014-07', 5]] |
13 reg_den = [['2014-07', 10], ['2014-02', 6], ['2014-01', 9]] | 46 reg_den = [['2014-07', 10], ['2014-02', 6], ['2014-01', 9]] |
14 reg_ratio = compute_stats.ratio_calculator(reg_num, reg_den) | 47 reg_ratio = compute_stats.ratio_calculator(reg_num, reg_den) |
15 self.assertEqual(reg_ratio, | 48 self.assertEqual(reg_ratio, |
16 [['2014-01', 0.111], ['2014-02', 0.5], ['2014-07', 0.5]]) | 49 [['2014-01', 0.111], ['2014-02', 0.5], ['2014-07', 0.5]]) |
17 | 50 |
18 zero_num = [['2014-01', 1], ['2014-02', 0], ['2014-07', 5]] | 51 zero_num = [['2014-01', 1], ['2014-02', 0], ['2014-07', 5]] |
19 zero_den = [['2014-07', 10], ['2014-02', 0], ['2014-01', 0]] | 52 zero_den = [['2014-07', 10], ['2014-02', 0], ['2014-01', 0]] |
20 zero_ratio = compute_stats.ratio_calculator(zero_num, zero_den) | 53 zero_ratio = compute_stats.ratio_calculator(zero_num, zero_den) |
21 self.assertEqual(zero_ratio, | 54 self.assertEqual(zero_ratio, |
22 [['2014-01', 0], ['2014-02', 0], ['2014-07', 0.5]]) | 55 [['2014-01', 0], ['2014-02', 0], ['2014-07', 0.5]]) |
23 | 56 |
24 missing_num = [['2014-01', 1], ['2014-02', 3], ['2014-07', 5]] | 57 missing_num = [['2014-01', 1], ['2014-02', 3], ['2014-07', 5]] |
25 missing_den = [['2014-02', 3], ['2014-07', 10]] | 58 missing_den = [['2014-02', 3], ['2014-07', 10]] |
26 missing_ratio = compute_stats.ratio_calculator(missing_num, missing_den) | 59 missing_ratio = compute_stats.ratio_calculator(missing_num, missing_den) |
27 self.assertEqual(missing_ratio, | 60 self.assertEqual(missing_ratio, |
28 [['2014-02', 1.0], ['2014-07', 0.5]]) | 61 [['2014-02', 1.0], ['2014-07', 0.5]]) |
29 | 62 |
30 extra_num = [['2014-01', 1], ['2014-02', 3], ['2014-07', 5]] | 63 extra_num = [['2014-01', 1], ['2014-02', 3], ['2014-07', 5]] |
31 extra_den = [['2014-02', 3], ['2015-07', 5], ['2015-07', 5]] | 64 extra_den = [['2014-02', 3], ['2015-07', 5], ['2015-07', 5]] |
32 extra_ratio = compute_stats.ratio_calculator(extra_num, extra_den) | 65 extra_ratio = compute_stats.ratio_calculator(extra_num, extra_den) |
33 self.assertEqual(extra_ratio, [['2014-02', 1.0]]) | 66 self.assertEqual(extra_ratio, [['2014-02', 1.0]]) |
34 | 67 |
35 def test_totaled_ratio_calculator(self): | 68 def test_totaled_ratio_calculator(self): |
36 ratio = compute_stats.totaled_ratio_calculator(3, 7) | 69 ratio = compute_stats.totaled_ratio_calculator(3, 7) |
37 self.assertEqual(ratio, 0.429) | 70 self.assertEqual(ratio, 0.429) |
38 self.assertRaises(ZeroDivisionError, | 71 zero_ratio = compute_stats.totaled_ratio_calculator(5, 0) |
39 compute_stats.totaled_ratio_calculator, 5, 0) | 72 self.assertEqual(zero_ratio, 0) |
| 73 |
| 74 def test_totaled_tbr_no_lgtm(self): |
| 75 sql_time_specification = 'DATEDIFF(git_commit.timestamp, NOW()) < 0' |
| 76 total_num, output = compute_stats.totaled_tbr_no_lgtm(self.cc, |
| 77 sql_time_specification) |
| 78 self.assertEqual(total_num, 4) |
| 79 self.assertEqual(output, |
| 80 [['https://codereview.chromium.org/1148323006', |
| 81 '2015-05-28 16:08:33', |
| 82 'suppress-uninit-error-from-sessions-SessionBackend-' |
| 83 'AppendCommandsToFile', |
| 84 'bf1cf11bb721eb52bf46868cb831afd1f53567af'], |
| 85 ['https://codereview.chromium.org/1159593004', |
| 86 '2015-06-01 03:37:20', |
| 87 'Revert-of-Converted-some-extension-browser-tests-into-using-' |
| 88 'event-pages-patchset-1-id-60001-of-https-codereview.chromium.' |
| 89 'org-1108133002', |
| 90 'cda8c938f06f9955ac895099d05a9db3b61f3ab5'], |
| 91 ['https://codereview.chromium.org/1156073004', |
| 92 '2015-05-26 20:52:41', |
| 93 'MemSheriff-Expand-suppressions-for-sqlite3-uninitialized-' |
| 94 'reads', |
| 95 'f48757cfe41e83e770095253b90775eb70f024b3'], |
| 96 ['https://codereview.chromium.org/1124083006', |
| 97 '2015-05-20 00:26:31', |
| 98 'Revert-of-Temporarily-disable-a-webgl-conformance-test-on-' |
| 99 'D3D9-only.-patchset-1-id-1-of-https-codereview.chromium.org-' |
| 100 '1135333004', |
| 101 '0b0b636093a7dbb56cc8712e2263b1c9a1ad8079']]) |
OLD | NEW |