2002-11-19 13:47:07 -04:00
|
|
|
|
2008-02-23 13:40:11 -04:00
|
|
|
import os, string
|
2002-11-19 13:47:07 -04:00
|
|
|
import unittest
|
2007-01-05 11:51:24 -04:00
|
|
|
import tempfile
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2003-01-28 13:20:44 -04:00
|
|
|
try:
|
2003-09-20 21:08:14 -03:00
|
|
|
# For Pythons w/distutils pybsddb
|
|
|
|
from bsddb3 import db, dbobj
|
|
|
|
except ImportError:
|
2003-01-28 13:20:44 -04:00
|
|
|
# For Python 2.3
|
|
|
|
from bsddb import db, dbobj
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2008-03-02 16:00:53 -04:00
|
|
|
try:
|
|
|
|
from bsddb3 import test_support
|
|
|
|
except ImportError:
|
|
|
|
from test import test_support
|
|
|
|
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
class dbobjTestCase(unittest.TestCase):
|
|
|
|
"""Verify that dbobj.DB and dbobj.DBEnv work properly"""
|
|
|
|
db_home = 'db_home'
|
|
|
|
db_name = 'test-dbobj.db'
|
|
|
|
|
|
|
|
def setUp(self):
|
2008-02-24 14:47:03 -04:00
|
|
|
homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid())
|
2002-11-19 13:47:07 -04:00
|
|
|
self.homeDir = homeDir
|
|
|
|
try: os.mkdir(homeDir)
|
|
|
|
except os.error: pass
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
if hasattr(self, 'db'):
|
|
|
|
del self.db
|
|
|
|
if hasattr(self, 'env'):
|
|
|
|
del self.env
|
2008-02-24 14:47:03 -04:00
|
|
|
test_support.rmtree(self.homeDir)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
def test01_both(self):
|
|
|
|
class TestDBEnv(dbobj.DBEnv): pass
|
|
|
|
class TestDB(dbobj.DB):
|
|
|
|
def put(self, key, *args, **kwargs):
|
|
|
|
key = string.upper(key)
|
|
|
|
# call our parent classes put method with an upper case key
|
|
|
|
return apply(dbobj.DB.put, (self, key) + args, kwargs)
|
|
|
|
self.env = TestDBEnv()
|
2002-12-30 16:53:52 -04:00
|
|
|
self.env.open(self.homeDir, db.DB_CREATE | db.DB_INIT_MPOOL)
|
2002-11-19 13:47:07 -04:00
|
|
|
self.db = TestDB(self.env)
|
|
|
|
self.db.open(self.db_name, db.DB_HASH, db.DB_CREATE)
|
|
|
|
self.db.put('spam', 'eggs')
|
2002-12-30 16:53:52 -04:00
|
|
|
assert self.db.get('spam') == None, \
|
|
|
|
"overridden dbobj.DB.put() method failed [1]"
|
|
|
|
assert self.db.get('SPAM') == 'eggs', \
|
|
|
|
"overridden dbobj.DB.put() method failed [2]"
|
2002-11-19 13:47:07 -04:00
|
|
|
self.db.close()
|
|
|
|
self.env.close()
|
|
|
|
|
|
|
|
def test02_dbobj_dict_interface(self):
|
|
|
|
self.env = dbobj.DBEnv()
|
2002-12-30 16:53:52 -04:00
|
|
|
self.env.open(self.homeDir, db.DB_CREATE | db.DB_INIT_MPOOL)
|
2002-11-19 13:47:07 -04:00
|
|
|
self.db = dbobj.DB(self.env)
|
|
|
|
self.db.open(self.db_name+'02', db.DB_HASH, db.DB_CREATE)
|
|
|
|
# __setitem__
|
|
|
|
self.db['spam'] = 'eggs'
|
|
|
|
# __len__
|
|
|
|
assert len(self.db) == 1
|
|
|
|
# __getitem__
|
|
|
|
assert self.db['spam'] == 'eggs'
|
|
|
|
# __del__
|
|
|
|
del self.db['spam']
|
|
|
|
assert self.db.get('spam') == None, "dbobj __del__ failed"
|
|
|
|
self.db.close()
|
|
|
|
self.env.close()
|
|
|
|
|
2006-11-21 01:26:22 -04:00
|
|
|
def test03_dbobj_type_before_open(self):
|
2006-11-21 01:51:51 -04:00
|
|
|
# Ensure this doesn't cause a segfault.
|
|
|
|
self.assertRaises(db.DBInvalidArgError, db.DB().type)
|
2006-11-21 01:26:22 -04:00
|
|
|
|
2002-11-19 13:47:07 -04:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2002-12-30 16:53:52 -04:00
|
|
|
def test_suite():
|
2002-11-19 13:47:07 -04:00
|
|
|
return unittest.makeSuite(dbobjTestCase)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2002-12-30 16:53:52 -04:00
|
|
|
unittest.main(defaultTest='test_suite')
|