PyXR

c:\python24\lib \ bsddb \ test \ test_dbobj.py



0001 
0002 import sys, os, string
0003 import unittest
0004 import glob
0005 
0006 try:
0007     # For Pythons w/distutils pybsddb
0008     from bsddb3 import db, dbobj
0009 except ImportError:
0010     # For Python 2.3
0011     from bsddb import db, dbobj
0012 
0013 
0014 #----------------------------------------------------------------------
0015 
0016 class dbobjTestCase(unittest.TestCase):
0017     """Verify that dbobj.DB and dbobj.DBEnv work properly"""
0018     db_home = 'db_home'
0019     db_name = 'test-dbobj.db'
0020 
0021     def setUp(self):
0022         homeDir = os.path.join(os.path.dirname(sys.argv[0]), 'db_home')
0023         self.homeDir = homeDir
0024         try: os.mkdir(homeDir)
0025         except os.error: pass
0026 
0027     def tearDown(self):
0028         if hasattr(self, 'db'):
0029             del self.db
0030         if hasattr(self, 'env'):
0031             del self.env
0032         files = glob.glob(os.path.join(self.homeDir, '*'))
0033         for file in files:
0034             os.remove(file)
0035 
0036     def test01_both(self):
0037         class TestDBEnv(dbobj.DBEnv): pass
0038         class TestDB(dbobj.DB):
0039             def put(self, key, *args, **kwargs):
0040                 key = string.upper(key)
0041                 # call our parent classes put method with an upper case key
0042                 return apply(dbobj.DB.put, (self, key) + args, kwargs)
0043         self.env = TestDBEnv()
0044         self.env.open(self.homeDir, db.DB_CREATE | db.DB_INIT_MPOOL)
0045         self.db = TestDB(self.env)
0046         self.db.open(self.db_name, db.DB_HASH, db.DB_CREATE)
0047         self.db.put('spam', 'eggs')
0048         assert self.db.get('spam') == None, \
0049                "overridden dbobj.DB.put() method failed [1]"
0050         assert self.db.get('SPAM') == 'eggs', \
0051                "overridden dbobj.DB.put() method failed [2]"
0052         self.db.close()
0053         self.env.close()
0054 
0055     def test02_dbobj_dict_interface(self):
0056         self.env = dbobj.DBEnv()
0057         self.env.open(self.homeDir, db.DB_CREATE | db.DB_INIT_MPOOL)
0058         self.db = dbobj.DB(self.env)
0059         self.db.open(self.db_name+'02', db.DB_HASH, db.DB_CREATE)
0060         # __setitem__
0061         self.db['spam'] = 'eggs'
0062         # __len__
0063         assert len(self.db) == 1
0064         # __getitem__
0065         assert self.db['spam'] == 'eggs'
0066         # __del__
0067         del self.db['spam']
0068         assert self.db.get('spam') == None, "dbobj __del__ failed"
0069         self.db.close()
0070         self.env.close()
0071 
0072 #----------------------------------------------------------------------
0073 
0074 def test_suite():
0075     return unittest.makeSuite(dbobjTestCase)
0076 
0077 if __name__ == '__main__':
0078     unittest.main(defaultTest='test_suite')
0079 

Generated by PyXR 0.9.4
SourceForge.net Logo