0001 #! /usr/bin/env python 0002 """Test script for the dumbdbm module 0003 Original by Roger E. Masse 0004 """ 0005 0006 import os 0007 import unittest 0008 import dumbdbm 0009 from test import test_support 0010 0011 _fname = test_support.TESTFN 0012 0013 def _delete_files(): 0014 for ext in [".dir", ".dat", ".bak"]: 0015 try: 0016 os.unlink(_fname + ext) 0017 except OSError: 0018 pass 0019 0020 class DumbDBMTestCase(unittest.TestCase): 0021 _dict = {'0': '', 0022 'a': 'Python:', 0023 'b': 'Programming', 0024 'c': 'the', 0025 'd': 'way', 0026 'f': 'Guido', 0027 'g': 'intended' 0028 } 0029 0030 def __init__(self, *args): 0031 unittest.TestCase.__init__(self, *args) 0032 0033 def test_dumbdbm_creation(self): 0034 f = dumbdbm.open(_fname, 'c') 0035 self.assertEqual(f.keys(), []) 0036 for key in self._dict: 0037 f[key] = self._dict[key] 0038 self.read_helper(f) 0039 f.close() 0040 0041 def test_close_twice(self): 0042 f = dumbdbm.open(_fname) 0043 f['a'] = 'b' 0044 self.assertEqual(f['a'], 'b') 0045 f.close() 0046 f.close() 0047 0048 def test_dumbdbm_modification(self): 0049 self.init_db() 0050 f = dumbdbm.open(_fname, 'w') 0051 self._dict['g'] = f['g'] = "indented" 0052 self.read_helper(f) 0053 f.close() 0054 0055 def test_dumbdbm_read(self): 0056 self.init_db() 0057 f = dumbdbm.open(_fname, 'r') 0058 self.read_helper(f) 0059 f.close() 0060 0061 def test_dumbdbm_keys(self): 0062 self.init_db() 0063 f = dumbdbm.open(_fname) 0064 keys = self.keys_helper(f) 0065 f.close() 0066 0067 def test_write_write_read(self): 0068 # test for bug #482460 0069 f = dumbdbm.open(_fname) 0070 f['1'] = 'hello' 0071 f['1'] = 'hello2' 0072 f.close() 0073 f = dumbdbm.open(_fname) 0074 self.assertEqual(f['1'], 'hello2') 0075 f.close() 0076 0077 def read_helper(self, f): 0078 keys = self.keys_helper(f) 0079 for key in self._dict: 0080 self.assertEqual(self._dict[key], f[key]) 0081 0082 def init_db(self): 0083 f = dumbdbm.open(_fname, 'w') 0084 for k in self._dict: 0085 f[k] = self._dict[k] 0086 f.close() 0087 0088 def keys_helper(self, f): 0089 keys = f.keys() 0090 keys.sort() 0091 dkeys = self._dict.keys() 0092 dkeys.sort() 0093 self.assertEqual(keys, dkeys) 0094 return keys 0095 0096 # Perform randomized operations. This doesn't make assumptions about 0097 # what *might* fail. 0098 def test_random(self): 0099 import random 0100 d = {} # mirror the database 0101 for dummy in range(5): 0102 f = dumbdbm.open(_fname) 0103 for dummy in range(100): 0104 k = random.choice('abcdefghijklm') 0105 if random.random() < 0.2: 0106 if k in d: 0107 del d[k] 0108 del f[k] 0109 else: 0110 v = random.choice('abc') * random.randrange(10000) 0111 d[k] = v 0112 f[k] = v 0113 self.assertEqual(f[k], v) 0114 f.close() 0115 0116 f = dumbdbm.open(_fname) 0117 expected = d.items() 0118 expected.sort() 0119 got = f.items() 0120 got.sort() 0121 self.assertEqual(expected, got) 0122 f.close() 0123 0124 def tearDown(self): 0125 _delete_files() 0126 0127 def setUp(self): 0128 _delete_files() 0129 0130 def test_main(): 0131 try: 0132 test_support.run_unittest(DumbDBMTestCase) 0133 finally: 0134 _delete_files() 0135 0136 if __name__ == "__main__": 0137 test_main() 0138
Generated by PyXR 0.9.4