blob: e5d19f5c85ee39ca5e4d7149406bf7f11a9d3ba2 [file] [log] [blame]
Skip Montanaro3bf99e32002-12-08 18:36:24 +00001import unittest
2import shelve
3import glob
4from test import test_support
Martin v. Löwis79c32082007-08-11 06:57:14 +00005from UserDict import DictMixin
Gregory P. Smith178fefb2007-08-24 21:59:45 +00006from test.test_anydbm import dbm_iterator
Martin v. Löwis79c32082007-08-11 06:57:14 +00007
8def L1(s):
9 return s.decode("latin-1")
10
11class byteskeydict(DictMixin):
12 "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
26 def iterkeys(self):
27 for k in self.d.keys():
Guido van Rossum83800a62007-08-29 02:57:31 +000028 yield k.encode("latin-1")
Martin v. Löwis79c32082007-08-11 06:57:14 +000029
30 def keys(self):
31 return list(self.iterkeys())
32
33 def copy(self):
34 return byteskeydict(self.d)
35
Skip Montanaro3bf99e32002-12-08 18:36:24 +000036
37class TestCase(unittest.TestCase):
38
Guido van Rossum94b59bb2007-05-18 22:12:08 +000039 fn = "shelftemp.db"
Tim Peters6578dc92002-12-24 18:31:27 +000040
Guido van Rossum8d9db042007-05-18 21:57:09 +000041 def tearDown(self):
42 for f in glob.glob(self.fn+"*"):
Guido van Rossum94b59bb2007-05-18 22:12:08 +000043 test_support.unlink(f)
Guido van Rossum8d9db042007-05-18 21:57:09 +000044
Skip Montanaro3bf99e32002-12-08 18:36:24 +000045 def test_ascii_file_shelf(self):
Guido van Rossum8d9db042007-05-18 21:57:09 +000046 s = shelve.open(self.fn, protocol=0)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000047 try:
Skip Montanaro3bf99e32002-12-08 18:36:24 +000048 s['key1'] = (1,2,3,4)
49 self.assertEqual(s['key1'], (1,2,3,4))
Skip Montanaro3bf99e32002-12-08 18:36:24 +000050 finally:
Guido van Rossum8d9db042007-05-18 21:57:09 +000051 s.close()
Skip Montanaro3bf99e32002-12-08 18:36:24 +000052
53 def test_binary_file_shelf(self):
Guido van Rossum8d9db042007-05-18 21:57:09 +000054 s = shelve.open(self.fn, protocol=1)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000055 try:
Skip Montanaro3bf99e32002-12-08 18:36:24 +000056 s['key1'] = (1,2,3,4)
57 self.assertEqual(s['key1'], (1,2,3,4))
Skip Montanaro3bf99e32002-12-08 18:36:24 +000058 finally:
Guido van Rossum8d9db042007-05-18 21:57:09 +000059 s.close()
Skip Montanaro3bf99e32002-12-08 18:36:24 +000060
Martin v. Löwis153c9e42003-04-19 20:59:03 +000061 def test_proto2_file_shelf(self):
Guido van Rossum8d9db042007-05-18 21:57:09 +000062 s = shelve.open(self.fn, protocol=2)
Martin v. Löwis153c9e42003-04-19 20:59:03 +000063 try:
Martin v. Löwis153c9e42003-04-19 20:59:03 +000064 s['key1'] = (1,2,3,4)
65 self.assertEqual(s['key1'], (1,2,3,4))
Martin v. Löwis153c9e42003-04-19 20:59:03 +000066 finally:
Guido van Rossum8d9db042007-05-18 21:57:09 +000067 s.close()
Martin v. Löwis153c9e42003-04-19 20:59:03 +000068
Skip Montanaro3bf99e32002-12-08 18:36:24 +000069 def test_in_memory_shelf(self):
Martin v. Löwis79c32082007-08-11 06:57:14 +000070 d1 = byteskeydict()
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000071 s = shelve.Shelf(d1, protocol=0)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000072 s['key1'] = (1,2,3,4)
73 self.assertEqual(s['key1'], (1,2,3,4))
74 s.close()
Martin v. Löwis79c32082007-08-11 06:57:14 +000075 d2 = byteskeydict()
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000076 s = shelve.Shelf(d2, protocol=1)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000077 s['key1'] = (1,2,3,4)
78 self.assertEqual(s['key1'], (1,2,3,4))
79 s.close()
80
81 self.assertEqual(len(d1), 1)
Guido van Rossum83800a62007-08-29 02:57:31 +000082 self.assertEqual(len(d2), 1)
83 self.assertNotEqual(d1.items(), d2.items())
Skip Montanaro3bf99e32002-12-08 18:36:24 +000084
Martin v. Löwis153c9e42003-04-19 20:59:03 +000085 def test_mutable_entry(self):
Martin v. Löwis79c32082007-08-11 06:57:14 +000086 d1 = byteskeydict()
Martin v. Löwis153c9e42003-04-19 20:59:03 +000087 s = shelve.Shelf(d1, protocol=2, writeback=False)
88 s['key1'] = [1,2,3,4]
89 self.assertEqual(s['key1'], [1,2,3,4])
90 s['key1'].append(5)
91 self.assertEqual(s['key1'], [1,2,3,4])
92 s.close()
93
Martin v. Löwis79c32082007-08-11 06:57:14 +000094 d2 = byteskeydict()
Martin v. Löwis153c9e42003-04-19 20:59:03 +000095 s = shelve.Shelf(d2, protocol=2, writeback=True)
96 s['key1'] = [1,2,3,4]
97 self.assertEqual(s['key1'], [1,2,3,4])
98 s['key1'].append(5)
99 self.assertEqual(s['key1'], [1,2,3,4,5])
100 s.close()
101
102 self.assertEqual(len(d1), 1)
103 self.assertEqual(len(d2), 1)
104
105
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000106from test import mapping_tests
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000107
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000108class TestShelveBase(mapping_tests.BasicTestMappingProtocol):
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000109 fn = "shelftemp.db"
110 counter = 0
111 def __init__(self, *args, **kw):
112 self._db = []
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000113 mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw)
Walter Dörwald118f9312004-06-02 18:42:25 +0000114 type2test = shelve.Shelf
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000115 def _reference(self):
116 return {"key1":"value1", "key2":2, "key3":(1,2,3)}
117 def _empty_mapping(self):
118 if self._in_mem:
Martin v. Löwis79c32082007-08-11 06:57:14 +0000119 x= shelve.Shelf(byteskeydict(), **self._args)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000120 else:
121 self.counter+=1
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000122 x= shelve.open(self.fn+str(self.counter), **self._args)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000123 self._db.append(x)
124 return x
125 def tearDown(self):
126 for db in self._db:
127 db.close()
128 self._db = []
129 if not self._in_mem:
130 for f in glob.glob(self.fn+"*"):
Guido van Rossum94b59bb2007-05-18 22:12:08 +0000131 test_support.unlink(f)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000132
133class TestAsciiFileShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000134 _args={'protocol':0}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000135 _in_mem = False
136class TestBinaryFileShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000137 _args={'protocol':1}
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000138 _in_mem = False
139class TestProto2FileShelve(TestShelveBase):
140 _args={'protocol':2}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000141 _in_mem = False
142class TestAsciiMemShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000143 _args={'protocol':0}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000144 _in_mem = True
145class TestBinaryMemShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000146 _args={'protocol':1}
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000147 _in_mem = True
148class TestProto2MemShelve(TestShelveBase):
149 _args={'protocol':2}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000150 _in_mem = True
151
152def test_main():
Gregory P. Smith178fefb2007-08-24 21:59:45 +0000153 for module in dbm_iterator():
154 test_support.run_unittest(
155 TestAsciiFileShelve,
156 TestBinaryFileShelve,
157 TestProto2FileShelve,
158 TestAsciiMemShelve,
159 TestBinaryMemShelve,
160 TestProto2MemShelve,
161 TestCase
162 )
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000163
164if __name__ == "__main__":
165 test_main()