Credit Nir Aides for r77288
[python.git] / Lib / bsddb / test / test_compat.py
blob0b0fa7069561e00f8a3bbf099102f00e8fafb3c7
1 """
2 Test cases adapted from the test_bsddb.py module in Python's
3 regression test suite.
4 """
6 import os, string
7 import unittest
9 from test_all import db, hashopen, btopen, rnopen, verbose, \
10 get_new_database_path
13 class CompatibilityTestCase(unittest.TestCase):
14 def setUp(self):
15 self.filename = get_new_database_path()
17 def tearDown(self):
18 try:
19 os.remove(self.filename)
20 except os.error:
21 pass
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()
32 if verbose:
33 print "\nTesting: rnopen"
35 f = rnopen(self.filename, 'c')
36 for x in range(len(data)):
37 f[x+1] = data[x]
39 getTest = (f[1], f[2], f[3])
40 if verbose:
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))
49 f[25] = 'twenty-five'
50 f.close()
51 del f
53 f = rnopen(self.filename, 'w')
54 f[20] = 'twenty'
56 def noRec(f):
57 rec = f[15]
58 self.assertRaises(KeyError, noRec, f)
60 def badKey(f):
61 rec = f['a string']
62 self.assertRaises(TypeError, badKey, f)
64 del f[3]
66 rec = f.first()
67 while rec:
68 if verbose:
69 print rec
70 try:
71 rec = f.next()
72 except KeyError:
73 break
75 f.close()
78 def test04_n_flag(self):
79 f = hashopen(self.filename, 'n')
80 f.close()
83 def do_bthash_test(self, factory, what):
84 if verbose:
85 print '\nTesting: ', what
87 f = factory(self.filename, 'c')
88 if verbose:
89 print 'creation...'
91 # truth test
92 if f:
93 if verbose: print "truth test: true"
94 else:
95 if verbose: print "truth test: false"
97 f['0'] = ''
98 f['a'] = 'Guido'
99 f['b'] = 'van'
100 f['c'] = 'Rossum'
101 f['d'] = 'invented'
102 # 'e' intentionally left out
103 f['f'] = 'Python'
104 if verbose:
105 print '%s %s %s' % (f['a'], f['b'], f['c'])
107 if verbose:
108 print 'key ordering...'
109 start = f.set_location(f.first()[0])
110 if start != ('0', ''):
111 self.fail("incorrect first() result: "+repr(start))
112 while 1:
113 try:
114 rec = f.next()
115 except KeyError:
116 self.assertEqual(rec, f.last(), 'Error, last <> last!')
117 f.previous()
118 break
119 if verbose:
120 print rec
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))
130 else:
131 try:
132 e = f.set_location('e')
133 except KeyError:
134 pass
135 else:
136 self.fail("set_location on non-existent key did not raise KeyError")
138 f.sync()
139 f.close()
140 # truth test
141 try:
142 if f:
143 if verbose: print "truth test: true"
144 else:
145 if verbose: print "truth test: false"
146 except db.DBError:
147 pass
148 else:
149 self.fail("Exception expected")
151 del f
153 if verbose:
154 print 'modification...'
155 f = factory(self.filename, 'w')
156 f['d'] = 'discovered'
158 if verbose:
159 print 'access...'
160 for key in f.keys():
161 word = f[key]
162 if verbose:
163 print word
165 def noRec(f):
166 rec = f['no such key']
167 self.assertRaises(KeyError, noRec, f)
169 def badKey(f):
170 rec = f[15]
171 self.assertRaises(TypeError, badKey, f)
173 f.close()
176 #----------------------------------------------------------------------
179 def test_suite():
180 return unittest.makeSuite(CompatibilityTestCase)
183 if __name__ == '__main__':
184 unittest.main(defaultTest='test_suite')