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