"""TestCases for exercising a Recno DB.
from pprint
import pprint
from test_all
import verbose
# For Pythons w/distutils pybsddb
letters
= 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
#----------------------------------------------------------------------
class SimpleRecnoTestCase(unittest
.TestCase
):
self
.filename
= tempfile
.mktemp()
if e
.errno
<> errno
.EEXIST
: raise
d
.open(self
.filename
, db
.DB_RECNO
, db
.DB_CREATE
)
assert type(recno
) == type(0)
for recno
in range(1, len(d
)+1):
assert type(data
) == type("")
assert data
== d
.get(recno
)
data
= d
[0] # This should raise a KeyError!?!?!
except db
.DBInvalidArgError
, val
:
assert val
[0] == db
.EINVAL
self
.fail("expected exception")
self
.fail("expected exception")
assert type(keys
) == type([])
assert type(keys
[0]) == type(123)
assert len(keys
) == len(d
)
assert type(items
) == type([])
assert type(items
[0]) == type(())
assert len(items
[0]) == 2
assert type(items
[0][0]) == type(123)
assert type(items
[0][1]) == type("")
assert len(items
) == len(d
)
data
= d
.get_both(26, "z" * 60)
c
.put(-1, "a replacement record", db
.DB_CURRENT
)
assert rec
== (50, "a replacement record")
# test that non-existant key lookups work (and that
# DBC_set_range doesn't have a memleak under valgrind)
rec
= c
.set_range(999999)
# put a record beyond the consecutive end of the recno's
assert d
[100] == "way out there"
self
.fail("expected exception")
except db
.DBKeyEmptyError
, val
:
assert val
[0] == db
.DB_KEYEMPTY
self
.fail("expected exception")
def test02_WithSource(self
):
A Recno file that is given a "backing source file" is essentially a
simple ASCII file. Normally each record is delimited by \n and so is
just a line in the file, but you can set a different record delimiter
source
= os
.path
.join(os
.path
.dirname(sys
.argv
[0]),
'db_home/test_recno.txt')
if not os
.path
.isdir('db_home'):
f
= open(source
, 'w') # create the file
# This is the default value, just checking if both int
d
.set_re_delim('\n') # and char can be used...
d
.open(self
.filename
, db
.DB_RECNO
, db
.DB_CREATE
)
data
= "The quick brown fox jumped over the lazy dog".split()
# get the text from the backing source
text
= open(source
, 'r').read()
assert text
.split('\n') == data
d
.open(self
.filename
, db
.DB_RECNO
)
text
= open(source
, 'r').read()
assert text
.split('\n') == \
"The quick reddish-brown fox jumped over the comatose dog".split()
def test03_FixedLength(self
):
d
.set_re_len(40) # fixed length records, 40 bytes long
d
.set_re_pad('-') # sets the pad character...
d
.set_re_pad(45) # ...test both int and char
d
.open(self
.filename
, db
.DB_RECNO
, db
.DB_CREATE
)
d
.append(x
* 35) # These will be padded
d
.append('.' * 40) # this one will be exact
try: # this one will fail
except db
.DBInvalidArgError
, val
:
assert val
[0] == db
.EINVAL
self
.fail("expected exception")
#----------------------------------------------------------------------
return unittest
.makeSuite(SimpleRecnoTestCase
)
if __name__
== '__main__':
unittest
.main(defaultTest
='test_suite')