blob: ffcc98da56e367c79f74d7d296be572b2427098c [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
91
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +000092from test import mapping_tests
Skip Montanaro3bf99e32002-12-08 18:36:24 +000093
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +000094class TestShelveBase(mapping_tests.BasicTestMappingProtocol):
Raymond Hettinger2c2d3222003-03-09 07:05:43 +000095 fn = "shelftemp.db"
96 counter = 0
97 def __init__(self, *args, **kw):
98 self._db = []
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +000099 mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw)
Walter Dörwald118f9312004-06-02 18:42:25 +0000100 type2test = shelve.Shelf
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000101 def _reference(self):
102 return {"key1":"value1", "key2":2, "key3":(1,2,3)}
103 def _empty_mapping(self):
104 if self._in_mem:
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000105 x= shelve.Shelf({}, **self._args)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000106 else:
107 self.counter+=1
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000108 x= shelve.open(self.fn+str(self.counter), **self._args)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000109 self._db.append(x)
110 return x
111 def tearDown(self):
112 for db in self._db:
113 db.close()
114 self._db = []
115 if not self._in_mem:
116 for f in glob.glob(self.fn+"*"):
Steven Betharded427e72008-03-18 16:00:19 +0000117 test_support.unlink(f)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000118
119class TestAsciiFileShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000120 _args={'protocol':0}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000121 _in_mem = False
122class TestBinaryFileShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000123 _args={'protocol':1}
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000124 _in_mem = False
125class TestProto2FileShelve(TestShelveBase):
126 _args={'protocol':2}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000127 _in_mem = False
128class TestAsciiMemShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000129 _args={'protocol':0}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000130 _in_mem = True
131class TestBinaryMemShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000132 _args={'protocol':1}
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000133 _in_mem = True
134class TestProto2MemShelve(TestShelveBase):
135 _args={'protocol':2}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000136 _in_mem = True
137
138def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000139 test_support.run_unittest(
140 TestAsciiFileShelve,
141 TestBinaryFileShelve,
142 TestProto2FileShelve,
143 TestAsciiMemShelve,
144 TestBinaryMemShelve,
145 TestProto2MemShelve,
146 TestCase
147 )
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000148
149if __name__ == "__main__":
150 test_main()