2 Test cases adapted from the test_bsddb.py module in Python's
9 from test_all
import db
, hashopen
, btopen
, rnopen
, verbose
, \
13 class CompatibilityTestCase(unittest
.TestCase
):
15 self
.filename
= get_new_database_path()
19 os
.remove(self
.filename
)
24 def test01_btopen(self
):
25 self
.do_bthash_test(btopen
, 'btopen')
27 def test02_hashopen(self
):
28 self
.do_bthash_test(hashopen
, 'hashopen')
30 def test03_rnopen(self
):
31 data
= "The quick brown fox jumped over the lazy dog.".split()
33 print "\nTesting: rnopen"
35 f
= rnopen(self
.filename
, 'c')
36 for x
in range(len(data
)):
39 getTest
= (f
[1], f
[2], f
[3])
41 print '%s %s %s' % getTest
43 self
.assertEqual(getTest
[1], 'quick', 'data mismatch!')
45 rv
= f
.set_location(3)
46 if rv
!= (3, 'brown'):
47 self
.fail('recno database set_location failed: '+repr(rv
))
53 f
= rnopen(self
.filename
, 'w')
58 self
.assertRaises(KeyError, noRec
, f
)
62 self
.assertRaises(TypeError, badKey
, f
)
78 def test04_n_flag(self
):
79 f
= hashopen(self
.filename
, 'n')
83 def do_bthash_test(self
, factory
, what
):
85 print '\nTesting: ', what
87 f
= factory(self
.filename
, 'c')
93 if verbose
: print "truth test: true"
95 if verbose
: print "truth test: false"
102 # 'e' intentionally left out
105 print '%s %s %s' % (f
['a'], f
['b'], f
['c'])
108 print 'key ordering...'
109 start
= f
.set_location(f
.first()[0])
110 if start
!= ('0', ''):
111 self
.fail("incorrect first() result: "+repr(start
))
116 self
.assertEqual(rec
, f
.last(), 'Error, last <> last!')
122 self
.assert_(f
.has_key('f'), 'Error, missing key!')
124 # test that set_location() returns the next nearest key, value
125 # on btree databases and raises KeyError on others.
126 if factory
== btopen
:
127 e
= f
.set_location('e')
128 if e
!= ('f', 'Python'):
129 self
.fail('wrong key,value returned: '+repr(e
))
132 e
= f
.set_location('e')
136 self
.fail("set_location on non-existent key did not raise KeyError")
143 if verbose
: print "truth test: true"
145 if verbose
: print "truth test: false"
149 self
.fail("Exception expected")
154 print 'modification...'
155 f
= factory(self
.filename
, 'w')
156 f
['d'] = 'discovered'
166 rec
= f
['no such key']
167 self
.assertRaises(KeyError, noRec
, f
)
171 self
.assertRaises(TypeError, badKey
, f
)
176 #----------------------------------------------------------------------
180 return unittest
.makeSuite(CompatibilityTestCase
)
183 if __name__
== '__main__':
184 unittest
.main(defaultTest
='test_suite')