blob: b71af2bac7403384551047e6212c513991e2c266 [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
Berker Peksag8faca612016-07-01 12:33:00 +0300165 def test_default_protocol(self):
166 with shelve.Shelf({}) as s:
167 self.assertEqual(s._protocol, 3)
168
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000169from test import mapping_tests
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000170
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000171class TestShelveBase(mapping_tests.BasicTestMappingProtocol):
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000172 fn = "shelftemp.db"
173 counter = 0
174 def __init__(self, *args, **kw):
175 self._db = []
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000176 mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw)
Walter Dörwald118f9312004-06-02 18:42:25 +0000177 type2test = shelve.Shelf
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000178 def _reference(self):
179 return {"key1":"value1", "key2":2, "key3":(1,2,3)}
180 def _empty_mapping(self):
181 if self._in_mem:
Martin v. Löwis79c32082007-08-11 06:57:14 +0000182 x= shelve.Shelf(byteskeydict(), **self._args)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000183 else:
184 self.counter+=1
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000185 x= shelve.open(self.fn+str(self.counter), **self._args)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000186 self._db.append(x)
187 return x
188 def tearDown(self):
189 for db in self._db:
190 db.close()
191 self._db = []
192 if not self._in_mem:
193 for f in glob.glob(self.fn+"*"):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000194 support.unlink(f)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000195
196class TestAsciiFileShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000197 _args={'protocol':0}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000198 _in_mem = False
199class TestBinaryFileShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000200 _args={'protocol':1}
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000201 _in_mem = False
202class TestProto2FileShelve(TestShelveBase):
203 _args={'protocol':2}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000204 _in_mem = False
205class TestAsciiMemShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000206 _args={'protocol':0}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000207 _in_mem = True
208class TestBinaryMemShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000209 _args={'protocol':1}
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000210 _in_mem = True
211class TestProto2MemShelve(TestShelveBase):
212 _args={'protocol':2}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000213 _in_mem = True
214
215def test_main():
Gregory P. Smith178fefb2007-08-24 21:59:45 +0000216 for module in dbm_iterator():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000217 support.run_unittest(
Gregory P. Smith178fefb2007-08-24 21:59:45 +0000218 TestAsciiFileShelve,
219 TestBinaryFileShelve,
220 TestProto2FileShelve,
221 TestAsciiMemShelve,
222 TestBinaryMemShelve,
223 TestProto2MemShelve,
224 TestCase
225 )
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000226
227if __name__ == "__main__":
228 test_main()