2002-08-02 14:10:10 -03:00
|
|
|
"""Test script for the whichdb module
|
|
|
|
based on test_anydbm.py
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import test.test_support
|
|
|
|
import unittest
|
|
|
|
import whichdb
|
|
|
|
import glob
|
|
|
|
|
2002-08-09 13:38:32 -03:00
|
|
|
_fname = test.test_support.TESTFN
|
2002-08-02 14:10:10 -03:00
|
|
|
|
2010-01-30 03:22:54 -04:00
|
|
|
# Silence Py3k warning
|
|
|
|
anydbm = test.test_support.import_module('anydbm', deprecated=True)
|
|
|
|
|
2002-08-02 14:10:10 -03:00
|
|
|
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 + "*"):
|
|
|
|
try:
|
|
|
|
os.unlink(f)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
class WhichDBTestCase(unittest.TestCase):
|
|
|
|
# Actual test methods are added to namespace
|
|
|
|
# after class definition.
|
|
|
|
def __init__(self, *args):
|
|
|
|
unittest.TestCase.__init__(self, *args)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
_delete_files()
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
_delete_files()
|
|
|
|
|
|
|
|
for name in anydbm._names:
|
|
|
|
# we define a new test method for each
|
|
|
|
# candidate database module.
|
|
|
|
try:
|
2010-01-30 03:22:54 -04:00
|
|
|
# Silence Py3k warning
|
|
|
|
mod = test.test_support.import_module(name, deprecated=True)
|
|
|
|
except unittest.SkipTest:
|
2002-08-02 14:10:10 -03:00
|
|
|
continue
|
2002-08-08 17:19:19 -03:00
|
|
|
|
2002-08-22 17:22:16 -03:00
|
|
|
def test_whichdb_name(self, name=name, mod=mod):
|
|
|
|
# Check whether whichdb correctly guesses module name
|
|
|
|
# for databases opened with module mod.
|
2003-06-14 05:16:34 -03:00
|
|
|
# Try with empty files first
|
2002-08-02 14:10:10 -03:00
|
|
|
f = mod.open(_fname, 'c')
|
2003-06-14 05:16:34 -03:00
|
|
|
f.close()
|
|
|
|
self.assertEqual(name, whichdb.whichdb(_fname))
|
|
|
|
# Now add a key
|
|
|
|
f = mod.open(_fname, 'w')
|
2002-08-02 14:10:10 -03:00
|
|
|
f["1"] = "1"
|
|
|
|
f.close()
|
|
|
|
self.assertEqual(name, whichdb.whichdb(_fname))
|
|
|
|
setattr(WhichDBTestCase,"test_whichdb_%s" % name, test_whichdb_name)
|
|
|
|
|
|
|
|
def test_main():
|
|
|
|
try:
|
|
|
|
test.test_support.run_unittest(WhichDBTestCase)
|
|
|
|
finally:
|
|
|
|
_delete_files()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|