1
1
import unittest
2
+ import dbm
3
+ import os
2
4
import shelve
3
5
import glob
4
6
import pickle
@@ -43,12 +45,8 @@ def copy(self):
43
45
44
46
45
47
class TestCase (unittest .TestCase ):
46
-
47
- fn = "shelftemp.db"
48
-
49
- def tearDown (self ):
50
- for f in glob .glob (self .fn + "*" ):
51
- os_helper .unlink (f )
48
+ dirname = os_helper .TESTFN
49
+ fn = os .path .join (os_helper .TESTFN , "shelftemp.db" )
52
50
53
51
def test_close (self ):
54
52
d1 = {}
@@ -65,29 +63,24 @@ def test_close(self):
65
63
else :
66
64
self .fail ('Closed shelf should not find a key' )
67
65
68
- def test_ascii_file_shelf (self ):
69
- s = shelve .open (self .fn , protocol = 0 )
66
+ def test_open_template (self , protocol = None ):
67
+ os .mkdir (self .dirname )
68
+ self .addCleanup (os_helper .rmtree , self .dirname )
69
+ s = shelve .open (self .fn , protocol = protocol )
70
70
try :
71
71
s ['key1' ] = (1 ,2 ,3 ,4 )
72
72
self .assertEqual (s ['key1' ], (1 ,2 ,3 ,4 ))
73
73
finally :
74
74
s .close ()
75
75
76
+ def test_ascii_file_shelf (self ):
77
+ self .test_open_template (protocol = 0 )
78
+
76
79
def test_binary_file_shelf (self ):
77
- s = shelve .open (self .fn , protocol = 1 )
78
- try :
79
- s ['key1' ] = (1 ,2 ,3 ,4 )
80
- self .assertEqual (s ['key1' ], (1 ,2 ,3 ,4 ))
81
- finally :
82
- s .close ()
80
+ self .test_open_template (protocol = 1 )
83
81
84
82
def test_proto2_file_shelf (self ):
85
- s = shelve .open (self .fn , protocol = 2 )
86
- try :
87
- s ['key1' ] = (1 ,2 ,3 ,4 )
88
- self .assertEqual (s ['key1' ], (1 ,2 ,3 ,4 ))
89
- finally :
90
- s .close ()
83
+ self .test_open_template (protocol = 2 )
91
84
92
85
def test_in_memory_shelf (self ):
93
86
d1 = byteskeydict ()
@@ -164,63 +157,52 @@ def test_default_protocol(self):
164
157
with shelve .Shelf ({}) as s :
165
158
self .assertEqual (s ._protocol , pickle .DEFAULT_PROTOCOL )
166
159
167
- from test import mapping_tests
168
160
169
- class TestShelveBase (mapping_tests .BasicTestMappingProtocol ):
170
- fn = "shelftemp.db"
171
- counter = 0
172
- def __init__ (self , * args , ** kw ):
173
- self ._db = []
174
- mapping_tests .BasicTestMappingProtocol .__init__ (self , * args , ** kw )
161
+ class TestShelveBase :
175
162
type2test = shelve .Shelf
163
+
176
164
def _reference (self ):
177
165
return {"key1" :"value1" , "key2" :2 , "key3" :(1 ,2 ,3 )}
166
+
167
+
168
+ class TestShelveInMemBase (TestShelveBase ):
178
169
def _empty_mapping (self ):
179
- if self ._in_mem :
180
- x = shelve .Shelf (byteskeydict (), ** self ._args )
181
- else :
182
- self .counter += 1
183
- x = shelve .open (self .fn + str (self .counter ), ** self ._args )
184
- self ._db .append (x )
170
+ return shelve .Shelf (byteskeydict (), ** self ._args )
171
+
172
+
173
+ class TestShelveFileBase (TestShelveBase ):
174
+ counter = 0
175
+
176
+ def _empty_mapping (self ):
177
+ self .counter += 1
178
+ x = shelve .open (self .base_path + str (self .counter ), ** self ._args )
179
+ self .addCleanup (x .close )
185
180
return x
186
- def tearDown (self ):
187
- for db in self ._db :
188
- db .close ()
189
- self ._db = []
190
- if not self ._in_mem :
191
- for f in glob .glob (self .fn + "*" ):
192
- os_helper .unlink (f )
193
-
194
- class TestAsciiFileShelve (TestShelveBase ):
195
- _args = {'protocol' :0 }
196
- _in_mem = False
197
- class TestBinaryFileShelve (TestShelveBase ):
198
- _args = {'protocol' :1 }
199
- _in_mem = False
200
- class TestProto2FileShelve (TestShelveBase ):
201
- _args = {'protocol' :2 }
202
- _in_mem = False
203
- class TestAsciiMemShelve (TestShelveBase ):
204
- _args = {'protocol' :0 }
205
- _in_mem = True
206
- class TestBinaryMemShelve (TestShelveBase ):
207
- _args = {'protocol' :1 }
208
- _in_mem = True
209
- class TestProto2MemShelve (TestShelveBase ):
210
- _args = {'protocol' :2 }
211
- _in_mem = True
212
-
213
- def test_main ():
214
- for module in dbm_iterator ():
215
- support .run_unittest (
216
- TestAsciiFileShelve ,
217
- TestBinaryFileShelve ,
218
- TestProto2FileShelve ,
219
- TestAsciiMemShelve ,
220
- TestBinaryMemShelve ,
221
- TestProto2MemShelve ,
222
- TestCase
223
- )
181
+
182
+ def setUp (self ):
183
+ dirname = os_helper .TESTFN
184
+ os .mkdir (dirname )
185
+ self .addCleanup (os_helper .rmtree , dirname )
186
+ self .base_path = os .path .join (dirname , "shelftemp.db" )
187
+ self .addCleanup (setattr , dbm , '_defaultmod' , dbm ._defaultmod )
188
+ dbm ._defaultmod = self .dbm_mod
189
+
190
+
191
+ from test import mapping_tests
192
+
193
+ for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
194
+ bases = (TestShelveInMemBase , mapping_tests .BasicTestMappingProtocol )
195
+ name = f'TestProto{ proto } MemShelve'
196
+ globals ()[name ] = type (name , bases ,
197
+ {'_args' : {'protocol' : proto }})
198
+ bases = (TestShelveFileBase , mapping_tests .BasicTestMappingProtocol )
199
+ for dbm_mod in dbm_iterator ():
200
+ assert dbm_mod .__name__ .startswith ('dbm.' )
201
+ suffix = dbm_mod .__name__ [4 :]
202
+ name = f'TestProto{ proto } File_{ suffix } Shelve'
203
+ globals ()[name ] = type (name , bases ,
204
+ {'dbm_mod' : dbm_mod , '_args' : {'protocol' : proto }})
205
+
224
206
225
207
if __name__ == "__main__" :
226
- test_main ()
208
+ unittest . main ()
0 commit comments