blob: 8e571218614bde6cbfe11602af1853b1f650a60e [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
Guido van Rossum8d9db042007-05-18 21:57:09 +000011 def tearDown(self):
12 for f in glob.glob(self.fn+"*"):
13 os.unlink(f)
14
Skip Montanaro3bf99e32002-12-08 18:36:24 +000015 def test_ascii_file_shelf(self):
Guido van Rossum8d9db042007-05-18 21:57:09 +000016 s = shelve.open(self.fn, protocol=0)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000017 try:
Skip Montanaro3bf99e32002-12-08 18:36:24 +000018 s['key1'] = (1,2,3,4)
19 self.assertEqual(s['key1'], (1,2,3,4))
Skip Montanaro3bf99e32002-12-08 18:36:24 +000020 finally:
Guido van Rossum8d9db042007-05-18 21:57:09 +000021 s.close()
Skip Montanaro3bf99e32002-12-08 18:36:24 +000022
23 def test_binary_file_shelf(self):
Guido van Rossum8d9db042007-05-18 21:57:09 +000024 s = shelve.open(self.fn, protocol=1)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000025 try:
Skip Montanaro3bf99e32002-12-08 18:36:24 +000026 s['key1'] = (1,2,3,4)
27 self.assertEqual(s['key1'], (1,2,3,4))
Skip Montanaro3bf99e32002-12-08 18:36:24 +000028 finally:
Guido van Rossum8d9db042007-05-18 21:57:09 +000029 s.close()
Skip Montanaro3bf99e32002-12-08 18:36:24 +000030
Martin v. Löwis153c9e42003-04-19 20:59:03 +000031 def test_proto2_file_shelf(self):
Guido van Rossum8d9db042007-05-18 21:57:09 +000032 s = shelve.open(self.fn, protocol=2)
Martin v. Löwis153c9e42003-04-19 20:59:03 +000033 try:
Martin v. Löwis153c9e42003-04-19 20:59:03 +000034 s['key1'] = (1,2,3,4)
35 self.assertEqual(s['key1'], (1,2,3,4))
Martin v. Löwis153c9e42003-04-19 20:59:03 +000036 finally:
Guido van Rossum8d9db042007-05-18 21:57:09 +000037 s.close()
Martin v. Löwis153c9e42003-04-19 20:59:03 +000038
Skip Montanaro3bf99e32002-12-08 18:36:24 +000039 def test_in_memory_shelf(self):
40 d1 = {}
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000041 s = shelve.Shelf(d1, protocol=0)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000042 s['key1'] = (1,2,3,4)
43 self.assertEqual(s['key1'], (1,2,3,4))
44 s.close()
45 d2 = {}
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000046 s = shelve.Shelf(d2, protocol=1)
Skip Montanaro3bf99e32002-12-08 18:36:24 +000047 s['key1'] = (1,2,3,4)
48 self.assertEqual(s['key1'], (1,2,3,4))
49 s.close()
50
51 self.assertEqual(len(d1), 1)
52 self.assertNotEqual(d1, d2)
53
Martin v. Löwis153c9e42003-04-19 20:59:03 +000054 def test_mutable_entry(self):
55 d1 = {}
56 s = shelve.Shelf(d1, protocol=2, writeback=False)
57 s['key1'] = [1,2,3,4]
58 self.assertEqual(s['key1'], [1,2,3,4])
59 s['key1'].append(5)
60 self.assertEqual(s['key1'], [1,2,3,4])
61 s.close()
62
63 d2 = {}
64 s = shelve.Shelf(d2, protocol=2, writeback=True)
65 s['key1'] = [1,2,3,4]
66 self.assertEqual(s['key1'], [1,2,3,4])
67 s['key1'].append(5)
68 self.assertEqual(s['key1'], [1,2,3,4,5])
69 s.close()
70
71 self.assertEqual(len(d1), 1)
72 self.assertEqual(len(d2), 1)
73
74
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +000075from test import mapping_tests
Skip Montanaro3bf99e32002-12-08 18:36:24 +000076
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +000077class TestShelveBase(mapping_tests.BasicTestMappingProtocol):
Raymond Hettinger2c2d3222003-03-09 07:05:43 +000078 fn = "shelftemp.db"
79 counter = 0
80 def __init__(self, *args, **kw):
81 self._db = []
Walter Dörwald0a6d0ff2004-05-31 16:29:04 +000082 mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw)
Walter Dörwald118f9312004-06-02 18:42:25 +000083 type2test = shelve.Shelf
Raymond Hettinger2c2d3222003-03-09 07:05:43 +000084 def _reference(self):
85 return {"key1":"value1", "key2":2, "key3":(1,2,3)}
86 def _empty_mapping(self):
87 if self._in_mem:
Martin v. Löwis153c9e42003-04-19 20:59:03 +000088 x= shelve.Shelf({}, **self._args)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +000089 else:
90 self.counter+=1
Martin v. Löwis153c9e42003-04-19 20:59:03 +000091 x= shelve.open(self.fn+str(self.counter), **self._args)
Raymond Hettinger2c2d3222003-03-09 07:05:43 +000092 self._db.append(x)
93 return x
94 def tearDown(self):
95 for db in self._db:
96 db.close()
97 self._db = []
98 if not self._in_mem:
99 for f in glob.glob(self.fn+"*"):
100 os.unlink(f)
101
102class TestAsciiFileShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000103 _args={'protocol':0}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000104 _in_mem = False
105class TestBinaryFileShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000106 _args={'protocol':1}
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000107 _in_mem = False
108class TestProto2FileShelve(TestShelveBase):
109 _args={'protocol':2}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000110 _in_mem = False
111class TestAsciiMemShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000112 _args={'protocol':0}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000113 _in_mem = True
114class TestBinaryMemShelve(TestShelveBase):
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000115 _args={'protocol':1}
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000116 _in_mem = True
117class TestProto2MemShelve(TestShelveBase):
118 _args={'protocol':2}
Raymond Hettinger2c2d3222003-03-09 07:05:43 +0000119 _in_mem = True
120
121def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000122 test_support.run_unittest(
123 TestAsciiFileShelve,
124 TestBinaryFileShelve,
125 TestProto2FileShelve,
126 TestAsciiMemShelve,
127 TestBinaryMemShelve,
128 TestProto2MemShelve,
129 TestCase
130 )
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000131
132if __name__ == "__main__":
133 test_main()