blob: bd51d868fee3baaf83d1729e8544075fe7b146b4 [file] [log] [blame]
Skip Montanaro3bf99e32002-12-08 18:36:24 +00001import unittest
2import shelve
3import glob
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test import support
Raymond Hettinger57d1a882011-02-23 00:46:28 +00005from collections.abc import MutableMapping
Georg Brandl24420152008-05-26 16:32:26 +00006from test.test_dbm import dbm_iterator
Martin v. Löwis79c32082007-08-11 06:57:14 +00007
8def L1(s):
9 return s.decode("latin-1")
10
Raymond Hettingeredf3b732008-02-04 22:07:15 +000011class byteskeydict(MutableMapping):
Martin v. Löwis79c32082007-08-11 06:57:14 +000012 "Mapping that supports bytes keys"
13
14 def __init__(self):
15 self.d = {}
16
17 def __getitem__(self, key):
18 return self.d[L1(key)]
19
20 def __setitem__(self, key, value):
21 self.d[L1(key)] = value
22
23 def __delitem__(self, key):
24 del self.d[L1(key)]
25
Raymond Hettingeredf3b732008-02-04 22:07:15 +000026 def __len__(self):
27 return len(self.d)
28
Martin v. Löwis79c32082007-08-11 06:57:14 +000029 def iterkeys(self):
30 for k in self.d.keys():
Guido van Rossum83800a62007-08-29 02:57:31 +000031 yield k.encode("latin-1")
Martin v. Löwis79c32082007-08-11 06:57:14 +000032
Raymond Hettingeredf3b732008-02-04 22:07:15 +000033 __iter__ = iterkeys
34
Martin v. Löwis79c32082007-08-11 06:57:14 +000035 def keys(self):
36 return list(self.iterkeys())
37
38 def copy(self):
39 return byteskeydict(self.d)
40
Skip Montanaro3bf99e32002-12-08 18:36:24 +000041
42class TestCase(unittest.TestCase):
43
Guido van Rossum94b59bb2007-05-18 22:12:08 +000044 fn = "shelftemp.db"
Tim Peters6578dc92002-12-24 18:31:27 +000045
Guido van Rossum8d9db042007-05-18 21:57:09 +000046 def tearDown(self):
47 for f in glob.glob(self.fn+"*"):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000048 support.unlink(f)
Guido van Rossum8d9db042007-05-18 21:57:09 +000049
Benjamin Petersond6313712008-07-31 16:23:04 +000050 def test_close(self):
51 d1 = {}
52 s = shelve.Shelf(d1, protocol=2, writeback=False)
53 s['key1'] = [1,2,3,4]
54 self.assertEqual(s['key1'], [1,2,3,4])
55 self.assertEqual(len(s), 1)
56 s.close()
57 self.assertRaises(ValueError, len, s)
58 try:
59 s['key1']
60 except ValueError:
61 pass
62 else:
63 self.fail('Closed shelf should not find a key')
64
Skip Montanaro3bf99e32002-12-08 18:36:24 +000065 def test_ascii_file_shelf(self):
Guido van Rossum8d9db042007-05-18 21:57:09 +000066 s = shelve.open(self.fn, protocol=0)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000067 try:
Skip Montanaro3bf99e32002-12-08 18:36:24 +000068 s['key1'] = (1,2,3,4)
69 self.assertEqual(s['key1'], (1,2,3,4))
Skip Montanaro3bf99e32002-12-08 18:36:24 +000070 finally:
Guido van Rossum8d9db042007-05-18 21:57:09 +000071 s.close()
Skip Montanaro3bf99e32002-12-08 18:36:24 +000072
73 def test_binary_file_shelf(self):
Guido van Rossum8d9db042007-05-18 21:57:09 +000074 s = shelve.open(self.fn, protocol=1)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000075 try:
Skip Montanaro3bf99e32002-12-08 18:36:24 +000076 s['key1'] = (1,2,3,4)
77 self.assertEqual(s['key1'], (1,2,3,4))
Skip Montanaro3bf99e32002-12-08 18:36:24 +000078 finally:
Guido van Rossum8d9db042007-05-18 21:57:09 +000079 s.close()
Skip Montanaro3bf99e32002-12-08 18:36:24 +000080
Martin v. Löwis153c9e42003-04-19 20:59:03 +000081 def test_proto2_file_shelf(self):
Guido van Rossum8d9db042007-05-18 21:57:09 +000082 s = shelve.open(self.fn, protocol=2)
Martin v. Löwis153c9e42003-04-19 20:59:03 +000083 try:
Martin v. Löwis153c9e42003-04-19 20:59:03 +000084 s['key1'] = (1,2,3,4)
85 self.assertEqual(s['key1'], (1,2,3,4))
Martin v. Löwis153c9e42003-04-19 20:59:03 +000086 finally:
Guido van Rossum8d9db042007-05-18 21:57:09 +000087 s.close()
Martin v. Löwis153c9e42003-04-19 20:59:03 +000088
Skip Montanaro3bf99e32002-12-08 18:36:24 +000089 def test_in_memory_shelf(self):
Martin v. Löwis79c32082007-08-11 06:57:14 +000090 d1 = byteskeydict()
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000091 s = shelve.Shelf(d1, protocol=0)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000092 s['key1'] = (1,2,3,4)
93 self.assertEqual(s['key1'], (1,2,3,4))
94 s.close()
Martin v. Löwis79c32082007-08-11 06:57:14 +000095 d2 = byteskeydict()
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000096 s = shelve.Shelf(d2, protocol=1)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000097 s['key1'] = (1,2,3,4)
98 self.assertEqual(s['key1'], (1,2,3,4))
99 s.close()
100
101 self.assertEqual(len(d1), 1)
Guido van Rossum83800a62007-08-29 02:57:31 +0000102 self.assertEqual(len(d2), 1)
103 self.assertNotEqual(d1.items(), d2.items())
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000104
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000105 def test_mutable_entry(self):
Martin v. Löwis79c32082007-08-11 06:57:14 +0000106 d1 = byteskeydict()
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000107 s = shelve.Shelf(d1, protocol=2, writeback=False)
108 s['key1'] = [1,2,3,4]
109 self.assertEqual(s['key1'], [1,2,3,4])
110 s['key1'].append(5)
111 self.assertEqual(s['key1'], [1,2,3,4])
112 s.close()
113
Martin v. Löwis79c32082007-08-11 06:57:14 +0000114 d2 = byteskeydict()
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000115 s = shelve.Shelf(d2, protocol=2, writeback=True)
116 s['key1'] = [1,2,3,4]
117 self.assertEqual(s['key1'], [1,2,3,4])
118 s['key1'].append(5)
119 self.assertEqual(s['key1'], [1,2,3,4,5])
120 s.close()
121
122 self.assertEqual(len(d1), 1)
123 self.assertEqual(len(d2), 1)
124
Georg Brandl732324a2010-12-04 11:12:43 +0000125 def test_keyencoding(self):
126 d = {}
127 key = 'Pöp'
128 # the default keyencoding is utf-8
129 shelve.Shelf(d)[key] = [1]
130 self.assertIn(key.encode('utf-8'), d)
131 # but a different one can be given
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000132 shelve.Shelf(d, keyencoding='latin-1')[key] = [1]
133 self.assertIn(key.encode('latin-1'), d)
Georg Brandl732324a2010-12-04 11:12:43 +0000134 # with all consequences
135 s = shelve.Shelf(d, keyencoding='ascii')
136 self.assertRaises(UnicodeEncodeError, s.__setitem__, key, [1])
137
R. David Murrayddb3ed02010-02-11 02:42:19 +0000138 def test_writeback_also_writes_immediately(self):
139 # Issue 5754
140 d = {}
141 key = 'key'
142 encodedkey = key.encode('utf-8')
143 s = shelve.Shelf(d, writeback=True)
144 s[key] = [1]
145 p1 = d[encodedkey] # Will give a KeyError if backing store not updated
146 s['key'].append(2)
147 s.close()
148 p2 = d[encodedkey]
149 self.assertNotEqual(p1, p2) # Write creates new object in store
150
Andrew Svetlovef08fb12012-10-06 13:52:19 +0300151 def test_with(self):
152 d1 = {}
153 with shelve.Shelf(d1, protocol=2, writeback=False) as s:
154 s['key1'] = [1,2,3,4]
155 self.assertEqual(s['key1'], [1,2,3,4])
156 self.assertEqual(len(s), 1)
157 self.assertRaises(ValueError, len, s)
158 try:
159 s['key1']
160 except ValueError:
161 pass
162 else:
163 self.fail('Closed shelf should not find a key')
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000164
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000165from test import mapping_tests
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000166
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000167class TestShelveBase(mapping_tests.BasicTestMappingProtocol):
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000168 fn = "shelftemp.db"
169 counter = 0
170 def __init__(self, *args, **kw):
171 self._db = []
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000172 mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw)
Walter Dörwald118f9312004-06-02 18:42:25 +0000173 type2test = shelve.Shelf
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000174 def _reference(self):
175 return {"key1":"value1", "key2":2, "key3":(1,2,3)}
176 def _empty_mapping(self):
177 if self._in_mem:
Martin v. Löwis79c32082007-08-11 06:57:14 +0000178 x= shelve.Shelf(byteskeydict(), **self._args)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000179 else:
180 self.counter+=1
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000181 x= shelve.open(self.fn+str(self.counter), **self._args)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000182 self._db.append(x)
183 return x
184 def tearDown(self):
185 for db in self._db:
186 db.close()
187 self._db = []
188 if not self._in_mem:
189 for f in glob.glob(self.fn+"*"):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000190 support.unlink(f)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000191
192class TestAsciiFileShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000193 _args={'protocol':0}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000194 _in_mem = False
195class TestBinaryFileShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000196 _args={'protocol':1}
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000197 _in_mem = False
198class TestProto2FileShelve(TestShelveBase):
199 _args={'protocol':2}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000200 _in_mem = False
201class TestAsciiMemShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000202 _args={'protocol':0}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000203 _in_mem = True
204class TestBinaryMemShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000205 _args={'protocol':1}
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000206 _in_mem = True
207class TestProto2MemShelve(TestShelveBase):
208 _args={'protocol':2}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000209 _in_mem = True
210
211def test_main():
Gregory P. Smith178fefb2007-08-24 21:59:45 +0000212 for module in dbm_iterator():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000213 support.run_unittest(
Gregory P. Smith178fefb2007-08-24 21:59:45 +0000214 TestAsciiFileShelve,
215 TestBinaryFileShelve,
216 TestProto2FileShelve,
217 TestAsciiMemShelve,
218 TestBinaryMemShelve,
219 TestProto2MemShelve,
220 TestCase
221 )
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000222
223if __name__ == "__main__":
224 test_main()