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 | |
| 13 | import mox |
| 14 | |
| 15 | from features import savedqueries_helpers |
| 16 | from testing import fake |
| 17 | from tracker import tracker_bizobj |
| 18 | |
| 19 | |
| 20 | class SavedQueriesHelperTest(unittest.TestCase): |
| 21 | |
| 22 | def setUp(self): |
| 23 | self.features = fake.FeaturesService() |
| 24 | self.project = fake.ProjectService() |
| 25 | self.cnxn = 'fake cnxn' |
| 26 | self.mox = mox.Mox() |
| 27 | |
| 28 | def tearDown(self): |
| 29 | self.mox.UnsetStubs() |
| 30 | self.mox.ResetAll() |
| 31 | |
| 32 | def testParseSavedQueries(self): |
| 33 | post_data = { |
| 34 | 'xyz_savedquery_name_1': '', |
| 35 | 'xyz_savedquery_name_2': 'name2', |
| 36 | 'xyz_savedquery_name_3': 'name3', |
| 37 | 'xyz_savedquery_id_1': 1, |
| 38 | 'xyz_savedquery_id_2': 2, |
| 39 | 'xyz_savedquery_id_3': 3, |
| 40 | 'xyz_savedquery_projects_1': '123', |
| 41 | 'xyz_savedquery_projects_2': 'abc', |
| 42 | 'xyz_savedquery_projects_3': 'def', |
| 43 | 'xyz_savedquery_base_1': 4, |
| 44 | 'xyz_savedquery_base_2': 5, |
| 45 | 'xyz_savedquery_base_3': 6, |
| 46 | 'xyz_savedquery_query_1': 'query1', |
| 47 | 'xyz_savedquery_query_2': 'query2', |
| 48 | 'xyz_savedquery_query_3': 'query3', |
| 49 | 'xyz_savedquery_sub_mode_1': 'sub_mode1', |
| 50 | 'xyz_savedquery_sub_mode_2': 'sub_mode2', |
| 51 | 'xyz_savedquery_sub_mode_3': 'sub_mode3', |
| 52 | } |
| 53 | self.project.TestAddProject(name='abc', project_id=1001) |
| 54 | self.project.TestAddProject(name='def', project_id=1002) |
| 55 | |
| 56 | saved_queries = savedqueries_helpers.ParseSavedQueries( |
| 57 | self.cnxn, post_data, self.project, prefix='xyz_') |
| 58 | self.assertEqual(2, len(saved_queries)) |
| 59 | |
| 60 | # pylint: disable=unbalanced-tuple-unpacking |
| 61 | saved_query1, saved_query2 = saved_queries |
| 62 | # Assert contents of saved_query1. |
| 63 | self.assertEqual(2, saved_query1.query_id) |
| 64 | self.assertEqual('name2', saved_query1.name) |
| 65 | self.assertEqual(5, saved_query1.base_query_id) |
| 66 | self.assertEqual('query2', saved_query1.query) |
| 67 | self.assertEqual([1001], saved_query1.executes_in_project_ids) |
| 68 | self.assertEqual('sub_mode2', saved_query1.subscription_mode) |
| 69 | # Assert contents of saved_query2. |
| 70 | self.assertEqual(3, saved_query2.query_id) |
| 71 | self.assertEqual('name3', saved_query2.name) |
| 72 | self.assertEqual(6, saved_query2.base_query_id) |
| 73 | self.assertEqual('query3', saved_query2.query) |
| 74 | self.assertEqual([1002], saved_query2.executes_in_project_ids) |
| 75 | self.assertEqual('sub_mode3', saved_query2.subscription_mode) |
| 76 | |
| 77 | def testSavedQueryToCond(self): |
| 78 | class MockSavedQuery: |
| 79 | def __init__(self): |
| 80 | self.base_query_id = 1 |
| 81 | self.query = 'query' |
| 82 | saved_query = MockSavedQuery() |
| 83 | |
| 84 | cond_for_missing_query = savedqueries_helpers.SavedQueryToCond(None) |
| 85 | self.assertEqual('', cond_for_missing_query) |
| 86 | |
| 87 | cond_with_no_base = savedqueries_helpers.SavedQueryToCond(saved_query) |
| 88 | self.assertEqual('query', cond_with_no_base) |
| 89 | |
| 90 | self.mox.StubOutWithMock(tracker_bizobj, 'GetBuiltInQuery') |
| 91 | tracker_bizobj.GetBuiltInQuery(1).AndReturn('base') |
| 92 | self.mox.ReplayAll() |
| 93 | cond_with_base = savedqueries_helpers.SavedQueryToCond(saved_query) |
| 94 | self.assertEqual('base query', cond_with_base) |
| 95 | self.mox.VerifyAll() |
| 96 | |
| 97 | def testSavedQueryIDToCond(self): |
| 98 | self.mox.StubOutWithMock(savedqueries_helpers, 'SavedQueryToCond') |
| 99 | savedqueries_helpers.SavedQueryToCond(mox.IgnoreArg()).AndReturn('ret') |
| 100 | self.mox.ReplayAll() |
| 101 | query_cond = savedqueries_helpers.SavedQueryIDToCond( |
| 102 | self.cnxn, self.features, 1) |
| 103 | self.assertEqual('ret', query_cond) |
| 104 | self.mox.VerifyAll() |
| 105 | |
| 106 | self.mox.StubOutWithMock(tracker_bizobj, 'GetBuiltInQuery') |
| 107 | tracker_bizobj.GetBuiltInQuery(1).AndReturn('built_in_query') |
| 108 | self.mox.ReplayAll() |
| 109 | query_cond = savedqueries_helpers.SavedQueryIDToCond( |
| 110 | self.cnxn, self.features, 1) |
| 111 | self.assertEqual('built_in_query', query_cond) |
| 112 | self.mox.VerifyAll() |