OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is govered 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 tracker_fulltext module.""" |
| 7 |
| 8 import unittest |
| 9 |
| 10 import mox |
| 11 |
| 12 from google.appengine.api import search |
| 13 |
| 14 import settings |
| 15 from framework import framework_views |
| 16 from proto import ast_pb2 |
| 17 from proto import tracker_pb2 |
| 18 from services import fulltext_helpers |
| 19 from services import tracker_fulltext |
| 20 from testing import fake |
| 21 from tracker import tracker_bizobj |
| 22 |
| 23 |
| 24 class TrackerFulltextTest(unittest.TestCase): |
| 25 |
| 26 def setUp(self): |
| 27 self.mox = mox.Mox() |
| 28 self.mock_index = self.mox.CreateMockAnything() |
| 29 self.mox.StubOutWithMock(search, 'Index') |
| 30 self.docs = None |
| 31 self.cnxn = 'fake connection' |
| 32 self.user_service = fake.UserService() |
| 33 self.user_service.TestAddUser('test@example.com', 111L) |
| 34 self.issue_service = fake.IssueService() |
| 35 self.config_service = fake.ConfigService() |
| 36 |
| 37 self.issue = fake.MakeTestIssue( |
| 38 123, 1, 'test summary', 'New', 111L) |
| 39 self.issue_service.TestAddIssue(self.issue) |
| 40 self.comment = tracker_pb2.IssueComment( |
| 41 project_id=789, issue_id=self.issue.issue_id, user_id=111L, |
| 42 content='comment content', |
| 43 attachments=[ |
| 44 tracker_pb2.Attachment(filename='hello.c'), |
| 45 tracker_pb2.Attachment(filename='hello.h')]) |
| 46 self.issue_service.TestAddComment(self.comment, 1) |
| 47 self.users_by_id = framework_views.MakeAllUserViews( |
| 48 self.cnxn, self.user_service, [111L]) |
| 49 |
| 50 def tearDown(self): |
| 51 self.mox.UnsetStubs() |
| 52 self.mox.ResetAll() |
| 53 |
| 54 def RecordDocs(self, docs): |
| 55 self.docs = docs |
| 56 |
| 57 def SetUpIndexIssues(self): |
| 58 search.Index(name=settings.search_index_name_format % 1).AndReturn( |
| 59 self.mock_index) |
| 60 self.mock_index.put(mox.IgnoreArg()).WithSideEffects(self.RecordDocs) |
| 61 |
| 62 def testIndexIssues(self): |
| 63 self.SetUpIndexIssues() |
| 64 self.mox.ReplayAll() |
| 65 tracker_fulltext.IndexIssues( |
| 66 self.cnxn, [self.issue], self.user_service, self.issue_service, |
| 67 self.config_service) |
| 68 self.mox.VerifyAll() |
| 69 self.assertEqual(1, len(self.docs)) |
| 70 issue_doc = self.docs[0] |
| 71 self.assertEqual(123, issue_doc.fields[0].value) |
| 72 self.assertEqual('test summary', issue_doc.fields[1].value) |
| 73 |
| 74 def SetUpCreateIssueSearchDocuments(self): |
| 75 self.mox.StubOutWithMock(tracker_fulltext, '_IndexDocsInShard') |
| 76 tracker_fulltext._IndexDocsInShard(1, mox.IgnoreArg()).WithSideEffects( |
| 77 lambda shard_id, docs: self.RecordDocs(docs)) |
| 78 |
| 79 def testCreateIssueSearchDocuments_Normal(self): |
| 80 self.SetUpCreateIssueSearchDocuments() |
| 81 self.mox.ReplayAll() |
| 82 config_dict = {123: tracker_bizobj.MakeDefaultProjectIssueConfig(123)} |
| 83 tracker_fulltext._CreateIssueSearchDocuments( |
| 84 [self.issue], {self.issue.issue_id: [self.comment]}, self.users_by_id, |
| 85 config_dict) |
| 86 self.mox.VerifyAll() |
| 87 self.assertEqual(1, len(self.docs)) |
| 88 issue_doc = self.docs[0] |
| 89 self.assertEqual(5, len(issue_doc.fields)) |
| 90 self.assertEqual(123, issue_doc.fields[0].value) |
| 91 self.assertEqual('test summary', issue_doc.fields[1].value) |
| 92 self.assertEqual('test@example.com comment content hello.c hello.h', |
| 93 issue_doc.fields[3].value) |
| 94 self.assertEqual('', issue_doc.fields[4].value) |
| 95 |
| 96 def testCreateIssueSearchDocuments_CustomFields(self): |
| 97 self.SetUpCreateIssueSearchDocuments() |
| 98 self.mox.ReplayAll() |
| 99 config = tracker_bizobj.MakeDefaultProjectIssueConfig(123) |
| 100 config_dict = {123: tracker_bizobj.MakeDefaultProjectIssueConfig(123)} |
| 101 int_field = tracker_bizobj.MakeFieldDef( |
| 102 1, 123, 'CustomInt', tracker_pb2.FieldTypes.INT_TYPE, None, False, |
| 103 False, None, None, None, None, False, None, None, None, |
| 104 'A custom int field', False) |
| 105 int_field_value = tracker_bizobj.MakeFieldValue(1, 42, None, None, False) |
| 106 str_field = tracker_bizobj.MakeFieldDef( |
| 107 2, 123, 'CustomStr', tracker_pb2.FieldTypes.STR_TYPE, None, False, |
| 108 False, None, None, None, None, False, None, None, None, |
| 109 'A custom string field', False) |
| 110 str_field_value = tracker_bizobj.MakeFieldValue( |
| 111 2, None, 'Greetings', None, False) |
| 112 config.field_defs.extend([int_field, str_field]) |
| 113 self.issue.field_values.extend([int_field_value, str_field_value]) |
| 114 |
| 115 tracker_fulltext._CreateIssueSearchDocuments( |
| 116 [self.issue], {self.issue.issue_id: [self.comment]}, self.users_by_id, |
| 117 config_dict) |
| 118 self.mox.VerifyAll() |
| 119 self.assertEqual(1, len(self.docs)) |
| 120 issue_doc = self.docs[0] |
| 121 metadata = issue_doc.fields[2] |
| 122 self.assertEqual( |
| 123 u'New test@example.com [] 42 Greetings ', |
| 124 metadata.value) |
| 125 |
| 126 def testExtractCommentText(self): |
| 127 extracted_text = tracker_fulltext._ExtractCommentText( |
| 128 self.comment, self.users_by_id) |
| 129 self.assertEqual( |
| 130 'test@example.com comment content hello.c hello.h', |
| 131 extracted_text) |
| 132 |
| 133 def testIndexableComments_Length(self): |
| 134 comments = [self.comment] |
| 135 indexable = tracker_fulltext._IndexableComments(comments, self.users_by_id) |
| 136 self.assertEquals(1, len(indexable)) |
| 137 |
| 138 comments = [self.comment] * 100 |
| 139 indexable = tracker_fulltext._IndexableComments(comments, self.users_by_id) |
| 140 self.assertEquals(100, len(indexable)) |
| 141 |
| 142 comments = [self.comment] * 101 |
| 143 indexable = tracker_fulltext._IndexableComments(comments, self.users_by_id) |
| 144 self.assertEquals(101, len(indexable)) |
| 145 |
| 146 comments = [self.comment] * 600 |
| 147 indexable = tracker_fulltext._IndexableComments(comments, self.users_by_id) |
| 148 self.assertEquals(600, len(indexable)) |
| 149 |
| 150 comments = [self.comment] * 601 |
| 151 indexable = tracker_fulltext._IndexableComments(comments, self.users_by_id) |
| 152 self.assertEquals(600, len(indexable)) |
| 153 self.assertNotIn(100, indexable) |
| 154 |
| 155 def SetUpUnindexIssues(self): |
| 156 search.Index(name=settings.search_index_name_format % 1).AndReturn( |
| 157 self.mock_index) |
| 158 self.mock_index.delete(['1']) |
| 159 |
| 160 def testUnindexIssues(self): |
| 161 self.SetUpUnindexIssues() |
| 162 self.mox.ReplayAll() |
| 163 tracker_fulltext.UnindexIssues([1]) |
| 164 self.mox.VerifyAll() |
| 165 |
| 166 def SetUpSearchIssueFullText(self): |
| 167 self.mox.StubOutWithMock(fulltext_helpers, 'ComprehensiveSearch') |
| 168 fulltext_helpers.ComprehensiveSearch( |
| 169 '(project_id:789) (summary:"test")', |
| 170 settings.search_index_name_format % 1).AndReturn([123, 234]) |
| 171 |
| 172 def testSearchIssueFullText_Normal(self): |
| 173 self.SetUpSearchIssueFullText() |
| 174 self.mox.ReplayAll() |
| 175 summary_fd = tracker_pb2.FieldDef( |
| 176 field_name='summary', field_type=tracker_pb2.FieldTypes.STR_TYPE) |
| 177 query_ast_conj = ast_pb2.Conjunction(conds=[ |
| 178 ast_pb2.Condition( |
| 179 op=ast_pb2.QueryOp.TEXT_HAS, field_defs=[summary_fd], |
| 180 str_values=['test'])]) |
| 181 issue_ids, capped = tracker_fulltext.SearchIssueFullText( |
| 182 [789], query_ast_conj, 1) |
| 183 self.mox.VerifyAll() |
| 184 self.assertItemsEqual([123, 234], issue_ids) |
| 185 self.assertFalse(capped) |
| 186 |
| 187 def testSearchIssueFullText_Capped(self): |
| 188 try: |
| 189 orig = settings.fulltext_limit_per_shard |
| 190 settings.fulltext_limit_per_shard = 1 |
| 191 self.SetUpSearchIssueFullText() |
| 192 self.mox.ReplayAll() |
| 193 summary_fd = tracker_pb2.FieldDef( |
| 194 field_name='summary', field_type=tracker_pb2.FieldTypes.STR_TYPE) |
| 195 query_ast_conj = ast_pb2.Conjunction(conds=[ |
| 196 ast_pb2.Condition( |
| 197 op=ast_pb2.QueryOp.TEXT_HAS, field_defs=[summary_fd], |
| 198 str_values=['test'])]) |
| 199 issue_ids, capped = tracker_fulltext.SearchIssueFullText( |
| 200 [789], query_ast_conj, 1) |
| 201 self.mox.VerifyAll() |
| 202 self.assertItemsEqual([123, 234], issue_ids) |
| 203 self.assertTrue(capped) |
| 204 finally: |
| 205 settings.fulltext_limit_per_shard = orig |
| 206 |
| 207 |
| 208 if __name__ == '__main__': |
| 209 unittest.main() |
OLD | NEW |