Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | # Copyright 2016 The Chromium Authors |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """Unit tests for savedqueries_helpers feature.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import unittest |
| 11 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 12 | try: |
| 13 | from mox3 import mox |
| 14 | except ImportError: |
| 15 | import mox |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 16 | |
| 17 | from features import savedqueries_helpers |
| 18 | from testing import fake |
| 19 | from tracker import tracker_bizobj |
| 20 | |
| 21 | |
| 22 | class SavedQueriesHelperTest(unittest.TestCase): |
| 23 | |
| 24 | def setUp(self): |
| 25 | self.features = fake.FeaturesService() |
| 26 | self.project = fake.ProjectService() |
| 27 | self.cnxn = 'fake cnxn' |
| 28 | self.mox = mox.Mox() |
| 29 | |
| 30 | def tearDown(self): |
| 31 | self.mox.UnsetStubs() |
| 32 | self.mox.ResetAll() |
| 33 | |
| 34 | def testParseSavedQueries(self): |
| 35 | post_data = { |
| 36 | 'xyz_savedquery_name_1': '', |
| 37 | 'xyz_savedquery_name_2': 'name2', |
| 38 | 'xyz_savedquery_name_3': 'name3', |
| 39 | 'xyz_savedquery_id_1': 1, |
| 40 | 'xyz_savedquery_id_2': 2, |
| 41 | 'xyz_savedquery_id_3': 3, |
| 42 | 'xyz_savedquery_projects_1': '123', |
| 43 | 'xyz_savedquery_projects_2': 'abc', |
| 44 | 'xyz_savedquery_projects_3': 'def', |
| 45 | 'xyz_savedquery_base_1': 4, |
| 46 | 'xyz_savedquery_base_2': 5, |
| 47 | 'xyz_savedquery_base_3': 6, |
| 48 | 'xyz_savedquery_query_1': 'query1', |
| 49 | 'xyz_savedquery_query_2': 'query2', |
| 50 | 'xyz_savedquery_query_3': 'query3', |
| 51 | 'xyz_savedquery_sub_mode_1': 'sub_mode1', |
| 52 | 'xyz_savedquery_sub_mode_2': 'sub_mode2', |
| 53 | 'xyz_savedquery_sub_mode_3': 'sub_mode3', |
| 54 | } |
| 55 | self.project.TestAddProject(name='abc', project_id=1001) |
| 56 | self.project.TestAddProject(name='def', project_id=1002) |
| 57 | |
| 58 | saved_queries = savedqueries_helpers.ParseSavedQueries( |
| 59 | self.cnxn, post_data, self.project, prefix='xyz_') |
| 60 | self.assertEqual(2, len(saved_queries)) |
| 61 | |
| 62 | # pylint: disable=unbalanced-tuple-unpacking |
| 63 | saved_query1, saved_query2 = saved_queries |
| 64 | # Assert contents of saved_query1. |
| 65 | self.assertEqual(2, saved_query1.query_id) |
| 66 | self.assertEqual('name2', saved_query1.name) |
| 67 | self.assertEqual(5, saved_query1.base_query_id) |
| 68 | self.assertEqual('query2', saved_query1.query) |
| 69 | self.assertEqual([1001], saved_query1.executes_in_project_ids) |
| 70 | self.assertEqual('sub_mode2', saved_query1.subscription_mode) |
| 71 | # Assert contents of saved_query2. |
| 72 | self.assertEqual(3, saved_query2.query_id) |
| 73 | self.assertEqual('name3', saved_query2.name) |
| 74 | self.assertEqual(6, saved_query2.base_query_id) |
| 75 | self.assertEqual('query3', saved_query2.query) |
| 76 | self.assertEqual([1002], saved_query2.executes_in_project_ids) |
| 77 | self.assertEqual('sub_mode3', saved_query2.subscription_mode) |
| 78 | |
| 79 | def testSavedQueryToCond(self): |
| 80 | class MockSavedQuery: |
| 81 | def __init__(self): |
| 82 | self.base_query_id = 1 |
| 83 | self.query = 'query' |
| 84 | saved_query = MockSavedQuery() |
| 85 | |
| 86 | cond_for_missing_query = savedqueries_helpers.SavedQueryToCond(None) |
| 87 | self.assertEqual('', cond_for_missing_query) |
| 88 | |
| 89 | cond_with_no_base = savedqueries_helpers.SavedQueryToCond(saved_query) |
| 90 | self.assertEqual('query', cond_with_no_base) |
| 91 | |
| 92 | self.mox.StubOutWithMock(tracker_bizobj, 'GetBuiltInQuery') |
| 93 | tracker_bizobj.GetBuiltInQuery(1).AndReturn('base') |
| 94 | self.mox.ReplayAll() |
| 95 | cond_with_base = savedqueries_helpers.SavedQueryToCond(saved_query) |
| 96 | self.assertEqual('base query', cond_with_base) |
| 97 | self.mox.VerifyAll() |
| 98 | |
| 99 | def testSavedQueryIDToCond(self): |
| 100 | self.mox.StubOutWithMock(savedqueries_helpers, 'SavedQueryToCond') |
| 101 | savedqueries_helpers.SavedQueryToCond(mox.IgnoreArg()).AndReturn('ret') |
| 102 | self.mox.ReplayAll() |
| 103 | query_cond = savedqueries_helpers.SavedQueryIDToCond( |
| 104 | self.cnxn, self.features, 1) |
| 105 | self.assertEqual('ret', query_cond) |
| 106 | self.mox.VerifyAll() |
| 107 | |
| 108 | self.mox.StubOutWithMock(tracker_bizobj, 'GetBuiltInQuery') |
| 109 | tracker_bizobj.GetBuiltInQuery(1).AndReturn('built_in_query') |
| 110 | self.mox.ReplayAll() |
| 111 | query_cond = savedqueries_helpers.SavedQueryIDToCond( |
| 112 | self.cnxn, self.features, 1) |
| 113 | self.assertEqual('built_in_query', query_cond) |
| 114 | self.mox.VerifyAll() |