11import unittest
2+ import dbm
23import shelve
3- import glob
4- from test import support
4+ import pickle
5+ import os
6+
57from test .support import os_helper
68from collections .abc import MutableMapping
79from test .test_dbm import dbm_iterator
@@ -41,12 +43,8 @@ def copy(self):
4143
4244
4345class TestCase (unittest .TestCase ):
44-
45- fn = "shelftemp.db"
46-
47- def tearDown (self ):
48- for f in glob .glob (self .fn + "*" ):
49- os_helper .unlink (f )
46+ dirname = os_helper .TESTFN
47+ fn = os .path .join (os_helper .TESTFN , "shelftemp.db" )
5048
5149 def test_close (self ):
5250 d1 = {}
@@ -63,29 +61,34 @@ def test_close(self):
6361 else :
6462 self .fail ('Closed shelf should not find a key' )
6563
66- def test_ascii_file_shelf (self ):
67- s = shelve .open (self .fn , protocol = 0 )
64+ def test_open_template (self , filename = None , protocol = None ):
65+ os .mkdir (self .dirname )
66+ self .addCleanup (os_helper .rmtree , self .dirname )
67+ s = shelve .open (filename = filename if filename is not None else self .fn ,
68+ protocol = protocol )
6869 try :
6970 s ['key1' ] = (1 ,2 ,3 ,4 )
7071 self .assertEqual (s ['key1' ], (1 ,2 ,3 ,4 ))
7172 finally :
7273 s .close ()
7374
75+ def test_ascii_file_shelf (self ):
76+ self .test_open_template (protocol = 0 )
77+
7478 def test_binary_file_shelf (self ):
75- s = shelve .open (self .fn , protocol = 1 )
76- try :
77- s ['key1' ] = (1 ,2 ,3 ,4 )
78- self .assertEqual (s ['key1' ], (1 ,2 ,3 ,4 ))
79- finally :
80- s .close ()
79+ self .test_open_template (protocol = 1 )
8180
8281 def test_proto2_file_shelf (self ):
83- s = shelve .open (self .fn , protocol = 2 )
84- try :
85- s ['key1' ] = (1 ,2 ,3 ,4 )
86- self .assertEqual (s ['key1' ], (1 ,2 ,3 ,4 ))
87- finally :
88- s .close ()
82+ self .test_open_template (protocol = 2 )
83+
84+ def test_pathlib_path_file_shelf (self ):
85+ self .test_open_template (filename = os_helper .FakePath (self .fn ))
86+
87+ def test_bytes_path_file_shelf (self ):
88+ self .test_open_template (filename = os .fsencode (self .fn ))
89+
90+ def test_pathlib_bytes_path_file_shelf (self ):
91+ self .test_open_template (filename = os_helper .FakePath (os .fsencode (self .fn )))
8992
9093 def test_in_memory_shelf (self ):
9194 d1 = byteskeydict ()
@@ -160,65 +163,54 @@ def test_with(self):
160163
161164 def test_default_protocol (self ):
162165 with shelve .Shelf ({}) as s :
163- self .assertEqual (s ._protocol , 3 )
166+ self .assertEqual (s ._protocol , pickle . DEFAULT_PROTOCOL )
164167
165- from test import mapping_tests
166168
167- class TestShelveBase (mapping_tests .BasicTestMappingProtocol ):
168- fn = "shelftemp.db"
169- counter = 0
170- def __init__ (self , * args , ** kw ):
171- self ._db = []
172- mapping_tests .BasicTestMappingProtocol .__init__ (self , * args , ** kw )
169+ class TestShelveBase :
173170 type2test = shelve .Shelf
171+
174172 def _reference (self ):
175173 return {"key1" :"value1" , "key2" :2 , "key3" :(1 ,2 ,3 )}
174+
175+
176+ class TestShelveInMemBase (TestShelveBase ):
176177 def _empty_mapping (self ):
177- if self ._in_mem :
178- x = shelve .Shelf (byteskeydict (), ** self ._args )
179- else :
180- self .counter += 1
181- x = shelve .open (self .fn + str (self .counter ), ** self ._args )
182- self ._db .append (x )
178+ return shelve .Shelf (byteskeydict (), ** self ._args )
179+
180+
181+ class TestShelveFileBase (TestShelveBase ):
182+ counter = 0
183+
184+ def _empty_mapping (self ):
185+ self .counter += 1
186+ x = shelve .open (self .base_path + str (self .counter ), ** self ._args )
187+ self .addCleanup (x .close )
183188 return x
184- def tearDown (self ):
185- for db in self ._db :
186- db .close ()
187- self ._db = []
188- if not self ._in_mem :
189- for f in glob .glob (self .fn + "*" ):
190- os_helper .unlink (f )
191-
192- class TestAsciiFileShelve (TestShelveBase ):
193- _args = {'protocol' :0 }
194- _in_mem = False
195- class TestBinaryFileShelve (TestShelveBase ):
196- _args = {'protocol' :1 }
197- _in_mem = False
198- class TestProto2FileShelve (TestShelveBase ):
199- _args = {'protocol' :2 }
200- _in_mem = False
201- class TestAsciiMemShelve (TestShelveBase ):
202- _args = {'protocol' :0 }
203- _in_mem = True
204- class TestBinaryMemShelve (TestShelveBase ):
205- _args = {'protocol' :1 }
206- _in_mem = True
207- class TestProto2MemShelve (TestShelveBase ):
208- _args = {'protocol' :2 }
209- _in_mem = True
210-
211- def test_main ():
212- for module in dbm_iterator ():
213- support .run_unittest (
214- TestAsciiFileShelve ,
215- TestBinaryFileShelve ,
216- TestProto2FileShelve ,
217- TestAsciiMemShelve ,
218- TestBinaryMemShelve ,
219- TestProto2MemShelve ,
220- TestCase
221- )
189+
190+ def setUp (self ):
191+ dirname = os_helper .TESTFN
192+ os .mkdir (dirname )
193+ self .addCleanup (os_helper .rmtree , dirname )
194+ self .base_path = os .path .join (dirname , "shelftemp.db" )
195+ self .addCleanup (setattr , dbm , '_defaultmod' , dbm ._defaultmod )
196+ dbm ._defaultmod = self .dbm_mod
197+
198+
199+ from test import mapping_tests
200+
201+ for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
202+ bases = (TestShelveInMemBase , mapping_tests .BasicTestMappingProtocol )
203+ name = f'TestProto{ proto } MemShelve'
204+ globals ()[name ] = type (name , bases ,
205+ {'_args' : {'protocol' : proto }})
206+ bases = (TestShelveFileBase , mapping_tests .BasicTestMappingProtocol )
207+ for dbm_mod in dbm_iterator ():
208+ assert dbm_mod .__name__ .startswith ('dbm.' )
209+ suffix = dbm_mod .__name__ [4 :]
210+ name = f'TestProto{ proto } File_{ suffix } Shelve'
211+ globals ()[name ] = type (name , bases ,
212+ {'dbm_mod' : dbm_mod , '_args' : {'protocol' : proto }})
213+
222214
223215if __name__ == "__main__" :
224- test_main ()
216+ unittest . main ()
0 commit comments