2002-11-19 13:47:07 -04:00
|
|
|
"""
|
|
|
|
TestCases for checking set_get_returns_none.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import sys, os, string
|
|
|
|
import tempfile
|
|
|
|
from pprint import pprint
|
|
|
|
import unittest
|
|
|
|
|
2003-01-28 13:20:44 -04:00
|
|
|
try:
|
|
|
|
# For Python 2.3
|
|
|
|
from bsddb import db
|
|
|
|
except ImportError:
|
|
|
|
# For earlier Pythons w/distutils pybsddb
|
|
|
|
from bsddb3 import db
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2002-12-30 16:53:52 -04:00
|
|
|
from test_all import verbose
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
class GetReturnsNoneTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.filename = tempfile.mktemp()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
try:
|
|
|
|
os.remove(self.filename)
|
|
|
|
except os.error:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def test01_get_returns_none(self):
|
|
|
|
d = db.DB()
|
|
|
|
d.open(self.filename, db.DB_BTREE, db.DB_CREATE)
|
|
|
|
d.set_get_returns_none(1)
|
|
|
|
|
|
|
|
for x in string.letters:
|
|
|
|
d.put(x, x * 40)
|
|
|
|
|
|
|
|
data = d.get('bad key')
|
|
|
|
assert data == None
|
|
|
|
|
|
|
|
data = d.get('a')
|
|
|
|
assert data == 'a'*40
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
c = d.cursor()
|
|
|
|
rec = c.first()
|
|
|
|
while rec:
|
|
|
|
count = count + 1
|
|
|
|
rec = c.next()
|
|
|
|
|
|
|
|
assert rec == None
|
|
|
|
assert count == 52
|
|
|
|
|
|
|
|
c.close()
|
|
|
|
d.close()
|
|
|
|
|
|
|
|
|
|
|
|
def test02_get_raises_exception(self):
|
|
|
|
d = db.DB()
|
|
|
|
d.open(self.filename, db.DB_BTREE, db.DB_CREATE)
|
|
|
|
d.set_get_returns_none(0)
|
|
|
|
|
|
|
|
for x in string.letters:
|
|
|
|
d.put(x, x * 40)
|
|
|
|
|
|
|
|
self.assertRaises(db.DBNotFoundError, d.get, 'bad key')
|
|
|
|
self.assertRaises(KeyError, d.get, 'bad key')
|
|
|
|
|
|
|
|
data = d.get('a')
|
|
|
|
assert data == 'a'*40
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
exceptionHappened = 0
|
|
|
|
c = d.cursor()
|
|
|
|
rec = c.first()
|
|
|
|
while rec:
|
|
|
|
count = count + 1
|
|
|
|
try:
|
|
|
|
rec = c.next()
|
|
|
|
except db.DBNotFoundError: # end of the records
|
|
|
|
exceptionHappened = 1
|
|
|
|
break
|
|
|
|
|
|
|
|
assert rec != None
|
|
|
|
assert exceptionHappened
|
|
|
|
assert count == 52
|
|
|
|
|
|
|
|
c.close()
|
|
|
|
d.close()
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2002-12-30 16:53:52 -04:00
|
|
|
def test_suite():
|
2002-11-19 13:47:07 -04:00
|
|
|
return unittest.makeSuite(GetReturnsNoneTestCase)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2002-12-30 16:53:52 -04:00
|
|
|
unittest.main(defaultTest='test_suite')
|