Fix test_bsddb3.
This commit is contained in:
parent
2cde0eb922
commit
32ca442b13
|
@ -27,14 +27,10 @@ import pickle
|
|||
from bsddb.db import *
|
||||
|
||||
# All table names, row names etc. must be ASCII strings
|
||||
# However, rowids, when represented as strings, are latin-1 encoded
|
||||
def _E(s):
|
||||
return s.encode("ascii")
|
||||
|
||||
# Yet, rowid are arbitrary bytes; if there is a need to hash
|
||||
# them, convert them to Latin-1 first
|
||||
def _D(s):
|
||||
return s.decode("latin-1")
|
||||
|
||||
# XXX(nnorwitz): is this correct? DBIncompleteError is conditional in _bsddb.c
|
||||
try:
|
||||
DBIncompleteError
|
||||
|
@ -55,22 +51,22 @@ class Cond:
|
|||
|
||||
class ExactCond(Cond):
|
||||
"""Acts as an exact match condition function"""
|
||||
def __init__(self, strtomatch):
|
||||
self.strtomatch = strtomatch
|
||||
def __init__(self, strtomatch, encoding="utf-8"):
|
||||
self.strtomatch = strtomatch.encode(encoding)
|
||||
def __call__(self, s):
|
||||
return s == self.strtomatch
|
||||
|
||||
class PrefixCond(Cond):
|
||||
"""Acts as a condition function for matching a string prefix"""
|
||||
def __init__(self, prefix):
|
||||
self.prefix = prefix
|
||||
def __init__(self, prefix, encoding="utf-8"):
|
||||
self.prefix = prefix.encode(encoding)
|
||||
def __call__(self, s):
|
||||
return s[:len(self.prefix)] == self.prefix
|
||||
|
||||
class PostfixCond(Cond):
|
||||
"""Acts as a condition function for matching a string postfix"""
|
||||
def __init__(self, postfix):
|
||||
self.postfix = postfix
|
||||
def __init__(self, postfix, encoding="utf-8"):
|
||||
self.postfix = postfix.encode(encoding)
|
||||
def __call__(self, s):
|
||||
return s[-len(self.postfix):] == self.postfix
|
||||
|
||||
|
@ -80,7 +76,7 @@ class LikeCond(Cond):
|
|||
string. Case insensitive and % signs are wild cards.
|
||||
This isn't perfect but it should work for the simple common cases.
|
||||
"""
|
||||
def __init__(self, likestr, re_flags=re.IGNORECASE):
|
||||
def __init__(self, likestr, re_flags=re.IGNORECASE, encoding="utf-8"):
|
||||
# escape python re characters
|
||||
chars_to_escape = '.*+()[]?'
|
||||
for char in chars_to_escape :
|
||||
|
@ -88,8 +84,9 @@ class LikeCond(Cond):
|
|||
# convert %s to wildcards
|
||||
self.likestr = likestr.replace('%', '.*')
|
||||
self.re = re.compile('^'+self.likestr+'$', re_flags)
|
||||
self.encoding = encoding
|
||||
def __call__(self, s):
|
||||
return self.re.match(s)
|
||||
return self.re.match(s.decode(self.encoding))
|
||||
|
||||
#
|
||||
# keys used to store database metadata
|
||||
|
@ -264,10 +261,11 @@ class bsdTableDB :
|
|||
txn.commit()
|
||||
txn = None
|
||||
except DBError as dberror:
|
||||
raise TableDBError, dberror.args[1]
|
||||
finally:
|
||||
if txn:
|
||||
txn.abort()
|
||||
raise TableDBError, dberror.args[1]
|
||||
|
||||
txn = None
|
||||
|
||||
def ListTableColumns(self, table):
|
||||
"""Return a list of columns in the given table.
|
||||
|
@ -342,9 +340,10 @@ class bsdTableDB :
|
|||
|
||||
self.__load_column_info(table)
|
||||
except DBError as dberror:
|
||||
raise TableDBError, dberror.args[1]
|
||||
finally:
|
||||
if txn:
|
||||
txn.abort()
|
||||
raise TableDBError, dberror.args[1]
|
||||
|
||||
|
||||
def __load_column_info(self, table) :
|
||||
|
@ -419,7 +418,11 @@ class bsdTableDB :
|
|||
if txn:
|
||||
txn.abort()
|
||||
self.db.delete(_rowid_key(table, rowid))
|
||||
txn = None
|
||||
raise TableDBError, dberror.args[1], info[2]
|
||||
finally:
|
||||
if txn:
|
||||
txn.abort()
|
||||
|
||||
|
||||
def Modify(self, table, conditions={}, mappings={}):
|
||||
|
@ -465,10 +468,9 @@ class bsdTableDB :
|
|||
txn = None
|
||||
|
||||
# catch all exceptions here since we call unknown callables
|
||||
except:
|
||||
finally:
|
||||
if txn:
|
||||
txn.abort()
|
||||
raise
|
||||
|
||||
except DBError as dberror:
|
||||
raise TableDBError, dberror.args[1]
|
||||
|
@ -493,23 +495,23 @@ class bsdTableDB :
|
|||
for column in columns:
|
||||
# delete the data key
|
||||
try:
|
||||
self.db.delete(_data_key(table, column, rowid),
|
||||
self.db.delete(_data_key(table, column,
|
||||
rowid.encode("latin-1")),
|
||||
txn)
|
||||
except DBNotFoundError:
|
||||
# XXXXXXX column may not exist, assume no error
|
||||
pass
|
||||
|
||||
try:
|
||||
self.db.delete(_rowid_key(table, rowid), txn)
|
||||
self.db.delete(_rowid_key(table, rowid.encode("latin-1")), txn)
|
||||
except DBNotFoundError:
|
||||
# XXXXXXX row key somehow didn't exist, assume no error
|
||||
pass
|
||||
txn.commit()
|
||||
txn = None
|
||||
except DBError as dberror:
|
||||
finally:
|
||||
if txn:
|
||||
txn.abort()
|
||||
raise
|
||||
except DBError as dberror:
|
||||
raise TableDBError, dberror.args[1]
|
||||
|
||||
|
@ -603,7 +605,7 @@ class bsdTableDB :
|
|||
key, data = cur.set_range(searchkey)
|
||||
while key[:len(searchkey)] == searchkey:
|
||||
# extract the rowid from the key
|
||||
rowid = _D(key[-_rowid_str_len:])
|
||||
rowid = key[-_rowid_str_len:].decode("latin-1")
|
||||
|
||||
if rowid not in rejected_rowids:
|
||||
# if no condition was specified or the condition
|
||||
|
@ -706,6 +708,7 @@ class bsdTableDB :
|
|||
del self.__tablecolumns[table]
|
||||
|
||||
except DBError as dberror:
|
||||
raise TableDBError, dberror.args[1]
|
||||
finally:
|
||||
if txn:
|
||||
txn.abort()
|
||||
raise TableDBError, dberror.args[1]
|
||||
|
|
|
@ -79,7 +79,7 @@ class TableDBTestCase(unittest.TestCase):
|
|||
values = list(values)
|
||||
|
||||
colval = pickle.loads(values[0][colname])
|
||||
assert(colval > 3.141 and colval < 3.142)
|
||||
self.assertTrue(colval > 3.141 and colval < 3.142)
|
||||
|
||||
|
||||
def test02(self):
|
||||
|
@ -105,11 +105,11 @@ class TableDBTestCase(unittest.TestCase):
|
|||
conditions={col0: lambda x: pickle.loads(x) >= 8})
|
||||
values = list(values)
|
||||
|
||||
assert len(values) == 2
|
||||
if values[0]['Species'] == 'Penguin' :
|
||||
assert values[1]['Species'] == 'SR-71A Blackbird'
|
||||
elif values[0]['Species'] == 'SR-71A Blackbird' :
|
||||
assert values[1]['Species'] == 'Penguin'
|
||||
self.assertEquals(len(values), 2)
|
||||
if values[0]['Species'] == b'Penguin' :
|
||||
self.assertEquals(values[1]['Species'], b'SR-71A Blackbird')
|
||||
elif values[0]['Species'] == b'SR-71A Blackbird' :
|
||||
self.assertEquals(values[1]['Species'], b'Penguin')
|
||||
else :
|
||||
if verbose:
|
||||
print("values= %r" % (values,))
|
||||
|
@ -139,13 +139,13 @@ class TableDBTestCase(unittest.TestCase):
|
|||
{'a': "",
|
||||
'e': pickle.dumps([{4:5, 6:7}, 'foo'], 1),
|
||||
'f': "Zero"})
|
||||
assert 0
|
||||
self.fail("exception not raised")
|
||||
except dbtables.TableDBError:
|
||||
pass
|
||||
|
||||
try:
|
||||
self.tdb.Select(tabname, [], conditions={'foo': '123'})
|
||||
assert 0
|
||||
self.fail("exception not raised")
|
||||
except dbtables.TableDBError:
|
||||
pass
|
||||
|
||||
|
@ -174,7 +174,7 @@ class TableDBTestCase(unittest.TestCase):
|
|||
values = self.tdb.Select(tabname, ['b', 'a', 'd'],
|
||||
conditions={'e': re.compile('wuzzy').search,
|
||||
'a': re.compile('^[0-9]+$').match})
|
||||
assert len(values) == 2
|
||||
self.assertEquals(len(values), 2)
|
||||
|
||||
# now lets delete one of them and try again
|
||||
self.tdb.Delete(tabname, conditions={'b': dbtables.ExactCond('good')})
|
||||
|
@ -182,14 +182,14 @@ class TableDBTestCase(unittest.TestCase):
|
|||
tabname, ['a', 'd', 'b'],
|
||||
conditions={'e': dbtables.PrefixCond('Fuzzy')})
|
||||
values = list(values)
|
||||
assert len(values) == 1
|
||||
assert values[0]['d'] == None
|
||||
self.assertEquals(len(values), 1)
|
||||
self.assertEquals(values[0]['d'], None)
|
||||
|
||||
values = self.tdb.Select(tabname, ['b'],
|
||||
conditions={'c': lambda c: c == 'meep'})
|
||||
conditions={'c': lambda c: c.decode("ascii") == 'meep'})
|
||||
values = list(values)
|
||||
assert len(values) == 1
|
||||
assert values[0]['b'] == "bad"
|
||||
self.assertEquals(len(values), 1)
|
||||
self.assertEquals(values[0]['b'], b"bad")
|
||||
|
||||
|
||||
def test04_MultiCondSelect(self):
|
||||
|
@ -205,7 +205,7 @@ class TableDBTestCase(unittest.TestCase):
|
|||
{'a': "",
|
||||
'e': pickle.dumps([{4:5, 6:7}, 'foo'], 1),
|
||||
'f': "Zero"})
|
||||
assert 0
|
||||
self.fail("exception not raised")
|
||||
except dbtables.TableDBError:
|
||||
pass
|
||||
|
||||
|
@ -229,7 +229,7 @@ class TableDBTestCase(unittest.TestCase):
|
|||
'a': dbtables.ExactCond('A'),
|
||||
'd': dbtables.PrefixCond('-')
|
||||
} )
|
||||
assert len(values) == 0, values
|
||||
self.assertEquals(len(values), 0, values)
|
||||
|
||||
|
||||
def test_CreateOrExtend(self):
|
||||
|
@ -242,7 +242,7 @@ class TableDBTestCase(unittest.TestCase):
|
|||
{'taste': 'crap',
|
||||
'filling': 'no',
|
||||
'is it Guinness?': 'no'})
|
||||
assert 0, "Insert should've failed due to bad column name"
|
||||
self.fail("Insert should've failed due to bad column name")
|
||||
except:
|
||||
pass
|
||||
self.tdb.CreateOrExtendTable(tabname,
|
||||
|
@ -277,17 +277,17 @@ class TableDBTestCase(unittest.TestCase):
|
|||
tabname, ['p', 'e'],
|
||||
conditions={'e': dbtables.PrefixCond('the l')})
|
||||
values = list(values)
|
||||
assert len(values) == 2, values
|
||||
assert values[0]['e'] == values[1]['e'], values
|
||||
assert values[0]['p'] != values[1]['p'], values
|
||||
self.assertEquals(len(values), 2)
|
||||
self.assertEquals(values[0]['e'], values[1]['e'])
|
||||
self.assertNotEquals(values[0]['p'], values[1]['p'])
|
||||
|
||||
values = self.tdb.Select(
|
||||
tabname, ['d', 'a'],
|
||||
conditions={'a': dbtables.LikeCond('%aardvark%')})
|
||||
values = list(values)
|
||||
assert len(values) == 1, values
|
||||
assert values[0]['d'] == "is for dog", values
|
||||
assert values[0]['a'] == "is for aardvark", values
|
||||
self.assertEquals(len(values), 1)
|
||||
self.assertEquals(values[0]['d'], b"is for dog")
|
||||
self.assertEquals(values[0]['a'], b"is for aardvark")
|
||||
|
||||
values = self.tdb.Select(tabname, None,
|
||||
{'b': dbtables.Cond(),
|
||||
|
@ -297,9 +297,9 @@ class TableDBTestCase(unittest.TestCase):
|
|||
'c':dbtables.PrefixCond('is for'),
|
||||
'p':lambda s: not s})
|
||||
values = list(values)
|
||||
assert len(values) == 1, values
|
||||
assert values[0]['d'] == "is for dog", values
|
||||
assert values[0]['a'] == "is for aardvark", values
|
||||
self.assertEquals(len(values), 1)
|
||||
self.assertEquals(values[0]['d'], b"is for dog")
|
||||
self.assertEquals(values[0]['a'], b"is for aardvark")
|
||||
|
||||
def test_Delete(self):
|
||||
tabname = "test_Delete"
|
||||
|
@ -315,7 +315,7 @@ class TableDBTestCase(unittest.TestCase):
|
|||
self.tdb.Delete(tabname, conditions={'x': dbtables.PrefixCond('X')})
|
||||
values = self.tdb.Select(tabname, ['y'],
|
||||
conditions={'x': dbtables.PrefixCond('X')})
|
||||
assert len(values) == 0
|
||||
self.assertEquals(len(values), 0)
|
||||
|
||||
def test_Modify(self):
|
||||
tabname = "test_Modify"
|
||||
|
@ -362,26 +362,26 @@ class TableDBTestCase(unittest.TestCase):
|
|||
tabname, None,
|
||||
conditions={'Type': dbtables.ExactCond('Unknown')})
|
||||
values = list(values)
|
||||
assert len(values) == 1, values
|
||||
assert values[0]['Name'] == None, values
|
||||
assert values[0]['Access'] == None, values
|
||||
self.assertEquals(len(values), 1)
|
||||
self.assertEquals(values[0]['Name'], None)
|
||||
self.assertEquals(values[0]['Access'], None)
|
||||
|
||||
# Modify value by select conditions
|
||||
values = self.tdb.Select(
|
||||
tabname, None,
|
||||
conditions={'Name': dbtables.ExactCond('Nifty.MP3')})
|
||||
values = list(values)
|
||||
assert len(values) == 1, values
|
||||
assert values[0]['Type'] == "MP3", values
|
||||
assert values[0]['Access'] == "2", values
|
||||
self.assertEquals(len(values), 1)
|
||||
self.assertEquals(values[0]['Type'], b"MP3")
|
||||
self.assertEquals(values[0]['Access'], b"2")
|
||||
|
||||
# Make sure change applied only to select conditions
|
||||
values = self.tdb.Select(
|
||||
tabname, None, conditions={'Name': dbtables.LikeCond('%doc%')})
|
||||
values = list(values)
|
||||
assert len(values) == 1, values
|
||||
assert values[0]['Type'] == "Word", values
|
||||
assert values[0]['Access'] == "9", values
|
||||
self.assertEquals(len(values), 1)
|
||||
self.assertEquals(values[0]['Type'], b"Word")
|
||||
self.assertEquals(values[0]['Access'], b"9")
|
||||
|
||||
|
||||
def test_suite():
|
||||
|
|
|
@ -24,8 +24,7 @@ if 'silent' in sys.argv: # take care of old flag, just in case
|
|||
def suite():
|
||||
try:
|
||||
# this is special, it used to segfault the interpreter
|
||||
#import bsddb.test.test_1413192
|
||||
pass
|
||||
import bsddb.test.test_1413192
|
||||
except:
|
||||
for f in ['__db.001', '__db.002', '__db.003', 'log.0000000001']:
|
||||
unlink(f)
|
||||
|
|
Loading…
Reference in New Issue