move sections
[python/dscho.git] / Lib / bsddb / test / test_all.py
blobe9fe618fdbc62bc361fed89e0ef6624ccaaa9b72
1 """Run all test cases.
2 """
4 import sys
5 import os
6 import unittest
7 try:
8 # For Pythons w/distutils pybsddb
9 import bsddb3 as bsddb
10 except ImportError:
11 # For Python 2.3
12 import bsddb
15 if sys.version_info[0] >= 3 :
16 charset = "iso8859-1" # Full 8 bit
18 class logcursor_py3k(object) :
19 def __init__(self, env) :
20 self._logcursor = env.log_cursor()
22 def __getattr__(self, v) :
23 return getattr(self._logcursor, v)
25 def __next__(self) :
26 v = getattr(self._logcursor, "next")()
27 if v is not None :
28 v = (v[0], v[1].decode(charset))
29 return v
31 next = __next__
33 def first(self) :
34 v = self._logcursor.first()
35 if v is not None :
36 v = (v[0], v[1].decode(charset))
37 return v
39 def last(self) :
40 v = self._logcursor.last()
41 if v is not None :
42 v = (v[0], v[1].decode(charset))
43 return v
45 def prev(self) :
46 v = self._logcursor.prev()
47 if v is not None :
48 v = (v[0], v[1].decode(charset))
49 return v
51 def current(self) :
52 v = self._logcursor.current()
53 if v is not None :
54 v = (v[0], v[1].decode(charset))
55 return v
57 def set(self, lsn) :
58 v = self._logcursor.set(lsn)
59 if v is not None :
60 v = (v[0], v[1].decode(charset))
61 return v
63 class cursor_py3k(object) :
64 def __init__(self, db, *args, **kwargs) :
65 self._dbcursor = db.cursor(*args, **kwargs)
67 def __getattr__(self, v) :
68 return getattr(self._dbcursor, v)
70 def _fix(self, v) :
71 if v is None : return None
72 key, value = v
73 if isinstance(key, bytes) :
74 key = key.decode(charset)
75 return (key, value.decode(charset))
77 def __next__(self) :
78 v = getattr(self._dbcursor, "next")()
79 return self._fix(v)
81 next = __next__
83 def previous(self) :
84 v = self._dbcursor.previous()
85 return self._fix(v)
87 def last(self) :
88 v = self._dbcursor.last()
89 return self._fix(v)
91 def set(self, k) :
92 if isinstance(k, str) :
93 k = bytes(k, charset)
94 v = self._dbcursor.set(k)
95 return self._fix(v)
97 def set_recno(self, num) :
98 v = self._dbcursor.set_recno(num)
99 return self._fix(v)
101 def set_range(self, k, dlen=-1, doff=-1) :
102 if isinstance(k, str) :
103 k = bytes(k, charset)
104 v = self._dbcursor.set_range(k, dlen=dlen, doff=doff)
105 return self._fix(v)
107 def dup(self, flags=0) :
108 cursor = self._dbcursor.dup(flags)
109 return dup_cursor_py3k(cursor)
111 def next_dup(self) :
112 v = self._dbcursor.next_dup()
113 return self._fix(v)
115 def next_nodup(self) :
116 v = self._dbcursor.next_nodup()
117 return self._fix(v)
119 def put(self, key, data, flags=0, dlen=-1, doff=-1) :
120 if isinstance(key, str) :
121 key = bytes(key, charset)
122 if isinstance(data, str) :
123 value = bytes(data, charset)
124 return self._dbcursor.put(key, data, flags=flags, dlen=dlen,
125 doff=doff)
127 def current(self, flags=0, dlen=-1, doff=-1) :
128 v = self._dbcursor.current(flags=flags, dlen=dlen, doff=doff)
129 return self._fix(v)
131 def first(self) :
132 v = self._dbcursor.first()
133 return self._fix(v)
135 def pget(self, key=None, data=None, flags=0) :
136 # Incorrect because key can be a bare number,
137 # but enough to pass testsuite
138 if isinstance(key, int) and (data is None) and (flags == 0) :
139 flags = key
140 key = None
141 if isinstance(key, str) :
142 key = bytes(key, charset)
143 if isinstance(data, int) and (flags==0) :
144 flags = data
145 data = None
146 if isinstance(data, str) :
147 data = bytes(data, charset)
148 v=self._dbcursor.pget(key=key, data=data, flags=flags)
149 if v is not None :
150 v1, v2, v3 = v
151 if isinstance(v1, bytes) :
152 v1 = v1.decode(charset)
153 if isinstance(v2, bytes) :
154 v2 = v2.decode(charset)
156 v = (v1, v2, v3.decode(charset))
158 return v
160 def join_item(self) :
161 v = self._dbcursor.join_item()
162 if v is not None :
163 v = v.decode(charset)
164 return v
166 def get(self, *args, **kwargs) :
167 l = len(args)
168 if l == 2 :
169 k, f = args
170 if isinstance(k, str) :
171 k = bytes(k, "iso8859-1")
172 args = (k, f)
173 elif l == 3 :
174 k, d, f = args
175 if isinstance(k, str) :
176 k = bytes(k, charset)
177 if isinstance(d, str) :
178 d = bytes(d, charset)
179 args =(k, d, f)
181 v = self._dbcursor.get(*args, **kwargs)
182 if v is not None :
183 k, v = v
184 if isinstance(k, bytes) :
185 k = k.decode(charset)
186 v = (k, v.decode(charset))
187 return v
189 def get_both(self, key, value) :
190 if isinstance(key, str) :
191 key = bytes(key, charset)
192 if isinstance(value, str) :
193 value = bytes(value, charset)
194 v=self._dbcursor.get_both(key, value)
195 return self._fix(v)
197 class dup_cursor_py3k(cursor_py3k) :
198 def __init__(self, dbcursor) :
199 self._dbcursor = dbcursor
201 class DB_py3k(object) :
202 def __init__(self, *args, **kwargs) :
203 args2=[]
204 for i in args :
205 if isinstance(i, DBEnv_py3k) :
206 i = i._dbenv
207 args2.append(i)
208 args = tuple(args2)
209 for k, v in kwargs.items() :
210 if isinstance(v, DBEnv_py3k) :
211 kwargs[k] = v._dbenv
213 self._db = bsddb._db.DB_orig(*args, **kwargs)
215 def __contains__(self, k) :
216 if isinstance(k, str) :
217 k = bytes(k, charset)
218 return getattr(self._db, "has_key")(k)
220 def __getitem__(self, k) :
221 if isinstance(k, str) :
222 k = bytes(k, charset)
223 v = self._db[k]
224 if v is not None :
225 v = v.decode(charset)
226 return v
228 def __setitem__(self, k, v) :
229 if isinstance(k, str) :
230 k = bytes(k, charset)
231 if isinstance(v, str) :
232 v = bytes(v, charset)
233 self._db[k] = v
235 def __delitem__(self, k) :
236 if isinstance(k, str) :
237 k = bytes(k, charset)
238 del self._db[k]
240 def __getattr__(self, v) :
241 return getattr(self._db, v)
243 def __len__(self) :
244 return len(self._db)
246 def has_key(self, k, txn=None) :
247 if isinstance(k, str) :
248 k = bytes(k, charset)
249 return self._db.has_key(k, txn=txn)
251 def set_re_delim(self, c) :
252 if isinstance(c, str) : # We can use a numeric value byte too
253 c = bytes(c, charset)
254 return self._db.set_re_delim(c)
256 def set_re_pad(self, c) :
257 if isinstance(c, str) : # We can use a numeric value byte too
258 c = bytes(c, charset)
259 return self._db.set_re_pad(c)
261 def get_re_source(self) :
262 source = self._db.get_re_source()
263 return source.decode(charset)
265 def put(self, key, data, txn=None, flags=0, dlen=-1, doff=-1) :
266 if isinstance(key, str) :
267 key = bytes(key, charset)
268 if isinstance(data, str) :
269 value = bytes(data, charset)
270 return self._db.put(key, data, flags=flags, txn=txn, dlen=dlen,
271 doff=doff)
273 def append(self, value, txn=None) :
274 if isinstance(value, str) :
275 value = bytes(value, charset)
276 return self._db.append(value, txn=txn)
278 def get_size(self, key) :
279 if isinstance(key, str) :
280 key = bytes(key, charset)
281 return self._db.get_size(key)
283 def exists(self, key, *args, **kwargs) :
284 if isinstance(key, str) :
285 key = bytes(key, charset)
286 return self._db.exists(key, *args, **kwargs)
288 def get(self, key, default="MagicCookie", txn=None, flags=0, dlen=-1, doff=-1) :
289 if isinstance(key, str) :
290 key = bytes(key, charset)
291 if default != "MagicCookie" : # Magic for 'test_get_none.py'
292 v=self._db.get(key, default=default, txn=txn, flags=flags,
293 dlen=dlen, doff=doff)
294 else :
295 v=self._db.get(key, txn=txn, flags=flags,
296 dlen=dlen, doff=doff)
297 if (v is not None) and isinstance(v, bytes) :
298 v = v.decode(charset)
299 return v
301 def pget(self, key, txn=None) :
302 if isinstance(key, str) :
303 key = bytes(key, charset)
304 v=self._db.pget(key, txn=txn)
305 if v is not None :
306 v1, v2 = v
307 if isinstance(v1, bytes) :
308 v1 = v1.decode(charset)
310 v = (v1, v2.decode(charset))
311 return v
313 def get_both(self, key, value, txn=None, flags=0) :
314 if isinstance(key, str) :
315 key = bytes(key, charset)
316 if isinstance(value, str) :
317 value = bytes(value, charset)
318 v=self._db.get_both(key, value, txn=txn, flags=flags)
319 if v is not None :
320 v = v.decode(charset)
321 return v
323 def delete(self, key, txn=None) :
324 if isinstance(key, str) :
325 key = bytes(key, charset)
326 return self._db.delete(key, txn=txn)
328 def keys(self) :
329 k = self._db.keys()
330 if len(k) and isinstance(k[0], bytes) :
331 return [i.decode(charset) for i in self._db.keys()]
332 else :
333 return k
335 def items(self) :
336 data = self._db.items()
337 if not len(data) : return data
338 data2 = []
339 for k, v in data :
340 if isinstance(k, bytes) :
341 k = k.decode(charset)
342 data2.append((k, v.decode(charset)))
343 return data2
345 def associate(self, secondarydb, callback, flags=0, txn=None) :
346 class associate_callback(object) :
347 def __init__(self, callback) :
348 self._callback = callback
350 def callback(self, key, data) :
351 if isinstance(key, str) :
352 key = key.decode(charset)
353 data = data.decode(charset)
354 key = self._callback(key, data)
355 if (key != bsddb._db.DB_DONOTINDEX) :
356 if isinstance(key, str) :
357 key = bytes(key, charset)
358 elif isinstance(key, list) :
359 key2 = []
360 for i in key :
361 if isinstance(i, str) :
362 i = bytes(i, charset)
363 key2.append(i)
364 key = key2
365 return key
367 return self._db.associate(secondarydb._db,
368 associate_callback(callback).callback, flags=flags,
369 txn=txn)
371 def cursor(self, txn=None, flags=0) :
372 return cursor_py3k(self._db, txn=txn, flags=flags)
374 def join(self, cursor_list) :
375 cursor_list = [i._dbcursor for i in cursor_list]
376 return dup_cursor_py3k(self._db.join(cursor_list))
378 class DBEnv_py3k(object) :
379 def __init__(self, *args, **kwargs) :
380 self._dbenv = bsddb._db.DBEnv_orig(*args, **kwargs)
382 def __getattr__(self, v) :
383 return getattr(self._dbenv, v)
385 def log_cursor(self, flags=0) :
386 return logcursor_py3k(self._dbenv)
388 def get_lg_dir(self) :
389 return self._dbenv.get_lg_dir().decode(charset)
391 def get_tmp_dir(self) :
392 return self._dbenv.get_tmp_dir().decode(charset)
394 def get_data_dirs(self) :
395 # Have to use a list comprehension and not
396 # generators, because we are supporting Python 2.3.
397 return tuple(
398 [i.decode(charset) for i in self._dbenv.get_data_dirs()])
400 class DBSequence_py3k(object) :
401 def __init__(self, db, *args, **kwargs) :
402 self._db=db
403 self._dbsequence = bsddb._db.DBSequence_orig(db._db, *args, **kwargs)
405 def __getattr__(self, v) :
406 return getattr(self._dbsequence, v)
408 def open(self, key, *args, **kwargs) :
409 return self._dbsequence.open(bytes(key, charset), *args, **kwargs)
411 def get_key(self) :
412 return self._dbsequence.get_key().decode(charset)
414 def get_dbp(self) :
415 return self._db
417 import string
418 string.letters=[chr(i) for i in xrange(65,91)]
420 bsddb._db.DBEnv_orig = bsddb._db.DBEnv
421 bsddb._db.DB_orig = bsddb._db.DB
422 if bsddb.db.version() <= (4, 3) :
423 bsddb._db.DBSequence_orig = None
424 else :
425 bsddb._db.DBSequence_orig = bsddb._db.DBSequence
427 def do_proxy_db_py3k(flag) :
428 flag2 = do_proxy_db_py3k.flag
429 do_proxy_db_py3k.flag = flag
430 if flag :
431 bsddb.DBEnv = bsddb.db.DBEnv = bsddb._db.DBEnv = DBEnv_py3k
432 bsddb.DB = bsddb.db.DB = bsddb._db.DB = DB_py3k
433 bsddb._db.DBSequence = DBSequence_py3k
434 else :
435 bsddb.DBEnv = bsddb.db.DBEnv = bsddb._db.DBEnv = bsddb._db.DBEnv_orig
436 bsddb.DB = bsddb.db.DB = bsddb._db.DB = bsddb._db.DB_orig
437 bsddb._db.DBSequence = bsddb._db.DBSequence_orig
438 return flag2
440 do_proxy_db_py3k.flag = False
441 do_proxy_db_py3k(True)
443 try:
444 # For Pythons w/distutils pybsddb
445 from bsddb3 import db, dbtables, dbutils, dbshelve, \
446 hashopen, btopen, rnopen, dbobj
447 except ImportError:
448 # For Python 2.3
449 from bsddb import db, dbtables, dbutils, dbshelve, \
450 hashopen, btopen, rnopen, dbobj
452 try:
453 from bsddb3 import test_support
454 except ImportError:
455 if sys.version_info[0] < 3 :
456 from test import test_support
457 else :
458 from test import support as test_support
461 try:
462 if sys.version_info[0] < 3 :
463 from threading import Thread, currentThread
464 del Thread, currentThread
465 else :
466 from threading import Thread, current_thread
467 del Thread, current_thread
468 have_threads = True
469 except ImportError:
470 have_threads = False
472 verbose = 0
473 if 'verbose' in sys.argv:
474 verbose = 1
475 sys.argv.remove('verbose')
477 if 'silent' in sys.argv: # take care of old flag, just in case
478 verbose = 0
479 sys.argv.remove('silent')
482 def print_versions():
483 print
484 print '-=' * 38
485 print db.DB_VERSION_STRING
486 print 'bsddb.db.version(): %s' % (db.version(), )
487 print 'bsddb.db.__version__: %s' % db.__version__
488 print 'bsddb.db.cvsid: %s' % db.cvsid
490 # Workaround for allowing generating an EGGs as a ZIP files.
491 suffix="__"
492 print 'py module: %s' % getattr(bsddb, "__file"+suffix)
493 print 'extension module: %s' % getattr(bsddb, "__file"+suffix)
495 print 'python version: %s' % sys.version
496 print 'My pid: %s' % os.getpid()
497 print '-=' * 38
500 def get_new_path(name) :
501 get_new_path.mutex.acquire()
502 try :
503 import os
504 path=os.path.join(get_new_path.prefix,
505 name+"_"+str(os.getpid())+"_"+str(get_new_path.num))
506 get_new_path.num+=1
507 finally :
508 get_new_path.mutex.release()
509 return path
511 def get_new_environment_path() :
512 path=get_new_path("environment")
513 import os
514 try:
515 os.makedirs(path,mode=0700)
516 except os.error:
517 test_support.rmtree(path)
518 os.makedirs(path)
519 return path
521 def get_new_database_path() :
522 path=get_new_path("database")
523 import os
524 if os.path.exists(path) :
525 os.remove(path)
526 return path
529 # This path can be overriden via "set_test_path_prefix()".
530 import os, os.path
531 get_new_path.prefix=os.path.join(os.sep,"tmp","z-Berkeley_DB")
532 get_new_path.num=0
534 def get_test_path_prefix() :
535 return get_new_path.prefix
537 def set_test_path_prefix(path) :
538 get_new_path.prefix=path
540 def remove_test_path_directory() :
541 test_support.rmtree(get_new_path.prefix)
543 if have_threads :
544 import threading
545 get_new_path.mutex=threading.Lock()
546 del threading
547 else :
548 class Lock(object) :
549 def acquire(self) :
550 pass
551 def release(self) :
552 pass
553 get_new_path.mutex=Lock()
554 del Lock
558 class PrintInfoFakeTest(unittest.TestCase):
559 def testPrintVersions(self):
560 print_versions()
563 # This little hack is for when this module is run as main and all the
564 # other modules import it so they will still be able to get the right
565 # verbose setting. It's confusing but it works.
566 if sys.version_info[0] < 3 :
567 import test_all
568 test_all.verbose = verbose
569 else :
570 import sys
571 print >>sys.stderr, "Work to do!"
574 def suite(module_prefix='', timing_check=None):
575 test_modules = [
576 'test_associate',
577 'test_basics',
578 'test_dbenv',
579 'test_db',
580 'test_compare',
581 'test_compat',
582 'test_cursor_pget_bug',
583 'test_dbobj',
584 'test_dbshelve',
585 'test_dbtables',
586 'test_distributed_transactions',
587 'test_early_close',
588 'test_fileid',
589 'test_get_none',
590 'test_join',
591 'test_lock',
592 'test_misc',
593 'test_pickle',
594 'test_queue',
595 'test_recno',
596 'test_replication',
597 'test_sequence',
598 'test_thread',
601 alltests = unittest.TestSuite()
602 for name in test_modules:
603 #module = __import__(name)
604 # Do it this way so that suite may be called externally via
605 # python's Lib/test/test_bsddb3.
606 module = __import__(module_prefix+name, globals(), locals(), name)
608 alltests.addTest(module.test_suite())
609 if timing_check:
610 alltests.addTest(unittest.makeSuite(timing_check))
611 return alltests
614 def test_suite():
615 suite = unittest.TestSuite()
616 suite.addTest(unittest.makeSuite(PrintInfoFakeTest))
617 return suite
620 if __name__ == '__main__':
621 print_versions()
622 unittest.main(defaultTest='suite')