Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: appengine/findit/lib/test/cache_decorator_test.py

Issue 2538373003: [Culprit-Finder] Merge lib/ to libs/. (Closed)
Patch Set: . Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « appengine/findit/lib/test/__init__.py ('k') | appengine/findit/lib/test/time_util_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 # found in the LICENSE file.
4
5 import pickle
6 import zlib
7
8 from testing_utils import testing
9
10 from lib import cache
11 from lib import cache_decorator
12
13
14 class _DummyCache(cache.Cache):
15 def __init__(self, cached_data):
16 self.cached_data = cached_data
17
18 def Get(self, key):
19 return self.cached_data.get(key)
20
21 def Set(self, key, data, expire_time=0):
22 self.cached_data[key] = data
23
24
25 def _DummyKeyGenerator(func, *_):
26 return func.__name__
27
28
29 class CacheDecoratorTest(testing.AppengineTestCase):
30 def testDefaultKeyGenerator(self):
31 expected_params = {
32 'id1': 'fi',
33 'id2': 'pi',
34 'url': 'http://url',
35 }
36 # Hexadecimal digits of MD5 digest of "pickled_params".
37 expected_key = 'f5f173c811f7c537a80d44511903a3e0'
38
39 def MockPickleDumps(params):
40 self.assertEqual(expected_params, params)
41 return 'pickled_params'
42
43 def Func(id1, id2, url=None): # Unused parameters-pylint: disable=W0613
44 return 1 # pragma: no cover.
45
46 class CallableIdentifier(object):
47 def identifier(self):
48 return 'fi'
49
50 class PropertyIdentifier(object):
51 @property
52 def identifier(self):
53 return 'pi'
54
55 self.mock(pickle, 'dumps', MockPickleDumps)
56
57 args = (CallableIdentifier(), PropertyIdentifier())
58 kwargs = {'url': 'http://url'}
59 key = cache_decorator._DefaultKeyGenerator(Func, args, kwargs)
60 self.assertEqual(expected_key, key)
61
62 def testCachedDecoratorWhenResultIsAlreadyCached(self):
63 dummy_cache = _DummyCache({'n-Func': 1})
64
65 @cache_decorator.Cached(
66 namespace='n', key_generator=_DummyKeyGenerator, cache=dummy_cache)
67 def Func():
68 return 2 # pragma: no cover.
69
70 self.assertEqual(1, Func())
71 self.assertEqual({'n-Func': 1}, dummy_cache.cached_data)
72
73 def testCachedDecoratorWhenResultIsNotCachedYet(self):
74 dummy_cache = _DummyCache({})
75
76 @cache_decorator.Cached(
77 namespace='n', key_generator=_DummyKeyGenerator, cache=dummy_cache)
78 def Func():
79 return 2
80
81 self.assertEqual(2, Func())
82 self.assertEqual({'n-Func': 2}, dummy_cache.cached_data)
83
84 def testCachedDecoratorWhenResultShouldNotBeCached(self):
85 dummy_cache = _DummyCache({})
86
87 results = [None, 0, [], {}, '']
88
89 @cache_decorator.Cached(
90 namespace='n', key_generator=_DummyKeyGenerator, cache=dummy_cache)
91 def Func():
92 return results.pop()
93
94 self.assertEqual('', Func())
95 self.assertEqual({}, dummy_cache.cached_data)
96 self.assertEqual({}, Func())
97 self.assertEqual({}, dummy_cache.cached_data)
98 self.assertEqual([], Func())
99 self.assertEqual({}, dummy_cache.cached_data)
100 self.assertEqual(0, Func())
101 self.assertEqual({}, dummy_cache.cached_data)
102 self.assertIsNone(Func())
103 self.assertEqual({}, dummy_cache.cached_data)
104
105 def testCachedDecoratorWithMethodInAClass(self):
106 class A(object):
107 def __init__(self, url, retries):
108 self.url = url
109 self.retries = retries
110 self.runs = 0
111
112 @property
113 def identifier(self):
114 return self.url
115
116 @cache_decorator.Cached(cache=_DummyCache({}))
117 def Func(self, path):
118 self.runs += 1
119 return self.url + '/' + path
120
121 a1 = A('http://test', 3)
122 self.assertEqual('http://test/p1', a1.Func('p1'))
123 self.assertEqual('http://test/p1', a1.Func('p1'))
124 self.assertEqual(1, a1.runs)
125
126 a2 = A('http://test', 5)
127 self.assertEqual('http://test/p1', a2.Func('p1'))
128 self.assertEqual(0, a2.runs)
OLDNEW
« no previous file with comments | « appengine/findit/lib/test/__init__.py ('k') | appengine/findit/lib/test/time_util_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698