• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env vpython3
2# Copyright 2021 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# pylint: disable=protected-access
7
8import json
9import unittest
10import unittest.mock as mock
11
12from flake_suppressor_common import queries
13from flake_suppressor_common import unittest_utils as uu
14
15
16class GetResultCountsUnittest(unittest.TestCase):
17  def setUp(self) -> None:
18    expectations_proceccor = uu.UnitTestExpectationProcessor()
19    results_processor = uu.UnitTestResultProcessor(expectations_proceccor)
20    self._querier_instance = uu.UnitTest_BigQueryQuerier(
21        1, 'project', results_processor)
22
23    self._querier_instance._submitted_builds = set(['build-1234', 'build-2345'])
24    self._subprocess_patcher = mock.patch(
25        'flake_suppressor_common.queries.subprocess.run')
26    self._subprocess_mock = self._subprocess_patcher.start()
27    self.addCleanup(self._subprocess_patcher.stop)
28
29  def testBasic(self) -> None:
30    """Tests that queried data is properly returned."""
31
32    def SideEffect(*_, **kwargs) -> uu.FakeProcess:
33      query = kwargs['input']
34      if 'submitted_builds' in query:
35        # Try results.
36        query_result = [{
37            'typ_tags': ['a1', 'a2', 'a3'],
38            'test_name': 'garbage.suite.garbage.alphanumeric',
39            'result_count': '200',
40        }, {
41            'typ_tags': ['a', 'b', 'c'],
42            'test_name': 'garbage.suite.garbage.alphabet',
43            'result_count': '50',
44        }]
45      else:
46        # CI Results.
47        query_result = [
48            {
49                'typ_tags': ['a', 'b', 'c'],
50                'test_name': 'garbage.suite.garbage.alphabet',
51                'result_count': '100',
52            },
53            {
54                'typ_tags': ['1', '2', '3'],
55                'test_name': 'garbage.suite.garbage.numbers',
56                'result_count': '50',
57            },
58        ]
59      return uu.FakeProcess(stdout=json.dumps(query_result))
60
61    self._subprocess_mock.side_effect = SideEffect
62    result_counts = self._querier_instance.GetResultCounts()
63    expected_result_counts = {
64        ('a', 'b', 'c'): {
65            'alphabet': 150,
66        },
67        ('1', '2', '3'): {
68            'numbers': 50,
69        },
70        ('a1', 'a2', 'a3'): {
71            'alphanumeric': 200,
72        }
73    }
74    self.assertEqual(result_counts, expected_result_counts)
75    self.assertEqual(self._subprocess_mock.call_count, 2)
76
77
78class GenerateBigQueryCommandUnittest(unittest.TestCase):
79
80  def testNoParametersSpecified(self) -> None:
81    """Tests that no parameters are added if none are specified."""
82    cmd = queries.GenerateBigQueryCommand('project', {})
83    for element in cmd:
84      self.assertFalse(element.startswith('--parameter'))
85
86  def testParameterAddition(self) -> None:
87    """Tests that specified parameters are added appropriately."""
88    cmd = queries.GenerateBigQueryCommand('project', {
89        '': {
90            'string': 'string_value'
91        },
92        'INT64': {
93            'int': 1
94        }
95    })
96    self.assertIn('--parameter=string::string_value', cmd)
97    self.assertIn('--parameter=int:INT64:1', cmd)
98
99  def testBatchMode(self) -> None:
100    """Tests that batch mode adds the necessary arg."""
101    cmd = queries.GenerateBigQueryCommand('project', {}, batch=True)
102    self.assertIn('--batch', cmd)
103
104
105if __name__ == '__main__':
106  unittest.main(verbosity=2)
107