11import unittest
2+ import dbm
3+ import os
24import shelve
35import glob
46import pickle
@@ -43,12 +45,8 @@ def copy(self):
4345
4446
4547class 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" )
5250
5351 def test_close (self ):
5452 d1 = {}
@@ -65,29 +63,24 @@ def test_close(self):
6563 else :
6664 self .fail ('Closed shelf should not find a key' )
6765
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 )
7070 try :
7171 s ['key1' ] = (1 ,2 ,3 ,4 )
7272 self .assertEqual (s ['key1' ], (1 ,2 ,3 ,4 ))
7373 finally :
7474 s .close ()
7575
76+ def test_ascii_file_shelf (self ):
77+ self .test_open_template (protocol = 0 )
78+
7679 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 )
8381
8482 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 )
9184
9285 def test_in_memory_shelf (self ):
9386 d1 = byteskeydict ()
@@ -164,63 +157,52 @@ def test_default_protocol(self):
164157 with shelve .Shelf ({}) as s :
165158 self .assertEqual (s ._protocol , pickle .DEFAULT_PROTOCOL )
166159
167- from test import mapping_tests
168160
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 :
175162 type2test = shelve .Shelf
163+
176164 def _reference (self ):
177165 return {"key1" :"value1" , "key2" :2 , "key3" :(1 ,2 ,3 )}
166+
167+
168+ class TestShelveInMemBase (TestShelveBase ):
178169 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 )
185180 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+
224206
225207if __name__ == "__main__" :
226- test_main ()
208+ unittest . main ()
0 commit comments