OLD | NEW |
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 from future import Future | 5 from future import Future |
6 from object_store import ObjectStore | 6 from object_store import ObjectStore |
7 | 7 |
8 class TestObjectStore(ObjectStore): | 8 class TestObjectStore(ObjectStore): |
9 '''An object store which records its namespace and behaves like a dict. | 9 '''An object store which records its namespace and behaves like a dict. |
10 Specify |init| with an initial object for the object store. | 10 Specify |init| with an initial object for the object store. |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 def GetMulti(self, keys): | 28 def GetMulti(self, keys): |
29 def callback(): | 29 def callback(): |
30 self._get_count += 1 | 30 self._get_count += 1 |
31 return dict((k, self._store.get(k)) for k in keys if k in self._store) | 31 return dict((k, self._store.get(k)) for k in keys if k in self._store) |
32 return Future(callback=callback) | 32 return Future(callback=callback) |
33 | 33 |
34 def SetMulti(self, mapping): | 34 def SetMulti(self, mapping): |
35 self._set_count += 1 | 35 self._set_count += 1 |
36 self._store.update(mapping) | 36 self._store.update(mapping) |
| 37 return Future(value=None) |
37 | 38 |
38 def DelMulti(self, keys): | 39 def DelMulti(self, keys): |
39 self._del_count += 1 | 40 self._del_count += 1 |
40 for k in keys: | 41 for k in keys: |
41 self._store.pop(k, None) | 42 self._store.pop(k, None) |
42 | 43 |
43 # | 44 # |
44 # Testing methods. | 45 # Testing methods. |
45 # | 46 # |
46 | 47 |
47 def CheckAndReset(self, get_count=0, set_count=0, del_count=0): | 48 def CheckAndReset(self, get_count=0, set_count=0, del_count=0): |
48 '''Returns a tuple (success, error). Use in tests like: | 49 '''Returns a tuple (success, error). Use in tests like: |
49 self.assertTrue(*object_store.CheckAndReset(...)) | 50 self.assertTrue(*object_store.CheckAndReset(...)) |
50 ''' | 51 ''' |
51 errors = [] | 52 errors = [] |
52 for desc, expected, actual in (('get_count', get_count, self._get_count), | 53 for desc, expected, actual in (('get_count', get_count, self._get_count), |
53 ('set_count', set_count, self._set_count), | 54 ('set_count', set_count, self._set_count), |
54 ('del_count', del_count, self._del_count)): | 55 ('del_count', del_count, self._del_count)): |
55 if actual != expected: | 56 if actual != expected: |
56 errors.append('%s: expected %s got %s' % (desc, expected, actual)) | 57 errors.append('%s: expected %s got %s' % (desc, expected, actual)) |
57 try: | 58 try: |
58 return (len(errors) == 0, ', '.join(errors)) | 59 return (len(errors) == 0, ', '.join(errors)) |
59 finally: | 60 finally: |
60 self.Reset() | 61 self.Reset() |
61 | 62 |
62 def Reset(self): | 63 def Reset(self): |
63 self._get_count = 0 | 64 self._get_count = 0 |
64 self._set_count = 0 | 65 self._set_count = 0 |
65 self._del_count = 0 | 66 self._del_count = 0 |
OLD | NEW |