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
3 # found in the LICENSE file.
5 from future
import Future
6 from object_store
import ObjectStore
8 class TestObjectStore(ObjectStore
):
9 '''An object store which records its namespace and behaves like a dict.
10 Specify |init| with an initial object for the object store.
11 Use CheckAndReset to assert how many times Get/Set/Del have been called. Get
12 is a special case; it is only incremented once the future has had Get called.
14 def __init__(self
, namespace
, start_empty
=False, init
=None):
15 self
.namespace
= namespace
16 self
.start_empty
= start_empty
17 self
._store
= {} if init
is None else init
19 assert not self
._store
25 # ObjectStore implementation.
28 def GetMulti(self
, keys
):
31 return dict((k
, self
._store
.get(k
)) for k
in keys
if k
in self
._store
)
32 return Future(callback
=callback
)
34 def SetMulti(self
, mapping
):
36 self
._store
.update(mapping
)
37 return Future(value
=None)
39 def DelMulti(self
, keys
):
42 self
._store
.pop(k
, None)
48 def CheckAndReset(self
, get_count
=0, set_count
=0, del_count
=0):
49 '''Returns a tuple (success, error). Use in tests like:
50 self.assertTrue(*object_store.CheckAndReset(...))
53 for desc
, expected
, actual
in (('get_count', get_count
, self
._get
_count
),
54 ('set_count', set_count
, self
._set
_count
),
55 ('del_count', del_count
, self
._del
_count
)):
56 if actual
!= expected
:
57 errors
.append('%s: expected %s got %s' % (desc
, expected
, actual
))
59 return (len(errors
) == 0, ', '.join(errors
))