PageRenderTime 414ms CodeModel.GetById 118ms app.highlight 108ms RepoModel.GetById 103ms app.codeStats 47ms

/Lib/bsddb/test/test_pickle.py

http://unladen-swallow.googlecode.com/
Python | 62 lines | 45 code | 13 blank | 4 comment | 6 complexity | c3f3bbd05d80ccb1f28a385c06c5f5e1 MD5 | raw file
 1
 2import os
 3import pickle
 4try:
 5    import cPickle
 6except ImportError:
 7    cPickle = None
 8import unittest
 9
10from test_all import db, test_support, get_new_environment_path, get_new_database_path
11
12#----------------------------------------------------------------------
13
14class pickleTestCase(unittest.TestCase):
15    """Verify that DBError can be pickled and unpickled"""
16    db_name = 'test-dbobj.db'
17
18    def setUp(self):
19        self.homeDir = get_new_environment_path()
20
21    def tearDown(self):
22        if hasattr(self, 'db'):
23            del self.db
24        if hasattr(self, 'env'):
25            del self.env
26        test_support.rmtree(self.homeDir)
27
28    def _base_test_pickle_DBError(self, pickle):
29        self.env = db.DBEnv()
30        self.env.open(self.homeDir, db.DB_CREATE | db.DB_INIT_MPOOL)
31        self.db = db.DB(self.env)
32        self.db.open(self.db_name, db.DB_HASH, db.DB_CREATE)
33        self.db.put('spam', 'eggs')
34        self.assertEqual(self.db['spam'], 'eggs')
35        try:
36            self.db.put('spam', 'ham', flags=db.DB_NOOVERWRITE)
37        except db.DBError, egg:
38            pickledEgg = pickle.dumps(egg)
39            #print repr(pickledEgg)
40            rottenEgg = pickle.loads(pickledEgg)
41            if rottenEgg.args != egg.args or type(rottenEgg) != type(egg):
42                raise Exception, (rottenEgg, '!=', egg)
43        else:
44            raise Exception, "where's my DBError exception?!?"
45
46        self.db.close()
47        self.env.close()
48
49    def test01_pickle_DBError(self):
50        self._base_test_pickle_DBError(pickle=pickle)
51
52    if cPickle:
53        def test02_cPickle_DBError(self):
54            self._base_test_pickle_DBError(pickle=cPickle)
55
56#----------------------------------------------------------------------
57
58def test_suite():
59    return unittest.makeSuite(pickleTestCase)
60
61if __name__ == '__main__':
62    unittest.main(defaultTest='test_suite')