| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import mock | |
| 6 import unittest | 5 import unittest |
| 7 | 6 |
| 8 from google.appengine.api import memcache | |
| 9 from google.appengine.ext import ndb | |
| 10 | |
| 11 from dashboard.common import stored_object | 7 from dashboard.common import stored_object |
| 12 from dashboard.common import testing_common | 8 from dashboard.common import testing_common |
| 13 | 9 |
| 14 | 10 |
| 15 class SampleSerializableClass(object): | 11 class SampleSerializableClass(object): |
| 16 | 12 |
| 17 def __init__(self, data): | 13 def __init__(self, data): |
| 18 self.data = data | 14 self.data = data |
| 19 self.user_name = 'Chris' | 15 self.user_name = 'Chris' |
| 20 self.user_id = 1234 | 16 self.user_id = 1234 |
| 21 self.family = { | 17 self.family = { |
| 22 'nieces': 1, | 18 'nieces': 1, |
| 23 'nephews': 6, | 19 'nephews': 6, |
| 24 } | 20 } |
| 25 | 21 |
| 26 def __eq__(self, other): | 22 def __eq__(self, other): |
| 27 return self.__dict__ == other.__dict__ | 23 return self.__dict__ == other.__dict__ |
| 28 | 24 |
| 29 | 25 |
| 30 class StoredObjectTest(testing_common.TestCase): | 26 class StoredObjectTest(testing_common.TestCase): |
| 31 | 27 |
| 32 def _GetCachedValues(self, key): | |
| 33 keys = stored_object.MultipartCache._GetCacheKeyList(key) | |
| 34 cache_values = memcache.get_multi(keys) | |
| 35 return [v for v in cache_values.values() if v is not None] | |
| 36 | |
| 37 def testSetAndGet(self): | 28 def testSetAndGet(self): |
| 38 new_account = SampleSerializableClass('Some account data.') | 29 new_account = SampleSerializableClass('Some account data.') |
| 39 stored_object.Set('chris', new_account) | 30 stored_object.Set('chris', new_account) |
| 40 chris_account = stored_object.Get('chris') | 31 chris_account = stored_object.Get('chris') |
| 41 self.assertEqual(new_account, chris_account) | 32 self.assertEqual(new_account, chris_account) |
| 42 | 33 |
| 43 @mock.patch.object(stored_object.MultipartCache, 'GetAsync') | |
| 44 def testSetAndGet_NoMemcache(self, async_mock): | |
| 45 async_mock.return_value = ndb.Future() | |
| 46 async_mock.return_value.set_result(None) | |
| 47 | |
| 48 new_account = SampleSerializableClass('Some account data.') | |
| 49 stored_object.Set('chris', new_account) | |
| 50 chris_account = stored_object.Get('chris') | |
| 51 self.assertEqual(new_account, chris_account) | |
| 52 | |
| 53 def testSetAndGet_CacheNotExist_CacheSet(self): | |
| 54 new_account = SampleSerializableClass('Some account data.') | |
| 55 stored_object.Set('chris', new_account) | |
| 56 stored_object.MultipartCache.Delete('chris') | |
| 57 chris_account = stored_object.Get('chris') | |
| 58 self.assertEqual(new_account, chris_account) | |
| 59 cache_values = self._GetCachedValues('chris') | |
| 60 self.assertGreater(len(cache_values), 0) | |
| 61 | |
| 62 def testSetAndGet_LargeObject(self): | 34 def testSetAndGet_LargeObject(self): |
| 63 a_large_string = '0' * 2097152 | 35 a_large_string = '0' * 2097152 |
| 64 new_account = SampleSerializableClass(a_large_string) | 36 new_account = SampleSerializableClass(a_large_string) |
| 65 stored_object.Set('chris', new_account) | 37 stored_object.Set('chris', new_account) |
| 66 chris_account = stored_object.Get('chris') | 38 chris_account = stored_object.Get('chris') |
| 67 | 39 |
| 68 part_entities = stored_object.PartEntity.query().fetch() | 40 part_entities = stored_object.PartEntity.query().fetch() |
| 69 | 41 |
| 70 self.assertEqual(new_account, chris_account) | 42 self.assertEqual(new_account, chris_account) |
| 71 | 43 |
| 72 # chris_account object should be stored over 3 PartEntity entities. | 44 # chris_account object should be stored over 3 PartEntity entities. |
| 73 self.assertEqual(3, len(part_entities)) | 45 self.assertEqual(3, len(part_entities)) |
| 74 | 46 |
| 75 # Stored over 4 caches here, one extra for the head cache. | |
| 76 cache_values = self._GetCachedValues('chris') | |
| 77 self.assertEqual(4, len(cache_values)) | |
| 78 | |
| 79 def testDelete_LargeObject_AllEntitiesDeleted(self): | 47 def testDelete_LargeObject_AllEntitiesDeleted(self): |
| 80 a_large_string = '0' * 2097152 | 48 a_large_string = '0' * 2097152 |
| 81 new_account = SampleSerializableClass(a_large_string) | 49 new_account = SampleSerializableClass(a_large_string) |
| 82 stored_object.Set('chris', new_account) | 50 stored_object.Set('chris', new_account) |
| 83 | 51 |
| 84 stored_object.Delete('chris') | 52 stored_object.Delete('chris') |
| 85 | 53 |
| 86 multipart_entities = stored_object.MultipartEntity.query().fetch() | 54 multipart_entities = stored_object.MultipartEntity.query().fetch() |
| 87 self.assertEqual(0, len(multipart_entities)) | 55 self.assertEqual(0, len(multipart_entities)) |
| 88 part_entities = stored_object.PartEntity.query().fetch() | 56 part_entities = stored_object.PartEntity.query().fetch() |
| 89 self.assertEqual(0, len(part_entities)) | 57 self.assertEqual(0, len(part_entities)) |
| 90 cache_values = self._GetCachedValues('chris') | |
| 91 self.assertEqual(0, len(cache_values)) | |
| 92 | 58 |
| 93 | 59 |
| 94 if __name__ == '__main__': | 60 if __name__ == '__main__': |
| 95 unittest.main() | 61 unittest.main() |
| OLD | NEW |