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 """Tests for the star service.""" |
| 7 |
| 8 import unittest |
| 9 |
| 10 import mox |
| 11 |
| 12 from google.appengine.ext import testbed |
| 13 |
| 14 import settings |
| 15 from framework import sql |
| 16 from proto import user_pb2 |
| 17 from services import star_svc |
| 18 from testing import fake |
| 19 |
| 20 |
| 21 class AbstractStarServiceTest(unittest.TestCase): |
| 22 |
| 23 def setUp(self): |
| 24 self.testbed = testbed.Testbed() |
| 25 self.testbed.activate() |
| 26 self.testbed.init_memcache_stub() |
| 27 |
| 28 self.mox = mox.Mox() |
| 29 self.mock_tbl = self.mox.CreateMock(sql.SQLTableManager) |
| 30 self.cnxn = 'fake connection' |
| 31 self.cache_manager = fake.CacheManager() |
| 32 self.star_service = star_svc.AbstractStarService( |
| 33 self.cache_manager, self.mock_tbl, 'item_id', 'user_id', 'project') |
| 34 |
| 35 def tearDown(self): |
| 36 self.testbed.deactivate() |
| 37 self.mox.UnsetStubs() |
| 38 self.mox.ResetAll() |
| 39 |
| 40 def SetUpExpungeStars(self): |
| 41 self.mock_tbl.Delete(self.cnxn, item_id=123) |
| 42 |
| 43 def testExpungeStars(self): |
| 44 self.SetUpExpungeStars() |
| 45 self.mox.ReplayAll() |
| 46 self.star_service.ExpungeStars(self.cnxn, 123) |
| 47 self.mox.VerifyAll() |
| 48 |
| 49 def SetUpLookupItemsStarrers(self): |
| 50 self.mock_tbl.Select( |
| 51 self.cnxn, cols=['item_id', 'user_id'], |
| 52 item_id=[234]).AndReturn([(234, 111L), (234, 222L)]) |
| 53 |
| 54 def testLookupItemsStarrers(self): |
| 55 self.star_service.starrer_cache.CacheItem(123, [111L, 333L]) |
| 56 self.SetUpLookupItemsStarrers() |
| 57 self.mox.ReplayAll() |
| 58 starrer_list_dict = self.star_service.LookupItemsStarrers( |
| 59 self.cnxn, [123, 234]) |
| 60 self.mox.VerifyAll() |
| 61 self.assertItemsEqual([123, 234], starrer_list_dict.keys()) |
| 62 self.assertItemsEqual([111L, 333L], starrer_list_dict[123]) |
| 63 self.assertItemsEqual([111L, 222L], starrer_list_dict[234]) |
| 64 self.assertItemsEqual([111L, 333L], |
| 65 self.star_service.starrer_cache.GetItem(123)) |
| 66 self.assertItemsEqual([111L, 222L], |
| 67 self.star_service.starrer_cache.GetItem(234)) |
| 68 |
| 69 def SetUpLookupStarredItemIDs(self): |
| 70 self.mock_tbl.Select( |
| 71 self.cnxn, cols=['item_id'], user_id=111L).AndReturn( |
| 72 [(123,), (234,)]) |
| 73 |
| 74 def testLookupStarredItemIDs(self): |
| 75 self.SetUpLookupStarredItemIDs() |
| 76 self.mox.ReplayAll() |
| 77 item_ids = self.star_service.LookupStarredItemIDs(self.cnxn, 111L) |
| 78 self.mox.VerifyAll() |
| 79 self.assertItemsEqual([123, 234], item_ids) |
| 80 self.assertItemsEqual([123, 234], |
| 81 self.star_service.star_cache.GetItem(111L)) |
| 82 |
| 83 def testIsItemStarredBy(self): |
| 84 self.SetUpLookupStarredItemIDs() |
| 85 self.mox.ReplayAll() |
| 86 self.assertTrue(self.star_service.IsItemStarredBy(self.cnxn, 123, 111L)) |
| 87 self.assertTrue(self.star_service.IsItemStarredBy(self.cnxn, 234, 111)) |
| 88 self.assertFalse( |
| 89 self.star_service.IsItemStarredBy(self.cnxn, 435, 111L)) |
| 90 self.mox.VerifyAll() |
| 91 |
| 92 def SetUpCountItemStars(self): |
| 93 self.mock_tbl.Select( |
| 94 self.cnxn, cols=['item_id', 'COUNT(user_id)'], item_id=[234], |
| 95 group_by=['item_id']).AndReturn([(234, 2)]) |
| 96 |
| 97 def testCountItemStars(self): |
| 98 self.star_service.star_count_cache.CacheItem(123, 3) |
| 99 self.SetUpCountItemStars() |
| 100 self.mox.ReplayAll() |
| 101 self.assertEqual(3, self.star_service.CountItemStars(self.cnxn, 123)) |
| 102 self.assertEqual(2, self.star_service.CountItemStars(self.cnxn, 234)) |
| 103 self.mox.VerifyAll() |
| 104 |
| 105 def testCountItemsStars(self): |
| 106 self.star_service.star_count_cache.CacheItem(123, 3) |
| 107 self.SetUpCountItemStars() |
| 108 self.mox.ReplayAll() |
| 109 count_dict = self.star_service.CountItemsStars( |
| 110 self.cnxn, [123, 234]) |
| 111 self.mox.VerifyAll() |
| 112 self.assertItemsEqual([123, 234], count_dict.keys()) |
| 113 self.assertEqual(3, count_dict[123]) |
| 114 self.assertEqual(2, count_dict[234]) |
| 115 |
| 116 def SetUpSetStar_Add(self): |
| 117 self.mock_tbl.InsertRow( |
| 118 self.cnxn, ignore=True, item_id=123, user_id=111L) |
| 119 |
| 120 def testSetStar_Add(self): |
| 121 self.SetUpSetStar_Add() |
| 122 self.mox.ReplayAll() |
| 123 self.star_service.SetStar(self.cnxn, 123, 111L, True) |
| 124 self.mox.VerifyAll() |
| 125 |
| 126 def SetUpSetStar_Remove(self): |
| 127 self.mock_tbl.Delete(self.cnxn, item_id=123, user_id=111L) |
| 128 |
| 129 def testSetProjectStar_Remove(self): |
| 130 self.SetUpSetStar_Remove() |
| 131 self.mox.ReplayAll() |
| 132 self.star_service.SetStar(self.cnxn, 123, 111L, False) |
| 133 self.mox.VerifyAll() |
| 134 |
| 135 |
| 136 if __name__ == '__main__': |
| 137 unittest.main() |
OLD | NEW |