1997-01-16 18:04:10 -04:00
|
|
|
#! /usr/bin/env python
|
|
|
|
"""Test script for the bsddb C module
|
|
|
|
Roger E. Masse
|
|
|
|
"""
|
1999-03-24 15:03:01 -04:00
|
|
|
import os
|
1997-01-16 18:04:10 -04:00
|
|
|
import bsddb
|
2001-12-07 12:43:19 -04:00
|
|
|
import dbhash # Just so we know it's imported
|
2002-08-09 13:38:32 -03:00
|
|
|
from test.test_support import verbose, verify, TESTFN
|
1997-01-16 18:04:10 -04:00
|
|
|
|
2002-04-22 23:11:05 -03:00
|
|
|
def test(openmethod, what, ondisk=1):
|
1997-01-16 18:04:10 -04:00
|
|
|
|
|
|
|
if verbose:
|
2002-04-22 23:11:05 -03:00
|
|
|
print '\nTesting: ', what, (ondisk and "on disk" or "in memory")
|
2000-10-23 14:22:08 -03:00
|
|
|
|
2002-04-22 23:11:05 -03:00
|
|
|
if ondisk:
|
2002-08-09 13:38:32 -03:00
|
|
|
fname = TESTFN
|
2002-04-22 23:11:05 -03:00
|
|
|
else:
|
|
|
|
fname = None
|
1997-01-16 18:04:10 -04:00
|
|
|
f = openmethod(fname, 'c')
|
2001-03-21 20:40:23 -04:00
|
|
|
verify(f.keys() == [])
|
1997-01-16 18:04:10 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'creation...'
|
2003-09-12 03:33:37 -03:00
|
|
|
keys = ['0', 'a', 'b', 'c', 'd', 'e', 'f']
|
|
|
|
values = ['', 'Guido', 'van', 'Rossum', 'invented', 'Python']
|
|
|
|
items = zip(keys, values)
|
|
|
|
for k, v in items:
|
|
|
|
f[k] = v
|
|
|
|
|
|
|
|
# test mapping iteration methods
|
|
|
|
from sets import Set
|
|
|
|
def verifyset(s1, s2):
|
|
|
|
verify(Set(s1) == Set(s2))
|
|
|
|
verify(keys, f.keys())
|
|
|
|
verify(values, f.values())
|
|
|
|
verify(items, f.items())
|
|
|
|
verify(keys, f)
|
|
|
|
verify(keys, f.iterkeys())
|
|
|
|
verify(values, f.itervalues())
|
|
|
|
verify(items, f.iteritems())
|
|
|
|
|
1997-01-16 18:04:10 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print '%s %s %s' % (f['a'], f['b'], f['c'])
|
1997-01-16 18:04:10 -04:00
|
|
|
|
|
|
|
if what == 'BTree' :
|
1998-03-26 15:42:58 -04:00
|
|
|
if verbose:
|
|
|
|
print 'key ordering...'
|
|
|
|
f.set_location(f.first()[0])
|
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
rec = f.next()
|
|
|
|
except KeyError:
|
2000-12-12 19:11:42 -04:00
|
|
|
if rec != f.last():
|
|
|
|
print 'Error, last != last!'
|
1998-03-26 15:42:58 -04:00
|
|
|
f.previous()
|
|
|
|
break
|
|
|
|
if verbose:
|
|
|
|
print rec
|
|
|
|
if not f.has_key('a'):
|
|
|
|
print 'Error, missing key!'
|
1997-01-16 18:04:10 -04:00
|
|
|
|
|
|
|
f.sync()
|
|
|
|
f.close()
|
2002-04-22 23:11:05 -03:00
|
|
|
if ondisk:
|
|
|
|
# if we're using an in-memory only db, we can't reopen it
|
|
|
|
# so finish here.
|
|
|
|
if verbose:
|
|
|
|
print 'modification...'
|
|
|
|
f = openmethod(fname, 'w')
|
|
|
|
f['d'] = 'discovered'
|
1997-01-16 18:04:10 -04:00
|
|
|
|
1998-03-26 15:42:58 -04:00
|
|
|
if verbose:
|
2002-04-22 23:11:05 -03:00
|
|
|
print 'access...'
|
|
|
|
for key in f.keys():
|
|
|
|
word = f[key]
|
|
|
|
if verbose:
|
|
|
|
print word
|
1997-01-16 18:04:10 -04:00
|
|
|
|
2002-04-22 23:11:05 -03:00
|
|
|
f.close()
|
|
|
|
try:
|
|
|
|
os.remove(fname)
|
|
|
|
except os.error:
|
|
|
|
pass
|
1997-01-16 18:04:10 -04:00
|
|
|
|
|
|
|
types = [(bsddb.btopen, 'BTree'),
|
1998-03-26 15:42:58 -04:00
|
|
|
(bsddb.hashopen, 'Hash Table'),
|
2002-04-22 23:11:05 -03:00
|
|
|
(bsddb.btopen, 'BTree', 0),
|
|
|
|
(bsddb.hashopen, 'Hash Table', 0),
|
1998-03-26 15:42:58 -04:00
|
|
|
# (bsddb.rnopen,'Record Numbers'), 'put' for RECNO for bsddb 1.85
|
1997-01-16 18:04:10 -04:00
|
|
|
# appears broken... at least on
|
1998-03-26 15:42:58 -04:00
|
|
|
# Solaris Intel - rmasse 1/97
|
|
|
|
]
|
1997-01-16 18:04:10 -04:00
|
|
|
|
|
|
|
for type in types:
|
2002-04-22 23:11:05 -03:00
|
|
|
test(*type)
|