blob: df56625a599843b0885c8ff45b19bf144a35b733 [file] [log] [blame]
Skip Montanaro3bf99e32002-12-08 18:36:24 +00001import os
2import unittest
3import shelve
4import glob
5from test import test_support
6
Ezio Melottia2d46532010-01-30 07:22:54 +00007test_support.import_module('anydbm', deprecated=True)
8
Skip Montanaro3bf99e32002-12-08 18:36:24 +00009class TestCase(unittest.TestCase):
10
Martin v. Löwisa94568a2003-05-10 07:36:56 +000011 fn = "shelftemp" + os.extsep + "db"
Tim Peters6578dc92002-12-24 18:31:27 +000012
Raymond Hettinger8c664e82008-07-25 18:43:33 +000013 def test_close(self):
14 d1 = {}
15 s = shelve.Shelf(d1, protocol=2, writeback=False)
16 s['key1'] = [1,2,3,4]
17 self.assertEqual(s['key1'], [1,2,3,4])
18 self.assertEqual(len(s), 1)
19 s.close()
20 self.assertRaises(ValueError, len, s)
21 try:
22 s['key1']
23 except ValueError:
24 pass
25 else:
26 self.fail('Closed shelf should not find a key')
27
Skip Montanaro3bf99e32002-12-08 18:36:24 +000028 def test_ascii_file_shelf(self):
29 try:
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000030 s = shelve.open(self.fn, protocol=0)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000031 s['key1'] = (1,2,3,4)
32 self.assertEqual(s['key1'], (1,2,3,4))
33 s.close()
34 finally:
35 for f in glob.glob(self.fn+"*"):
36 os.unlink(f)
37
38 def test_binary_file_shelf(self):
39 try:
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000040 s = shelve.open(self.fn, protocol=1)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000041 s['key1'] = (1,2,3,4)
42 self.assertEqual(s['key1'], (1,2,3,4))
43 s.close()
44 finally:
45 for f in glob.glob(self.fn+"*"):
46 os.unlink(f)
47
Martin v. Löwis153c9e42003-04-19 20:59:03 +000048 def test_proto2_file_shelf(self):
49 try:
50 s = shelve.open(self.fn, protocol=2)
51 s['key1'] = (1,2,3,4)
52 self.assertEqual(s['key1'], (1,2,3,4))
53 s.close()
54 finally:
55 for f in glob.glob(self.fn+"*"):
56 os.unlink(f)
57
Skip Montanaro3bf99e32002-12-08 18:36:24 +000058 def test_in_memory_shelf(self):
59 d1 = {}
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000060 s = shelve.Shelf(d1, protocol=0)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000061 s['key1'] = (1,2,3,4)
62 self.assertEqual(s['key1'], (1,2,3,4))
63 s.close()
64 d2 = {}
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000065 s = shelve.Shelf(d2, protocol=1)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000066 s['key1'] = (1,2,3,4)
67 self.assertEqual(s['key1'], (1,2,3,4))
68 s.close()
69
70 self.assertEqual(len(d1), 1)
71 self.assertNotEqual(d1, d2)
72
Martin v. Löwis153c9e42003-04-19 20:59:03 +000073 def test_mutable_entry(self):
74 d1 = {}
75 s = shelve.Shelf(d1, protocol=2, writeback=False)
76 s['key1'] = [1,2,3,4]
77 self.assertEqual(s['key1'], [1,2,3,4])
78 s['key1'].append(5)
79 self.assertEqual(s['key1'], [1,2,3,4])
80 s.close()
81
82 d2 = {}
83 s = shelve.Shelf(d2, protocol=2, writeback=True)
84 s['key1'] = [1,2,3,4]
85 self.assertEqual(s['key1'], [1,2,3,4])
86 s['key1'].append(5)
87 self.assertEqual(s['key1'], [1,2,3,4,5])
88 s.close()
89
90 self.assertEqual(len(d1), 1)
91 self.assertEqual(len(d2), 1)
92
R. David Murray7c29f072010-02-11 01:38:42 +000093 def test_writeback_also_writes_immediately(self):
94 # Issue 5754
95 d = {}
96 s = shelve.Shelf(d, writeback=True)
97 s['key'] = [1]
98 p1 = d['key'] # Will give a KeyError if backing store not updated
99 s['key'].append(2)
100 s.close()
101 p2 = d['key']
102 self.assertNotEqual(p1, p2) # Write creates new object in store
103
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000104
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000105from test import mapping_tests
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000106
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000107class TestShelveBase(mapping_tests.BasicTestMappingProtocol):
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000108 fn = "shelftemp.db"
109 counter = 0
110 def __init__(self, *args, **kw):
111 self._db = []
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000112 mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw)
Walter Dörwald118f9312004-06-02 18:42:25 +0000113 type2test = shelve.Shelf
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000114 def _reference(self):
115 return {"key1":"value1", "key2":2, "key3":(1,2,3)}
116 def _empty_mapping(self):
117 if self._in_mem:
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000118 x= shelve.Shelf({}, **self._args)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000119 else:
120 self.counter+=1
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000121 x= shelve.open(self.fn+str(self.counter), **self._args)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000122 self._db.append(x)
123 return x
124 def tearDown(self):
125 for db in self._db:
126 db.close()
127 self._db = []
128 if not self._in_mem:
129 for f in glob.glob(self.fn+"*"):
Steven Betharded427e72008-03-18 16:00:19 +0000130 test_support.unlink(f)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000131
132class TestAsciiFileShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000133 _args={'protocol':0}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000134 _in_mem = False
135class TestBinaryFileShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000136 _args={'protocol':1}
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000137 _in_mem = False
138class TestProto2FileShelve(TestShelveBase):
139 _args={'protocol':2}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000140 _in_mem = False
141class TestAsciiMemShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000142 _args={'protocol':0}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000143 _in_mem = True
144class TestBinaryMemShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000145 _args={'protocol':1}
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000146 _in_mem = True
147class TestProto2MemShelve(TestShelveBase):
148 _args={'protocol':2}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000149 _in_mem = True
150
151def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000152 test_support.run_unittest(
153 TestAsciiFileShelve,
154 TestBinaryFileShelve,
155 TestProto2FileShelve,
156 TestAsciiMemShelve,
157 TestBinaryMemShelve,
158 TestProto2MemShelve,
159 TestCase
160 )
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000161
162if __name__ == "__main__":
163 test_main()