summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaurent Bachelier <laurent@bachelier.name>2011-12-28 20:09:53 +0100
committerLaurent Bachelier <laurent@bachelier.name>2011-12-28 20:09:53 +0100
commit3cb3f5728a6d667a497dcd100e6ca9b8365d9599 (patch)
tree7df2924b55c09f6943af7f0f464a3f77fbbf1777
parentUse filesystem rename instead of reading and writing values (diff)
downloadffs-master.tar.xz
Add ability to check if a DictList is healthyHEADmaster
-rw-r--r--ffs.py10
-rw-r--r--test.py18
2 files changed, 27 insertions, 1 deletions
diff --git a/ffs.py b/ffs.py
index 6af1b67..f5c4f40 100644
--- a/ffs.py
+++ b/ffs.py
@@ -74,6 +74,16 @@ class DictList(MutableSequence):
raise IndexError('list index out of range')
return index
+ def _is_healthy(self):
+ keys = self.dct.keys()
+ # We don't have to check the len(keys) == len(self), as it is
+ # how len(self) is found. What we check is that all the keys are
+ # part of the list.
+ for i in xrange(0, len(self)):
+ if str(i) not in keys:
+ return False
+ return True
+
class Dict(MutableMapping):
def __init__(self, root, router):
diff --git a/test.py b/test.py
index a38541c..85c3d6c 100644
--- a/test.py
+++ b/test.py
@@ -151,7 +151,7 @@ class FfsTest(TestCase):
rtr = Router(looool=[str])
dct1 = Dict(self.root, rtr)
self.assertRaises(KeyError, dct1.__getitem__, 'looool')
- os.mkdir(os.path.join(self.root, 'looool'))
+ dct1['looool'] = []
assert isinstance(dct1['looool'], DictList)
dl1 = dct1['looool']
# validate index types (mimics list)
@@ -161,13 +161,16 @@ class FfsTest(TestCase):
self.assertRaises(IndexError, dl1.__setitem__, 0, "a")
self.assertRaises(IndexError, dl1.__delitem__, 0)
assert len(dl1) == 0
+ assert dl1._is_healthy()
# add an element
dl1.append("b")
assert len(dl1) == 1
assert dl1[0] == "b"
+ assert dl1._is_healthy()
# alter the element
dl1[0] = "c"
assert dl1[0] == "c"
+ assert dl1._is_healthy()
# check it's the same with a new DictList instance
dl2 = dct1['looool']
assert dl1 is not dl2
@@ -179,6 +182,7 @@ class FfsTest(TestCase):
assert dl1[1] == "d"
assert dl1[2] == "e"
assert len(dl1) == 3
+ assert dl1._is_healthy()
# deletion
del dl1[1]
assert dl1[0] == "c"
@@ -198,19 +202,23 @@ class FfsTest(TestCase):
assert dl1[1] == "b"
assert dl1[2] == "a"
assert len(dl1) == 3
+ assert dl1._is_healthy()
del dl1[2]
dl1.insert(1, "d")
assert dl1[0] == "c"
assert dl1[1] == "d"
assert dl1[2] == "b"
assert len(dl1) == 3
+ assert dl1._is_healthy()
dl1.insert(42, "e")
assert dl1[3] == "e"
assert len(dl1) == 4
+ assert dl1._is_healthy()
dl1.insert(3, "f")
assert dl1[3] == "f"
assert dl1[4] == "e"
assert len(dl1) == 5
+ assert dl1._is_healthy()
# negative indexes
assert dl1[-1] == dl1[4]
assert dl1[-2] == dl1[3]
@@ -222,13 +230,21 @@ class FfsTest(TestCase):
del dct1['looool']
assert 'looool' not in dct1
dct1['looool'] = ["x", "y"]
+ assert dl1._is_healthy()
assert len(dct1['looool']) == 2
assert len(dl1) == 2
assert dl1[0] == "x"
assert dl1[1] == "y"
+ assert dl1._is_healthy()
dct1['looool'] = []
assert 'looool' in dct1
assert len(dl1) == 0
+ assert dl1._is_healthy()
+
+ def test_brokenList(self):
+ rtr = Router(lol=[str])
+ dct1 = Dict(self.root, rtr)
+ assert not dct1['lol']._is_healthy()
def test_mutableSave(self):
class MutableObject(object):