|
1 import os |
|
2 import unittest |
|
3 import shelve |
|
4 import glob |
|
5 from test import test_support |
|
6 |
|
7 class TestCase(unittest.TestCase): |
|
8 |
|
9 fn = "shelftemp" + os.extsep + "db" |
|
10 |
|
11 def test_ascii_file_shelf(self): |
|
12 try: |
|
13 s = shelve.open(self.fn, protocol=0) |
|
14 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: |
|
23 s = shelve.open(self.fn, protocol=1) |
|
24 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 |
|
31 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 |
|
41 def test_in_memory_shelf(self): |
|
42 d1 = {} |
|
43 s = shelve.Shelf(d1, protocol=0) |
|
44 s['key1'] = (1,2,3,4) |
|
45 self.assertEqual(s['key1'], (1,2,3,4)) |
|
46 s.close() |
|
47 d2 = {} |
|
48 s = shelve.Shelf(d2, protocol=1) |
|
49 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 |
|
56 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 |
|
77 from test import mapping_tests |
|
78 |
|
79 class TestShelveBase(mapping_tests.BasicTestMappingProtocol): |
|
80 fn = "shelftemp.db" |
|
81 counter = 0 |
|
82 def __init__(self, *args, **kw): |
|
83 self._db = [] |
|
84 mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw) |
|
85 type2test = shelve.Shelf |
|
86 def _reference(self): |
|
87 return {"key1":"value1", "key2":2, "key3":(1,2,3)} |
|
88 def _empty_mapping(self): |
|
89 if self._in_mem: |
|
90 x= shelve.Shelf({}, **self._args) |
|
91 else: |
|
92 self.counter+=1 |
|
93 x= shelve.open(self.fn+str(self.counter), **self._args) |
|
94 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 |
|
104 class TestAsciiFileShelve(TestShelveBase): |
|
105 _args={'protocol':0} |
|
106 _in_mem = False |
|
107 class TestBinaryFileShelve(TestShelveBase): |
|
108 _args={'protocol':1} |
|
109 _in_mem = False |
|
110 class TestProto2FileShelve(TestShelveBase): |
|
111 _args={'protocol':2} |
|
112 _in_mem = False |
|
113 class TestAsciiMemShelve(TestShelveBase): |
|
114 _args={'protocol':0} |
|
115 _in_mem = True |
|
116 class TestBinaryMemShelve(TestShelveBase): |
|
117 _args={'protocol':1} |
|
118 _in_mem = True |
|
119 class TestProto2MemShelve(TestShelveBase): |
|
120 _args={'protocol':2} |
|
121 _in_mem = True |
|
122 |
|
123 def test_main(): |
|
124 test_support.run_unittest( |
|
125 TestAsciiFileShelve, |
|
126 TestBinaryFileShelve, |
|
127 TestProto2FileShelve, |
|
128 TestAsciiMemShelve, |
|
129 TestBinaryMemShelve, |
|
130 TestProto2MemShelve, |
|
131 TestCase |
|
132 ) |
|
133 |
|
134 if __name__ == "__main__": |
|
135 test_main() |