Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | """ |
2 | Test cases adapted from the test_bsddb.py module in Python's | |
3 | regression test suite. | |
4 | """ | |
5 | ||
6 | import sys, os, string | |
7 | import unittest | |
8 | import tempfile | |
9 | ||
10 | from test_all import verbose | |
11 | ||
12 | try: | |
13 | # For Pythons w/distutils pybsddb | |
14 | from bsddb3 import db, hashopen, btopen, rnopen | |
15 | except ImportError: | |
16 | # For Python 2.3 | |
17 | from bsddb import db, hashopen, btopen, rnopen | |
18 | ||
19 | ||
20 | class CompatibilityTestCase(unittest.TestCase): | |
21 | def setUp(self): | |
22 | self.filename = tempfile.mktemp() | |
23 | ||
24 | def tearDown(self): | |
25 | try: | |
26 | os.remove(self.filename) | |
27 | except os.error: | |
28 | pass | |
29 | ||
30 | ||
31 | def test01_btopen(self): | |
32 | self.do_bthash_test(btopen, 'btopen') | |
33 | ||
34 | def test02_hashopen(self): | |
35 | self.do_bthash_test(hashopen, 'hashopen') | |
36 | ||
37 | def test03_rnopen(self): | |
38 | data = string.split("The quick brown fox jumped over the lazy dog.") | |
39 | if verbose: | |
40 | print "\nTesting: rnopen" | |
41 | ||
42 | f = rnopen(self.filename, 'c') | |
43 | for x in range(len(data)): | |
44 | f[x+1] = data[x] | |
45 | ||
46 | getTest = (f[1], f[2], f[3]) | |
47 | if verbose: | |
48 | print '%s %s %s' % getTest | |
49 | ||
50 | assert getTest[1] == 'quick', 'data mismatch!' | |
51 | ||
52 | rv = f.set_location(3) | |
53 | if rv != (3, 'brown'): | |
54 | self.fail('recno database set_location failed: '+repr(rv)) | |
55 | ||
56 | f[25] = 'twenty-five' | |
57 | f.close() | |
58 | del f | |
59 | ||
60 | f = rnopen(self.filename, 'w') | |
61 | f[20] = 'twenty' | |
62 | ||
63 | def noRec(f): | |
64 | rec = f[15] | |
65 | self.assertRaises(KeyError, noRec, f) | |
66 | ||
67 | def badKey(f): | |
68 | rec = f['a string'] | |
69 | self.assertRaises(TypeError, badKey, f) | |
70 | ||
71 | del f[3] | |
72 | ||
73 | rec = f.first() | |
74 | while rec: | |
75 | if verbose: | |
76 | print rec | |
77 | try: | |
78 | rec = f.next() | |
79 | except KeyError: | |
80 | break | |
81 | ||
82 | f.close() | |
83 | ||
84 | ||
85 | def test04_n_flag(self): | |
86 | f = hashopen(self.filename, 'n') | |
87 | f.close() | |
88 | ||
89 | ||
90 | def do_bthash_test(self, factory, what): | |
91 | if verbose: | |
92 | print '\nTesting: ', what | |
93 | ||
94 | f = factory(self.filename, 'c') | |
95 | if verbose: | |
96 | print 'creation...' | |
97 | ||
98 | # truth test | |
99 | if f: | |
100 | if verbose: print "truth test: true" | |
101 | else: | |
102 | if verbose: print "truth test: false" | |
103 | ||
104 | f['0'] = '' | |
105 | f['a'] = 'Guido' | |
106 | f['b'] = 'van' | |
107 | f['c'] = 'Rossum' | |
108 | f['d'] = 'invented' | |
109 | # 'e' intentionally left out | |
110 | f['f'] = 'Python' | |
111 | if verbose: | |
112 | print '%s %s %s' % (f['a'], f['b'], f['c']) | |
113 | ||
114 | if verbose: | |
115 | print 'key ordering...' | |
116 | start = f.set_location(f.first()[0]) | |
117 | if start != ('0', ''): | |
118 | self.fail("incorrect first() result: "+repr(start)) | |
119 | while 1: | |
120 | try: | |
121 | rec = f.next() | |
122 | except KeyError: | |
123 | assert rec == f.last(), 'Error, last <> last!' | |
124 | f.previous() | |
125 | break | |
126 | if verbose: | |
127 | print rec | |
128 | ||
129 | assert f.has_key('f'), 'Error, missing key!' | |
130 | ||
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)) | |
137 | else: | |
138 | try: | |
139 | e = f.set_location('e') | |
140 | except KeyError: | |
141 | pass | |
142 | else: | |
143 | self.fail("set_location on non-existant key did not raise KeyError") | |
144 | ||
145 | f.sync() | |
146 | f.close() | |
147 | # truth test | |
148 | try: | |
149 | if f: | |
150 | if verbose: print "truth test: true" | |
151 | else: | |
152 | if verbose: print "truth test: false" | |
153 | except db.DBError: | |
154 | pass | |
155 | else: | |
156 | self.fail("Exception expected") | |
157 | ||
158 | del f | |
159 | ||
160 | if verbose: | |
161 | print 'modification...' | |
162 | f = factory(self.filename, 'w') | |
163 | f['d'] = 'discovered' | |
164 | ||
165 | if verbose: | |
166 | print 'access...' | |
167 | for key in f.keys(): | |
168 | word = f[key] | |
169 | if verbose: | |
170 | print word | |
171 | ||
172 | def noRec(f): | |
173 | rec = f['no such key'] | |
174 | self.assertRaises(KeyError, noRec, f) | |
175 | ||
176 | def badKey(f): | |
177 | rec = f[15] | |
178 | self.assertRaises(TypeError, badKey, f) | |
179 | ||
180 | f.close() | |
181 | ||
182 | ||
183 | #---------------------------------------------------------------------- | |
184 | ||
185 | ||
186 | def test_suite(): | |
187 | return unittest.makeSuite(CompatibilityTestCase) | |
188 | ||
189 | ||
190 | if __name__ == '__main__': | |
191 | unittest.main(defaultTest='test_suite') |