blob: f03457f97828c2305798adda78ccdd1b3e9fe654 [file] [log] [blame]
Skip Montanaro3bf99e32002-12-08 18:36:24 +00001import os
2import unittest
3import shelve
4import glob
5from test import test_support
6
7class TestCase(unittest.TestCase):
8
Martin v. Löwisa94568a2003-05-10 07:36:56 +00009 fn = "shelftemp" + os.extsep + "db"
Tim Peters6578dc92002-12-24 18:31:27 +000010
Raymond Hettinger8c664e82008-07-25 18:43:33 +000011 def test_close(self):
12 d1 = {}
13 s = shelve.Shelf(d1, protocol=2, writeback=False)
14 s['key1'] = [1,2,3,4]
15 self.assertEqual(s['key1'], [1,2,3,4])
16 self.assertEqual(len(s), 1)
17 s.close()
18 self.assertRaises(ValueError, len, s)
19 try:
20 s['key1']
21 except ValueError:
22 pass
23 else:
24 self.fail('Closed shelf should not find a key')
25
Skip Montanaro3bf99e32002-12-08 18:36:24 +000026 def test_ascii_file_shelf(self):
27 try:
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000028 s = shelve.open(self.fn, protocol=0)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000029 s['key1'] = (1,2,3,4)
30 self.assertEqual(s['key1'], (1,2,3,4))
31 s.close()
32 finally:
33 for f in glob.glob(self.fn+"*"):
34 os.unlink(f)
35
36 def test_binary_file_shelf(self):
37 try:
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000038 s = shelve.open(self.fn, protocol=1)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000039 s['key1'] = (1,2,3,4)
40 self.assertEqual(s['key1'], (1,2,3,4))
41 s.close()
42 finally:
43 for f in glob.glob(self.fn+"*"):
44 os.unlink(f)
45
Martin v. Löwis153c9e42003-04-19 20:59:03 +000046 def test_proto2_file_shelf(self):
47 try:
48 s = shelve.open(self.fn, protocol=2)
49 s['key1'] = (1,2,3,4)
50 self.assertEqual(s['key1'], (1,2,3,4))
51 s.close()
52 finally:
53 for f in glob.glob(self.fn+"*"):
54 os.unlink(f)
55
Skip Montanaro3bf99e32002-12-08 18:36:24 +000056 def test_in_memory_shelf(self):
57 d1 = {}
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000058 s = shelve.Shelf(d1, protocol=0)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000059 s['key1'] = (1,2,3,4)
60 self.assertEqual(s['key1'], (1,2,3,4))
61 s.close()
62 d2 = {}
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000063 s = shelve.Shelf(d2, protocol=1)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000064 s['key1'] = (1,2,3,4)
65 self.assertEqual(s['key1'], (1,2,3,4))
66 s.close()
67
68 self.assertEqual(len(d1), 1)
69 self.assertNotEqual(d1, d2)
70
Martin v. Löwis153c9e42003-04-19 20:59:03 +000071 def test_mutable_entry(self):
72 d1 = {}
73 s = shelve.Shelf(d1, protocol=2, writeback=False)
74 s['key1'] = [1,2,3,4]
75 self.assertEqual(s['key1'], [1,2,3,4])
76 s['key1'].append(5)
77 self.assertEqual(s['key1'], [1,2,3,4])
78 s.close()
79
80 d2 = {}
81 s = shelve.Shelf(d2, protocol=2, writeback=True)
82 s['key1'] = [1,2,3,4]
83 self.assertEqual(s['key1'], [1,2,3,4])
84 s['key1'].append(5)
85 self.assertEqual(s['key1'], [1,2,3,4,5])
86 s.close()
87
88 self.assertEqual(len(d1), 1)
89 self.assertEqual(len(d2), 1)
90
R. David Murrayb92a3052010-02-11 02:01:02 +000091 def test_writeback_also_writes_immediately(self):
92 # Issue 5754
93 d = {}
94 s = shelve.Shelf(d, writeback=True)
95 s['key'] = [1]
96 p1 = d['key'] # Will give a KeyError if backing store not updated
97 s['key'].append(2)
98 s.close()
99 p2 = d['key']
100 self.assertNotEqual(p1, p2) # Write creates new object in store
101
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000102
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000103from test import mapping_tests
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000104
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000105class TestShelveBase(mapping_tests.BasicTestMappingProtocol):
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000106 fn = "shelftemp.db"
107 counter = 0
108 def __init__(self, *args, **kw):
109 self._db = []
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +0000110 mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw)
Walter Dörwald118f9312004-06-02 18:42:25 +0000111 type2test = shelve.Shelf
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000112 def _reference(self):
113 return {"key1":"value1", "key2":2, "key3":(1,2,3)}
114 def _empty_mapping(self):
115 if self._in_mem:
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000116 x= shelve.Shelf({}, **self._args)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000117 else:
118 self.counter+=1
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000119 x= shelve.open(self.fn+str(self.counter), **self._args)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000120 self._db.append(x)
121 return x
122 def tearDown(self):
123 for db in self._db:
124 db.close()
125 self._db = []
126 if not self._in_mem:
127 for f in glob.glob(self.fn+"*"):
Steven Betharded427e72008-03-18 16:00:19 +0000128 test_support.unlink(f)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000129
130class TestAsciiFileShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000131 _args={'protocol':0}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000132 _in_mem = False
133class TestBinaryFileShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000134 _args={'protocol':1}
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000135 _in_mem = False
136class TestProto2FileShelve(TestShelveBase):
137 _args={'protocol':2}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000138 _in_mem = False
139class TestAsciiMemShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000140 _args={'protocol':0}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000141 _in_mem = True
142class TestBinaryMemShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000143 _args={'protocol':1}
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000144 _in_mem = True
145class TestProto2MemShelve(TestShelveBase):
146 _args={'protocol':2}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000147 _in_mem = True
148
149def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000150 test_support.run_unittest(
151 TestAsciiFileShelve,
152 TestBinaryFileShelve,
153 TestProto2FileShelve,
154 TestAsciiMemShelve,
155 TestBinaryMemShelve,
156 TestProto2MemShelve,
157 TestCase
158 )
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000159
160if __name__ == "__main__":
161 test_main()