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 star service.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import unittest |
| 12 | |
Adrià Vilanova MartÃnez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame^] | 13 | try: |
| 14 | from mox3 import mox |
| 15 | except ImportError: |
| 16 | import mox |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 17 | import mock |
| 18 | |
| 19 | from google.appengine.ext import testbed |
| 20 | |
| 21 | import settings |
| 22 | from mock import Mock |
| 23 | from framework import sql |
| 24 | from proto import user_pb2 |
| 25 | from services import star_svc |
| 26 | from testing import fake |
| 27 | |
| 28 | |
| 29 | class 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() |
| 81 | self.assertItemsEqual([123, 234], list(starrer_list_dict.keys())) |
| 82 | self.assertItemsEqual([111, 333], starrer_list_dict[123]) |
| 83 | self.assertItemsEqual([111, 222], starrer_list_dict[234]) |
| 84 | self.assertItemsEqual([111, 333], |
| 85 | self.star_service.starrer_cache.GetItem(123)) |
| 86 | self.assertItemsEqual([111, 222], |
| 87 | self.star_service.starrer_cache.GetItem(234)) |
| 88 | |
| 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() |
| 99 | self.assertItemsEqual([123, 234], item_ids) |
| 100 | self.assertItemsEqual([123, 234], |
| 101 | self.star_service.star_cache.GetItem(111)) |
| 102 | |
| 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() |
| 132 | self.assertItemsEqual([123, 234], list(count_dict.keys())) |
| 133 | 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 | |
| 189 | class IssueStarServiceTest(unittest.TestCase): |
| 190 | |
| 191 | def setUp(self): |
| 192 | self.mock_tbl = mock.Mock() |
| 193 | self.mock_tbl.Delete = mock.Mock() |
| 194 | self.mock_tbl.InsertRows = mock.Mock() |
| 195 | |
| 196 | self.cache_manager = fake.CacheManager() |
| 197 | with mock.patch( |
| 198 | 'framework.sql.SQLTableManager', return_value=self.mock_tbl): |
| 199 | self.issue_star = star_svc.IssueStarService( |
| 200 | self.cache_manager) |
| 201 | |
| 202 | self.cnxn = 'fake connection' |
| 203 | |
| 204 | def testSetStarsBatch_SkipIssueUpdate_Remove(self): |
| 205 | self.issue_star.SetStarsBatch_SkipIssueUpdate( |
| 206 | self.cnxn, 78901, [111, 222], False) |
| 207 | self.mock_tbl.Delete.assert_called_once_with( |
| 208 | self.cnxn, issue_id=78901, user_id=[111, 222], commit=True) |
| 209 | |
| 210 | def testSetStarsBatch_SkipIssueUpdate_Remove_NoCommit(self): |
| 211 | self.issue_star.SetStarsBatch_SkipIssueUpdate( |
| 212 | self.cnxn, 78901, [111, 222], False, commit=False) |
| 213 | self.mock_tbl.Delete.assert_called_once_with( |
| 214 | self.cnxn, issue_id=78901, user_id=[111, 222], commit=False) |
| 215 | |
| 216 | def testSetStarsBatch_SkipIssueUpdate_Add(self): |
| 217 | self.issue_star.SetStarsBatch_SkipIssueUpdate( |
| 218 | self.cnxn, 78901, [111, 222], True) |
| 219 | self.mock_tbl.InsertRows.assert_called_once_with( |
| 220 | self.cnxn, ['issue_id', 'user_id'], [(78901, 111), (78901, 222)], |
| 221 | ignore=True, commit=True) |
| 222 | |
| 223 | def testSetStarsBatch_SkipIssueUpdate_Add_NoCommit(self): |
| 224 | self.issue_star.SetStarsBatch_SkipIssueUpdate( |
| 225 | self.cnxn, 78901, [111, 222], True, commit=False) |
| 226 | self.mock_tbl.InsertRows.assert_called_once_with( |
| 227 | self.cnxn, ['issue_id', 'user_id'], [(78901, 111), (78901, 222)], |
| 228 | ignore=True, commit=False) |