1 """Miscellaneous bsddb module test cases
7 from test_all
import db
, dbshelve
, hashopen
, test_support
, get_new_environment_path
, get_new_database_path
9 #----------------------------------------------------------------------
11 class MiscTestCase(unittest
.TestCase
):
12 if sys
.version_info
< (2, 4) :
13 def assertTrue(self
, expr
, msg
=None):
14 self
.failUnless(expr
, msg
=msg
)
16 def assertFalse(self
, expr
, msg
=None):
17 self
.failIf(expr
, msg
=msg
)
20 self
.filename
= get_new_database_path()
21 self
.homeDir
= get_new_environment_path()
24 test_support
.unlink(self
.filename
)
25 test_support
.rmtree(self
.homeDir
)
27 def test01_badpointer(self
):
28 dbs
= dbshelve
.open(self
.filename
)
30 self
.assertRaises(db
.DBError
, dbs
.get
, "foo")
32 def test02_db_home(self
):
34 # check for crash fixed when db_home is used before open()
35 self
.assert_(env
.db_home
is None)
36 env
.open(self
.homeDir
, db
.DB_CREATE
)
37 if sys
.version_info
[0] < 3 :
38 self
.assertEqual(self
.homeDir
, env
.db_home
)
40 self
.assertEqual(bytes(self
.homeDir
, "ascii"), env
.db_home
)
42 def test03_repr_closed_db(self
):
43 db
= hashopen(self
.filename
)
46 self
.assertEquals(rp
, "{}")
48 def test04_repr_db(self
) :
49 db
= hashopen(self
.filename
)
51 for i
in xrange(100) :
52 db
[repr(i
)] = repr(100*i
)
53 d
[repr(i
)] = repr(100*i
)
55 db
= hashopen(self
.filename
)
57 self
.assertEquals(rp
, repr(d
))
60 # http://sourceforge.net/tracker/index.php?func=detail&aid=1708868&group_id=13900&atid=313900
62 # See the bug report for details.
64 # The problem was that make_key_dbt() was not allocating a copy of
65 # string keys but FREE_DBT() was always being told to free it when the
66 # database was opened with DB_THREAD.
67 def test05_double_free_make_key_dbt(self
):
70 db1
.open(self
.filename
, None, db
.DB_BTREE
,
71 db
.DB_CREATE | db
.DB_THREAD
)
74 t
= curs
.get("/foo", db
.DB_SET
)
75 # double free happened during exit from DBC_get
78 test_support
.unlink(self
.filename
)
80 def test06_key_with_null_bytes(self
):
83 db1
.open(self
.filename
, None, db
.DB_HASH
, db
.DB_CREATE
)
85 db1
['a\x00'] = 'eh zed.'
86 db1
['a\x00a'] = 'eh zed eh?'
87 db1
['aaa'] = 'eh eh eh!'
90 self
.assertEqual(['a', 'a\x00', 'a\x00a', 'aaa'], keys
)
91 self
.assertEqual(db1
['a'], 'eh?')
92 self
.assertEqual(db1
['a\x00'], 'eh zed.')
93 self
.assertEqual(db1
['a\x00a'], 'eh zed eh?')
94 self
.assertEqual(db1
['aaa'], 'eh eh eh!')
97 test_support
.unlink(self
.filename
)
99 def test07_DB_set_flags_persists(self
):
100 if db
.version() < (4,2):
101 # The get_flags API required for this to work is only available
102 # in Berkeley DB >= 4.2
106 db1
.set_flags(db
.DB_DUPSORT
)
107 db1
.open(self
.filename
, db
.DB_HASH
, db
.DB_CREATE
)
110 self
.assertEqual([('a', 'A')], db1
.items())
112 self
.assertEqual([('a', 'A'), ('a', 'Aa')], db1
.items())
115 # no set_flags call, we're testing that it reads and obeys
117 db1
.open(self
.filename
, db
.DB_HASH
)
118 self
.assertEqual([('a', 'A'), ('a', 'Aa')], db1
.items())
119 # if it read the flags right this will replace all values
120 # for key 'a' instead of adding a new one. (as a dict should)
122 self
.assertEqual([('a', 'new A')], db1
.items())
125 test_support
.unlink(self
.filename
)
128 def test08_ExceptionTypes(self
) :
129 self
.assertTrue(issubclass(db
.DBError
, Exception))
130 for i
, j
in db
.__dict
__.items() :
131 if i
.startswith("DB") and i
.endswith("Error") :
132 self
.assertTrue(issubclass(j
, db
.DBError
), msg
=i
)
133 if i
not in ("DBKeyEmptyError", "DBNotFoundError") :
134 self
.assertFalse(issubclass(j
, KeyError), msg
=i
)
136 # This two exceptions have two bases
137 self
.assertTrue(issubclass(db
.DBKeyEmptyError
, KeyError))
138 self
.assertTrue(issubclass(db
.DBNotFoundError
, KeyError))
141 #----------------------------------------------------------------------
145 return unittest
.makeSuite(MiscTestCase
)
148 if __name__
== '__main__':
149 unittest
.main(defaultTest
='test_suite')