2003-09-13 02:51:09 -03:00
|
|
|
"""Test script for the bsddb C module by Roger E. Masse
|
|
|
|
Adapted to unittest format and expanded scope by Raymond Hettinger
|
1997-01-16 18:04:10 -04:00
|
|
|
"""
|
2003-11-02 05:10:16 -04:00
|
|
|
import os, sys
|
2003-09-13 02:51:09 -03:00
|
|
|
import unittest
|
|
|
|
from test import test_support
|
|
|
|
|
2009-03-31 15:32:17 -03:00
|
|
|
# Skip test if _bsddb wasn't built.
|
|
|
|
test_support.import_module('_bsddb')
|
|
|
|
|
2010-01-30 03:22:54 -04:00
|
|
|
bsddb = test_support.import_module('bsddb', deprecated=True)
|
|
|
|
# Just so we know it's imported:
|
|
|
|
test_support.import_module('dbhash', deprecated=True)
|
2009-03-31 15:32:17 -03:00
|
|
|
|
|
|
|
|
2003-09-13 02:51:09 -03:00
|
|
|
class TestBSDDB(unittest.TestCase):
|
2006-04-12 17:16:56 -03:00
|
|
|
openflag = 'c'
|
2003-09-13 02:51:09 -03:00
|
|
|
|
|
|
|
def setUp(self):
|
2006-04-12 17:35:02 -03:00
|
|
|
self.f = self.openmethod[0](self.fname, self.openflag, cachesize=32768)
|
2003-09-13 02:51:09 -03:00
|
|
|
self.d = dict(q='Guido', w='van', e='Rossum', r='invented', t='Python', y='')
|
|
|
|
for k, v in self.d.iteritems():
|
|
|
|
self.f[k] = v
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.f.sync()
|
|
|
|
self.f.close()
|
|
|
|
if self.fname is None:
|
|
|
|
return
|
2002-04-22 23:11:05 -03:00
|
|
|
try:
|
2003-09-13 02:51:09 -03:00
|
|
|
os.remove(self.fname)
|
2002-04-22 23:11:05 -03:00
|
|
|
except os.error:
|
|
|
|
pass
|
1997-01-16 18:04:10 -04:00
|
|
|
|
2003-09-13 02:51:09 -03:00
|
|
|
def test_getitem(self):
|
|
|
|
for k, v in self.d.iteritems():
|
|
|
|
self.assertEqual(self.f[k], v)
|
|
|
|
|
|
|
|
def test_len(self):
|
|
|
|
self.assertEqual(len(self.f), len(self.d))
|
|
|
|
|
|
|
|
def test_change(self):
|
|
|
|
self.f['r'] = 'discovered'
|
|
|
|
self.assertEqual(self.f['r'], 'discovered')
|
2010-01-23 19:04:36 -04:00
|
|
|
self.assertIn('r', self.f.keys())
|
|
|
|
self.assertIn('discovered', self.f.values())
|
2003-09-13 02:51:09 -03:00
|
|
|
|
|
|
|
def test_close_and_reopen(self):
|
2013-12-10 16:09:20 -04:00
|
|
|
self.assertIsNotNone(self.fname)
|
2003-09-13 02:51:09 -03:00
|
|
|
self.f.close()
|
|
|
|
self.f = self.openmethod[0](self.fname, 'w')
|
|
|
|
for k, v in self.d.iteritems():
|
|
|
|
self.assertEqual(self.f[k], v)
|
|
|
|
|
|
|
|
def assertSetEquals(self, seqn1, seqn2):
|
2006-07-17 10:23:46 -03:00
|
|
|
self.assertEqual(set(seqn1), set(seqn2))
|
2003-09-13 02:51:09 -03:00
|
|
|
|
|
|
|
def test_mapping_iteration_methods(self):
|
|
|
|
f = self.f
|
|
|
|
d = self.d
|
|
|
|
self.assertSetEquals(d, f)
|
|
|
|
self.assertSetEquals(d.keys(), f.keys())
|
|
|
|
self.assertSetEquals(d.values(), f.values())
|
|
|
|
self.assertSetEquals(d.items(), f.items())
|
|
|
|
self.assertSetEquals(d.iterkeys(), f.iterkeys())
|
|
|
|
self.assertSetEquals(d.itervalues(), f.itervalues())
|
|
|
|
self.assertSetEquals(d.iteritems(), f.iteritems())
|
|
|
|
|
2003-11-02 21:04:41 -04:00
|
|
|
def test_iter_while_modifying_values(self):
|
|
|
|
di = iter(self.d)
|
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
key = di.next()
|
|
|
|
self.d[key] = 'modified '+key
|
|
|
|
except StopIteration:
|
|
|
|
break
|
|
|
|
|
|
|
|
# it should behave the same as a dict. modifying values
|
|
|
|
# of existing keys should not break iteration. (adding
|
|
|
|
# or removing keys should)
|
2008-05-25 05:28:29 -03:00
|
|
|
loops_left = len(self.f)
|
2003-11-02 21:04:41 -04:00
|
|
|
fi = iter(self.f)
|
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
key = fi.next()
|
|
|
|
self.f[key] = 'modified '+key
|
2008-05-25 05:28:29 -03:00
|
|
|
loops_left -= 1
|
2003-11-02 21:04:41 -04:00
|
|
|
except StopIteration:
|
|
|
|
break
|
2008-05-25 05:28:29 -03:00
|
|
|
self.assertEqual(loops_left, 0)
|
2003-11-02 21:04:41 -04:00
|
|
|
|
|
|
|
self.test_mapping_iteration_methods()
|
|
|
|
|
2008-05-25 05:28:29 -03:00
|
|
|
def test_iter_abort_on_changed_size(self):
|
|
|
|
def DictIterAbort():
|
|
|
|
di = iter(self.d)
|
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
di.next()
|
|
|
|
self.d['newkey'] = 'SPAM'
|
|
|
|
except StopIteration:
|
|
|
|
break
|
|
|
|
self.assertRaises(RuntimeError, DictIterAbort)
|
|
|
|
|
|
|
|
def DbIterAbort():
|
|
|
|
fi = iter(self.f)
|
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
fi.next()
|
|
|
|
self.f['newkey'] = 'SPAM'
|
|
|
|
except StopIteration:
|
|
|
|
break
|
|
|
|
self.assertRaises(RuntimeError, DbIterAbort)
|
2003-11-02 21:04:41 -04:00
|
|
|
|
2008-05-25 05:28:29 -03:00
|
|
|
def test_iteritems_abort_on_changed_size(self):
|
|
|
|
def DictIteritemsAbort():
|
|
|
|
di = self.d.iteritems()
|
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
di.next()
|
|
|
|
self.d['newkey'] = 'SPAM'
|
|
|
|
except StopIteration:
|
|
|
|
break
|
|
|
|
self.assertRaises(RuntimeError, DictIteritemsAbort)
|
|
|
|
|
|
|
|
def DbIteritemsAbort():
|
|
|
|
fi = self.f.iteritems()
|
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
key, value = fi.next()
|
|
|
|
del self.f[key]
|
|
|
|
except StopIteration:
|
|
|
|
break
|
|
|
|
self.assertRaises(RuntimeError, DbIteritemsAbort)
|
|
|
|
|
|
|
|
def test_iteritems_while_modifying_values(self):
|
2003-11-02 21:04:41 -04:00
|
|
|
di = self.d.iteritems()
|
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
k, v = di.next()
|
|
|
|
self.d[k] = 'modified '+v
|
|
|
|
except StopIteration:
|
|
|
|
break
|
|
|
|
|
|
|
|
# it should behave the same as a dict. modifying values
|
|
|
|
# of existing keys should not break iteration. (adding
|
|
|
|
# or removing keys should)
|
2008-05-25 05:28:29 -03:00
|
|
|
loops_left = len(self.f)
|
2003-11-02 21:04:41 -04:00
|
|
|
fi = self.f.iteritems()
|
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
k, v = fi.next()
|
|
|
|
self.f[k] = 'modified '+v
|
2008-05-25 05:28:29 -03:00
|
|
|
loops_left -= 1
|
2003-11-02 21:04:41 -04:00
|
|
|
except StopIteration:
|
|
|
|
break
|
2008-05-25 05:28:29 -03:00
|
|
|
self.assertEqual(loops_left, 0)
|
2003-11-02 21:04:41 -04:00
|
|
|
|
|
|
|
self.test_mapping_iteration_methods()
|
|
|
|
|
2003-09-13 02:51:09 -03:00
|
|
|
def test_first_next_looping(self):
|
|
|
|
items = [self.f.first()]
|
|
|
|
for i in xrange(1, len(self.f)):
|
|
|
|
items.append(self.f.next())
|
|
|
|
self.assertSetEquals(items, self.d.items())
|
|
|
|
|
|
|
|
def test_previous_last_looping(self):
|
|
|
|
items = [self.f.last()]
|
|
|
|
for i in xrange(1, len(self.f)):
|
|
|
|
items.append(self.f.previous())
|
|
|
|
self.assertSetEquals(items, self.d.items())
|
|
|
|
|
2007-08-24 02:11:38 -03:00
|
|
|
def test_first_while_deleting(self):
|
|
|
|
# Test for bug 1725856
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(len(self.d) >= 2, "test requires >=2 items")
|
2007-08-24 02:11:38 -03:00
|
|
|
for _ in self.d:
|
|
|
|
key = self.f.first()[0]
|
|
|
|
del self.f[key]
|
|
|
|
self.assertEqual([], self.f.items(), "expected empty db after test")
|
|
|
|
|
|
|
|
def test_last_while_deleting(self):
|
|
|
|
# Test for bug 1725856's evil twin
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(len(self.d) >= 2, "test requires >=2 items")
|
2007-08-24 02:11:38 -03:00
|
|
|
for _ in self.d:
|
|
|
|
key = self.f.last()[0]
|
|
|
|
del self.f[key]
|
|
|
|
self.assertEqual([], self.f.items(), "expected empty db after test")
|
|
|
|
|
2003-09-13 02:51:09 -03:00
|
|
|
def test_set_location(self):
|
|
|
|
self.assertEqual(self.f.set_location('e'), ('e', self.d['e']))
|
|
|
|
|
|
|
|
def test_contains(self):
|
|
|
|
for k in self.d:
|
2010-01-23 19:04:36 -04:00
|
|
|
self.assertIn(k, self.f)
|
|
|
|
self.assertNotIn('not here', self.f)
|
2003-09-13 02:51:09 -03:00
|
|
|
|
|
|
|
def test_has_key(self):
|
|
|
|
for k in self.d:
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(self.f.has_key(k))
|
|
|
|
self.assertTrue(not self.f.has_key('not here'))
|
2003-09-13 02:51:09 -03:00
|
|
|
|
|
|
|
def test_clear(self):
|
|
|
|
self.f.clear()
|
|
|
|
self.assertEqual(len(self.f), 0)
|
|
|
|
|
2003-11-02 05:10:16 -04:00
|
|
|
def test__no_deadlock_first(self, debug=0):
|
|
|
|
# do this so that testers can see what function we're in in
|
|
|
|
# verbose mode when we deadlock.
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
# in pybsddb's _DBWithCursor this causes an internal DBCursor
|
|
|
|
# object is created. Other test_ methods in this class could
|
|
|
|
# inadvertently cause the deadlock but an explicit test is needed.
|
|
|
|
if debug: print "A"
|
|
|
|
k,v = self.f.first()
|
|
|
|
if debug: print "B", k
|
|
|
|
self.f[k] = "deadlock. do not pass go. do not collect $200."
|
|
|
|
if debug: print "C"
|
|
|
|
# if the bsddb implementation leaves the DBCursor open during
|
|
|
|
# the database write and locking+threading support is enabled
|
|
|
|
# the cursor's read lock will deadlock the write lock request..
|
|
|
|
|
2008-05-25 05:28:29 -03:00
|
|
|
# test the iterator interface
|
|
|
|
if True:
|
2003-11-02 05:10:16 -04:00
|
|
|
if debug: print "D"
|
2003-11-02 21:04:41 -04:00
|
|
|
i = self.f.iteritems()
|
|
|
|
k,v = i.next()
|
2003-11-02 05:10:16 -04:00
|
|
|
if debug: print "E"
|
|
|
|
self.f[k] = "please don't deadlock"
|
|
|
|
if debug: print "F"
|
|
|
|
while 1:
|
|
|
|
try:
|
2003-11-02 21:04:41 -04:00
|
|
|
k,v = i.next()
|
2003-11-02 05:10:16 -04:00
|
|
|
except StopIteration:
|
|
|
|
break
|
|
|
|
if debug: print "F2"
|
|
|
|
|
|
|
|
i = iter(self.f)
|
|
|
|
if debug: print "G"
|
|
|
|
while i:
|
|
|
|
try:
|
|
|
|
if debug: print "H"
|
|
|
|
k = i.next()
|
|
|
|
if debug: print "I"
|
|
|
|
self.f[k] = "deadlocks-r-us"
|
|
|
|
if debug: print "J"
|
|
|
|
except StopIteration:
|
|
|
|
i = None
|
|
|
|
if debug: print "K"
|
|
|
|
|
|
|
|
# test the legacy cursor interface mixed with writes
|
2010-01-23 19:04:36 -04:00
|
|
|
self.assertIn(self.f.first()[0], self.d)
|
2003-11-02 05:10:16 -04:00
|
|
|
k = self.f.next()[0]
|
2010-01-23 19:04:36 -04:00
|
|
|
self.assertIn(k, self.d)
|
2003-11-02 05:10:16 -04:00
|
|
|
self.f[k] = "be gone with ye deadlocks"
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(self.f[k], "be gone with ye deadlocks")
|
2003-11-02 05:10:16 -04:00
|
|
|
|
2003-11-02 21:04:41 -04:00
|
|
|
def test_for_cursor_memleak(self):
|
2008-05-25 05:28:29 -03:00
|
|
|
# do the bsddb._DBWithCursor iterator internals leak cursors?
|
2003-11-02 21:04:41 -04:00
|
|
|
nc1 = len(self.f._cursor_refs)
|
|
|
|
# create iterator
|
|
|
|
i = self.f.iteritems()
|
|
|
|
nc2 = len(self.f._cursor_refs)
|
2007-02-22 20:22:39 -04:00
|
|
|
# use the iterator (should run to the first yield, creating the cursor)
|
2003-11-02 21:04:41 -04:00
|
|
|
k, v = i.next()
|
|
|
|
nc3 = len(self.f._cursor_refs)
|
|
|
|
# destroy the iterator; this should cause the weakref callback
|
|
|
|
# to remove the cursor object from self.f._cursor_refs
|
|
|
|
del i
|
|
|
|
nc4 = len(self.f._cursor_refs)
|
|
|
|
|
|
|
|
self.assertEqual(nc1, nc2)
|
|
|
|
self.assertEqual(nc1, nc4)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(nc3 == nc1+1)
|
2003-11-02 21:04:41 -04:00
|
|
|
|
2003-09-13 02:51:09 -03:00
|
|
|
def test_popitem(self):
|
|
|
|
k, v = self.f.popitem()
|
2010-01-23 19:04:36 -04:00
|
|
|
self.assertIn(k, self.d)
|
|
|
|
self.assertIn(v, self.d.values())
|
|
|
|
self.assertNotIn(k, self.f)
|
2003-09-13 02:51:09 -03:00
|
|
|
self.assertEqual(len(self.d)-1, len(self.f))
|
|
|
|
|
|
|
|
def test_pop(self):
|
|
|
|
k = 'w'
|
|
|
|
v = self.f.pop(k)
|
|
|
|
self.assertEqual(v, self.d[k])
|
2010-01-23 19:04:36 -04:00
|
|
|
self.assertNotIn(k, self.f)
|
|
|
|
self.assertNotIn(v, self.f.values())
|
2003-09-13 02:51:09 -03:00
|
|
|
self.assertEqual(len(self.d)-1, len(self.f))
|
|
|
|
|
|
|
|
def test_get(self):
|
|
|
|
self.assertEqual(self.f.get('NotHere'), None)
|
|
|
|
self.assertEqual(self.f.get('NotHere', 'Default'), 'Default')
|
|
|
|
self.assertEqual(self.f.get('q', 'Default'), self.d['q'])
|
|
|
|
|
|
|
|
def test_setdefault(self):
|
|
|
|
self.assertEqual(self.f.setdefault('new', 'dog'), 'dog')
|
|
|
|
self.assertEqual(self.f.setdefault('r', 'cat'), self.d['r'])
|
|
|
|
|
|
|
|
def test_update(self):
|
|
|
|
new = dict(y='life', u='of', i='brian')
|
|
|
|
self.f.update(new)
|
|
|
|
self.d.update(new)
|
|
|
|
for k, v in self.d.iteritems():
|
|
|
|
self.assertEqual(self.f[k], v)
|
|
|
|
|
|
|
|
def test_keyordering(self):
|
2013-12-10 16:09:20 -04:00
|
|
|
self.assertIs(self.openmethod[0], bsddb.btopen)
|
2003-09-13 02:51:09 -03:00
|
|
|
keys = self.d.keys()
|
|
|
|
keys.sort()
|
|
|
|
self.assertEqual(self.f.first()[0], keys[0])
|
|
|
|
self.assertEqual(self.f.next()[0], keys[1])
|
|
|
|
self.assertEqual(self.f.last()[0], keys[-1])
|
|
|
|
self.assertEqual(self.f.previous()[0], keys[-2])
|
|
|
|
self.assertEqual(list(self.f), keys)
|
|
|
|
|
|
|
|
class TestBTree(TestBSDDB):
|
|
|
|
fname = test_support.TESTFN
|
|
|
|
openmethod = [bsddb.btopen]
|
|
|
|
|
|
|
|
class TestBTree_InMemory(TestBSDDB):
|
|
|
|
fname = None
|
|
|
|
openmethod = [bsddb.btopen]
|
|
|
|
|
2013-12-10 16:09:20 -04:00
|
|
|
# if we're using an in-memory only db, we can't reopen it
|
|
|
|
test_close_and_reopen = None
|
|
|
|
|
2006-04-12 17:16:56 -03:00
|
|
|
class TestBTree_InMemory_Truncate(TestBSDDB):
|
|
|
|
fname = None
|
|
|
|
openflag = 'n'
|
|
|
|
openmethod = [bsddb.btopen]
|
|
|
|
|
2013-12-10 16:09:20 -04:00
|
|
|
# if we're using an in-memory only db, we can't reopen it
|
|
|
|
test_close_and_reopen = None
|
|
|
|
|
2003-09-13 02:51:09 -03:00
|
|
|
class TestHashTable(TestBSDDB):
|
|
|
|
fname = test_support.TESTFN
|
|
|
|
openmethod = [bsddb.hashopen]
|
|
|
|
|
2013-12-10 16:09:20 -04:00
|
|
|
# keyordering is specific to btopen method
|
|
|
|
test_keyordering = None
|
|
|
|
|
2003-09-13 02:51:09 -03:00
|
|
|
class TestHashTable_InMemory(TestBSDDB):
|
|
|
|
fname = None
|
|
|
|
openmethod = [bsddb.hashopen]
|
|
|
|
|
2013-12-10 16:09:20 -04:00
|
|
|
# if we're using an in-memory only db, we can't reopen it
|
|
|
|
test_close_and_reopen = None
|
|
|
|
|
|
|
|
# keyordering is specific to btopen method
|
|
|
|
test_keyordering = None
|
|
|
|
|
2003-09-13 02:51:09 -03:00
|
|
|
## # (bsddb.rnopen,'Record Numbers'), 'put' for RECNO for bsddb 1.85
|
|
|
|
## # appears broken... at least on
|
|
|
|
## # Solaris Intel - rmasse 1/97
|
|
|
|
|
|
|
|
def test_main(verbose=None):
|
|
|
|
test_support.run_unittest(
|
|
|
|
TestBTree,
|
|
|
|
TestHashTable,
|
|
|
|
TestBTree_InMemory,
|
|
|
|
TestHashTable_InMemory,
|
2006-04-12 17:16:56 -03:00
|
|
|
TestBTree_InMemory_Truncate,
|
2003-09-13 02:51:09 -03:00
|
|
|
)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main(verbose=True)
|