10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
20a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport os, string
30a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport unittest
40a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
50a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom test_all import db, dbobj, test_support, get_new_environment_path, \
60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        get_new_database_path
70a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#----------------------------------------------------------------------
90a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass dbobjTestCase(unittest.TestCase):
110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    """Verify that dbobj.DB and dbobj.DBEnv work properly"""
120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    db_name = 'test-dbobj.db'
130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def setUp(self):
150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.homeDir = get_new_environment_path()
160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def tearDown(self):
180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if hasattr(self, 'db'):
190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            del self.db
200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if hasattr(self, 'env'):
210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            del self.env
220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        test_support.rmtree(self.homeDir)
230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test01_both(self):
250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class TestDBEnv(dbobj.DBEnv): pass
260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class TestDB(dbobj.DB):
270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def put(self, key, *args, **kwargs):
280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                key = key.upper()
290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # call our parent classes put method with an upper case key
300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return dbobj.DB.put(self, key, *args, **kwargs)
310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.env = TestDBEnv()
320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.env.open(self.homeDir, db.DB_CREATE | db.DB_INIT_MPOOL)
330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.db = TestDB(self.env)
340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.db.open(self.db_name, db.DB_HASH, db.DB_CREATE)
350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.db.put('spam', 'eggs')
360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(self.db.get('spam'), None,
370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao               "overridden dbobj.DB.put() method failed [1]")
380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(self.db.get('SPAM'), 'eggs',
390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao               "overridden dbobj.DB.put() method failed [2]")
400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.db.close()
410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.env.close()
420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test02_dbobj_dict_interface(self):
440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.env = dbobj.DBEnv()
450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.env.open(self.homeDir, db.DB_CREATE | db.DB_INIT_MPOOL)
460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.db = dbobj.DB(self.env)
470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.db.open(self.db_name+'02', db.DB_HASH, db.DB_CREATE)
480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # __setitem__
490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.db['spam'] = 'eggs'
500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # __len__
510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(len(self.db), 1)
520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # __getitem__
530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(self.db['spam'], 'eggs')
540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # __del__
550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        del self.db['spam']
560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(self.db.get('spam'), None, "dbobj __del__ failed")
570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.db.close()
580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.env.close()
590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test03_dbobj_type_before_open(self):
610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Ensure this doesn't cause a segfault.
620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(db.DBInvalidArgError, db.DB().type)
630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#----------------------------------------------------------------------
650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
660a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef test_suite():
670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    return unittest.makeSuite(dbobjTestCase)
680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
690a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif __name__ == '__main__':
700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    unittest.main(defaultTest='test_suite')
71