PyXR

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



0001 """
0002 Test cases adapted from the test_bsddb.py module in Python's
0003 regression test suite.
0004 """
0005 
0006 import sys, os, string
0007 import unittest
0008 import tempfile
0009 
0010 from test_all import verbose
0011 
0012 try:
0013     # For Pythons w/distutils pybsddb
0014     from bsddb3 import db, hashopen, btopen, rnopen
0015 except ImportError:
0016     # For Python 2.3
0017     from bsddb import db, hashopen, btopen, rnopen
0018 
0019 
0020 class CompatibilityTestCase(unittest.TestCase):
0021     def setUp(self):
0022         self.filename = tempfile.mktemp()
0023 
0024     def tearDown(self):
0025         try:
0026             os.remove(self.filename)
0027         except os.error:
0028             pass
0029 
0030 
0031     def test01_btopen(self):
0032         self.do_bthash_test(btopen, 'btopen')
0033 
0034     def test02_hashopen(self):
0035         self.do_bthash_test(hashopen, 'hashopen')
0036 
0037     def test03_rnopen(self):
0038         data = string.split("The quick brown fox jumped over the lazy dog.")
0039         if verbose:
0040             print "\nTesting: rnopen"
0041 
0042         f = rnopen(self.filename, 'c')
0043         for x in range(len(data)):
0044             f[x+1] = data[x]
0045 
0046         getTest = (f[1], f[2], f[3])
0047         if verbose:
0048             print '%s %s %s' % getTest
0049 
0050         assert getTest[1] == 'quick', 'data mismatch!'
0051 
0052         rv = f.set_location(3)
0053         if rv != (3, 'brown'):
0054             self.fail('recno database set_location failed: '+repr(rv))
0055 
0056         f[25] = 'twenty-five'
0057         f.close()
0058         del f
0059 
0060         f = rnopen(self.filename, 'w')
0061         f[20] = 'twenty'
0062 
0063         def noRec(f):
0064             rec = f[15]
0065         self.assertRaises(KeyError, noRec, f)
0066 
0067         def badKey(f):
0068             rec = f['a string']
0069         self.assertRaises(TypeError, badKey, f)
0070 
0071         del f[3]
0072 
0073         rec = f.first()
0074         while rec:
0075             if verbose:
0076                 print rec
0077             try:
0078                 rec = f.next()
0079             except KeyError:
0080                 break
0081 
0082         f.close()
0083 
0084 
0085     def test04_n_flag(self):
0086         f = hashopen(self.filename, 'n')
0087         f.close()
0088 
0089 
0090     def do_bthash_test(self, factory, what):
0091         if verbose:
0092             print '\nTesting: ', what
0093 
0094         f = factory(self.filename, 'c')
0095         if verbose:
0096             print 'creation...'
0097 
0098         # truth test
0099         if f:
0100             if verbose: print "truth test: true"
0101         else:
0102             if verbose: print "truth test: false"
0103 
0104         f['0'] = ''
0105         f['a'] = 'Guido'
0106         f['b'] = 'van'
0107         f['c'] = 'Rossum'
0108         f['d'] = 'invented'
0109         # 'e' intentionally left out
0110         f['f'] = 'Python'
0111         if verbose:
0112             print '%s %s %s' % (f['a'], f['b'], f['c'])
0113 
0114         if verbose:
0115             print 'key ordering...'
0116         start = f.set_location(f.first()[0])
0117         if start != ('0', ''):
0118             self.fail("incorrect first() result: "+repr(start))
0119         while 1:
0120             try:
0121                 rec = f.next()
0122             except KeyError:
0123                 assert rec == f.last(), 'Error, last <> last!'
0124                 f.previous()
0125                 break
0126             if verbose:
0127                 print rec
0128 
0129         assert f.has_key('f'), 'Error, missing key!'
0130 
0131         # test that set_location() returns the next nearest key, value
0132         # on btree databases and raises KeyError on others.
0133         if factory == btopen:
0134             e = f.set_location('e')
0135             if e != ('f', 'Python'):
0136                 self.fail('wrong key,value returned: '+repr(e))
0137         else:
0138             try:
0139                 e = f.set_location('e')
0140             except KeyError:
0141                 pass
0142             else:
0143                 self.fail("set_location on non-existant key did not raise KeyError")
0144 
0145         f.sync()
0146         f.close()
0147         # truth test
0148         try:
0149             if f:
0150                 if verbose: print "truth test: true"
0151             else:
0152                 if verbose: print "truth test: false"
0153         except db.DBError:
0154             pass
0155         else:
0156             self.fail("Exception expected")
0157 
0158         del f
0159 
0160         if verbose:
0161             print 'modification...'
0162         f = factory(self.filename, 'w')
0163         f['d'] = 'discovered'
0164 
0165         if verbose:
0166             print 'access...'
0167         for key in f.keys():
0168             word = f[key]
0169             if verbose:
0170                 print word
0171 
0172         def noRec(f):
0173             rec = f['no such key']
0174         self.assertRaises(KeyError, noRec, f)
0175 
0176         def badKey(f):
0177             rec = f[15]
0178         self.assertRaises(TypeError, badKey, f)
0179 
0180         f.close()
0181 
0182 
0183 #----------------------------------------------------------------------
0184 
0185 
0186 def test_suite():
0187     return unittest.makeSuite(CompatibilityTestCase)
0188 
0189 
0190 if __name__ == '__main__':
0191     unittest.main(defaultTest='test_suite')
0192 

Generated by PyXR 0.9.4
SourceForge.net Logo