2008-05-26 07:30:20 -03:00
|
|
|
"""Test script for the dbm.open function based on testdumbdbm.py"""
|
|
|
|
|
|
|
|
import unittest
|
2021-10-19 16:11:28 -03:00
|
|
|
import dbm
|
2021-09-10 09:26:16 -03:00
|
|
|
import os
|
2020-07-06 06:15:08 -03:00
|
|
|
from test.support import import_helper
|
|
|
|
from test.support import os_helper
|
2008-05-26 07:30:20 -03:00
|
|
|
|
2013-07-07 08:15:08 -03:00
|
|
|
try:
|
|
|
|
from dbm import ndbm
|
|
|
|
except ImportError:
|
|
|
|
ndbm = None
|
|
|
|
|
2021-10-19 16:11:28 -03:00
|
|
|
dirname = os_helper.TESTFN
|
|
|
|
_fname = os.path.join(dirname, os_helper.TESTFN)
|
2008-05-26 07:30:20 -03:00
|
|
|
|
|
|
|
#
|
2021-10-19 16:11:28 -03:00
|
|
|
# Iterates over every database module supported by dbm currently available.
|
2008-05-26 07:30:20 -03:00
|
|
|
#
|
|
|
|
def dbm_iterator():
|
2008-05-28 05:43:17 -03:00
|
|
|
for name in dbm._names:
|
|
|
|
try:
|
|
|
|
mod = __import__(name, fromlist=['open'])
|
|
|
|
except ImportError:
|
|
|
|
continue
|
|
|
|
dbm._modules[name] = mod
|
|
|
|
yield mod
|
2008-05-26 07:30:20 -03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Clean up all scratch databases we might have created during testing
|
|
|
|
#
|
2021-10-19 16:11:28 -03:00
|
|
|
def cleaunup_test_dir():
|
|
|
|
os_helper.rmtree(dirname)
|
|
|
|
|
|
|
|
def setup_test_dir():
|
|
|
|
cleaunup_test_dir()
|
|
|
|
os.mkdir(dirname)
|
2008-05-26 07:30:20 -03:00
|
|
|
|
|
|
|
|
2013-03-01 05:23:28 -04:00
|
|
|
class AnyDBMTestCase:
|
2018-04-29 09:45:03 -03:00
|
|
|
_dict = {'a': b'Python:',
|
2008-05-26 07:30:20 -03:00
|
|
|
'b': b'Programming',
|
|
|
|
'c': b'the',
|
|
|
|
'd': b'way',
|
|
|
|
'f': b'Guido',
|
|
|
|
'g': b'intended',
|
|
|
|
}
|
|
|
|
|
2008-05-28 05:43:17 -03:00
|
|
|
def init_db(self):
|
|
|
|
f = dbm.open(_fname, 'n')
|
|
|
|
for k in self._dict:
|
|
|
|
f[k.encode("ascii")] = self._dict[k]
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def keys_helper(self, f):
|
|
|
|
keys = sorted(k.decode("ascii") for k in f.keys())
|
|
|
|
dkeys = sorted(self._dict.keys())
|
|
|
|
self.assertEqual(keys, dkeys)
|
|
|
|
return keys
|
|
|
|
|
|
|
|
def test_error(self):
|
2012-12-25 10:47:37 -04:00
|
|
|
self.assertTrue(issubclass(self.module.error, OSError))
|
2008-05-26 07:30:20 -03:00
|
|
|
|
2008-09-25 19:27:43 -03:00
|
|
|
def test_anydbm_not_existing(self):
|
|
|
|
self.assertRaises(dbm.error, dbm.open, _fname)
|
|
|
|
|
2008-05-26 07:30:20 -03:00
|
|
|
def test_anydbm_creation(self):
|
|
|
|
f = dbm.open(_fname, 'c')
|
|
|
|
self.assertEqual(list(f.keys()), [])
|
|
|
|
for key in self._dict:
|
|
|
|
f[key.encode("ascii")] = self._dict[key]
|
|
|
|
self.read_helper(f)
|
|
|
|
f.close()
|
|
|
|
|
2011-03-14 17:03:54 -03:00
|
|
|
def test_anydbm_creation_n_file_exists_with_invalid_contents(self):
|
2011-06-30 18:25:47 -03:00
|
|
|
# create an empty file
|
2020-07-06 06:15:08 -03:00
|
|
|
os_helper.create_empty_file(_fname)
|
2018-06-05 10:03:00 -03:00
|
|
|
with dbm.open(_fname, 'n') as f:
|
|
|
|
self.assertEqual(len(f), 0)
|
2011-03-14 17:03:54 -03:00
|
|
|
|
2008-05-26 07:30:20 -03:00
|
|
|
def test_anydbm_modification(self):
|
|
|
|
self.init_db()
|
|
|
|
f = dbm.open(_fname, 'c')
|
|
|
|
self._dict['g'] = f[b'g'] = b"indented"
|
|
|
|
self.read_helper(f)
|
2018-04-29 06:38:06 -03:00
|
|
|
# setdefault() works as in the dict interface
|
|
|
|
self.assertEqual(f.setdefault(b'xxx', b'foo'), b'foo')
|
|
|
|
self.assertEqual(f[b'xxx'], b'foo')
|
2008-05-26 07:30:20 -03:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
def test_anydbm_read(self):
|
|
|
|
self.init_db()
|
|
|
|
f = dbm.open(_fname, 'r')
|
|
|
|
self.read_helper(f)
|
2018-04-29 06:38:06 -03:00
|
|
|
# get() works as in the dict interface
|
|
|
|
self.assertEqual(f.get(b'a'), self._dict['a'])
|
|
|
|
self.assertEqual(f.get(b'xxx', b'foo'), b'foo')
|
|
|
|
self.assertIsNone(f.get(b'xxx'))
|
|
|
|
with self.assertRaises(KeyError):
|
|
|
|
f[b'xxx']
|
2008-05-26 07:30:20 -03:00
|
|
|
f.close()
|
|
|
|
|
|
|
|
def test_anydbm_keys(self):
|
|
|
|
self.init_db()
|
|
|
|
f = dbm.open(_fname, 'r')
|
|
|
|
keys = self.keys_helper(f)
|
|
|
|
f.close()
|
|
|
|
|
2018-04-29 09:45:03 -03:00
|
|
|
def test_empty_value(self):
|
|
|
|
if getattr(dbm._defaultmod, 'library', None) == 'Berkeley DB':
|
|
|
|
self.skipTest("Berkeley DB doesn't distinguish the empty value "
|
|
|
|
"from the absent one")
|
|
|
|
f = dbm.open(_fname, 'c')
|
|
|
|
self.assertEqual(f.keys(), [])
|
|
|
|
f[b'empty'] = b''
|
|
|
|
self.assertEqual(f.keys(), [b'empty'])
|
|
|
|
self.assertIn(b'empty', f)
|
|
|
|
self.assertEqual(f[b'empty'], b'')
|
|
|
|
self.assertEqual(f.get(b'empty'), b'')
|
|
|
|
self.assertEqual(f.setdefault(b'empty'), b'')
|
|
|
|
f.close()
|
|
|
|
|
2008-05-26 07:30:20 -03:00
|
|
|
def test_anydbm_access(self):
|
|
|
|
self.init_db()
|
|
|
|
f = dbm.open(_fname, 'r')
|
|
|
|
key = "a".encode("ascii")
|
2010-01-23 11:40:09 -04:00
|
|
|
self.assertIn(key, f)
|
2008-05-26 07:30:20 -03:00
|
|
|
assert(f[key] == b"Python:")
|
|
|
|
f.close()
|
|
|
|
|
2021-09-10 09:26:16 -03:00
|
|
|
def test_open_with_bytes(self):
|
|
|
|
dbm.open(os.fsencode(_fname), "c").close()
|
|
|
|
|
|
|
|
def test_open_with_pathlib_path(self):
|
|
|
|
dbm.open(os_helper.FakePath(_fname), "c").close()
|
|
|
|
|
|
|
|
def test_open_with_pathlib_path_bytes(self):
|
|
|
|
dbm.open(os_helper.FakePath(os.fsencode(_fname)), "c").close()
|
|
|
|
|
2008-05-26 07:30:20 -03:00
|
|
|
def read_helper(self, f):
|
|
|
|
keys = self.keys_helper(f)
|
|
|
|
for key in self._dict:
|
|
|
|
self.assertEqual(self._dict[key], f[key.encode("ascii")])
|
|
|
|
|
2021-10-19 16:11:28 -03:00
|
|
|
def test_keys(self):
|
|
|
|
with dbm.open(_fname, 'c') as d:
|
|
|
|
self.assertEqual(d.keys(), [])
|
|
|
|
a = [(b'a', b'b'), (b'12345678910', b'019237410982340912840198242')]
|
|
|
|
for k, v in a:
|
|
|
|
d[k] = v
|
|
|
|
self.assertEqual(sorted(d.keys()), sorted(k for (k, v) in a))
|
|
|
|
for k, v in a:
|
|
|
|
self.assertIn(k, d)
|
|
|
|
self.assertEqual(d[k], v)
|
|
|
|
self.assertNotIn(b'xxx', d)
|
|
|
|
self.assertRaises(KeyError, lambda: d[b'xxx'])
|
2008-05-26 07:30:20 -03:00
|
|
|
|
|
|
|
def setUp(self):
|
2021-10-19 16:11:28 -03:00
|
|
|
self.addCleanup(setattr, dbm, '_defaultmod', dbm._defaultmod)
|
2008-05-28 05:43:17 -03:00
|
|
|
dbm._defaultmod = self.module
|
2021-10-19 16:11:28 -03:00
|
|
|
self.addCleanup(cleaunup_test_dir)
|
|
|
|
setup_test_dir()
|
2008-05-26 07:30:20 -03:00
|
|
|
|
|
|
|
|
|
|
|
class WhichDBTestCase(unittest.TestCase):
|
|
|
|
def test_whichdb(self):
|
2021-10-19 16:11:28 -03:00
|
|
|
self.addCleanup(setattr, dbm, '_defaultmod', dbm._defaultmod)
|
2021-09-10 09:26:16 -03:00
|
|
|
_bytes_fname = os.fsencode(_fname)
|
2021-10-19 16:11:28 -03:00
|
|
|
fnames = [_fname, os_helper.FakePath(_fname),
|
|
|
|
_bytes_fname, os_helper.FakePath(_bytes_fname)]
|
|
|
|
for module in dbm_iterator():
|
|
|
|
# Check whether whichdb correctly guesses module name
|
|
|
|
# for databases opened with "module" module.
|
|
|
|
name = module.__name__
|
|
|
|
setup_test_dir()
|
|
|
|
dbm._defaultmod = module
|
|
|
|
# Try with empty files first
|
|
|
|
with module.open(_fname, 'c'): pass
|
|
|
|
for path in fnames:
|
2021-09-10 09:26:16 -03:00
|
|
|
self.assertEqual(name, self.dbm.whichdb(path))
|
2021-10-19 16:11:28 -03:00
|
|
|
# Now add a key
|
|
|
|
with module.open(_fname, 'w') as f:
|
2021-09-10 09:26:16 -03:00
|
|
|
f[b"1"] = b"1"
|
|
|
|
# and test that we can find it
|
|
|
|
self.assertIn(b"1", f)
|
|
|
|
# and read it
|
|
|
|
self.assertEqual(f[b"1"], b"1")
|
2021-10-19 16:11:28 -03:00
|
|
|
for path in fnames:
|
2021-09-10 09:26:16 -03:00
|
|
|
self.assertEqual(name, self.dbm.whichdb(path))
|
2013-07-07 08:15:08 -03:00
|
|
|
|
|
|
|
@unittest.skipUnless(ndbm, reason='Test requires ndbm')
|
|
|
|
def test_whichdb_ndbm(self):
|
|
|
|
# Issue 17198: check that ndbm which is referenced in whichdb is defined
|
2021-10-19 16:11:28 -03:00
|
|
|
with open(_fname + '.db', 'wb'): pass
|
|
|
|
_bytes_fname = os.fsencode(_fname)
|
|
|
|
fnames = [_fname, os_helper.FakePath(_fname),
|
|
|
|
_bytes_fname, os_helper.FakePath(_bytes_fname)]
|
|
|
|
for path in fnames:
|
|
|
|
self.assertIsNone(self.dbm.whichdb(path))
|
2008-05-26 07:30:20 -03:00
|
|
|
|
|
|
|
def setUp(self):
|
2021-10-19 16:11:28 -03:00
|
|
|
self.addCleanup(cleaunup_test_dir)
|
|
|
|
setup_test_dir()
|
2020-07-06 06:15:08 -03:00
|
|
|
self.dbm = import_helper.import_fresh_module('dbm')
|
2008-10-10 21:49:57 -03:00
|
|
|
|
2021-10-19 16:11:28 -03:00
|
|
|
|
|
|
|
for mod in dbm_iterator():
|
|
|
|
assert mod.__name__.startswith('dbm.')
|
|
|
|
suffix = mod.__name__[4:]
|
|
|
|
testname = f'TestCase_{suffix}'
|
|
|
|
globals()[testname] = type(testname,
|
|
|
|
(AnyDBMTestCase, unittest.TestCase),
|
|
|
|
{'module': mod})
|
|
|
|
|
2008-05-26 07:30:20 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2013-03-01 05:23:28 -04:00
|
|
|
unittest.main()
|