PyXR

c:\python24\lib \ bsddb \ test \ test_get_none.py



0001 """
0002 TestCases for checking set_get_returns_none.
0003 """
0004 
0005 import sys, os, string
0006 import tempfile
0007 from pprint import pprint
0008 import unittest
0009 
0010 try:
0011     # For Pythons w/distutils pybsddb
0012     from bsddb3 import db
0013 except ImportError:
0014     # For Python 2.3
0015     from bsddb import db
0016 
0017 from test_all import verbose
0018 
0019 
0020 #----------------------------------------------------------------------
0021 
0022 class GetReturnsNoneTestCase(unittest.TestCase):
0023     def setUp(self):
0024         self.filename = tempfile.mktemp()
0025 
0026     def tearDown(self):
0027         try:
0028             os.remove(self.filename)
0029         except os.error:
0030             pass
0031 
0032 
0033     def test01_get_returns_none(self):
0034         d = db.DB()
0035         d.open(self.filename, db.DB_BTREE, db.DB_CREATE)
0036         d.set_get_returns_none(1)
0037 
0038         for x in string.letters:
0039             d.put(x, x * 40)
0040 
0041         data = d.get('bad key')
0042         assert data == None
0043 
0044         data = d.get('a')
0045         assert data == 'a'*40
0046 
0047         count = 0
0048         c = d.cursor()
0049         rec = c.first()
0050         while rec:
0051             count = count + 1
0052             rec = c.next()
0053 
0054         assert rec == None
0055         assert count == 52
0056 
0057         c.close()
0058         d.close()
0059 
0060 
0061     def test02_get_raises_exception(self):
0062         d = db.DB()
0063         d.open(self.filename, db.DB_BTREE, db.DB_CREATE)
0064         d.set_get_returns_none(0)
0065 
0066         for x in string.letters:
0067             d.put(x, x * 40)
0068 
0069         self.assertRaises(db.DBNotFoundError, d.get, 'bad key')
0070         self.assertRaises(KeyError, d.get, 'bad key')
0071 
0072         data = d.get('a')
0073         assert data == 'a'*40
0074 
0075         count = 0
0076         exceptionHappened = 0
0077         c = d.cursor()
0078         rec = c.first()
0079         while rec:
0080             count = count + 1
0081             try:
0082                 rec = c.next()
0083             except db.DBNotFoundError:  # end of the records
0084                 exceptionHappened = 1
0085                 break
0086 
0087         assert rec != None
0088         assert exceptionHappened
0089         assert count == 52
0090 
0091         c.close()
0092         d.close()
0093 
0094 #----------------------------------------------------------------------
0095 
0096 def test_suite():
0097     return unittest.makeSuite(GetReturnsNoneTestCase)
0098 
0099 
0100 if __name__ == '__main__':
0101     unittest.main(defaultTest='test_suite')
0102 

Generated by PyXR 0.9.4
SourceForge.net Logo