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 | """Tests for the searchpipeline module.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import unittest |
| 12 | |
| 13 | from proto import ast_pb2 |
| 14 | from proto import tracker_pb2 |
| 15 | from search import searchpipeline |
| 16 | from services import service_manager |
| 17 | from testing import fake |
| 18 | from tracker import tracker_bizobj |
| 19 | |
| 20 | |
| 21 | class SearchPipelineTest(unittest.TestCase): |
| 22 | |
| 23 | def setUp(self): |
| 24 | self.cnxn = 'fake cnxn' |
| 25 | self.config = tracker_bizobj.MakeDefaultProjectIssueConfig(789) |
| 26 | self.services = service_manager.Services( |
| 27 | user=fake.UserService(), |
| 28 | project=fake.ProjectService(), |
| 29 | issue=fake.IssueService(), |
| 30 | config=fake.ConfigService()) |
| 31 | self.services.user.TestAddUser('a@example.com', 111) |
| 32 | |
| 33 | def testIsStarredRE(self): |
| 34 | """IS_STARRED_RE matches only the is:starred term.""" |
| 35 | input_output = { |
| 36 | 'something:else': 'something:else', |
| 37 | 'genesis:starred': 'genesis:starred', |
| 38 | 'is:starred-in-bookmarks': 'is:starred-in-bookmarks', |
| 39 | 'is:starred': 'foo', |
| 40 | 'Is:starred': 'foo', |
| 41 | 'is:STARRED': 'foo', |
| 42 | 'is:starred is:open': 'foo is:open', |
| 43 | 'is:open is:starred': 'is:open foo', |
| 44 | } |
| 45 | for i, o in input_output.items(): |
| 46 | self.assertEqual(o, searchpipeline.IS_STARRED_RE.sub('foo', i)) |
| 47 | |
| 48 | def testMeRE(self): |
| 49 | """ME_RE matches only the 'me' value keyword.""" |
| 50 | input_output = { |
| 51 | 'something:else': 'something:else', |
| 52 | 'else:some': 'else:some', |
| 53 | 'me': 'me', # It needs to have a ":" in front. |
| 54 | 'cc:me-team': 'cc:me-team', |
| 55 | 'cc:me=domain@otherdomain': 'cc:me=domain@otherdomain', |
| 56 | 'cc:me@example.com': 'cc:me@example.com', |
| 57 | 'me:the-boss': 'me:the-boss', |
| 58 | 'cc:me': 'cc:foo', |
| 59 | 'cc=me': 'cc=foo', |
| 60 | 'owner:Me': 'owner:foo', |
| 61 | 'reporter:ME': 'reporter:foo', |
| 62 | 'cc:me is:open': 'cc:foo is:open', |
| 63 | 'is:open cc:me': 'is:open cc:foo', |
| 64 | } |
| 65 | for i, o in input_output.items(): |
| 66 | self.assertEqual(o, searchpipeline.ME_RE.sub('foo', i)) |
| 67 | |
| 68 | def testAccumulateIssueProjectsAndConfigs(self): |
| 69 | pass # TODO(jrobbins): write tests |
| 70 | |
| 71 | def testReplaceKeywordsWithUserIDs_IsStarred(self): |
| 72 | """The term is:starred is replaced with starredby:USERID.""" |
| 73 | actual, warnings = searchpipeline.ReplaceKeywordsWithUserIDs( |
| 74 | [111], 'is:starred') |
| 75 | self.assertEqual('starredby:111', actual) |
| 76 | self.assertEqual([], warnings) |
| 77 | |
| 78 | actual, warnings = searchpipeline.ReplaceKeywordsWithUserIDs( |
| 79 | [111], 'Pri=1 is:starred M=61') |
| 80 | self.assertEqual('Pri=1 starredby:111 M=61', actual) |
| 81 | self.assertEqual([], warnings) |
| 82 | |
| 83 | actual, warnings = searchpipeline.ReplaceKeywordsWithUserIDs( |
| 84 | [], 'Pri=1 is:starred M=61') |
| 85 | self.assertEqual('Pri=1 M=61', actual) |
| 86 | self.assertEqual( |
| 87 | ['"is:starred" ignored because you are not signed in.'], |
| 88 | warnings) |
| 89 | |
| 90 | def testReplaceKeywordsWithUserIDs_IsStarred_linked(self): |
| 91 | """is:starred is replaced by starredby:uid1,uid2 for linked accounts.""" |
| 92 | actual, warnings = searchpipeline.ReplaceKeywordsWithUserIDs( |
| 93 | [111, 222], 'is:starred') |
| 94 | self.assertEqual('starredby:111,222', actual) |
| 95 | self.assertEqual([], warnings) |
| 96 | |
| 97 | def testReplaceKeywordsWithUserIDs_Me(self): |
| 98 | """Terms like owner:me are replaced with owner:USERID.""" |
| 99 | actual, warnings = searchpipeline.ReplaceKeywordsWithUserIDs( |
| 100 | [111], 'owner:me') |
| 101 | self.assertEqual('owner:111', actual) |
| 102 | self.assertEqual([], warnings) |
| 103 | |
| 104 | actual, warnings = searchpipeline.ReplaceKeywordsWithUserIDs( |
| 105 | [111], 'Pri=1 cc:me M=61') |
| 106 | self.assertEqual('Pri=1 cc:111 M=61', actual) |
| 107 | self.assertEqual([], warnings) |
| 108 | |
| 109 | actual, warnings = searchpipeline.ReplaceKeywordsWithUserIDs( |
| 110 | [], 'Pri=1 reporter:me M=61') |
| 111 | self.assertEqual('Pri=1 M=61', actual) |
| 112 | self.assertEqual( |
| 113 | ['"me" keyword ignored because you are not signed in.'], |
| 114 | warnings) |
| 115 | |
| 116 | def testReplaceKeywordsWithUserIDs_Me_LinkedAccounts(self): |
| 117 | """owner:me is replaced with owner:uid,uid for each linked account.""" |
| 118 | actual, warnings = searchpipeline.ReplaceKeywordsWithUserIDs( |
| 119 | [111, 222], 'owner:me') |
| 120 | self.assertEqual('owner:111,222', actual) |
| 121 | self.assertEqual([], warnings) |