11import unittest
2+ import dbm
3+ import os
24import shelve
35import glob
6+ import pickle
47from test import support
58from collections .abc import MutableMapping
69from test .test_dbm import dbm_iterator
@@ -40,12 +43,8 @@ def copy(self):
4043
4144
4245class 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" )
4948
5049 def test_close (self ):
5150 d1 = {}
@@ -62,29 +61,24 @@ def test_close(self):
6261 else :
6362 self .fail ('Closed shelf should not find a key' )
6463
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 )
6768 try :
6869 s ['key1' ] = (1 ,2 ,3 ,4 )
6970 self .assertEqual (s ['key1' ], (1 ,2 ,3 ,4 ))
7071 finally :
7172 s .close ()
7273
74+ def test_ascii_file_shelf (self ):
75+ self .test_open_template (protocol = 0 )
76+
7377 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 )
8079
8180 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 )
8882
8983 def test_in_memory_shelf (self ):
9084 d1 = byteskeydict ()
@@ -161,63 +155,52 @@ def test_default_protocol(self):
161155 with shelve .Shelf ({}) as s :
162156 self .assertEqual (s ._protocol , 3 )
163157
164- from test import mapping_tests
165158
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 :
172160 type2test = shelve .Shelf
161+
173162 def _reference (self ):
174163 return {"key1" :"value1" , "key2" :2 , "key3" :(1 ,2 ,3 )}
164+
165+
166+ class TestShelveInMemBase (TestShelveBase ):
175167 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 )
182178 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+
221204
222205if __name__ == "__main__" :
223- test_main ()
206+ unittest . main ()
0 commit comments