blob: d3b4cea7f3da1a754b3f7eb0f4c79da5c71ee177 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# 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.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Tests for the star service."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010010import six
Copybara854996b2021-09-07 19:36:02 +000011import unittest
12
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020013try:
14 from mox3 import mox
15except ImportError:
16 import mox
Copybara854996b2021-09-07 19:36:02 +000017import mock
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010018import time
Copybara854996b2021-09-07 19:36:02 +000019
20from google.appengine.ext import testbed
21
Copybara854996b2021-09-07 19:36:02 +000022from mock import Mock
23from framework import sql
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010024from services import service_manager
Copybara854996b2021-09-07 19:36:02 +000025from services import star_svc
26from testing import fake
27
28
29class AbstractStarServiceTest(unittest.TestCase):
30
31 def setUp(self):
32 self.testbed = testbed.Testbed()
33 self.testbed.activate()
34 self.testbed.init_memcache_stub()
35
36 self.mox = mox.Mox()
37 self.mock_tbl = self.mox.CreateMock(sql.SQLTableManager)
38 self.cnxn = 'fake connection'
39 self.cache_manager = fake.CacheManager()
40 self.star_service = star_svc.AbstractStarService(
41 self.cache_manager, self.mock_tbl, 'item_id', 'user_id', 'project')
42 self.mock_tbl.Delete = Mock()
43
44 def tearDown(self):
45 self.testbed.deactivate()
46 self.mox.UnsetStubs()
47 self.mox.ResetAll()
48
49 def SetUpExpungeStars(self):
50 self.mock_tbl.Delete(self.cnxn, item_id=123, commit=True)
51
52 def testExpungeStars(self):
53 self.SetUpExpungeStars()
54 self.mox.ReplayAll()
55 self.star_service.ExpungeStars(self.cnxn, 123)
56 self.mox.VerifyAll()
57
58 def testExpungeStars_Limit(self):
59 self.star_service.ExpungeStars(self.cnxn, 123, limit=50)
60 self.mock_tbl.Delete.assert_called_once_with(
61 self.cnxn, commit=True, limit=50, item_id=123)
62
63 def testExpungeStarsByUsers(self):
64 user_ids = [2, 3, 4]
65 self.star_service.ExpungeStarsByUsers(self.cnxn, user_ids, limit=40)
66 self.mock_tbl.Delete.assert_called_once_with(
67 self.cnxn, user_id=user_ids, commit=False, limit=40)
68
69 def SetUpLookupItemsStarrers(self):
70 self.mock_tbl.Select(
71 self.cnxn, cols=['item_id', 'user_id'],
72 item_id=[234]).AndReturn([(234, 111), (234, 222)])
73
74 def testLookupItemsStarrers(self):
75 self.star_service.starrer_cache.CacheItem(123, [111, 333])
76 self.SetUpLookupItemsStarrers()
77 self.mox.ReplayAll()
78 starrer_list_dict = self.star_service.LookupItemsStarrers(
79 self.cnxn, [123, 234])
80 self.mox.VerifyAll()
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010081 six.assertCountEqual(self, [123, 234], list(starrer_list_dict.keys()))
82 six.assertCountEqual(self, [111, 333], starrer_list_dict[123])
83 six.assertCountEqual(self, [111, 222], starrer_list_dict[234])
84 six.assertCountEqual(
85 self, [111, 333], self.star_service.starrer_cache.GetItem(123))
86 six.assertCountEqual(
87 self, [111, 222], self.star_service.starrer_cache.GetItem(234))
Copybara854996b2021-09-07 19:36:02 +000088
89 def SetUpLookupStarredItemIDs(self):
90 self.mock_tbl.Select(
91 self.cnxn, cols=['item_id'], user_id=111).AndReturn(
92 [(123,), (234,)])
93
94 def testLookupStarredItemIDs(self):
95 self.SetUpLookupStarredItemIDs()
96 self.mox.ReplayAll()
97 item_ids = self.star_service.LookupStarredItemIDs(self.cnxn, 111)
98 self.mox.VerifyAll()
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010099 six.assertCountEqual(self, [123, 234], item_ids)
100 six.assertCountEqual(
101 self, [123, 234], self.star_service.star_cache.GetItem(111))
Copybara854996b2021-09-07 19:36:02 +0000102
103 def testIsItemStarredBy(self):
104 self.SetUpLookupStarredItemIDs()
105 self.mox.ReplayAll()
106 self.assertTrue(self.star_service.IsItemStarredBy(self.cnxn, 123, 111))
107 self.assertTrue(self.star_service.IsItemStarredBy(self.cnxn, 234, 111))
108 self.assertFalse(
109 self.star_service.IsItemStarredBy(self.cnxn, 435, 111))
110 self.mox.VerifyAll()
111
112 def SetUpCountItemStars(self):
113 self.mock_tbl.Select(
114 self.cnxn, cols=['item_id', 'COUNT(user_id)'], item_id=[234],
115 group_by=['item_id']).AndReturn([(234, 2)])
116
117 def testCountItemStars(self):
118 self.star_service.star_count_cache.CacheItem(123, 3)
119 self.SetUpCountItemStars()
120 self.mox.ReplayAll()
121 self.assertEqual(3, self.star_service.CountItemStars(self.cnxn, 123))
122 self.assertEqual(2, self.star_service.CountItemStars(self.cnxn, 234))
123 self.mox.VerifyAll()
124
125 def testCountItemsStars(self):
126 self.star_service.star_count_cache.CacheItem(123, 3)
127 self.SetUpCountItemStars()
128 self.mox.ReplayAll()
129 count_dict = self.star_service.CountItemsStars(
130 self.cnxn, [123, 234])
131 self.mox.VerifyAll()
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100132 six.assertCountEqual(self, [123, 234], list(count_dict.keys()))
Copybara854996b2021-09-07 19:36:02 +0000133 self.assertEqual(3, count_dict[123])
134 self.assertEqual(2, count_dict[234])
135
136 def SetUpSetStar_Add(self):
137 self.mock_tbl.InsertRows(
138 self.cnxn, ['item_id', 'user_id'], [(123, 111)], ignore=True,
139 commit=True)
140
141 def testSetStar_Add(self):
142 self.SetUpSetStar_Add()
143 self.mox.ReplayAll()
144 self.star_service.SetStar(self.cnxn, 123, 111, True)
145 self.mox.VerifyAll()
146 self.assertFalse(self.star_service.star_cache.HasItem(123))
147 self.assertFalse(self.star_service.starrer_cache.HasItem(123))
148 self.assertFalse(self.star_service.star_count_cache.HasItem(123))
149
150 def SetUpSetStar_Remove(self):
151 self.mock_tbl.Delete(self.cnxn, item_id=123, user_id=[111])
152
153 def testSetStar_Remove(self):
154 self.SetUpSetStar_Remove()
155 self.mox.ReplayAll()
156 self.star_service.SetStar(self.cnxn, 123, 111, False)
157 self.mox.VerifyAll()
158 self.assertFalse(self.star_service.star_cache.HasItem(123))
159 self.assertFalse(self.star_service.starrer_cache.HasItem(123))
160 self.assertFalse(self.star_service.star_count_cache.HasItem(123))
161
162 def SetUpSetStarsBatch_Add(self):
163 self.mock_tbl.InsertRows(
164 self.cnxn, ['item_id', 'user_id'], [(123, 111), (123, 222)],
165 ignore=True, commit=True)
166
167 def testSetStarsBatch_Add(self):
168 self.SetUpSetStarsBatch_Add()
169 self.mox.ReplayAll()
170 self.star_service.SetStarsBatch(self.cnxn, 123, [111, 222], True)
171 self.mox.VerifyAll()
172 self.assertFalse(self.star_service.star_cache.HasItem(123))
173 self.assertFalse(self.star_service.starrer_cache.HasItem(123))
174 self.assertFalse(self.star_service.star_count_cache.HasItem(123))
175
176 def SetUpSetStarsBatch_Remove(self):
177 self.mock_tbl.Delete(self.cnxn, item_id=123, user_id=[111, 222])
178
179 def testSetStarsBatch_Remove(self):
180 self.SetUpSetStarsBatch_Remove()
181 self.mox.ReplayAll()
182 self.star_service.SetStarsBatch(self.cnxn, 123, [111, 222], False)
183 self.mox.VerifyAll()
184 self.assertFalse(self.star_service.star_cache.HasItem(123))
185 self.assertFalse(self.star_service.starrer_cache.HasItem(123))
186 self.assertFalse(self.star_service.star_count_cache.HasItem(123))
187
188
189class IssueStarServiceTest(unittest.TestCase):
190
191 def setUp(self):
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100192 self.mox = mox.Mox()
193 self.mock_tbl = self.mox.CreateMock(sql.SQLTableManager)
Copybara854996b2021-09-07 19:36:02 +0000194 self.mock_tbl.Delete = mock.Mock()
195 self.mock_tbl.InsertRows = mock.Mock()
196
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100197 self.mock_issue_tbl = self.mox.CreateMock(sql.SQLTableManager)
198
199 self.services = service_manager.Services()
200 self.services.issue = fake.IssueService()
201 self.services.config = fake.ConfigService()
202 self.services.features = fake.FeaturesService()
203
Copybara854996b2021-09-07 19:36:02 +0000204 self.cache_manager = fake.CacheManager()
205 with mock.patch(
206 'framework.sql.SQLTableManager', return_value=self.mock_tbl):
207 self.issue_star = star_svc.IssueStarService(
208 self.cache_manager)
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100209 self.issue_star.issue_tbl = self.mock_issue_tbl
Copybara854996b2021-09-07 19:36:02 +0000210
211 self.cnxn = 'fake connection'
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +0100212 self.now = int(time.time())
213
214 def testExpungeStarsByUsers(self):
215 self.mock_tbl.Select = mock.Mock(return_value=[(78901,), (78902,)])
216 self.mock_issue_tbl.Update = mock.Mock()
217
218 user_ids = [2, 3, 4]
219
220 self.mox.ReplayAll()
221 self.issue_star.ExpungeStarsByUsers(self.cnxn, user_ids, limit=40)
222 self.mox.VerifyAll()
223
224 self.mock_tbl.Select.assert_called_once_with(
225 self.cnxn,
226 cols=['IssueStar.issue_id'],
227 user_id=user_ids,
228 shard_id=mox.IgnoreArg(),
229 limit=40)
230 self.mock_tbl.Delete.assert_called_once_with(
231 self.cnxn, user_id=user_ids, commit=False, limit=40)
232 self.mock_issue_tbl.Update.assert_called_once_with(
233 self.cnxn, {'migration_modified': self.now},
234 id=[78901, 78902],
235 commit=False,
236 limit=40)
237
238 def testSetStarsBatch_Add(self):
239 issue = fake.MakeTestIssue(
240 project_id=789,
241 local_id=1,
242 reporter_id=111,
243 owner_id=456,
244 summary='sum',
245 status='Live',
246 issue_id=78901,
247 project_name='proj',
248 migration_modified_timestamp=1234567)
249 self.services.issue.TestAddIssue(issue)
250 config = self.services.config.GetProjectConfig(self.cnxn, 789)
251
252 # Set up mock for getting counts.
253 self.mock_tbl.Select(
254 self.cnxn,
255 cols=['issue_id', 'COUNT(user_id)'],
256 group_by=['issue_id'],
257 issue_id=[78901]).AndReturn([(78901, 2)])
258 self.mox.ReplayAll()
259
260 self.issue_star.SetStarsBatch(
261 self.cnxn, self.services, config, 78901, [111, 222], True)
262
263 self.mox.VerifyAll()
264 self.mock_tbl.InsertRows.assert_called_once_with(
265 self.cnxn, ['issue_id', 'user_id'], [(78901, 111), (78901, 222)],
266 ignore=True,
267 commit=True)
268
269 self.assertIn(issue, self.services.issue.updated_issues)
270 self.assertEqual(issue.migration_modified_timestamp, self.now)
271 self.assertEqual(issue.star_count, 2)
Copybara854996b2021-09-07 19:36:02 +0000272
273 def testSetStarsBatch_SkipIssueUpdate_Remove(self):
274 self.issue_star.SetStarsBatch_SkipIssueUpdate(
275 self.cnxn, 78901, [111, 222], False)
276 self.mock_tbl.Delete.assert_called_once_with(
277 self.cnxn, issue_id=78901, user_id=[111, 222], commit=True)
278
279 def testSetStarsBatch_SkipIssueUpdate_Remove_NoCommit(self):
280 self.issue_star.SetStarsBatch_SkipIssueUpdate(
281 self.cnxn, 78901, [111, 222], False, commit=False)
282 self.mock_tbl.Delete.assert_called_once_with(
283 self.cnxn, issue_id=78901, user_id=[111, 222], commit=False)
284
285 def testSetStarsBatch_SkipIssueUpdate_Add(self):
286 self.issue_star.SetStarsBatch_SkipIssueUpdate(
287 self.cnxn, 78901, [111, 222], True)
288 self.mock_tbl.InsertRows.assert_called_once_with(
289 self.cnxn, ['issue_id', 'user_id'], [(78901, 111), (78901, 222)],
290 ignore=True, commit=True)
291
292 def testSetStarsBatch_SkipIssueUpdate_Add_NoCommit(self):
293 self.issue_star.SetStarsBatch_SkipIssueUpdate(
294 self.cnxn, 78901, [111, 222], True, commit=False)
295 self.mock_tbl.InsertRows.assert_called_once_with(
296 self.cnxn, ['issue_id', 'user_id'], [(78901, 111), (78901, 222)],
297 ignore=True, commit=False)