2002-11-19 13:47:07 -04:00
|
|
|
"""
|
|
|
|
TestCases for checking dbShelve objects.
|
|
|
|
"""
|
|
|
|
|
2008-02-23 13:40:11 -04:00
|
|
|
import os, string
|
2008-05-13 17:57:59 -03:00
|
|
|
import random
|
2002-11-19 13:47:07 -04:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
2008-08-31 11:00:51 -03:00
|
|
|
from test_all import db, dbshelve, test_support, verbose, \
|
|
|
|
get_new_environment_path, get_new_database_path
|
2008-05-13 17:57:59 -03:00
|
|
|
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
# We want the objects to be comparable so we can test dbshelve.values
|
|
|
|
# later on.
|
|
|
|
class DataClass:
|
|
|
|
def __init__(self):
|
|
|
|
self.value = random.random()
|
|
|
|
|
2008-08-31 11:00:51 -03:00
|
|
|
def __repr__(self) : # For Python 3.0 comparison
|
|
|
|
return "DataClass %f" %self.value
|
|
|
|
|
|
|
|
def __cmp__(self, other): # For Python 2.x comparison
|
2002-11-19 13:47:07 -04:00
|
|
|
return cmp(self.value, other)
|
|
|
|
|
2008-08-31 11:00:51 -03:00
|
|
|
|
2002-11-19 13:47:07 -04:00
|
|
|
class DBShelveTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2008-08-31 11:00:51 -03:00
|
|
|
import sys
|
|
|
|
if sys.version_info[0] >= 3 :
|
|
|
|
from test_all import do_proxy_db_py3k
|
|
|
|
self._flag_proxy_db_py3k = do_proxy_db_py3k(False)
|
2008-05-13 17:57:59 -03:00
|
|
|
self.filename = get_new_database_path()
|
2002-11-19 13:47:07 -04:00
|
|
|
self.do_open()
|
|
|
|
|
|
|
|
def tearDown(self):
|
2008-08-31 11:00:51 -03:00
|
|
|
import sys
|
|
|
|
if sys.version_info[0] >= 3 :
|
|
|
|
from test_all import do_proxy_db_py3k
|
|
|
|
do_proxy_db_py3k(self._flag_proxy_db_py3k)
|
2002-11-19 13:47:07 -04:00
|
|
|
self.do_close()
|
2008-03-05 01:10:48 -04:00
|
|
|
test_support.unlink(self.filename)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2007-10-12 15:44:06 -03:00
|
|
|
def mk(self, key):
|
|
|
|
"""Turn key into an appropriate key type for this db"""
|
|
|
|
# override in child class for RECNO
|
2008-08-31 11:00:51 -03:00
|
|
|
import sys
|
|
|
|
if sys.version_info[0] < 3 :
|
|
|
|
return key
|
|
|
|
else :
|
|
|
|
return bytes(key, "iso8859-1") # 8 bits
|
2007-10-12 15:44:06 -03:00
|
|
|
|
2002-11-19 13:47:07 -04:00
|
|
|
def populateDB(self, d):
|
|
|
|
for x in string.letters:
|
2007-10-12 15:44:06 -03:00
|
|
|
d[self.mk('S' + x)] = 10 * x # add a string
|
|
|
|
d[self.mk('I' + x)] = ord(x) # add an integer
|
|
|
|
d[self.mk('L' + x)] = [x] * 10 # add a list
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
inst = DataClass() # add an instance
|
|
|
|
inst.S = 10 * x
|
|
|
|
inst.I = ord(x)
|
|
|
|
inst.L = [x] * 10
|
2007-10-12 15:44:06 -03:00
|
|
|
d[self.mk('O' + x)] = inst
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
# overridable in derived classes to affect how the shelf is created/opened
|
|
|
|
def do_open(self):
|
|
|
|
self.d = dbshelve.open(self.filename)
|
|
|
|
|
|
|
|
# and closed...
|
|
|
|
def do_close(self):
|
|
|
|
self.d.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test01_basics(self):
|
|
|
|
if verbose:
|
|
|
|
print '\n', '-=' * 30
|
|
|
|
print "Running %s.test01_basics..." % self.__class__.__name__
|
|
|
|
|
|
|
|
self.populateDB(self.d)
|
|
|
|
self.d.sync()
|
|
|
|
self.do_close()
|
|
|
|
self.do_open()
|
|
|
|
d = self.d
|
|
|
|
|
|
|
|
l = len(d)
|
|
|
|
k = d.keys()
|
|
|
|
s = d.stat()
|
|
|
|
f = d.fd()
|
|
|
|
|
|
|
|
if verbose:
|
|
|
|
print "length:", l
|
|
|
|
print "keys:", k
|
|
|
|
print "stats:", s
|
|
|
|
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(0, d.has_key(self.mk('bad key')))
|
|
|
|
self.assertEqual(1, d.has_key(self.mk('IA')))
|
|
|
|
self.assertEqual(1, d.has_key(self.mk('OA')))
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2007-10-12 15:44:06 -03:00
|
|
|
d.delete(self.mk('IA'))
|
|
|
|
del d[self.mk('OA')]
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(0, d.has_key(self.mk('IA')))
|
|
|
|
self.assertEqual(0, d.has_key(self.mk('OA')))
|
|
|
|
self.assertEqual(len(d), l-2)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
values = []
|
|
|
|
for key in d.keys():
|
|
|
|
value = d[key]
|
|
|
|
values.append(value)
|
|
|
|
if verbose:
|
|
|
|
print "%s: %s" % (key, value)
|
|
|
|
self.checkrec(key, value)
|
|
|
|
|
|
|
|
dbvalues = d.values()
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(len(dbvalues), len(d.keys()))
|
2008-08-31 11:00:51 -03:00
|
|
|
import sys
|
|
|
|
if sys.version_info[0] < 3 :
|
|
|
|
values.sort()
|
|
|
|
dbvalues.sort()
|
|
|
|
self.assertEqual(values, dbvalues)
|
|
|
|
else : # XXX: Convert all to strings. Please, improve
|
|
|
|
values.sort(key=lambda x : str(x))
|
|
|
|
dbvalues.sort(key=lambda x : str(x))
|
|
|
|
self.assertEqual(repr(values), repr(dbvalues))
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
items = d.items()
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(len(items), len(values))
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
for key, value in items:
|
|
|
|
self.checkrec(key, value)
|
|
|
|
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(d.get(self.mk('bad key')), None)
|
|
|
|
self.assertEqual(d.get(self.mk('bad key'), None), None)
|
|
|
|
self.assertEqual(d.get(self.mk('bad key'), 'a string'), 'a string')
|
|
|
|
self.assertEqual(d.get(self.mk('bad key'), [1, 2, 3]), [1, 2, 3])
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
d.set_get_returns_none(0)
|
2007-10-12 15:44:06 -03:00
|
|
|
self.assertRaises(db.DBNotFoundError, d.get, self.mk('bad key'))
|
2002-11-19 13:47:07 -04:00
|
|
|
d.set_get_returns_none(1)
|
|
|
|
|
2007-10-12 15:44:06 -03:00
|
|
|
d.put(self.mk('new key'), 'new data')
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(d.get(self.mk('new key')), 'new data')
|
|
|
|
self.assertEqual(d[self.mk('new key')], 'new data')
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test02_cursors(self):
|
|
|
|
if verbose:
|
|
|
|
print '\n', '-=' * 30
|
|
|
|
print "Running %s.test02_cursors..." % self.__class__.__name__
|
|
|
|
|
|
|
|
self.populateDB(self.d)
|
|
|
|
d = self.d
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
c = d.cursor()
|
|
|
|
rec = c.first()
|
|
|
|
while rec is not None:
|
|
|
|
count = count + 1
|
|
|
|
if verbose:
|
|
|
|
print rec
|
|
|
|
key, value = rec
|
|
|
|
self.checkrec(key, value)
|
2008-08-31 11:00:51 -03:00
|
|
|
# Hack to avoid conversion by 2to3 tool
|
|
|
|
rec = getattr(c, "next")()
|
2002-12-30 16:53:52 -04:00
|
|
|
del c
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(count, len(d))
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
c = d.cursor()
|
|
|
|
rec = c.last()
|
|
|
|
while rec is not None:
|
|
|
|
count = count + 1
|
|
|
|
if verbose:
|
|
|
|
print rec
|
|
|
|
key, value = rec
|
|
|
|
self.checkrec(key, value)
|
|
|
|
rec = c.prev()
|
|
|
|
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(count, len(d))
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2007-10-12 15:44:06 -03:00
|
|
|
c.set(self.mk('SS'))
|
2002-11-19 13:47:07 -04:00
|
|
|
key, value = c.current()
|
|
|
|
self.checkrec(key, value)
|
2002-12-30 16:53:52 -04:00
|
|
|
del c
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
|
2007-10-12 15:44:06 -03:00
|
|
|
def test03_append(self):
|
|
|
|
# NOTE: this is overridden in RECNO subclass, don't change its name.
|
|
|
|
if verbose:
|
|
|
|
print '\n', '-=' * 30
|
|
|
|
print "Running %s.test03_append..." % self.__class__.__name__
|
|
|
|
|
|
|
|
self.assertRaises(dbshelve.DBShelveError,
|
|
|
|
self.d.append, 'unit test was here')
|
|
|
|
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
def checkrec(self, key, value):
|
2007-10-12 15:44:06 -03:00
|
|
|
# override this in a subclass if the key type is different
|
2008-08-31 11:00:51 -03:00
|
|
|
|
|
|
|
import sys
|
|
|
|
if sys.version_info[0] >= 3 :
|
|
|
|
if isinstance(key, bytes) :
|
|
|
|
key = key.decode("iso8859-1") # 8 bits
|
|
|
|
|
2002-11-19 13:47:07 -04:00
|
|
|
x = key[1]
|
|
|
|
if key[0] == 'S':
|
2008-08-31 11:00:51 -03:00
|
|
|
self.assertEqual(type(value), str)
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(value, 10 * x)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
elif key[0] == 'I':
|
2008-08-31 11:00:51 -03:00
|
|
|
self.assertEqual(type(value), int)
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(value, ord(x))
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
elif key[0] == 'L':
|
2008-08-31 11:00:51 -03:00
|
|
|
self.assertEqual(type(value), list)
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(value, [x] * 10)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
elif key[0] == 'O':
|
2008-08-31 11:00:51 -03:00
|
|
|
import sys
|
|
|
|
if sys.version_info[0] < 3 :
|
|
|
|
from types import InstanceType
|
|
|
|
self.assertEqual(type(value), InstanceType)
|
|
|
|
else :
|
|
|
|
self.assertEqual(type(value), DataClass)
|
|
|
|
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(value.S, 10 * x)
|
|
|
|
self.assertEqual(value.I, ord(x))
|
|
|
|
self.assertEqual(value.L, [x] * 10)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
else:
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assert_(0, 'Unknown key type, fix the test')
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
class BasicShelveTestCase(DBShelveTestCase):
|
|
|
|
def do_open(self):
|
|
|
|
self.d = dbshelve.DBShelf()
|
|
|
|
self.d.open(self.filename, self.dbtype, self.dbflags)
|
|
|
|
|
|
|
|
def do_close(self):
|
|
|
|
self.d.close()
|
|
|
|
|
|
|
|
|
|
|
|
class BTreeShelveTestCase(BasicShelveTestCase):
|
|
|
|
dbtype = db.DB_BTREE
|
|
|
|
dbflags = db.DB_CREATE
|
|
|
|
|
|
|
|
|
|
|
|
class HashShelveTestCase(BasicShelveTestCase):
|
2002-11-23 07:26:07 -04:00
|
|
|
dbtype = db.DB_HASH
|
2002-11-19 13:47:07 -04:00
|
|
|
dbflags = db.DB_CREATE
|
|
|
|
|
|
|
|
|
|
|
|
class ThreadBTreeShelveTestCase(BasicShelveTestCase):
|
|
|
|
dbtype = db.DB_BTREE
|
|
|
|
dbflags = db.DB_CREATE | db.DB_THREAD
|
|
|
|
|
|
|
|
|
|
|
|
class ThreadHashShelveTestCase(BasicShelveTestCase):
|
2002-11-23 07:26:07 -04:00
|
|
|
dbtype = db.DB_HASH
|
2002-11-19 13:47:07 -04:00
|
|
|
dbflags = db.DB_CREATE | db.DB_THREAD
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
class BasicEnvShelveTestCase(DBShelveTestCase):
|
|
|
|
def do_open(self):
|
|
|
|
self.env = db.DBEnv()
|
2008-05-13 17:57:59 -03:00
|
|
|
self.env.open(self.homeDir,
|
|
|
|
self.envflags | db.DB_INIT_MPOOL | db.DB_CREATE)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
self.filename = os.path.split(self.filename)[1]
|
|
|
|
self.d = dbshelve.DBShelf(self.env)
|
|
|
|
self.d.open(self.filename, self.dbtype, self.dbflags)
|
|
|
|
|
|
|
|
|
|
|
|
def do_close(self):
|
|
|
|
self.d.close()
|
|
|
|
self.env.close()
|
|
|
|
|
|
|
|
|
2008-05-13 17:57:59 -03:00
|
|
|
def setUp(self) :
|
|
|
|
self.homeDir = get_new_environment_path()
|
|
|
|
DBShelveTestCase.setUp(self)
|
|
|
|
|
2002-11-19 13:47:07 -04:00
|
|
|
def tearDown(self):
|
2008-08-31 11:00:51 -03:00
|
|
|
import sys
|
|
|
|
if sys.version_info[0] >= 3 :
|
|
|
|
from test_all import do_proxy_db_py3k
|
|
|
|
do_proxy_db_py3k(self._flag_proxy_db_py3k)
|
2002-11-19 13:47:07 -04:00
|
|
|
self.do_close()
|
2008-03-05 01:10:48 -04:00
|
|
|
test_support.rmtree(self.homeDir)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
class EnvBTreeShelveTestCase(BasicEnvShelveTestCase):
|
|
|
|
envflags = 0
|
|
|
|
dbtype = db.DB_BTREE
|
|
|
|
dbflags = db.DB_CREATE
|
|
|
|
|
|
|
|
|
|
|
|
class EnvHashShelveTestCase(BasicEnvShelveTestCase):
|
|
|
|
envflags = 0
|
2002-11-23 07:26:07 -04:00
|
|
|
dbtype = db.DB_HASH
|
2002-11-19 13:47:07 -04:00
|
|
|
dbflags = db.DB_CREATE
|
|
|
|
|
|
|
|
|
|
|
|
class EnvThreadBTreeShelveTestCase(BasicEnvShelveTestCase):
|
|
|
|
envflags = db.DB_THREAD
|
|
|
|
dbtype = db.DB_BTREE
|
|
|
|
dbflags = db.DB_CREATE | db.DB_THREAD
|
|
|
|
|
|
|
|
|
|
|
|
class EnvThreadHashShelveTestCase(BasicEnvShelveTestCase):
|
|
|
|
envflags = db.DB_THREAD
|
2002-11-23 07:26:07 -04:00
|
|
|
dbtype = db.DB_HASH
|
2002-11-19 13:47:07 -04:00
|
|
|
dbflags = db.DB_CREATE | db.DB_THREAD
|
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2007-10-12 15:44:06 -03:00
|
|
|
# test cases for a DBShelf in a RECNO DB.
|
|
|
|
|
|
|
|
class RecNoShelveTestCase(BasicShelveTestCase):
|
|
|
|
dbtype = db.DB_RECNO
|
|
|
|
dbflags = db.DB_CREATE
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
BasicShelveTestCase.setUp(self)
|
|
|
|
|
|
|
|
# pool to assign integer key values out of
|
|
|
|
self.key_pool = list(range(1, 5000))
|
|
|
|
self.key_map = {} # map string keys to the number we gave them
|
|
|
|
self.intkey_map = {} # reverse map of above
|
|
|
|
|
|
|
|
def mk(self, key):
|
|
|
|
if key not in self.key_map:
|
|
|
|
self.key_map[key] = self.key_pool.pop(0)
|
|
|
|
self.intkey_map[self.key_map[key]] = key
|
|
|
|
return self.key_map[key]
|
|
|
|
|
|
|
|
def checkrec(self, intkey, value):
|
|
|
|
key = self.intkey_map[intkey]
|
|
|
|
BasicShelveTestCase.checkrec(self, key, value)
|
|
|
|
|
|
|
|
def test03_append(self):
|
|
|
|
if verbose:
|
|
|
|
print '\n', '-=' * 30
|
|
|
|
print "Running %s.test03_append..." % self.__class__.__name__
|
|
|
|
|
|
|
|
self.d[1] = 'spam'
|
|
|
|
self.d[5] = 'eggs'
|
|
|
|
self.assertEqual(6, self.d.append('spam'))
|
|
|
|
self.assertEqual(7, self.d.append('baked beans'))
|
|
|
|
self.assertEqual('spam', self.d.get(6))
|
|
|
|
self.assertEqual('spam', self.d.get(1))
|
|
|
|
self.assertEqual('baked beans', self.d.get(7))
|
|
|
|
self.assertEqual('eggs', self.d.get(5))
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2002-12-30 16:53:52 -04:00
|
|
|
def test_suite():
|
|
|
|
suite = unittest.TestSuite()
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2002-12-30 16:53:52 -04:00
|
|
|
suite.addTest(unittest.makeSuite(DBShelveTestCase))
|
|
|
|
suite.addTest(unittest.makeSuite(BTreeShelveTestCase))
|
|
|
|
suite.addTest(unittest.makeSuite(HashShelveTestCase))
|
|
|
|
suite.addTest(unittest.makeSuite(ThreadBTreeShelveTestCase))
|
|
|
|
suite.addTest(unittest.makeSuite(ThreadHashShelveTestCase))
|
|
|
|
suite.addTest(unittest.makeSuite(EnvBTreeShelveTestCase))
|
|
|
|
suite.addTest(unittest.makeSuite(EnvHashShelveTestCase))
|
|
|
|
suite.addTest(unittest.makeSuite(EnvThreadBTreeShelveTestCase))
|
|
|
|
suite.addTest(unittest.makeSuite(EnvThreadHashShelveTestCase))
|
2007-10-12 15:44:06 -03:00
|
|
|
suite.addTest(unittest.makeSuite(RecNoShelveTestCase))
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2002-12-30 16:53:52 -04:00
|
|
|
return suite
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2002-12-30 16:53:52 -04:00
|
|
|
unittest.main(defaultTest='test_suite')
|