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
|
2003-01-10 15:28:15 -04:00
|
|
|
import errno
|
2002-11-19 13:47:07 -04:00
|
|
|
from pprint import pprint
|
|
|
|
import unittest
|
|
|
|
|
2008-08-31 11:00:51 -03:00
|
|
|
from test_all import db, test_support, verbose, get_new_environment_path, get_new_database_path
|
2008-03-02 16:00:53 -04:00
|
|
|
|
2003-01-10 15:28:15 -04:00
|
|
|
letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
|
|
|
|
|
|
|
2002-11-19 13:47:07 -04:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
class SimpleRecnoTestCase(unittest.TestCase):
|
2008-05-13 17:57:59 -03:00
|
|
|
import sys
|
|
|
|
if sys.version_info[:3] < (2, 4, 0):
|
|
|
|
def assertFalse(self, expr, msg=None):
|
|
|
|
self.failIf(expr,msg=msg)
|
|
|
|
|
2002-11-19 13:47:07 -04:00
|
|
|
def setUp(self):
|
2008-05-13 17:57:59 -03:00
|
|
|
self.filename = get_new_database_path()
|
2008-02-24 14:47:03 -04:00
|
|
|
self.homeDir = None
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
def tearDown(self):
|
2008-02-24 14:47:03 -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:
|
2002-11-19 13:47:07 -04:00
|
|
|
recno = d.append(x * 60)
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(type(recno), type(0))
|
|
|
|
self.assert_(recno >= 1)
|
2002-11-19 13:47:07 -04:00
|
|
|
if verbose:
|
|
|
|
print recno,
|
|
|
|
|
|
|
|
if verbose: print
|
|
|
|
|
|
|
|
stat = d.stat()
|
|
|
|
if verbose:
|
|
|
|
pprint(stat)
|
|
|
|
|
|
|
|
for recno in range(1, len(d)+1):
|
|
|
|
data = d[recno]
|
|
|
|
if verbose:
|
|
|
|
print data
|
|
|
|
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(type(data), type(""))
|
|
|
|
self.assertEqual(data, d.get(recno))
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
data = d[0] # This should raise a KeyError!?!?!
|
|
|
|
except db.DBInvalidArgError, val:
|
2008-08-31 11:00:51 -03:00
|
|
|
import sys
|
|
|
|
if sys.version_info[0] < 3 :
|
|
|
|
self.assertEqual(val[0], db.EINVAL)
|
|
|
|
else :
|
|
|
|
self.assertEqual(val.args[0], db.EINVAL)
|
2002-11-19 13:47:07 -04:00
|
|
|
if verbose: print val
|
|
|
|
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)
|
|
|
|
except db.DBError, val:
|
|
|
|
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)
|
|
|
|
except db.DBNotFoundError, val:
|
|
|
|
if get_returns_none:
|
|
|
|
self.fail("unexpected exception")
|
|
|
|
else:
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(data, None)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
keys = d.keys()
|
|
|
|
if verbose:
|
|
|
|
print keys
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(type(keys), type([]))
|
|
|
|
self.assertEqual(type(keys[0]), type(123))
|
|
|
|
self.assertEqual(len(keys), len(d))
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
items = d.items()
|
|
|
|
if verbose:
|
|
|
|
pprint(items)
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(type(items), type([]))
|
|
|
|
self.assertEqual(type(items[0]), type(()))
|
|
|
|
self.assertEqual(len(items[0]), 2)
|
|
|
|
self.assertEqual(type(items[0][0]), type(123))
|
|
|
|
self.assertEqual(type(items[0][1]), type(""))
|
|
|
|
self.assertEqual(len(items), len(d))
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assert_(d.has_key(25))
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
del d[25]
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertFalse(d.has_key(25))
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
d.delete(13)
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertFalse(d.has_key(13))
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
data = d.get_both(26, "z" * 60)
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(data, "z" * 60, 'was %r' % data)
|
2002-11-19 13:47:07 -04:00
|
|
|
if verbose:
|
|
|
|
print data
|
|
|
|
|
|
|
|
fd = d.fd()
|
|
|
|
if verbose:
|
|
|
|
print fd
|
|
|
|
|
|
|
|
c = d.cursor()
|
|
|
|
rec = c.first()
|
|
|
|
while rec:
|
|
|
|
if verbose:
|
|
|
|
print rec
|
|
|
|
rec = c.next()
|
|
|
|
|
|
|
|
c.set(50)
|
|
|
|
rec = c.current()
|
|
|
|
if verbose:
|
|
|
|
print rec
|
|
|
|
|
|
|
|
c.put(-1, "a replacement record", db.DB_CURRENT)
|
|
|
|
|
|
|
|
c.set(50)
|
|
|
|
rec = c.current()
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(rec, (50, "a replacement record"))
|
2002-11-19 13:47:07 -04:00
|
|
|
if verbose:
|
|
|
|
print rec
|
|
|
|
|
|
|
|
rec = c.set_range(30)
|
|
|
|
if verbose:
|
|
|
|
print rec
|
|
|
|
|
2009-02-21 16:27:01 -04:00
|
|
|
# test that non-existent key lookups work (and that
|
2004-06-27 20:32:34 -03:00
|
|
|
# DBC_set_range doesn't have a memleak under valgrind)
|
|
|
|
rec = c.set_range(999999)
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(rec, None)
|
2004-06-27 20:32:34 -03:00
|
|
|
if verbose:
|
|
|
|
print rec
|
|
|
|
|
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
|
|
|
|
d[100] = "way out there"
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(d[100], "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)
|
|
|
|
except db.DBKeyEmptyError, val:
|
2005-06-08 01:35:50 -03:00
|
|
|
if get_returns_none:
|
|
|
|
self.fail("unexpected DBKeyEmptyError exception")
|
|
|
|
else:
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(val[0], db.DB_KEYEMPTY)
|
2005-06-08 01:35:50 -03: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:
|
|
|
|
print rec
|
|
|
|
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-05-13 17:57:59 -03:00
|
|
|
homeDir = get_new_environment_path()
|
2008-02-24 14:47:03 -04:00
|
|
|
self.homeDir = homeDir
|
2007-01-05 11:51:24 -04:00
|
|
|
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:
|
|
|
|
d.append(datum)
|
|
|
|
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:
|
|
|
|
print text
|
|
|
|
print data
|
2002-12-30 16:53:52 -04:00
|
|
|
print text.split('\n')
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(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)
|
|
|
|
|
|
|
|
d[3] = 'reddish-brown'
|
|
|
|
d[8] = 'comatose'
|
|
|
|
|
|
|
|
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:
|
|
|
|
print text
|
2002-12-30 16:53:52 -04:00
|
|
|
print text.split('\n')
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(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:
|
2002-11-19 13:47:07 -04:00
|
|
|
d.append(x * 35) # These will be padded
|
|
|
|
|
|
|
|
d.append('.' * 40) # this one will be exact
|
|
|
|
|
|
|
|
try: # this one will fail
|
|
|
|
d.append('bad' * 20)
|
|
|
|
except db.DBInvalidArgError, val:
|
2008-08-31 11:00:51 -03:00
|
|
|
import sys
|
|
|
|
if sys.version_info[0] < 3 :
|
|
|
|
self.assertEqual(val[0], db.EINVAL)
|
|
|
|
else :
|
|
|
|
self.assertEqual(val.args[0], db.EINVAL)
|
2002-11-19 13:47:07 -04:00
|
|
|
if verbose: print val
|
|
|
|
else:
|
|
|
|
self.fail("expected exception")
|
|
|
|
|
|
|
|
c = d.cursor()
|
|
|
|
rec = c.first()
|
|
|
|
while rec:
|
|
|
|
if verbose:
|
|
|
|
print rec
|
|
|
|
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')
|