blob: 3b20281a02b9b9e660889287607b35f64469d95d [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
93
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +000094from test import mapping_tests
Skip Montanaro3bf99e32002-12-08 18:36:24 +000095
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +000096class TestShelveBase(mapping_tests.BasicTestMappingProtocol):
Raymond Hettinger2c2d3222003-03-09 07:05:43 +000097 fn = "shelftemp.db"
98 counter = 0
99 def __init__(self, *args, **kw):
100 self._db = []
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000101 mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw)
Walter Dörwald118f9312004-06-02 18:42:25 +0000102 type2test = shelve.Shelf
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000103 def _reference(self):
104 return {"key1":"value1", "key2":2, "key3":(1,2,3)}
105 def _empty_mapping(self):
106 if self._in_mem:
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000107 x= shelve.Shelf({}, **self._args)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000108 else:
109 self.counter+=1
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000110 x= shelve.open(self.fn+str(self.counter), **self._args)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000111 self._db.append(x)
112 return x
113 def tearDown(self):
114 for db in self._db:
115 db.close()
116 self._db = []
117 if not self._in_mem:
118 for f in glob.glob(self.fn+"*"):
Steven Betharded427e72008-03-18 16:00:19 +0000119 test_support.unlink(f)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000120
121class TestAsciiFileShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000122 _args={'protocol':0}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000123 _in_mem = False
124class TestBinaryFileShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000125 _args={'protocol':1}
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000126 _in_mem = False
127class TestProto2FileShelve(TestShelveBase):
128 _args={'protocol':2}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000129 _in_mem = False
130class TestAsciiMemShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000131 _args={'protocol':0}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000132 _in_mem = True
133class TestBinaryMemShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000134 _args={'protocol':1}
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000135 _in_mem = True
136class TestProto2MemShelve(TestShelveBase):
137 _args={'protocol':2}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000138 _in_mem = True
139
140def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000141 test_support.run_unittest(
142 TestAsciiFileShelve,
143 TestBinaryFileShelve,
144 TestProto2FileShelve,
145 TestAsciiMemShelve,
146 TestBinaryMemShelve,
147 TestProto2MemShelve,
148 TestCase
149 )
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000150
151if __name__ == "__main__":
152 test_main()