home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 June / PCWorld_2005-06_cd.bin / software / vyzkuste / firewally / firewally.exe / framework-2.3.exe / test_dbobj.py < prev    next >
Text File  |  2003-12-30  |  3KB  |  79 lines

  1.  
  2. import sys, os, string
  3. import unittest
  4. import glob
  5.  
  6. try:
  7.     # For Python 2.3
  8.     from bsddb import db, dbobj
  9. except ImportError:
  10.     # For earlier Pythons w/distutils pybsddb
  11.     from bsddb3 import db, dbobj
  12.  
  13.  
  14. #----------------------------------------------------------------------
  15.  
  16. class dbobjTestCase(unittest.TestCase):
  17.     """Verify that dbobj.DB and dbobj.DBEnv work properly"""
  18.     db_home = 'db_home'
  19.     db_name = 'test-dbobj.db'
  20.  
  21.     def setUp(self):
  22.         homeDir = os.path.join(os.path.dirname(sys.argv[0]), 'db_home')
  23.         self.homeDir = homeDir
  24.         try: os.mkdir(homeDir)
  25.         except os.error: pass
  26.  
  27.     def tearDown(self):
  28.         if hasattr(self, 'db'):
  29.             del self.db
  30.         if hasattr(self, 'env'):
  31.             del self.env
  32.         files = glob.glob(os.path.join(self.homeDir, '*'))
  33.         for file in files:
  34.             os.remove(file)
  35.  
  36.     def test01_both(self):
  37.         class TestDBEnv(dbobj.DBEnv): pass
  38.         class TestDB(dbobj.DB):
  39.             def put(self, key, *args, **kwargs):
  40.                 key = string.upper(key)
  41.                 # call our parent classes put method with an upper case key
  42.                 return apply(dbobj.DB.put, (self, key) + args, kwargs)
  43.         self.env = TestDBEnv()
  44.         self.env.open(self.homeDir, db.DB_CREATE | db.DB_INIT_MPOOL)
  45.         self.db = TestDB(self.env)
  46.         self.db.open(self.db_name, db.DB_HASH, db.DB_CREATE)
  47.         self.db.put('spam', 'eggs')
  48.         assert self.db.get('spam') == None, \
  49.                "overridden dbobj.DB.put() method failed [1]"
  50.         assert self.db.get('SPAM') == 'eggs', \
  51.                "overridden dbobj.DB.put() method failed [2]"
  52.         self.db.close()
  53.         self.env.close()
  54.  
  55.     def test02_dbobj_dict_interface(self):
  56.         self.env = dbobj.DBEnv()
  57.         self.env.open(self.homeDir, db.DB_CREATE | db.DB_INIT_MPOOL)
  58.         self.db = dbobj.DB(self.env)
  59.         self.db.open(self.db_name+'02', db.DB_HASH, db.DB_CREATE)
  60.         # __setitem__
  61.         self.db['spam'] = 'eggs'
  62.         # __len__
  63.         assert len(self.db) == 1
  64.         # __getitem__
  65.         assert self.db['spam'] == 'eggs'
  66.         # __del__
  67.         del self.db['spam']
  68.         assert self.db.get('spam') == None, "dbobj __del__ failed"
  69.         self.db.close()
  70.         self.env.close()
  71.  
  72. #----------------------------------------------------------------------
  73.  
  74. def test_suite():
  75.     return unittest.makeSuite(dbobjTestCase)
  76.  
  77. if __name__ == '__main__':
  78.     unittest.main(defaultTest='test_suite')
  79.