0001 #! /usr/bin/env python 0002 """Test script for the anydbm module 0003 based on testdumbdbm.py 0004 """ 0005 0006 import os 0007 import unittest 0008 import anydbm 0009 import glob 0010 from test import test_support 0011 0012 _fname = test_support.TESTFN 0013 0014 def _delete_files(): 0015 # we don't know the precise name the underlying database uses 0016 # so we use glob to locate all names 0017 for f in glob.glob(_fname + "*"): 0018 try: 0019 os.unlink(f) 0020 except OSError: 0021 pass 0022 0023 class AnyDBMTestCase(unittest.TestCase): 0024 _dict = {'0': '', 0025 'a': 'Python:', 0026 'b': 'Programming', 0027 'c': 'the', 0028 'd': 'way', 0029 'f': 'Guido', 0030 'g': 'intended' 0031 } 0032 0033 def __init__(self, *args): 0034 unittest.TestCase.__init__(self, *args) 0035 0036 def test_anydbm_creation(self): 0037 f = anydbm.open(_fname, 'c') 0038 self.assertEqual(f.keys(), []) 0039 for key in self._dict: 0040 f[key] = self._dict[key] 0041 self.read_helper(f) 0042 f.close() 0043 0044 def test_anydbm_modification(self): 0045 self.init_db() 0046 f = anydbm.open(_fname, 'c') 0047 self._dict['g'] = f['g'] = "indented" 0048 self.read_helper(f) 0049 f.close() 0050 0051 def test_anydbm_read(self): 0052 self.init_db() 0053 f = anydbm.open(_fname, 'r') 0054 self.read_helper(f) 0055 f.close() 0056 0057 def test_anydbm_keys(self): 0058 self.init_db() 0059 f = anydbm.open(_fname, 'r') 0060 keys = self.keys_helper(f) 0061 f.close() 0062 0063 def read_helper(self, f): 0064 keys = self.keys_helper(f) 0065 for key in self._dict: 0066 self.assertEqual(self._dict[key], f[key]) 0067 0068 def init_db(self): 0069 f = anydbm.open(_fname, 'n') 0070 for k in self._dict: 0071 f[k] = self._dict[k] 0072 f.close() 0073 0074 def keys_helper(self, f): 0075 keys = f.keys() 0076 keys.sort() 0077 dkeys = self._dict.keys() 0078 dkeys.sort() 0079 self.assertEqual(keys, dkeys) 0080 return keys 0081 0082 def tearDown(self): 0083 _delete_files() 0084 0085 def setUp(self): 0086 _delete_files() 0087 0088 def test_main(): 0089 try: 0090 test_support.run_unittest(AnyDBMTestCase) 0091 finally: 0092 _delete_files() 0093 0094 if __name__ == "__main__": 0095 test_main() 0096
Generated by PyXR 0.9.4