Home | History | Annotate | Download | only in test
      1 """TestCase for reseting File ID.
      2 """
      3 
      4 import os
      5 import shutil
      6 import unittest
      7 
      8 from test_all import db, test_support, get_new_environment_path, get_new_database_path
      9 
     10 class FileidResetTestCase(unittest.TestCase):
     11     def setUp(self):
     12         self.db_path_1 = get_new_database_path()
     13         self.db_path_2 = get_new_database_path()
     14         self.db_env_path = get_new_environment_path()
     15 
     16     def test_fileid_reset(self):
     17         # create DB 1
     18         self.db1 = db.DB()
     19         self.db1.open(self.db_path_1, dbtype=db.DB_HASH, flags=(db.DB_CREATE|db.DB_EXCL))
     20         self.db1.put('spam', 'eggs')
     21         self.db1.close()
     22 
     23         shutil.copy(self.db_path_1, self.db_path_2)
     24 
     25         self.db2 = db.DB()
     26         self.db2.open(self.db_path_2, dbtype=db.DB_HASH)
     27         self.db2.put('spam', 'spam')
     28         self.db2.close()
     29 
     30         self.db_env = db.DBEnv()
     31         self.db_env.open(self.db_env_path, db.DB_CREATE|db.DB_INIT_MPOOL)
     32 
     33         # use fileid_reset() here
     34         self.db_env.fileid_reset(self.db_path_2)
     35 
     36         self.db1 = db.DB(self.db_env)
     37         self.db1.open(self.db_path_1, dbtype=db.DB_HASH, flags=db.DB_RDONLY)
     38         self.assertEqual(self.db1.get('spam'), 'eggs')
     39 
     40         self.db2 = db.DB(self.db_env)
     41         self.db2.open(self.db_path_2, dbtype=db.DB_HASH, flags=db.DB_RDONLY)
     42         self.assertEqual(self.db2.get('spam'), 'spam')
     43 
     44         self.db1.close()
     45         self.db2.close()
     46 
     47         self.db_env.close()
     48 
     49     def tearDown(self):
     50         test_support.unlink(self.db_path_1)
     51         test_support.unlink(self.db_path_2)
     52         test_support.rmtree(self.db_env_path)
     53 
     54 def test_suite():
     55     suite = unittest.TestSuite()
     56     if db.version() >= (4, 4):
     57         suite.addTest(unittest.makeSuite(FileidResetTestCase))
     58     return suite
     59 
     60 if __name__ == '__main__':
     61     unittest.main(defaultTest='test_suite')
     62