xref: /aosp_15_r20/external/cronet/testing/flake_suppressor_common/queries_unittest.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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
78if __name__ == '__main__':
79  unittest.main(verbosity=2)
80