blob: 03a0d23b7d3f777d266704c24d2ee6c49164daa6 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# 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 star service."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import unittest
12
13import mox
14import mock
15
16from google.appengine.ext import testbed
17
18import settings
19from mock import Mock
20from framework import sql
21from proto import user_pb2
22from services import star_svc
23from testing import fake
24
25
26class AbstractStarServiceTest(unittest.TestCase):
27
28 def setUp(self):
29 self.testbed = testbed.Testbed()
30 self.testbed.activate()
31 self.testbed.init_memcache_stub()
32
33 self.mox = mox.Mox()
34 self.mock_tbl = self.mox.CreateMock(sql.SQLTableManager)
35 self.cnxn = 'fake connection'
36 self.cache_manager = fake.CacheManager()
37 self.star_service = star_svc.AbstractStarService(
38 self.cache_manager, self.mock_tbl, 'item_id', 'user_id', 'project')
39 self.mock_tbl.Delete = Mock()
40
41 def tearDown(self):
42 self.testbed.deactivate()
43 self.mox.UnsetStubs()
44 self.mox.ResetAll()
45
46 def SetUpExpungeStars(self):
47 self.mock_tbl.Delete(self.cnxn, item_id=123, commit=True)
48
49 def testExpungeStars(self):
50 self.SetUpExpungeStars()
51 self.mox.ReplayAll()
52 self.star_service.ExpungeStars(self.cnxn, 123)
53 self.mox.VerifyAll()
54
55 def testExpungeStars_Limit(self):
56 self.star_service.ExpungeStars(self.cnxn, 123, limit=50)
57 self.mock_tbl.Delete.assert_called_once_with(
58 self.cnxn, commit=True, limit=50, item_id=123)
59
60 def testExpungeStarsByUsers(self):
61 user_ids = [2, 3, 4]
62 self.star_service.ExpungeStarsByUsers(self.cnxn, user_ids, limit=40)
63 self.mock_tbl.Delete.assert_called_once_with(
64 self.cnxn, user_id=user_ids, commit=False, limit=40)
65
66 def SetUpLookupItemsStarrers(self):
67 self.mock_tbl.Select(
68 self.cnxn, cols=['item_id', 'user_id'],
69 item_id=[234]).AndReturn([(234, 111), (234, 222)])
70
71 def testLookupItemsStarrers(self):
72 self.star_service.starrer_cache.CacheItem(123, [111, 333])
73 self.SetUpLookupItemsStarrers()
74 self.mox.ReplayAll()
75 starrer_list_dict = self.star_service.LookupItemsStarrers(
76 self.cnxn, [123, 234])
77 self.mox.VerifyAll()
78 self.assertItemsEqual([123, 234], list(starrer_list_dict.keys()))
79 self.assertItemsEqual([111, 333], starrer_list_dict[123])
80 self.assertItemsEqual([111, 222], starrer_list_dict[234])
81 self.assertItemsEqual([111, 333],
82 self.star_service.starrer_cache.GetItem(123))
83 self.assertItemsEqual([111, 222],
84 self.star_service.starrer_cache.GetItem(234))
85
86 def SetUpLookupStarredItemIDs(self):
87 self.mock_tbl.Select(
88 self.cnxn, cols=['item_id'], user_id=111).AndReturn(
89 [(123,), (234,)])
90
91 def testLookupStarredItemIDs(self):
92 self.SetUpLookupStarredItemIDs()
93 self.mox.ReplayAll()
94 item_ids = self.star_service.LookupStarredItemIDs(self.cnxn, 111)
95 self.mox.VerifyAll()
96 self.assertItemsEqual([123, 234], item_ids)
97 self.assertItemsEqual([123, 234],
98 self.star_service.star_cache.GetItem(111))
99
100 def testIsItemStarredBy(self):
101 self.SetUpLookupStarredItemIDs()
102 self.mox.ReplayAll()
103 self.assertTrue(self.star_service.IsItemStarredBy(self.cnxn, 123, 111))
104 self.assertTrue(self.star_service.IsItemStarredBy(self.cnxn, 234, 111))
105 self.assertFalse(
106 self.star_service.IsItemStarredBy(self.cnxn, 435, 111))
107 self.mox.VerifyAll()
108
109 def SetUpCountItemStars(self):
110 self.mock_tbl.Select(
111 self.cnxn, cols=['item_id', 'COUNT(user_id)'], item_id=[234],
112 group_by=['item_id']).AndReturn([(234, 2)])
113
114 def testCountItemStars(self):
115 self.star_service.star_count_cache.CacheItem(123, 3)
116 self.SetUpCountItemStars()
117 self.mox.ReplayAll()
118 self.assertEqual(3, self.star_service.CountItemStars(self.cnxn, 123))
119 self.assertEqual(2, self.star_service.CountItemStars(self.cnxn, 234))
120 self.mox.VerifyAll()
121
122 def testCountItemsStars(self):
123 self.star_service.star_count_cache.CacheItem(123, 3)
124 self.SetUpCountItemStars()
125 self.mox.ReplayAll()
126 count_dict = self.star_service.CountItemsStars(
127 self.cnxn, [123, 234])
128 self.mox.VerifyAll()
129 self.assertItemsEqual([123, 234], list(count_dict.keys()))
130 self.assertEqual(3, count_dict[123])
131 self.assertEqual(2, count_dict[234])
132
133 def SetUpSetStar_Add(self):
134 self.mock_tbl.InsertRows(
135 self.cnxn, ['item_id', 'user_id'], [(123, 111)], ignore=True,
136 commit=True)
137
138 def testSetStar_Add(self):
139 self.SetUpSetStar_Add()
140 self.mox.ReplayAll()
141 self.star_service.SetStar(self.cnxn, 123, 111, True)
142 self.mox.VerifyAll()
143 self.assertFalse(self.star_service.star_cache.HasItem(123))
144 self.assertFalse(self.star_service.starrer_cache.HasItem(123))
145 self.assertFalse(self.star_service.star_count_cache.HasItem(123))
146
147 def SetUpSetStar_Remove(self):
148 self.mock_tbl.Delete(self.cnxn, item_id=123, user_id=[111])
149
150 def testSetStar_Remove(self):
151 self.SetUpSetStar_Remove()
152 self.mox.ReplayAll()
153 self.star_service.SetStar(self.cnxn, 123, 111, False)
154 self.mox.VerifyAll()
155 self.assertFalse(self.star_service.star_cache.HasItem(123))
156 self.assertFalse(self.star_service.starrer_cache.HasItem(123))
157 self.assertFalse(self.star_service.star_count_cache.HasItem(123))
158
159 def SetUpSetStarsBatch_Add(self):
160 self.mock_tbl.InsertRows(
161 self.cnxn, ['item_id', 'user_id'], [(123, 111), (123, 222)],
162 ignore=True, commit=True)
163
164 def testSetStarsBatch_Add(self):
165 self.SetUpSetStarsBatch_Add()
166 self.mox.ReplayAll()
167 self.star_service.SetStarsBatch(self.cnxn, 123, [111, 222], True)
168 self.mox.VerifyAll()
169 self.assertFalse(self.star_service.star_cache.HasItem(123))
170 self.assertFalse(self.star_service.starrer_cache.HasItem(123))
171 self.assertFalse(self.star_service.star_count_cache.HasItem(123))
172
173 def SetUpSetStarsBatch_Remove(self):
174 self.mock_tbl.Delete(self.cnxn, item_id=123, user_id=[111, 222])
175
176 def testSetStarsBatch_Remove(self):
177 self.SetUpSetStarsBatch_Remove()
178 self.mox.ReplayAll()
179 self.star_service.SetStarsBatch(self.cnxn, 123, [111, 222], False)
180 self.mox.VerifyAll()
181 self.assertFalse(self.star_service.star_cache.HasItem(123))
182 self.assertFalse(self.star_service.starrer_cache.HasItem(123))
183 self.assertFalse(self.star_service.star_count_cache.HasItem(123))
184
185
186class IssueStarServiceTest(unittest.TestCase):
187
188 def setUp(self):
189 self.mock_tbl = mock.Mock()
190 self.mock_tbl.Delete = mock.Mock()
191 self.mock_tbl.InsertRows = mock.Mock()
192
193 self.cache_manager = fake.CacheManager()
194 with mock.patch(
195 'framework.sql.SQLTableManager', return_value=self.mock_tbl):
196 self.issue_star = star_svc.IssueStarService(
197 self.cache_manager)
198
199 self.cnxn = 'fake connection'
200
201 def testSetStarsBatch_SkipIssueUpdate_Remove(self):
202 self.issue_star.SetStarsBatch_SkipIssueUpdate(
203 self.cnxn, 78901, [111, 222], False)
204 self.mock_tbl.Delete.assert_called_once_with(
205 self.cnxn, issue_id=78901, user_id=[111, 222], commit=True)
206
207 def testSetStarsBatch_SkipIssueUpdate_Remove_NoCommit(self):
208 self.issue_star.SetStarsBatch_SkipIssueUpdate(
209 self.cnxn, 78901, [111, 222], False, commit=False)
210 self.mock_tbl.Delete.assert_called_once_with(
211 self.cnxn, issue_id=78901, user_id=[111, 222], commit=False)
212
213 def testSetStarsBatch_SkipIssueUpdate_Add(self):
214 self.issue_star.SetStarsBatch_SkipIssueUpdate(
215 self.cnxn, 78901, [111, 222], True)
216 self.mock_tbl.InsertRows.assert_called_once_with(
217 self.cnxn, ['issue_id', 'user_id'], [(78901, 111), (78901, 222)],
218 ignore=True, commit=True)
219
220 def testSetStarsBatch_SkipIssueUpdate_Add_NoCommit(self):
221 self.issue_star.SetStarsBatch_SkipIssueUpdate(
222 self.cnxn, 78901, [111, 222], True, commit=False)
223 self.mock_tbl.InsertRows.assert_called_once_with(
224 self.cnxn, ['issue_id', 'user_id'], [(78901, 111), (78901, 222)],
225 ignore=True, commit=False)