Commit 77f84f3c authored by Tres Seaver's avatar Tres Seaver

Coverage for _Tree.___{g,s}etstate__.

parent b8f7d29f
......@@ -1896,6 +1896,70 @@ class Test_Tree(unittest.TestCase):
del tree[key]
self.assertTrue(tree._data[1].child._firstbucket._next is next_b)
def test___getstate___empty(self):
tree = self._makeOne()
self.assertEqual(tree.__getstate__(), None)
def test___getstate___single_bucket_wo_oid(self):
tree = self._makeOne({'a': 'b'})
self.assertEqual(tree.__getstate__(), (((('a', 'b'),),),))
def test___getstate___single_bucket_w_oid(self):
tree = self._makeOne({'a': 'b'})
bucket = tree._firstbucket
jar = _Jar()
bucket._p_jar = jar
bucket._p_oid = 'OID'
self.assertEqual(tree.__getstate__(), ((bucket,), bucket))
def test___getstate___multiple_buckets(self):
tree = self._makeOne()
FMT = '%05d'
for i in range(1000):
key = FMT % i
tree[key] = i
bucket = tree._firstbucket
EXPECTED = (tree._data[0].child,)
for item in tree._data[1:]:
EXPECTED += (item.key, item.child)
self.assertEqual(tree.__getstate__(), (EXPECTED, bucket))
def test___setstate___invalid(self):
tree = self._makeOne()
self.assertRaises(TypeError, tree.__setstate__, ('a', 'b'))
def test___setstate___to_empty(self):
tree = self._makeOne({'a': 'b'})
tree.__setstate__(None)
self.assertEqual(len(tree), 0)
def test___setstate___to_single_bucket_wo_oid(self):
tree = self._makeOne()
tree.__setstate__((((('a', 'b'),),),))
self.assertEqual(list(tree.keys()), ['a'])
self.assertEqual(tree._findbucket('a')['a'], 'b')
self.assertTrue(len(tree._data), 1)
self.assertTrue(tree._data[0].child is tree._firstbucket)
self.assertTrue(tree._firstbucket._p_oid is None)
def test___setstate___to_multiple_buckets(self):
from .._base import Bucket
class _Bucket(Bucket):
def _to_key(self, x):
return x
tree = self._makeOne()
b1 = _Bucket({'a': 0, 'b': 1})
b2 = _Bucket({'c': 2, 'd': 3})
b1._next = b2
tree.__setstate__(((b1, 'c', b2), b1))
self.assertEqual(list(tree.keys()), ['a', 'b', 'c', 'd'])
self.assertTrue(len(tree._data), 2)
self.assertEqual(tree._data[0].key, None)
self.assertEqual(tree._data[0].child, b1)
self.assertEqual(tree._data[1].key, 'c')
self.assertEqual(tree._data[1].child, b2)
self.assertTrue(tree._firstbucket is b1)
class _Jar(object):
def __init__(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment