bpo-45502: Fix test_shelve (GH-29003)

Run test_shelve with all underlying dbm implementations and pickle protocols.
Also make test_shelve discoverable.
This commit is contained in:
Serhiy Storchaka 2021-10-22 17:14:58 +03:00 committed by GitHub
parent 03e9f5dc75
commit b781cc3bfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 44 additions and 56 deletions

View File

@ -1,4 +1,5 @@
import unittest import unittest
import dbm
import shelve import shelve
import glob import glob
import pickle import pickle
@ -44,12 +45,8 @@ class byteskeydict(MutableMapping):
class TestCase(unittest.TestCase): class TestCase(unittest.TestCase):
dirname = os_helper.TESTFN
fn = "shelftemp.db" fn = os.path.join(os_helper.TESTFN, "shelftemp.db")
def tearDown(self):
for f in glob.glob(self.fn+"*"):
os_helper.unlink(f)
def test_close(self): def test_close(self):
d1 = {} d1 = {}
@ -67,6 +64,8 @@ class TestCase(unittest.TestCase):
self.fail('Closed shelf should not find a key') self.fail('Closed shelf should not find a key')
def test_open_template(self, filename=None, protocol=None): def test_open_template(self, filename=None, protocol=None):
os.mkdir(self.dirname)
self.addCleanup(os_helper.rmtree, self.dirname)
s = shelve.open(filename=filename if filename is not None else self.fn, s = shelve.open(filename=filename if filename is not None else self.fn,
protocol=protocol) protocol=protocol)
try: try:
@ -168,63 +167,52 @@ class TestCase(unittest.TestCase):
with shelve.Shelf({}) as s: with shelve.Shelf({}) as s:
self.assertEqual(s._protocol, pickle.DEFAULT_PROTOCOL) self.assertEqual(s._protocol, pickle.DEFAULT_PROTOCOL)
from test import mapping_tests
class TestShelveBase(mapping_tests.BasicTestMappingProtocol): class TestShelveBase:
fn = "shelftemp.db"
counter = 0
def __init__(self, *args, **kw):
self._db = []
mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw)
type2test = shelve.Shelf type2test = shelve.Shelf
def _reference(self): def _reference(self):
return {"key1":"value1", "key2":2, "key3":(1,2,3)} return {"key1":"value1", "key2":2, "key3":(1,2,3)}
class TestShelveInMemBase(TestShelveBase):
def _empty_mapping(self): def _empty_mapping(self):
if self._in_mem: return shelve.Shelf(byteskeydict(), **self._args)
x= shelve.Shelf(byteskeydict(), **self._args)
else:
self.counter+=1 class TestShelveFileBase(TestShelveBase):
x= shelve.open(self.fn+str(self.counter), **self._args) counter = 0
self._db.append(x)
def _empty_mapping(self):
self.counter += 1
x = shelve.open(self.base_path + str(self.counter), **self._args)
self.addCleanup(x.close)
return x return x
def tearDown(self):
for db in self._db:
db.close()
self._db = []
if not self._in_mem:
for f in glob.glob(self.fn+"*"):
os_helper.unlink(f)
class TestAsciiFileShelve(TestShelveBase): def setUp(self):
_args={'protocol':0} dirname = os_helper.TESTFN
_in_mem = False os.mkdir(dirname)
class TestBinaryFileShelve(TestShelveBase): self.addCleanup(os_helper.rmtree, dirname)
_args={'protocol':1} self.base_path = os.path.join(dirname, "shelftemp.db")
_in_mem = False self.addCleanup(setattr, dbm, '_defaultmod', dbm._defaultmod)
class TestProto2FileShelve(TestShelveBase): dbm._defaultmod = self.dbm_mod
_args={'protocol':2}
_in_mem = False
class TestAsciiMemShelve(TestShelveBase): from test import mapping_tests
_args={'protocol':0}
_in_mem = True for proto in range(pickle.HIGHEST_PROTOCOL + 1):
class TestBinaryMemShelve(TestShelveBase): bases = (TestShelveInMemBase, mapping_tests.BasicTestMappingProtocol)
_args={'protocol':1} name = f'TestProto{proto}MemShelve'
_in_mem = True globals()[name] = type(name, bases,
class TestProto2MemShelve(TestShelveBase): {'_args': {'protocol': proto}})
_args={'protocol':2} bases = (TestShelveFileBase, mapping_tests.BasicTestMappingProtocol)
_in_mem = True for dbm_mod in dbm_iterator():
assert dbm_mod.__name__.startswith('dbm.')
suffix = dbm_mod.__name__[4:]
name = f'TestProto{proto}File_{suffix}Shelve'
globals()[name] = type(name, bases,
{'dbm_mod': dbm_mod, '_args': {'protocol': proto}})
def test_main():
for module in dbm_iterator():
support.run_unittest(
TestAsciiFileShelve,
TestBinaryFileShelve,
TestProto2FileShelve,
TestAsciiMemShelve,
TestBinaryMemShelve,
TestProto2MemShelve,
TestCase
)
if __name__ == "__main__": if __name__ == "__main__":
test_main() unittest.main()