2008-05-26 07:30:20 -03:00
|
|
|
"""Test script for the dbm.open function based on testdumbdbm.py"""
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import glob
|
|
|
|
import test.support
|
|
|
|
|
Merged revisions 70734,70775,70856,70874,70876-70877 via svnmerge
........
r70734 | r.david.murray | 2009-03-30 15:04:00 -0400 (Mon, 30 Mar 2009) | 7 lines
Add import_function method to test.test_support, and modify a number of
tests that expect to be skipped if imports fail or functions don't
exist to use import_function and import_module. The ultimate goal is
to change regrtest to not skip automatically on ImportError. Checking
in now to make sure the buldbots don't show any errors on platforms
I can't direct test on.
........
r70775 | r.david.murray | 2009-03-30 19:05:48 -0400 (Mon, 30 Mar 2009) | 4 lines
Change more tests to use import_module for the modules that
should cause tests to be skipped. Also rename import_function
to the more descriptive get_attribute and add a docstring.
........
r70856 | r.david.murray | 2009-03-31 14:32:17 -0400 (Tue, 31 Mar 2009) | 7 lines
A few more test skips via import_module, and change import_module to
return the error message produced by importlib, so that if an import
in the package whose import is being wrapped is what failed the skip
message will contain the name of that module instead of the name of the
wrapped module. Also fixed formatting of some previous comments.
........
r70874 | r.david.murray | 2009-03-31 15:33:15 -0400 (Tue, 31 Mar 2009) | 5 lines
Improve test_support.import_module docstring, remove
deprecated flag from get_attribute since it isn't likely
to do anything useful.
........
r70876 | r.david.murray | 2009-03-31 15:49:15 -0400 (Tue, 31 Mar 2009) | 4 lines
Remove the regrtest check that turns any ImportError into a skipped test.
Hopefully all modules whose imports legitimately result in a skipped
test have been properly wrapped by the previous commits.
........
r70877 | r.david.murray | 2009-03-31 15:57:24 -0400 (Tue, 31 Mar 2009) | 2 lines
Add NEWS entry for regrtest change.
........
2009-03-31 20:16:50 -03:00
|
|
|
# Skip tests if dbm module doesn't exist.
|
|
|
|
dbm = test.support.import_module('dbm')
|
|
|
|
|
2013-07-07 08:15:08 -03:00
|
|
|
try:
|
|
|
|
from dbm import ndbm
|
|
|
|
except ImportError:
|
|
|
|
ndbm = None
|
|
|
|
|
2008-05-26 07:30:20 -03:00
|
|
|
_fname = test.support.TESTFN
|
|
|
|
|
|
|
|
#
|
|
|
|
# Iterates over every database module supported by dbm currently available,
|
|
|
|
# setting dbm to use each in turn, and yielding that module
|
|
|
|
#
|
|
|
|
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
|
|
|
|
#
|
|
|
|
def delete_files():
|
|
|
|
# we don't know the precise name the underlying database uses
|
|
|
|
# so we use glob to locate all names
|
|
|
|
for f in glob.glob(_fname + "*"):
|
|
|
|
test.support.unlink(f)
|
|
|
|
|
|
|
|
|
2013-03-01 05:23:28 -04:00
|
|
|
class AnyDBMTestCase:
|
2008-05-26 07:30:20 -03:00
|
|
|
_dict = {'0': b'',
|
|
|
|
'a': b'Python:',
|
|
|
|
'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
|
|
|
|
test.support.create_empty_file(_fname)
|
2011-03-14 17:03:54 -03:00
|
|
|
|
|
|
|
f = dbm.open(_fname, 'n')
|
|
|
|
self.addCleanup(f.close)
|
|
|
|
self.assertEqual(len(f), 0)
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
def read_helper(self, f):
|
|
|
|
keys = self.keys_helper(f)
|
|
|
|
for key in self._dict:
|
|
|
|
self.assertEqual(self._dict[key], f[key.encode("ascii")])
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
delete_files()
|
|
|
|
|
|
|
|
def setUp(self):
|
2008-05-28 05:43:17 -03:00
|
|
|
dbm._defaultmod = self.module
|
2008-05-26 07:30:20 -03:00
|
|
|
delete_files()
|
|
|
|
|
|
|
|
|
|
|
|
class WhichDBTestCase(unittest.TestCase):
|
|
|
|
def test_whichdb(self):
|
|
|
|
for module in dbm_iterator():
|
|
|
|
# Check whether whichdb correctly guesses module name
|
|
|
|
# for databases opened with "module" module.
|
|
|
|
# Try with empty files first
|
|
|
|
name = module.__name__
|
|
|
|
if name == 'dbm.dumb':
|
|
|
|
continue # whichdb can't support dbm.dumb
|
2011-02-26 03:11:27 -04:00
|
|
|
delete_files()
|
2008-05-26 07:30:20 -03:00
|
|
|
f = module.open(_fname, 'c')
|
|
|
|
f.close()
|
2013-07-07 08:15:08 -03:00
|
|
|
self.assertEqual(name, self.dbm.whichdb(_fname))
|
2008-05-26 07:30:20 -03:00
|
|
|
# Now add a key
|
|
|
|
f = module.open(_fname, 'w')
|
|
|
|
f[b"1"] = b"1"
|
|
|
|
# and test that we can find it
|
2010-01-18 20:09:57 -04:00
|
|
|
self.assertIn(b"1", f)
|
2008-05-26 07:30:20 -03:00
|
|
|
# and read it
|
|
|
|
self.assertTrue(f[b"1"] == b"1")
|
|
|
|
f.close()
|
2013-07-07 08:15:08 -03:00
|
|
|
self.assertEqual(name, self.dbm.whichdb(_fname))
|
|
|
|
|
|
|
|
@unittest.skipUnless(ndbm, reason='Test requires ndbm')
|
|
|
|
def test_whichdb_ndbm(self):
|
|
|
|
# Issue 17198: check that ndbm which is referenced in whichdb is defined
|
|
|
|
db_file = '{}_ndbm.db'.format(_fname)
|
|
|
|
with open(db_file, 'w'):
|
|
|
|
self.addCleanup(test.support.unlink, db_file)
|
|
|
|
self.assertIsNone(self.dbm.whichdb(db_file[:-3]))
|
2008-05-26 07:30:20 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
delete_files()
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
delete_files()
|
2008-10-10 21:49:57 -03:00
|
|
|
self.filename = test.support.TESTFN
|
|
|
|
self.d = dbm.open(self.filename, 'c')
|
|
|
|
self.d.close()
|
2013-07-07 08:15:08 -03:00
|
|
|
self.dbm = test.support.import_fresh_module('dbm')
|
2008-10-10 21:49:57 -03:00
|
|
|
|
|
|
|
def test_keys(self):
|
|
|
|
self.d = dbm.open(self.filename, 'c')
|
|
|
|
self.assertEqual(self.d.keys(), [])
|
|
|
|
a = [(b'a', b'b'), (b'12345678910', b'019237410982340912840198242')]
|
|
|
|
for k, v in a:
|
|
|
|
self.d[k] = v
|
|
|
|
self.assertEqual(sorted(self.d.keys()), sorted(k for (k, v) in a))
|
|
|
|
for k, v in a:
|
2010-01-18 20:09:57 -04:00
|
|
|
self.assertIn(k, self.d)
|
2008-10-10 21:49:57 -03:00
|
|
|
self.assertEqual(self.d[k], v)
|
2010-01-18 20:09:57 -04:00
|
|
|
self.assertNotIn(b'xxx', self.d)
|
2008-10-10 23:19:18 -03:00
|
|
|
self.assertRaises(KeyError, lambda: self.d[b'xxx'])
|
2008-10-10 21:49:57 -03:00
|
|
|
self.d.close()
|
2008-05-26 07:30:20 -03:00
|
|
|
|
|
|
|
|
2013-03-01 05:23:28 -04:00
|
|
|
def load_tests(loader, tests, pattern):
|
|
|
|
classes = []
|
2008-05-28 05:43:17 -03:00
|
|
|
for mod in dbm_iterator():
|
2013-03-01 05:23:28 -04:00
|
|
|
classes.append(type("TestCase-" + mod.__name__,
|
|
|
|
(AnyDBMTestCase, unittest.TestCase),
|
2008-05-28 05:43:17 -03:00
|
|
|
{'module': mod}))
|
2013-03-01 05:23:28 -04:00
|
|
|
suites = [unittest.makeSuite(c) for c in classes]
|
|
|
|
|
|
|
|
tests.addTests(suites)
|
|
|
|
return tests
|
2008-05-26 07:30:20 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2013-03-01 05:23:28 -04:00
|
|
|
unittest.main()
|