Add better error reporting for MemoryErrors caused by str->float conversions.
[python.git] / Lib / test / test_shelve.py
blobffcc98da56e367c79f74d7d296be572b2427098c
1 import os
2 import unittest
3 import shelve
4 import glob
5 from test import test_support
7 class TestCase(unittest.TestCase):
9 fn = "shelftemp" + os.extsep + "db"
11 def test_close(self):
12 d1 = {}
13 s = shelve.Shelf(d1, protocol=2, writeback=False)
14 s['key1'] = [1,2,3,4]
15 self.assertEqual(s['key1'], [1,2,3,4])
16 self.assertEqual(len(s), 1)
17 s.close()
18 self.assertRaises(ValueError, len, s)
19 try:
20 s['key1']
21 except ValueError:
22 pass
23 else:
24 self.fail('Closed shelf should not find a key')
26 def test_ascii_file_shelf(self):
27 try:
28 s = shelve.open(self.fn, protocol=0)
29 s['key1'] = (1,2,3,4)
30 self.assertEqual(s['key1'], (1,2,3,4))
31 s.close()
32 finally:
33 for f in glob.glob(self.fn+"*"):
34 os.unlink(f)
36 def test_binary_file_shelf(self):
37 try:
38 s = shelve.open(self.fn, protocol=1)
39 s['key1'] = (1,2,3,4)
40 self.assertEqual(s['key1'], (1,2,3,4))
41 s.close()
42 finally:
43 for f in glob.glob(self.fn+"*"):
44 os.unlink(f)
46 def test_proto2_file_shelf(self):
47 try:
48 s = shelve.open(self.fn, protocol=2)
49 s['key1'] = (1,2,3,4)
50 self.assertEqual(s['key1'], (1,2,3,4))
51 s.close()
52 finally:
53 for f in glob.glob(self.fn+"*"):
54 os.unlink(f)
56 def test_in_memory_shelf(self):
57 d1 = {}
58 s = shelve.Shelf(d1, protocol=0)
59 s['key1'] = (1,2,3,4)
60 self.assertEqual(s['key1'], (1,2,3,4))
61 s.close()
62 d2 = {}
63 s = shelve.Shelf(d2, protocol=1)
64 s['key1'] = (1,2,3,4)
65 self.assertEqual(s['key1'], (1,2,3,4))
66 s.close()
68 self.assertEqual(len(d1), 1)
69 self.assertNotEqual(d1, d2)
71 def test_mutable_entry(self):
72 d1 = {}
73 s = shelve.Shelf(d1, protocol=2, writeback=False)
74 s['key1'] = [1,2,3,4]
75 self.assertEqual(s['key1'], [1,2,3,4])
76 s['key1'].append(5)
77 self.assertEqual(s['key1'], [1,2,3,4])
78 s.close()
80 d2 = {}
81 s = shelve.Shelf(d2, protocol=2, writeback=True)
82 s['key1'] = [1,2,3,4]
83 self.assertEqual(s['key1'], [1,2,3,4])
84 s['key1'].append(5)
85 self.assertEqual(s['key1'], [1,2,3,4,5])
86 s.close()
88 self.assertEqual(len(d1), 1)
89 self.assertEqual(len(d2), 1)
92 from test import mapping_tests
94 class TestShelveBase(mapping_tests.BasicTestMappingProtocol):
95 fn = "shelftemp.db"
96 counter = 0
97 def __init__(self, *args, **kw):
98 self._db = []
99 mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw)
100 type2test = shelve.Shelf
101 def _reference(self):
102 return {"key1":"value1", "key2":2, "key3":(1,2,3)}
103 def _empty_mapping(self):
104 if self._in_mem:
105 x= shelve.Shelf({}, **self._args)
106 else:
107 self.counter+=1
108 x= shelve.open(self.fn+str(self.counter), **self._args)
109 self._db.append(x)
110 return x
111 def tearDown(self):
112 for db in self._db:
113 db.close()
114 self._db = []
115 if not self._in_mem:
116 for f in glob.glob(self.fn+"*"):
117 test_support.unlink(f)
119 class TestAsciiFileShelve(TestShelveBase):
120 _args={'protocol':0}
121 _in_mem = False
122 class TestBinaryFileShelve(TestShelveBase):
123 _args={'protocol':1}
124 _in_mem = False
125 class TestProto2FileShelve(TestShelveBase):
126 _args={'protocol':2}
127 _in_mem = False
128 class TestAsciiMemShelve(TestShelveBase):
129 _args={'protocol':0}
130 _in_mem = True
131 class TestBinaryMemShelve(TestShelveBase):
132 _args={'protocol':1}
133 _in_mem = True
134 class TestProto2MemShelve(TestShelveBase):
135 _args={'protocol':2}
136 _in_mem = True
138 def test_main():
139 test_support.run_unittest(
140 TestAsciiFileShelve,
141 TestBinaryFileShelve,
142 TestProto2FileShelve,
143 TestAsciiMemShelve,
144 TestBinaryMemShelve,
145 TestProto2MemShelve,
146 TestCase
149 if __name__ == "__main__":
150 test_main()