cpython/Lib/bsddb/test/test_compare.py

248 lines
8.0 KiB
Python
Raw Normal View History

"""
TestCases for python DB Btree key comparison function.
"""
import shutil
import sys, os, re
from io import StringIO
import tempfile
import unittest
try:
# For Pythons w/distutils pybsddb
from bsddb3 import db, dbshelve
except ImportError:
# For Python 2.3
from bsddb import db, dbshelve
lexical_cmp = cmp
def lowercase_cmp(left, right):
return cmp (str(left, encoding='ascii').lower(),
str(right, encoding='ascii').lower())
def make_reverse_comparator (cmp):
def reverse (left, right, delegate=cmp):
return - delegate (left, right)
2005-06-03 19:40:27 -03:00
return reverse
2007-08-29 02:06:13 -03:00
_expected_lexical_test_data = [s.encode('ascii') for s in
('', 'CCCP', 'a', 'aaa', 'b', 'c', 'cccce', 'ccccf')]
2007-08-29 02:06:13 -03:00
_expected_lowercase_test_data = [s.encode('ascii') for s in
('', 'a', 'aaa', 'b', 'c', 'CC', 'cccce', 'ccccf', 'CCCP')]
class ComparatorTests (unittest.TestCase):
2005-06-03 19:40:27 -03:00
def comparator_test_helper (self, comparator, expected_data):
data = expected_data[:]
data.sort (comparator)
2005-06-03 19:40:27 -03:00
self.failUnless (data == expected_data,
"comparator `%s' is not right: %s vs. %s"
% (comparator, expected_data, data))
def test_lexical_comparator (self):
self.comparator_test_helper (lexical_cmp, _expected_lexical_test_data)
def test_reverse_lexical_comparator (self):
rev = _expected_lexical_test_data[:]
rev.reverse ()
self.comparator_test_helper (make_reverse_comparator (lexical_cmp),
rev)
def test_lowercase_comparator (self):
self.comparator_test_helper (lowercase_cmp,
_expected_lowercase_test_data)
class AbstractBtreeKeyCompareTestCase (unittest.TestCase):
2005-06-03 19:40:27 -03:00
env = None
db = None
def setUp (self):
self.filename = self.__class__.__name__ + '.db'
self.homeDir = tempfile.mkdtemp()
2005-06-03 19:40:27 -03:00
env = db.DBEnv ()
env.open (self.homeDir,
2005-06-03 19:40:27 -03:00
db.DB_CREATE | db.DB_INIT_MPOOL
| db.DB_INIT_LOCK | db.DB_THREAD)
self.env = env
def tearDown (self):
self.closeDB ()
if self.env is not None:
self.env.close ()
self.env = None
shutil.rmtree(self.homeDir)
2005-06-03 19:40:27 -03:00
def addDataToDB (self, data):
i = 0
for item in data:
2007-08-29 02:06:13 -03:00
self.db.put (item, str(i).encode("ascii"))
2005-06-03 19:40:27 -03:00
i = i + 1
def createDB (self, key_comparator):
self.db = db.DB (self.env)
self.setupDB (key_comparator)
self.db.open (self.filename, "test", db.DB_BTREE, db.DB_CREATE)
def setupDB (self, key_comparator):
self.db.set_bt_compare (key_comparator)
def closeDB (self):
if self.db is not None:
self.db.close ()
self.db = None
def startTest (self):
pass
def finishTest (self, expected = None):
if expected is not None:
self.check_results (expected)
self.closeDB ()
def check_results (self, expected):
curs = self.db.cursor ()
try:
index = 0
rec = curs.first ()
while rec:
key, ignore = rec
self.failUnless (index < len (expected),
"to many values returned from cursor")
self.failUnless (expected[index] == key,
"expected value %r at %d but got %r"
2005-06-03 19:40:27 -03:00
% (expected[index], index, key))
index = index + 1
rec = curs.next ()
self.failUnless (index == len (expected),
"not enough values returned from cursor")
finally:
curs.close ()
class BtreeKeyCompareTestCase (AbstractBtreeKeyCompareTestCase):
2005-06-03 19:40:27 -03:00
def runCompareTest (self, comparator, data):
self.startTest ()
self.createDB (comparator)
self.addDataToDB (data)
self.finishTest (data)
def test_lexical_ordering (self):
self.runCompareTest (lexical_cmp, _expected_lexical_test_data)
def test_reverse_lexical_ordering (self):
expected_rev_data = _expected_lexical_test_data[:]
expected_rev_data.reverse ()
self.runCompareTest (make_reverse_comparator (lexical_cmp),
expected_rev_data)
def test_compare_function_useless (self):
self.startTest ()
def socialist_comparator (l, r):
2005-06-03 19:40:27 -03:00
return 0
self.createDB (socialist_comparator)
self.addDataToDB ([b'b', b'a', b'd'])
2005-06-03 19:40:27 -03:00
# all things being equal the first key will be the only key
# in the database... (with the last key's value fwiw)
self.finishTest ([b'b'])
2005-06-03 19:40:27 -03:00
class BtreeExceptionsTestCase (AbstractBtreeKeyCompareTestCase):
2005-06-03 19:40:27 -03:00
def test_raises_non_callable (self):
self.startTest ()
self.assertRaises (TypeError, self.createDB, 'abc')
self.assertRaises (TypeError, self.createDB, None)
self.finishTest ()
def test_set_bt_compare_with_function (self):
self.startTest ()
self.createDB (lexical_cmp)
self.finishTest ()
def check_results (self, results):
pass
def test_compare_function_incorrect (self):
self.startTest ()
def bad_comparator (l, r):
2005-06-03 19:40:27 -03:00
return 1
# verify that set_bt_compare checks that comparator('', '') == 0
2005-06-03 19:40:27 -03:00
self.assertRaises (TypeError, self.createDB, bad_comparator)
self.finishTest ()
def verifyStderr(self, method, successRe):
"""
Call method() while capturing sys.stderr output internally and
call self.fail() if successRe.search() does not match the stderr
output. This is used to test for uncatchable exceptions.
"""
stdErr = sys.stderr
sys.stderr = StringIO()
try:
method()
finally:
temp = sys.stderr
sys.stderr = stdErr
errorOut = temp.getvalue()
if not successRe.search(errorOut):
2007-08-29 02:06:13 -03:00
self.fail("unexpected stderr output: %r" % errorOut)
def _test_compare_function_exception (self):
2005-06-03 19:40:27 -03:00
self.startTest ()
def bad_comparator (l, r):
2005-06-03 19:40:27 -03:00
if l == r:
# pass the set_bt_compare test
return 0
2007-08-22 18:45:20 -03:00
raise RuntimeError("i'm a naughty comparison function")
2005-06-03 19:40:27 -03:00
self.createDB (bad_comparator)
#print "\n*** test should print 2 uncatchable tracebacks ***"
self.addDataToDB (['a', 'b', 'c']) # this should raise, but...
self.finishTest ()
2005-06-03 19:40:27 -03:00
def test_compare_function_exception(self):
self.verifyStderr(
self._test_compare_function_exception,
re.compile('(^RuntimeError:.* naughty.*){2}', re.M|re.S)
)
def _test_compare_function_bad_return (self):
2005-06-03 19:40:27 -03:00
self.startTest ()
def bad_comparator (l, r):
2005-06-03 19:40:27 -03:00
if l == r:
# pass the set_bt_compare test
return 0
return l
self.createDB (bad_comparator)
#print "\n*** test should print 2 errors about returning an int ***"
2005-06-03 19:40:27 -03:00
self.addDataToDB (['a', 'b', 'c']) # this should raise, but...
self.finishTest ()
def test_compare_function_bad_return(self):
self.verifyStderr(
self._test_compare_function_bad_return,
re.compile('(^TypeError:.* return an int.*){2}', re.M|re.S)
)
2005-06-03 19:40:27 -03:00
def test_cannot_assign_twice (self):
def my_compare (a, b):
2005-06-03 19:40:27 -03:00
return 0
self.startTest ()
self.createDB (my_compare)
try:
self.db.set_bt_compare (my_compare)
assert False, "this set should fail"
except RuntimeError as msg:
2005-06-03 19:40:27 -03:00
pass
def test_suite ():
2005-06-03 19:40:27 -03:00
res = unittest.TestSuite ()
2005-06-03 19:40:27 -03:00
res.addTest (unittest.makeSuite (ComparatorTests))
if db.version () >= (3, 3, 11):
res.addTest (unittest.makeSuite (BtreeExceptionsTestCase))
res.addTest (unittest.makeSuite (BtreeKeyCompareTestCase))
return res
if __name__ == '__main__':
unittest.main (defaultTest = 'test_suite')