2003-01-10 15:28:15 -04:00
|
|
|
"""TestCases for exercising a Recno DB.
|
2002-11-19 13:47:07 -04:00
|
|
|
"""
|
|
|
|
|
2002-12-30 16:53:52 -04:00
|
|
|
import os
|
2007-08-28 05:05:56 -03:00
|
|
|
import shutil
|
2002-12-30 16:53:52 -04:00
|
|
|
import sys
|
2003-01-10 15:28:15 -04:00
|
|
|
import errno
|
2002-11-19 13:47:07 -04:00
|
|
|
import tempfile
|
|
|
|
from pprint import pprint
|
|
|
|
import unittest
|
|
|
|
|
2007-08-28 05:05:56 -03:00
|
|
|
from bsddb.test.test_all import verbose
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2003-01-28 13:20:44 -04:00
|
|
|
try:
|
2003-09-20 21:08:14 -03:00
|
|
|
# For Pythons w/distutils pybsddb
|
|
|
|
from bsddb3 import db
|
|
|
|
except ImportError:
|
2003-01-28 13:20:44 -04:00
|
|
|
# For Python 2.3
|
|
|
|
from bsddb import db
|
|
|
|
|
Merged revisions 61143-61144,61146-61147,61150-61151,61157,61165-61168,61170-61173,61176-61177,61183 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61143 | barry.warsaw | 2008-03-01 03:23:38 +0100 (Sat, 01 Mar 2008) | 2 lines
Bump to version 2.6a1
........
r61144 | barry.warsaw | 2008-03-01 03:26:42 +0100 (Sat, 01 Mar 2008) | 1 line
bump idle version number
........
r61146 | fred.drake | 2008-03-01 03:45:07 +0100 (Sat, 01 Mar 2008) | 2 lines
fix typo
........
r61147 | barry.warsaw | 2008-03-01 03:53:36 +0100 (Sat, 01 Mar 2008) | 1 line
Add date to NEWS
........
r61150 | barry.warsaw | 2008-03-01 04:00:52 +0100 (Sat, 01 Mar 2008) | 1 line
Give IDLE a release date
........
r61151 | barry.warsaw | 2008-03-01 04:15:20 +0100 (Sat, 01 Mar 2008) | 1 line
More copyright year and version number bumps
........
r61157 | barry.warsaw | 2008-03-01 18:11:41 +0100 (Sat, 01 Mar 2008) | 2 lines
Set things up for 2.6a2.
........
r61165 | georg.brandl | 2008-03-02 07:28:16 +0100 (Sun, 02 Mar 2008) | 2 lines
It's 2.6 now.
........
r61166 | georg.brandl | 2008-03-02 07:32:32 +0100 (Sun, 02 Mar 2008) | 2 lines
Update year.
........
r61167 | georg.brandl | 2008-03-02 07:44:08 +0100 (Sun, 02 Mar 2008) | 2 lines
Make patchlevel print out the release if called as a script.
........
r61168 | georg.brandl | 2008-03-02 07:45:40 +0100 (Sun, 02 Mar 2008) | 2 lines
New default basename for HTML help files.
........
r61170 | raymond.hettinger | 2008-03-02 11:59:31 +0100 (Sun, 02 Mar 2008) | 1 line
Finish-up docs for combinations() and permutations() in itertools.
........
r61171 | raymond.hettinger | 2008-03-02 12:17:51 +0100 (Sun, 02 Mar 2008) | 1 line
Tighten example code.
........
r61172 | raymond.hettinger | 2008-03-02 12:57:16 +0100 (Sun, 02 Mar 2008) | 1 line
Simplify code for itertools.product().
........
r61173 | raymond.hettinger | 2008-03-02 13:02:19 +0100 (Sun, 02 Mar 2008) | 1 line
Handle 0-tuples which can be singletons.
........
r61176 | georg.brandl | 2008-03-02 14:41:39 +0100 (Sun, 02 Mar 2008) | 2 lines
Make clear that the constants are strings.
........
r61177 | georg.brandl | 2008-03-02 15:15:04 +0100 (Sun, 02 Mar 2008) | 2 lines
Fix factual error.
........
r61183 | gregory.p.smith | 2008-03-02 21:00:53 +0100 (Sun, 02 Mar 2008) | 4 lines
Modify import of test_support so that the code can also be used with a
stand alone distribution of bsddb that includes its own small copy of
test_support for the needed functionality on older pythons.
........
2008-03-02 18:46:37 -04:00
|
|
|
try:
|
|
|
|
from bsddb3 import test_support
|
|
|
|
except ImportError:
|
2008-05-20 18:35:26 -03:00
|
|
|
from test import support as test_support
|
Merged revisions 61143-61144,61146-61147,61150-61151,61157,61165-61168,61170-61173,61176-61177,61183 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61143 | barry.warsaw | 2008-03-01 03:23:38 +0100 (Sat, 01 Mar 2008) | 2 lines
Bump to version 2.6a1
........
r61144 | barry.warsaw | 2008-03-01 03:26:42 +0100 (Sat, 01 Mar 2008) | 1 line
bump idle version number
........
r61146 | fred.drake | 2008-03-01 03:45:07 +0100 (Sat, 01 Mar 2008) | 2 lines
fix typo
........
r61147 | barry.warsaw | 2008-03-01 03:53:36 +0100 (Sat, 01 Mar 2008) | 1 line
Add date to NEWS
........
r61150 | barry.warsaw | 2008-03-01 04:00:52 +0100 (Sat, 01 Mar 2008) | 1 line
Give IDLE a release date
........
r61151 | barry.warsaw | 2008-03-01 04:15:20 +0100 (Sat, 01 Mar 2008) | 1 line
More copyright year and version number bumps
........
r61157 | barry.warsaw | 2008-03-01 18:11:41 +0100 (Sat, 01 Mar 2008) | 2 lines
Set things up for 2.6a2.
........
r61165 | georg.brandl | 2008-03-02 07:28:16 +0100 (Sun, 02 Mar 2008) | 2 lines
It's 2.6 now.
........
r61166 | georg.brandl | 2008-03-02 07:32:32 +0100 (Sun, 02 Mar 2008) | 2 lines
Update year.
........
r61167 | georg.brandl | 2008-03-02 07:44:08 +0100 (Sun, 02 Mar 2008) | 2 lines
Make patchlevel print out the release if called as a script.
........
r61168 | georg.brandl | 2008-03-02 07:45:40 +0100 (Sun, 02 Mar 2008) | 2 lines
New default basename for HTML help files.
........
r61170 | raymond.hettinger | 2008-03-02 11:59:31 +0100 (Sun, 02 Mar 2008) | 1 line
Finish-up docs for combinations() and permutations() in itertools.
........
r61171 | raymond.hettinger | 2008-03-02 12:17:51 +0100 (Sun, 02 Mar 2008) | 1 line
Tighten example code.
........
r61172 | raymond.hettinger | 2008-03-02 12:57:16 +0100 (Sun, 02 Mar 2008) | 1 line
Simplify code for itertools.product().
........
r61173 | raymond.hettinger | 2008-03-02 13:02:19 +0100 (Sun, 02 Mar 2008) | 1 line
Handle 0-tuples which can be singletons.
........
r61176 | georg.brandl | 2008-03-02 14:41:39 +0100 (Sun, 02 Mar 2008) | 2 lines
Make clear that the constants are strings.
........
r61177 | georg.brandl | 2008-03-02 15:15:04 +0100 (Sun, 02 Mar 2008) | 2 lines
Fix factual error.
........
r61183 | gregory.p.smith | 2008-03-02 21:00:53 +0100 (Sun, 02 Mar 2008) | 4 lines
Modify import of test_support so that the code can also be used with a
stand alone distribution of bsddb that includes its own small copy of
test_support for the needed functionality on older pythons.
........
2008-03-02 18:46:37 -04:00
|
|
|
|
2003-01-10 15:28:15 -04:00
|
|
|
letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
|
|
|
|
|
|
|
2002-11-19 13:47:07 -04:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
class SimpleRecnoTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.filename = tempfile.mktemp()
|
2008-02-25 08:39:23 -04:00
|
|
|
self.homeDir = None
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
def tearDown(self):
|
2008-02-25 08:39:23 -04:00
|
|
|
test_support.unlink(self.filename)
|
|
|
|
if self.homeDir:
|
|
|
|
test_support.rmtree(self.homeDir)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
def test01_basic(self):
|
|
|
|
d = db.DB()
|
2005-06-04 03:46:59 -03:00
|
|
|
|
2005-06-08 01:35:50 -03:00
|
|
|
get_returns_none = d.set_get_returns_none(2)
|
|
|
|
d.set_get_returns_none(get_returns_none)
|
2005-06-04 03:46:59 -03:00
|
|
|
|
2002-11-19 13:47:07 -04:00
|
|
|
d.open(self.filename, db.DB_RECNO, db.DB_CREATE)
|
|
|
|
|
2003-01-10 15:28:15 -04:00
|
|
|
for x in letters:
|
2007-08-29 02:52:49 -03:00
|
|
|
recno = d.append(x.encode('ascii') * 60)
|
2002-11-19 13:47:07 -04:00
|
|
|
assert type(recno) == type(0)
|
|
|
|
assert recno >= 1
|
|
|
|
if verbose:
|
2007-02-09 01:37:30 -04:00
|
|
|
print(recno, end=' ')
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2007-02-09 01:37:30 -04:00
|
|
|
if verbose: print()
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
stat = d.stat()
|
|
|
|
if verbose:
|
|
|
|
pprint(stat)
|
|
|
|
|
|
|
|
for recno in range(1, len(d)+1):
|
|
|
|
data = d[recno]
|
|
|
|
if verbose:
|
2007-02-09 01:37:30 -04:00
|
|
|
print(data)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2007-08-08 19:08:30 -03:00
|
|
|
assert type(data) == bytes
|
2002-11-19 13:47:07 -04:00
|
|
|
assert data == d.get(recno)
|
|
|
|
|
|
|
|
try:
|
|
|
|
data = d[0] # This should raise a KeyError!?!?!
|
2007-01-10 12:19:56 -04:00
|
|
|
except db.DBInvalidArgError as val:
|
2007-03-28 18:02:43 -03:00
|
|
|
assert val.args[0] == db.EINVAL
|
2007-02-09 01:37:30 -04:00
|
|
|
if verbose: print(val)
|
2002-11-19 13:47:07 -04:00
|
|
|
else:
|
|
|
|
self.fail("expected exception")
|
|
|
|
|
2005-06-08 01:35:50 -03:00
|
|
|
# test that has_key raises DB exceptions (fixed in pybsddb 4.3.2)
|
|
|
|
try:
|
|
|
|
d.has_key(0)
|
2007-01-10 12:19:56 -04:00
|
|
|
except db.DBError as val:
|
2005-06-08 01:35:50 -03:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.fail("has_key did not raise a proper exception")
|
2005-06-04 03:46:59 -03:00
|
|
|
|
2002-11-19 13:47:07 -04:00
|
|
|
try:
|
|
|
|
data = d[100]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.fail("expected exception")
|
|
|
|
|
2005-06-08 01:35:50 -03:00
|
|
|
try:
|
|
|
|
data = d.get(100)
|
2007-01-10 12:19:56 -04:00
|
|
|
except db.DBNotFoundError as val:
|
2005-06-08 01:35:50 -03:00
|
|
|
if get_returns_none:
|
|
|
|
self.fail("unexpected exception")
|
|
|
|
else:
|
|
|
|
assert data == None
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
keys = d.keys()
|
|
|
|
if verbose:
|
2007-02-09 01:37:30 -04:00
|
|
|
print(keys)
|
2002-11-19 13:47:07 -04:00
|
|
|
assert type(keys) == type([])
|
|
|
|
assert type(keys[0]) == type(123)
|
|
|
|
assert len(keys) == len(d)
|
|
|
|
|
|
|
|
items = d.items()
|
|
|
|
if verbose:
|
|
|
|
pprint(items)
|
|
|
|
assert type(items) == type([])
|
|
|
|
assert type(items[0]) == type(())
|
|
|
|
assert len(items[0]) == 2
|
|
|
|
assert type(items[0][0]) == type(123)
|
2007-08-08 19:08:30 -03:00
|
|
|
assert type(items[0][1]) == bytes
|
2002-11-19 13:47:07 -04:00
|
|
|
assert len(items) == len(d)
|
|
|
|
|
|
|
|
assert d.has_key(25)
|
|
|
|
|
|
|
|
del d[25]
|
|
|
|
assert not d.has_key(25)
|
|
|
|
|
|
|
|
d.delete(13)
|
|
|
|
assert not d.has_key(13)
|
|
|
|
|
2007-08-08 19:08:30 -03:00
|
|
|
data = d.get_both(26, b"z" * 60)
|
|
|
|
assert data == b"z" * 60, 'was %r' % data
|
2002-11-19 13:47:07 -04:00
|
|
|
if verbose:
|
2007-02-09 01:37:30 -04:00
|
|
|
print(data)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
fd = d.fd()
|
|
|
|
if verbose:
|
2007-02-09 01:37:30 -04:00
|
|
|
print(fd)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
c = d.cursor()
|
|
|
|
rec = c.first()
|
|
|
|
while rec:
|
|
|
|
if verbose:
|
2007-02-09 01:37:30 -04:00
|
|
|
print(rec)
|
2002-11-19 13:47:07 -04:00
|
|
|
rec = c.next()
|
|
|
|
|
|
|
|
c.set(50)
|
|
|
|
rec = c.current()
|
|
|
|
if verbose:
|
2007-02-09 01:37:30 -04:00
|
|
|
print(rec)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2007-08-08 19:08:30 -03:00
|
|
|
c.put(-1, b"a replacement record", db.DB_CURRENT)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
c.set(50)
|
|
|
|
rec = c.current()
|
2007-08-08 19:08:30 -03:00
|
|
|
assert rec == (50, b"a replacement record")
|
2002-11-19 13:47:07 -04:00
|
|
|
if verbose:
|
2007-02-09 01:37:30 -04:00
|
|
|
print(rec)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
rec = c.set_range(30)
|
|
|
|
if verbose:
|
2007-02-09 01:37:30 -04:00
|
|
|
print(rec)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2004-06-27 20:32:34 -03:00
|
|
|
# test that non-existant key lookups work (and that
|
|
|
|
# DBC_set_range doesn't have a memleak under valgrind)
|
|
|
|
rec = c.set_range(999999)
|
|
|
|
assert rec == None
|
|
|
|
if verbose:
|
2007-02-09 01:37:30 -04:00
|
|
|
print(rec)
|
2004-06-27 20:32:34 -03:00
|
|
|
|
2002-11-19 13:47:07 -04:00
|
|
|
c.close()
|
|
|
|
d.close()
|
|
|
|
|
|
|
|
d = db.DB()
|
|
|
|
d.open(self.filename)
|
|
|
|
c = d.cursor()
|
|
|
|
|
|
|
|
# put a record beyond the consecutive end of the recno's
|
2007-08-08 19:08:30 -03:00
|
|
|
d[100] = b"way out there"
|
|
|
|
assert d[100] == b"way out there"
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
data = d[99]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.fail("expected exception")
|
|
|
|
|
|
|
|
try:
|
|
|
|
d.get(99)
|
2007-01-10 12:19:56 -04:00
|
|
|
except db.DBKeyEmptyError as val:
|
2005-06-08 01:35:50 -03:00
|
|
|
if get_returns_none:
|
|
|
|
self.fail("unexpected DBKeyEmptyError exception")
|
|
|
|
else:
|
2007-03-28 18:02:43 -03:00
|
|
|
assert val.args[0] == db.DB_KEYEMPTY
|
2007-02-09 01:37:30 -04:00
|
|
|
if verbose: print(val)
|
2002-11-19 13:47:07 -04:00
|
|
|
else:
|
2005-06-08 01:35:50 -03:00
|
|
|
if not get_returns_none:
|
|
|
|
self.fail("expected exception")
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
rec = c.set(40)
|
|
|
|
while rec:
|
|
|
|
if verbose:
|
2007-02-09 01:37:30 -04:00
|
|
|
print(rec)
|
2002-11-19 13:47:07 -04:00
|
|
|
rec = c.next()
|
|
|
|
|
|
|
|
c.close()
|
|
|
|
d.close()
|
|
|
|
|
|
|
|
def test02_WithSource(self):
|
|
|
|
"""
|
2002-12-30 16:53:52 -04:00
|
|
|
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
|
|
|
|
if needed.
|
2002-11-19 13:47:07 -04:00
|
|
|
"""
|
2008-02-25 08:39:23 -04:00
|
|
|
homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid())
|
|
|
|
self.homeDir = homeDir
|
|
|
|
source = os.path.join(homeDir, 'test_recno.txt')
|
|
|
|
if not os.path.isdir(homeDir):
|
|
|
|
os.mkdir(homeDir)
|
2002-11-19 13:47:07 -04:00
|
|
|
f = open(source, 'w') # create the file
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
d = db.DB()
|
2002-12-30 16:53:52 -04:00
|
|
|
# This is the default value, just checking if both int
|
|
|
|
d.set_re_delim(0x0A)
|
2002-11-19 13:47:07 -04:00
|
|
|
d.set_re_delim('\n') # and char can be used...
|
|
|
|
d.set_re_source(source)
|
|
|
|
d.open(self.filename, db.DB_RECNO, db.DB_CREATE)
|
|
|
|
|
2002-12-30 16:53:52 -04:00
|
|
|
data = "The quick brown fox jumped over the lazy dog".split()
|
2002-11-19 13:47:07 -04:00
|
|
|
for datum in data:
|
2007-08-29 02:52:49 -03:00
|
|
|
d.append(datum.encode('ascii'))
|
2002-11-19 13:47:07 -04:00
|
|
|
d.sync()
|
|
|
|
d.close()
|
|
|
|
|
|
|
|
# get the text from the backing source
|
|
|
|
text = open(source, 'r').read()
|
2002-12-30 16:53:52 -04:00
|
|
|
text = text.strip()
|
2002-11-19 13:47:07 -04:00
|
|
|
if verbose:
|
2007-02-09 01:37:30 -04:00
|
|
|
print(text)
|
|
|
|
print(data)
|
|
|
|
print(text.split('\n'))
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2002-12-30 16:53:52 -04:00
|
|
|
assert text.split('\n') == data
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
# open as a DB again
|
|
|
|
d = db.DB()
|
|
|
|
d.set_re_source(source)
|
|
|
|
d.open(self.filename, db.DB_RECNO)
|
|
|
|
|
2007-08-23 04:32:27 -03:00
|
|
|
d[3] = b'reddish-brown'
|
|
|
|
d[8] = b'comatose'
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
d.sync()
|
|
|
|
d.close()
|
|
|
|
|
|
|
|
text = open(source, 'r').read()
|
2002-12-30 16:53:52 -04:00
|
|
|
text = text.strip()
|
2002-11-19 13:47:07 -04:00
|
|
|
if verbose:
|
2007-02-09 01:37:30 -04:00
|
|
|
print(text)
|
|
|
|
print(text.split('\n'))
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2002-12-30 16:53:52 -04:00
|
|
|
assert text.split('\n') == \
|
|
|
|
"The quick reddish-brown fox jumped over the comatose dog".split()
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
def test03_FixedLength(self):
|
|
|
|
d = db.DB()
|
|
|
|
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)
|
|
|
|
|
2003-01-10 15:28:15 -04:00
|
|
|
for x in letters:
|
2007-08-29 02:52:49 -03:00
|
|
|
d.append(x.encode('ascii') * 35) # These will be padded
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2007-08-23 04:32:27 -03:00
|
|
|
d.append(b'.' * 40) # this one will be exact
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
try: # this one will fail
|
2007-08-23 04:32:27 -03:00
|
|
|
d.append(b'bad' * 20)
|
2007-01-10 12:19:56 -04:00
|
|
|
except db.DBInvalidArgError as val:
|
2007-03-28 18:02:43 -03:00
|
|
|
assert val.args[0] == db.EINVAL
|
2007-02-09 01:37:30 -04:00
|
|
|
if verbose: print(val)
|
2002-11-19 13:47:07 -04:00
|
|
|
else:
|
|
|
|
self.fail("expected exception")
|
|
|
|
|
|
|
|
c = d.cursor()
|
|
|
|
rec = c.first()
|
|
|
|
while rec:
|
|
|
|
if verbose:
|
2007-02-09 01:37:30 -04:00
|
|
|
print(rec)
|
2002-11-19 13:47:07 -04:00
|
|
|
rec = c.next()
|
|
|
|
|
|
|
|
c.close()
|
|
|
|
d.close()
|
|
|
|
|
2003-01-10 15:28:15 -04:00
|
|
|
|
2002-11-19 13:47:07 -04:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2002-12-30 16:53:52 -04:00
|
|
|
def test_suite():
|
2002-11-19 13:47:07 -04:00
|
|
|
return unittest.makeSuite(SimpleRecnoTestCase)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2002-12-30 16:53:52 -04:00
|
|
|
unittest.main(defaultTest='test_suite')
|