2002-11-19 13:47:07 -04:00
|
|
|
"""
|
|
|
|
TestCases for checking set_get_returns_none.
|
|
|
|
"""
|
|
|
|
|
2008-02-23 13:40:11 -04:00
|
|
|
import os, string
|
2002-11-19 13:47:07 -04:00
|
|
|
import unittest
|
|
|
|
|
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
|
2002-11-19 13:47:07 -04:00
|
|
|
|
2008-05-13 17:57:59 -03:00
|
|
|
from test_all import verbose, get_new_database_path
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
class GetReturnsNoneTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2008-05-13 17:57:59 -03:00
|
|
|
self.filename = get_new_database_path()
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
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')
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(data, None)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
data = d.get('a')
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(data, 'a'*40)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
count = 0
|
|
|
|
c = d.cursor()
|
|
|
|
rec = c.first()
|
|
|
|
while rec:
|
|
|
|
count = count + 1
|
|
|
|
rec = c.next()
|
|
|
|
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(rec, None)
|
|
|
|
self.assertEqual(count, 52)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
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')
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertEqual(data, 'a'*40)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2008-05-13 17:57:59 -03:00
|
|
|
self.assertNotEqual(rec, None)
|
|
|
|
self.assert_(exceptionHappened)
|
|
|
|
self.assertEqual(count, 52)
|
2002-11-19 13:47:07 -04:00
|
|
|
|
|
|
|
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')
|