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_dumbdbm.py < prev    next >
Text File  |  2003-12-30  |  3KB  |  138 lines

  1. #! /usr/bin/env python
  2. """Test script for the dumbdbm module
  3.    Original by Roger E. Masse
  4. """
  5.  
  6. import os
  7. import unittest
  8. import dumbdbm
  9. from test import test_support
  10.  
  11. _fname = test_support.TESTFN
  12.  
  13. def _delete_files():
  14.     for ext in [".dir", ".dat", ".bak"]:
  15.         try:
  16.             os.unlink(_fname + ext)
  17.         except OSError:
  18.             pass
  19.  
  20. class DumbDBMTestCase(unittest.TestCase):
  21.     _dict = {'0': '',
  22.              'a': 'Python:',
  23.              'b': 'Programming',
  24.              'c': 'the',
  25.              'd': 'way',
  26.              'f': 'Guido',
  27.              'g': 'intended'
  28.              }
  29.  
  30.     def __init__(self, *args):
  31.         unittest.TestCase.__init__(self, *args)
  32.  
  33.     def test_dumbdbm_creation(self):
  34.         f = dumbdbm.open(_fname, 'c')
  35.         self.assertEqual(f.keys(), [])
  36.         for key in self._dict:
  37.             f[key] = self._dict[key]
  38.         self.read_helper(f)
  39.         f.close()
  40.  
  41.     def test_close_twice(self):
  42.         f = dumbdbm.open(_fname)
  43.         f['a'] = 'b'
  44.         self.assertEqual(f['a'], 'b')
  45.         f.close()
  46.         f.close()
  47.  
  48.     def test_dumbdbm_modification(self):
  49.         self.init_db()
  50.         f = dumbdbm.open(_fname, 'w')
  51.         self._dict['g'] = f['g'] = "indented"
  52.         self.read_helper(f)
  53.         f.close()
  54.  
  55.     def test_dumbdbm_read(self):
  56.         self.init_db()
  57.         f = dumbdbm.open(_fname, 'r')
  58.         self.read_helper(f)
  59.         f.close()
  60.  
  61.     def test_dumbdbm_keys(self):
  62.         self.init_db()
  63.         f = dumbdbm.open(_fname)
  64.         keys = self.keys_helper(f)
  65.         f.close()
  66.  
  67.     def test_write_write_read(self):
  68.         # test for bug #482460
  69.         f = dumbdbm.open(_fname)
  70.         f['1'] = 'hello'
  71.         f['1'] = 'hello2'
  72.         f.close()
  73.         f = dumbdbm.open(_fname)
  74.         self.assertEqual(f['1'], 'hello2')
  75.         f.close()
  76.  
  77.     def read_helper(self, f):
  78.         keys = self.keys_helper(f)
  79.         for key in self._dict:
  80.             self.assertEqual(self._dict[key], f[key])
  81.  
  82.     def init_db(self):
  83.         f = dumbdbm.open(_fname, 'w')
  84.         for k in self._dict:
  85.             f[k] = self._dict[k]
  86.         f.close()
  87.  
  88.     def keys_helper(self, f):
  89.         keys = f.keys()
  90.         keys.sort()
  91.         dkeys = self._dict.keys()
  92.         dkeys.sort()
  93.         self.assertEqual(keys, dkeys)
  94.         return keys
  95.  
  96.     # Perform randomized operations.  This doesn't make assumptions about
  97.     # what *might* fail.
  98.     def test_random(self):
  99.         import random
  100.         d = {}  # mirror the database
  101.         for dummy in range(5):
  102.             f = dumbdbm.open(_fname)
  103.             for dummy in range(100):
  104.                 k = random.choice('abcdefghijklm')
  105.                 if random.random() < 0.2:
  106.                     if k in d:
  107.                         del d[k]
  108.                         del f[k]
  109.                 else:
  110.                     v = random.choice('abc') * random.randrange(10000)
  111.                     d[k] = v
  112.                     f[k] = v
  113.                     self.assertEqual(f[k], v)
  114.             f.close()
  115.  
  116.             f = dumbdbm.open(_fname)
  117.             expected = d.items()
  118.             expected.sort()
  119.             got = f.items()
  120.             got.sort()
  121.             self.assertEqual(expected, got)
  122.             f.close()
  123.  
  124.     def tearDown(self):
  125.         _delete_files()
  126.  
  127.     def setUp(self):
  128.         _delete_files()
  129.  
  130. def test_main():
  131.     try:
  132.         test_support.run_unittest(DumbDBMTestCase)
  133.     finally:
  134.         _delete_files()
  135.  
  136. if __name__ == "__main__":
  137.     test_main()
  138.