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
|
|
|
|
|
2008-08-31 11:00:51 -03:00
|
|
|
from test_all import db, dbobj, test_support, get_new_environment_path, \
|
|
|
|
get_new_database_path
|
2008-03-02 16:00:53 -04:00
|
|
|
|
2002-11-19 13:47:07 -04:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
class dbobjTestCase(unittest.TestCase):
|
|
|
|
"""Verify that dbobj.DB and dbobj.DBEnv work properly"""
|
|
|
|
db_name = 'test-dbobj.db'
|
|
|
|
|
|
|
|
def setUp(self):
|
2008-05-13 17:57:59 -03:00
|
|
|
self.homeDir = get_new_environment_path()
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
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):
|
2008-08-31 11:00:51 -03:00
|
|
|
key = key.upper()
|
2002-11-19 13:47:07 -04:00
|
|
|
# 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')
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(self.db.get('spam'), None,
|
|
|
|
"overridden dbobj.DB.put() method failed [1]")
|
|
|
|
self.assertEqual(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__
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(len(self.db), 1)
|
2002-11-19 13:47:07 -04:00
|
|
|
# __getitem__
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(self.db['spam'], 'eggs')
|
2002-11-19 13:47:07 -04:00
|
|
|
# __del__
|
|
|
|
del self.db['spam']
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(self.db.get('spam'), None, "dbobj __del__ failed")
|
2002-11-19 13:47:07 -04:00
|
|
|
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')
|