home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 June / PCWorld_2005-06_cd.bin / software / vyzkuste / firewally / firewally.exe / framework-2.3.exe / test_compat.py < prev    next >
Text File  |  2003-12-30  |  4KB  |  172 lines

  1. """
  2. Test cases adapted from the test_bsddb.py module in Python's
  3. regression test suite.
  4. """
  5.  
  6. import sys, os, string
  7. import unittest
  8. import tempfile
  9.  
  10. from test_all import verbose
  11.  
  12. try:
  13.     # For Python 2.3
  14.     from bsddb import db, hashopen, btopen, rnopen
  15. except ImportError:
  16.     # For earlier Pythons w/distutils pybsddb
  17.     from bsddb3 import db, hashopen, btopen, rnopen
  18.  
  19.  
  20. class CompatibilityTestCase(unittest.TestCase):
  21.     def setUp(self):
  22.         self.filename = tempfile.mktemp()
  23.  
  24.     def tearDown(self):
  25.         try:
  26.             os.remove(self.filename)
  27.         except os.error:
  28.             pass
  29.  
  30.  
  31.     def test01_btopen(self):
  32.         self.do_bthash_test(btopen, 'btopen')
  33.  
  34.     def test02_hashopen(self):
  35.         self.do_bthash_test(hashopen, 'hashopen')
  36.  
  37.     def test03_rnopen(self):
  38.         data = string.split("The quick brown fox jumped over the lazy dog.")
  39.         if verbose:
  40.             print "\nTesting: rnopen"
  41.  
  42.         f = rnopen(self.filename, 'c')
  43.         for x in range(len(data)):
  44.             f[x+1] = data[x]
  45.  
  46.         getTest = (f[1], f[2], f[3])
  47.         if verbose:
  48.             print '%s %s %s' % getTest
  49.  
  50.         assert getTest[1] == 'quick', 'data mismatch!'
  51.  
  52.         f[25] = 'twenty-five'
  53.         f.close()
  54.         del f
  55.  
  56.         f = rnopen(self.filename, 'w')
  57.         f[20] = 'twenty'
  58.  
  59.         def noRec(f):
  60.             rec = f[15]
  61.         self.assertRaises(KeyError, noRec, f)
  62.  
  63.         def badKey(f):
  64.             rec = f['a string']
  65.         self.assertRaises(TypeError, badKey, f)
  66.  
  67.         del f[3]
  68.  
  69.         rec = f.first()
  70.         while rec:
  71.             if verbose:
  72.                 print rec
  73.             try:
  74.                 rec = f.next()
  75.             except KeyError:
  76.                 break
  77.  
  78.         f.close()
  79.  
  80.  
  81.     def test04_n_flag(self):
  82.         f = hashopen(self.filename, 'n')
  83.         f.close()
  84.  
  85.  
  86.  
  87.     def do_bthash_test(self, factory, what):
  88.         if verbose:
  89.             print '\nTesting: ', what
  90.  
  91.         f = factory(self.filename, 'c')
  92.         if verbose:
  93.             print 'creation...'
  94.  
  95.         # truth test
  96.         if f:
  97.             if verbose: print "truth test: true"
  98.         else:
  99.             if verbose: print "truth test: false"
  100.  
  101.         f['0'] = ''
  102.         f['a'] = 'Guido'
  103.         f['b'] = 'van'
  104.         f['c'] = 'Rossum'
  105.         f['d'] = 'invented'
  106.         f['f'] = 'Python'
  107.         if verbose:
  108.             print '%s %s %s' % (f['a'], f['b'], f['c'])
  109.  
  110.         if verbose:
  111.             print 'key ordering...'
  112.         f.set_location(f.first()[0])
  113.         while 1:
  114.             try:
  115.                 rec = f.next()
  116.             except KeyError:
  117.                 assert rec == f.last(), 'Error, last <> last!'
  118.                 f.previous()
  119.                 break
  120.             if verbose:
  121.                 print rec
  122.  
  123.         assert f.has_key('f'), 'Error, missing key!'
  124.  
  125.         f.sync()
  126.         f.close()
  127.         # truth test
  128.         try:
  129.             if f:
  130.                 if verbose: print "truth test: true"
  131.             else:
  132.                 if verbose: print "truth test: false"
  133.         except db.DBError:
  134.             pass
  135.         else:
  136.             self.fail("Exception expected")
  137.  
  138.         del f
  139.  
  140.         if verbose:
  141.             print 'modification...'
  142.         f = factory(self.filename, 'w')
  143.         f['d'] = 'discovered'
  144.  
  145.         if verbose:
  146.             print 'access...'
  147.         for key in f.keys():
  148.             word = f[key]
  149.             if verbose:
  150.                 print word
  151.  
  152.         def noRec(f):
  153.             rec = f['no such key']
  154.         self.assertRaises(KeyError, noRec, f)
  155.  
  156.         def badKey(f):
  157.             rec = f[15]
  158.         self.assertRaises(TypeError, badKey, f)
  159.  
  160.         f.close()
  161.  
  162.  
  163. #----------------------------------------------------------------------
  164.  
  165.  
  166. def test_suite():
  167.     return unittest.makeSuite(CompatibilityTestCase)
  168.  
  169.  
  170. if __name__ == '__main__':
  171.     unittest.main(defaultTest='test_suite')
  172.