2
Test cases adapted from the test_bsddb.py module in Python's
10
from test_all import verbose
13
# For Pythons w/distutils pybsddb
14
from bsddb3 import db, hashopen, btopen, rnopen
17
from bsddb import db, hashopen, btopen, rnopen
20
class CompatibilityTestCase(unittest.TestCase):
22
self.filename = tempfile.mktemp()
26
os.remove(self.filename)
31
def test01_btopen(self):
32
self.do_bthash_test(btopen, 'btopen')
34
def test02_hashopen(self):
35
self.do_bthash_test(hashopen, 'hashopen')
37
def test03_rnopen(self):
38
data = string.split("The quick brown fox jumped over the lazy dog.")
40
print "\nTesting: rnopen"
42
f = rnopen(self.filename, 'c')
43
for x in range(len(data)):
46
getTest = (f[1], f[2], f[3])
48
print '%s %s %s' % getTest
50
assert getTest[1] == 'quick', 'data mismatch!'
52
rv = f.set_location(3)
53
if rv != (3, 'brown'):
54
self.fail('recno database set_location failed: '+repr(rv))
60
f = rnopen(self.filename, 'w')
65
self.assertRaises(KeyError, noRec, f)
69
self.assertRaises(TypeError, badKey, f)
85
def test04_n_flag(self):
86
f = hashopen(self.filename, 'n')
90
def do_bthash_test(self, factory, what):
92
print '\nTesting: ', what
94
f = factory(self.filename, 'c')
100
if verbose: print "truth test: true"
102
if verbose: print "truth test: false"
109
# 'e' intentionally left out
112
print '%s %s %s' % (f['a'], f['b'], f['c'])
115
print 'key ordering...'
116
start = f.set_location(f.first()[0])
117
if start != ('0', ''):
118
self.fail("incorrect first() result: "+repr(start))
123
assert rec == f.last(), 'Error, last <> last!'
129
assert f.has_key('f'), 'Error, missing key!'
131
# test that set_location() returns the next nearest key, value
132
# on btree databases and raises KeyError on others.
133
if factory == btopen:
134
e = f.set_location('e')
135
if e != ('f', 'Python'):
136
self.fail('wrong key,value returned: '+repr(e))
139
e = f.set_location('e')
143
self.fail("set_location on non-existant key did not raise KeyError")
150
if verbose: print "truth test: true"
152
if verbose: print "truth test: false"
156
self.fail("Exception expected")
161
print 'modification...'
162
f = factory(self.filename, 'w')
163
f['d'] = 'discovered'
173
rec = f['no such key']
174
self.assertRaises(KeyError, noRec, f)
178
self.assertRaises(TypeError, badKey, f)
183
#----------------------------------------------------------------------
187
return unittest.makeSuite(CompatibilityTestCase)
190
if __name__ == '__main__':
191
unittest.main(defaultTest='test_suite')