blob: 447d06d70ebe1bc9f0b72042e38c6b1d924f1cf0 [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
Skip Montanaro3bf99e32002-12-08 18:36:24 +000011 def test_ascii_file_shelf(self):
12 try:
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000013 s = shelve.open(self.fn, protocol=0)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000014 s['key1'] = (1,2,3,4)
15 self.assertEqual(s['key1'], (1,2,3,4))
16 s.close()
17 finally:
18 for f in glob.glob(self.fn+"*"):
19 os.unlink(f)
20
21 def test_binary_file_shelf(self):
22 try:
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000023 s = shelve.open(self.fn, protocol=1)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000024 s['key1'] = (1,2,3,4)
25 self.assertEqual(s['key1'], (1,2,3,4))
26 s.close()
27 finally:
28 for f in glob.glob(self.fn+"*"):
29 os.unlink(f)
30
Martin v. Löwis153c9e42003-04-19 20:59:03 +000031 def test_proto2_file_shelf(self):
32 try:
33 s = shelve.open(self.fn, protocol=2)
34 s['key1'] = (1,2,3,4)
35 self.assertEqual(s['key1'], (1,2,3,4))
36 s.close()
37 finally:
38 for f in glob.glob(self.fn+"*"):
39 os.unlink(f)
40
Skip Montanaro3bf99e32002-12-08 18:36:24 +000041 def test_in_memory_shelf(self):
42 d1 = {}
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000043 s = shelve.Shelf(d1, protocol=0)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000044 s['key1'] = (1,2,3,4)
45 self.assertEqual(s['key1'], (1,2,3,4))
46 s.close()
47 d2 = {}
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000048 s = shelve.Shelf(d2, protocol=1)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000049 s['key1'] = (1,2,3,4)
50 self.assertEqual(s['key1'], (1,2,3,4))
51 s.close()
52
53 self.assertEqual(len(d1), 1)
54 self.assertNotEqual(d1, d2)
55
Martin v. Löwis153c9e42003-04-19 20:59:03 +000056 def test_mutable_entry(self):
57 d1 = {}
58 s = shelve.Shelf(d1, protocol=2, writeback=False)
59 s['key1'] = [1,2,3,4]
60 self.assertEqual(s['key1'], [1,2,3,4])
61 s['key1'].append(5)
62 self.assertEqual(s['key1'], [1,2,3,4])
63 s.close()
64
65 d2 = {}
66 s = shelve.Shelf(d2, protocol=2, writeback=True)
67 s['key1'] = [1,2,3,4]
68 self.assertEqual(s['key1'], [1,2,3,4])
69 s['key1'].append(5)
70 self.assertEqual(s['key1'], [1,2,3,4,5])
71 s.close()
72
73 self.assertEqual(len(d1), 1)
74 self.assertEqual(len(d2), 1)
75
76
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +000077from test import mapping_tests
Skip Montanaro3bf99e32002-12-08 18:36:24 +000078
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +000079class TestShelveBase(mapping_tests.BasicTestMappingProtocol):
Raymond Hettinger2c2d3222003-03-09 07:05:43 +000080 fn = "shelftemp.db"
81 counter = 0
82 def __init__(self, *args, **kw):
83 self._db = []
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +000084 mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw)
Walter Dörwald118f9312004-06-02 18:42:25 +000085 type2test = shelve.Shelf
Raymond Hettinger2c2d3222003-03-09 07:05:43 +000086 def _reference(self):
87 return {"key1":"value1", "key2":2, "key3":(1,2,3)}
88 def _empty_mapping(self):
89 if self._in_mem:
Martin v. Löwis153c9e42003-04-19 20:59:03 +000090 x= shelve.Shelf({}, **self._args)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +000091 else:
92 self.counter+=1
Martin v. Löwis153c9e42003-04-19 20:59:03 +000093 x= shelve.open(self.fn+str(self.counter), **self._args)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +000094 self._db.append(x)
95 return x
96 def tearDown(self):
97 for db in self._db:
98 db.close()
99 self._db = []
100 if not self._in_mem:
101 for f in glob.glob(self.fn+"*"):
102 os.unlink(f)
103
104class TestAsciiFileShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000105 _args={'protocol':0}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000106 _in_mem = False
107class TestBinaryFileShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000108 _args={'protocol':1}
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000109 _in_mem = False
110class TestProto2FileShelve(TestShelveBase):
111 _args={'protocol':2}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000112 _in_mem = False
113class TestAsciiMemShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000114 _args={'protocol':0}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000115 _in_mem = True
116class TestBinaryMemShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000117 _args={'protocol':1}
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000118 _in_mem = True
119class TestProto2MemShelve(TestShelveBase):
120 _args={'protocol':2}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000121 _in_mem = True
122
123def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000124 test_support.run_unittest(
125 TestAsciiFileShelve,
126 TestBinaryFileShelve,
127 TestProto2FileShelve,
128 TestAsciiMemShelve,
129 TestBinaryMemShelve,
130 TestProto2MemShelve,
131 TestCase
132 )
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000133
134if __name__ == "__main__":
135 test_main()