1"""Test script for the whichdb module 2 based on test_anydbm.py 3""" 4 5import os 6import test.test_support 7import unittest 8import whichdb 9import glob 10 11_fname = test.test_support.TESTFN 12 13# Silence Py3k warning 14anydbm = test.test_support.import_module('anydbm', deprecated=True) 15 16def _delete_files(): 17 # we don't know the precise name the underlying database uses 18 # so we use glob to locate all names 19 for f in glob.glob(_fname + "*"): 20 try: 21 os.unlink(f) 22 except OSError: 23 pass 24 25class WhichDBTestCase(unittest.TestCase): 26 # Actual test methods are added to namespace 27 # after class definition. 28 def __init__(self, *args): 29 unittest.TestCase.__init__(self, *args) 30 31 def tearDown(self): 32 _delete_files() 33 34 def setUp(self): 35 _delete_files() 36 37for name in anydbm._names: 38 # we define a new test method for each 39 # candidate database module. 40 try: 41 # Silence Py3k warning 42 mod = test.test_support.import_module(name, deprecated=True) 43 except unittest.SkipTest: 44 continue 45 46 def test_whichdb_name(self, name=name, mod=mod): 47 # Check whether whichdb correctly guesses module name 48 # for databases opened with module mod. 49 # Try with empty files first 50 f = mod.open(_fname, 'c') 51 f.close() 52 self.assertEqual(name, whichdb.whichdb(_fname)) 53 # Now add a key 54 f = mod.open(_fname, 'w') 55 f["1"] = "1" 56 f.close() 57 self.assertEqual(name, whichdb.whichdb(_fname)) 58 setattr(WhichDBTestCase,"test_whichdb_%s" % name, test_whichdb_name) 59 60def test_main(): 61 try: 62 test.test_support.run_unittest(WhichDBTestCase) 63 finally: 64 _delete_files() 65 66if __name__ == "__main__": 67 test_main() 68