0001 import os 0002 import unittest 0003 import shelve 0004 import glob 0005 from test import test_support 0006 0007 class TestCase(unittest.TestCase): 0008 0009 fn = "shelftemp" + os.extsep + "db" 0010 0011 def test_ascii_file_shelf(self): 0012 try: 0013 s = shelve.open(self.fn, binary=False) 0014 s['key1'] = (1,2,3,4) 0015 self.assertEqual(s['key1'], (1,2,3,4)) 0016 s.close() 0017 finally: 0018 for f in glob.glob(self.fn+"*"): 0019 os.unlink(f) 0020 0021 def test_binary_file_shelf(self): 0022 try: 0023 s = shelve.open(self.fn, binary=True) 0024 s['key1'] = (1,2,3,4) 0025 self.assertEqual(s['key1'], (1,2,3,4)) 0026 s.close() 0027 finally: 0028 for f in glob.glob(self.fn+"*"): 0029 os.unlink(f) 0030 0031 def test_proto2_file_shelf(self): 0032 try: 0033 s = shelve.open(self.fn, protocol=2) 0034 s['key1'] = (1,2,3,4) 0035 self.assertEqual(s['key1'], (1,2,3,4)) 0036 s.close() 0037 finally: 0038 for f in glob.glob(self.fn+"*"): 0039 os.unlink(f) 0040 0041 def test_in_memory_shelf(self): 0042 d1 = {} 0043 s = shelve.Shelf(d1, binary=False) 0044 s['key1'] = (1,2,3,4) 0045 self.assertEqual(s['key1'], (1,2,3,4)) 0046 s.close() 0047 d2 = {} 0048 s = shelve.Shelf(d2, binary=True) 0049 s['key1'] = (1,2,3,4) 0050 self.assertEqual(s['key1'], (1,2,3,4)) 0051 s.close() 0052 0053 self.assertEqual(len(d1), 1) 0054 self.assertNotEqual(d1, d2) 0055 0056 def test_mutable_entry(self): 0057 d1 = {} 0058 s = shelve.Shelf(d1, protocol=2, writeback=False) 0059 s['key1'] = [1,2,3,4] 0060 self.assertEqual(s['key1'], [1,2,3,4]) 0061 s['key1'].append(5) 0062 self.assertEqual(s['key1'], [1,2,3,4]) 0063 s.close() 0064 0065 d2 = {} 0066 s = shelve.Shelf(d2, protocol=2, writeback=True) 0067 s['key1'] = [1,2,3,4] 0068 self.assertEqual(s['key1'], [1,2,3,4]) 0069 s['key1'].append(5) 0070 self.assertEqual(s['key1'], [1,2,3,4,5]) 0071 s.close() 0072 0073 self.assertEqual(len(d1), 1) 0074 self.assertEqual(len(d2), 1) 0075 0076 0077 from test import mapping_tests 0078 0079 class TestShelveBase(mapping_tests.BasicTestMappingProtocol): 0080 fn = "shelftemp.db" 0081 counter = 0 0082 def __init__(self, *args, **kw): 0083 self._db = [] 0084 mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw) 0085 type2test = shelve.Shelf 0086 def _reference(self): 0087 return {"key1":"value1", "key2":2, "key3":(1,2,3)} 0088 def _empty_mapping(self): 0089 if self._in_mem: 0090 x= shelve.Shelf({}, **self._args) 0091 else: 0092 self.counter+=1 0093 x= shelve.open(self.fn+str(self.counter), **self._args) 0094 self._db.append(x) 0095 return x 0096 def tearDown(self): 0097 for db in self._db: 0098 db.close() 0099 self._db = [] 0100 if not self._in_mem: 0101 for f in glob.glob(self.fn+"*"): 0102 os.unlink(f) 0103 0104 class TestAsciiFileShelve(TestShelveBase): 0105 _args={'binary':False} 0106 _in_mem = False 0107 class TestBinaryFileShelve(TestShelveBase): 0108 _args={'binary':True} 0109 _in_mem = False 0110 class TestProto2FileShelve(TestShelveBase): 0111 _args={'protocol':2} 0112 _in_mem = False 0113 class TestAsciiMemShelve(TestShelveBase): 0114 _args={'binary':False} 0115 _in_mem = True 0116 class TestBinaryMemShelve(TestShelveBase): 0117 _args={'binary':True} 0118 _in_mem = True 0119 class TestProto2MemShelve(TestShelveBase): 0120 _args={'protocol':2} 0121 _in_mem = True 0122 0123 def test_main(): 0124 test_support.run_unittest( 0125 TestAsciiFileShelve, 0126 TestBinaryFileShelve, 0127 TestProto2FileShelve, 0128 TestAsciiMemShelve, 0129 TestBinaryMemShelve, 0130 TestProto2MemShelve, 0131 TestCase 0132 ) 0133 0134 if __name__ == "__main__": 0135 test_main() 0136
Generated by PyXR 0.9.4