home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_287 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  5.0 KB  |  196 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import string
  6. import unittest
  7. from test_all import db, hashopen, btopen, rnopen, verbose, get_new_database_path
  8.  
  9. class CompatibilityTestCase(unittest.TestCase):
  10.     
  11.     def setUp(self):
  12.         self.filename = get_new_database_path()
  13.  
  14.     
  15.     def tearDown(self):
  16.         
  17.         try:
  18.             os.remove(self.filename)
  19.         except os.error:
  20.             pass
  21.  
  22.  
  23.     
  24.     def test01_btopen(self):
  25.         self.do_bthash_test(btopen, 'btopen')
  26.  
  27.     
  28.     def test02_hashopen(self):
  29.         self.do_bthash_test(hashopen, 'hashopen')
  30.  
  31.     
  32.     def test03_rnopen(self):
  33.         data = 'The quick brown fox jumped over the lazy dog.'.split()
  34.         if verbose:
  35.             print '\nTesting: rnopen'
  36.         
  37.         f = rnopen(self.filename, 'c')
  38.         for x in range(len(data)):
  39.             f[x + 1] = data[x]
  40.         
  41.         getTest = (f[1], f[2], f[3])
  42.         if verbose:
  43.             print '%s %s %s' % getTest
  44.         
  45.         self.assertEqual(getTest[1], 'quick', 'data mismatch!')
  46.         rv = f.set_location(3)
  47.         if rv != (3, 'brown'):
  48.             self.fail('recno database set_location failed: ' + repr(rv))
  49.         
  50.         f[25] = 'twenty-five'
  51.         f.close()
  52.         del f
  53.         f = rnopen(self.filename, 'w')
  54.         f[20] = 'twenty'
  55.         
  56.         def noRec(f):
  57.             rec = f[15]
  58.  
  59.         self.assertRaises(KeyError, noRec, f)
  60.         
  61.         def badKey(f):
  62.             rec = f['a string']
  63.  
  64.         self.assertRaises(TypeError, badKey, f)
  65.         del f[3]
  66.         rec = f.first()
  67.         while rec:
  68.             if verbose:
  69.                 print rec
  70.             
  71.             
  72.             try:
  73.                 rec = f.next()
  74.             continue
  75.             except KeyError:
  76.                 break
  77.                 continue
  78.             
  79.  
  80.             None<EXCEPTION MATCH>KeyError
  81.         f.close()
  82.  
  83.     
  84.     def test04_n_flag(self):
  85.         f = hashopen(self.filename, 'n')
  86.         f.close()
  87.  
  88.     
  89.     def do_bthash_test(self, factory, what):
  90.         if verbose:
  91.             print '\nTesting: ', what
  92.         
  93.         f = factory(self.filename, 'c')
  94.         if verbose:
  95.             print 'creation...'
  96.         
  97.         if f:
  98.             if verbose:
  99.                 print 'truth test: true'
  100.             
  101.         elif verbose:
  102.             print 'truth test: false'
  103.         
  104.         f['0'] = ''
  105.         f['a'] = 'Guido'
  106.         f['b'] = 'van'
  107.         f['c'] = 'Rossum'
  108.         f['d'] = 'invented'
  109.         f['f'] = 'Python'
  110.         if verbose:
  111.             print '%s %s %s' % (f['a'], f['b'], f['c'])
  112.         
  113.         if verbose:
  114.             print 'key ordering...'
  115.         
  116.         start = f.set_location(f.first()[0])
  117.         if start != ('0', ''):
  118.             self.fail('incorrect first() result: ' + repr(start))
  119.         
  120.         while None:
  121.             
  122.             try:
  123.                 rec = f.next()
  124.             except KeyError:
  125.                 self.assertEqual(rec, f.last(), 'Error, last <> last!')
  126.                 f.previous()
  127.                 break
  128.  
  129.             if verbose:
  130.                 print rec
  131.                 continue
  132.             continue
  133.             self.assert_(f.has_key('f'), 'Error, missing key!')
  134.             if factory == btopen:
  135.                 e = f.set_location('e')
  136.                 if e != ('f', 'Python'):
  137.                     self.fail('wrong key,value returned: ' + repr(e))
  138.                 
  139.             else:
  140.                 
  141.                 try:
  142.                     e = f.set_location('e')
  143.                 except KeyError:
  144.                     pass
  145.  
  146.                 self.fail('set_location on non-existent key did not raise KeyError')
  147.         f.sync()
  148.         f.close()
  149.         
  150.         try:
  151.             if f:
  152.                 if verbose:
  153.                     print 'truth test: true'
  154.                 
  155.             elif verbose:
  156.                 print 'truth test: false'
  157.         except db.DBError:
  158.             pass
  159.  
  160.         self.fail('Exception expected')
  161.         del f
  162.         if verbose:
  163.             print 'modification...'
  164.         
  165.         f = factory(self.filename, 'w')
  166.         f['d'] = 'discovered'
  167.         if verbose:
  168.             print 'access...'
  169.         
  170.         for key in f.keys():
  171.             word = f[key]
  172.             if verbose:
  173.                 print word
  174.                 continue
  175.         
  176.         
  177.         def noRec(f):
  178.             rec = f['no such key']
  179.  
  180.         self.assertRaises(KeyError, noRec, f)
  181.         
  182.         def badKey(f):
  183.             rec = f[15]
  184.  
  185.         self.assertRaises(TypeError, badKey, f)
  186.         f.close()
  187.  
  188.  
  189.  
  190. def test_suite():
  191.     return unittest.makeSuite(CompatibilityTestCase)
  192.  
  193. if __name__ == '__main__':
  194.     unittest.main(defaultTest = 'test_suite')
  195.  
  196.