1 | """
|
---|
2 | Test cases adapted from the test_bsddb.py module in Python's
|
---|
3 | regression test suite.
|
---|
4 | """
|
---|
5 |
|
---|
6 | import os, string
|
---|
7 | import unittest
|
---|
8 |
|
---|
9 | from test_all import db, hashopen, btopen, rnopen, verbose, \
|
---|
10 | get_new_database_path
|
---|
11 |
|
---|
12 |
|
---|
13 | class CompatibilityTestCase(unittest.TestCase):
|
---|
14 | def setUp(self):
|
---|
15 | self.filename = get_new_database_path()
|
---|
16 |
|
---|
17 | def tearDown(self):
|
---|
18 | try:
|
---|
19 | os.remove(self.filename)
|
---|
20 | except os.error:
|
---|
21 | pass
|
---|
22 |
|
---|
23 |
|
---|
24 | def test01_btopen(self):
|
---|
25 | self.do_bthash_test(btopen, 'btopen')
|
---|
26 |
|
---|
27 | def test02_hashopen(self):
|
---|
28 | self.do_bthash_test(hashopen, 'hashopen')
|
---|
29 |
|
---|
30 | def test03_rnopen(self):
|
---|
31 | data = "The quick brown fox jumped over the lazy dog.".split()
|
---|
32 | if verbose:
|
---|
33 | print "\nTesting: rnopen"
|
---|
34 |
|
---|
35 | f = rnopen(self.filename, 'c')
|
---|
36 | for x in range(len(data)):
|
---|
37 | f[x+1] = data[x]
|
---|
38 |
|
---|
39 | getTest = (f[1], f[2], f[3])
|
---|
40 | if verbose:
|
---|
41 | print '%s %s %s' % getTest
|
---|
42 |
|
---|
43 | self.assertEqual(getTest[1], 'quick', 'data mismatch!')
|
---|
44 |
|
---|
45 | rv = f.set_location(3)
|
---|
46 | if rv != (3, 'brown'):
|
---|
47 | self.fail('recno database set_location failed: '+repr(rv))
|
---|
48 |
|
---|
49 | f[25] = 'twenty-five'
|
---|
50 | f.close()
|
---|
51 | del f
|
---|
52 |
|
---|
53 | f = rnopen(self.filename, 'w')
|
---|
54 | f[20] = 'twenty'
|
---|
55 |
|
---|
56 | def noRec(f):
|
---|
57 | rec = f[15]
|
---|
58 | self.assertRaises(KeyError, noRec, f)
|
---|
59 |
|
---|
60 | def badKey(f):
|
---|
61 | rec = f['a string']
|
---|
62 | self.assertRaises(TypeError, badKey, f)
|
---|
63 |
|
---|
64 | del f[3]
|
---|
65 |
|
---|
66 | rec = f.first()
|
---|
67 | while rec:
|
---|
68 | if verbose:
|
---|
69 | print rec
|
---|
70 | try:
|
---|
71 | rec = f.next()
|
---|
72 | except KeyError:
|
---|
73 | break
|
---|
74 |
|
---|
75 | f.close()
|
---|
76 |
|
---|
77 |
|
---|
78 | def test04_n_flag(self):
|
---|
79 | f = hashopen(self.filename, 'n')
|
---|
80 | f.close()
|
---|
81 |
|
---|
82 |
|
---|
83 | def do_bthash_test(self, factory, what):
|
---|
84 | if verbose:
|
---|
85 | print '\nTesting: ', what
|
---|
86 |
|
---|
87 | f = factory(self.filename, 'c')
|
---|
88 | if verbose:
|
---|
89 | print 'creation...'
|
---|
90 |
|
---|
91 | # truth test
|
---|
92 | if f:
|
---|
93 | if verbose: print "truth test: true"
|
---|
94 | else:
|
---|
95 | if verbose: print "truth test: false"
|
---|
96 |
|
---|
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'])
|
---|
106 |
|
---|
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
|
---|
121 |
|
---|
122 | self.assertTrue(f.has_key('f'), 'Error, missing key!')
|
---|
123 |
|
---|
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")
|
---|
137 |
|
---|
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")
|
---|
150 |
|
---|
151 | del f
|
---|
152 |
|
---|
153 | if verbose:
|
---|
154 | print 'modification...'
|
---|
155 | f = factory(self.filename, 'w')
|
---|
156 | f['d'] = 'discovered'
|
---|
157 |
|
---|
158 | if verbose:
|
---|
159 | print 'access...'
|
---|
160 | for key in f.keys():
|
---|
161 | word = f[key]
|
---|
162 | if verbose:
|
---|
163 | print word
|
---|
164 |
|
---|
165 | def noRec(f):
|
---|
166 | rec = f['no such key']
|
---|
167 | self.assertRaises(KeyError, noRec, f)
|
---|
168 |
|
---|
169 | def badKey(f):
|
---|
170 | rec = f[15]
|
---|
171 | self.assertRaises(TypeError, badKey, f)
|
---|
172 |
|
---|
173 | f.close()
|
---|
174 |
|
---|
175 |
|
---|
176 | #----------------------------------------------------------------------
|
---|
177 |
|
---|
178 |
|
---|
179 | def test_suite():
|
---|
180 | return unittest.makeSuite(CompatibilityTestCase)
|
---|
181 |
|
---|
182 |
|
---|
183 | if __name__ == '__main__':
|
---|
184 | unittest.main(defaultTest='test_suite')
|
---|