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