0001 #! /usr/bin/env python 0002 """Test script for the whichdb module 0003 based on test_anydbm.py 0004 """ 0005 0006 import os 0007 import test.test_support 0008 import unittest 0009 import whichdb 0010 import anydbm 0011 import tempfile 0012 import glob 0013 0014 _fname = test.test_support.TESTFN 0015 0016 def _delete_files(): 0017 # we don't know the precise name the underlying database uses 0018 # so we use glob to locate all names 0019 for f in glob.glob(_fname + "*"): 0020 try: 0021 os.unlink(f) 0022 except OSError: 0023 pass 0024 0025 class WhichDBTestCase(unittest.TestCase): 0026 # Actual test methods are added to namespace 0027 # after class definition. 0028 def __init__(self, *args): 0029 unittest.TestCase.__init__(self, *args) 0030 0031 def tearDown(self): 0032 _delete_files() 0033 0034 def setUp(self): 0035 _delete_files() 0036 0037 for name in anydbm._names: 0038 # we define a new test method for each 0039 # candidate database module. 0040 try: 0041 mod = __import__(name) 0042 except ImportError: 0043 continue 0044 0045 def test_whichdb_name(self, name=name, mod=mod): 0046 # Check whether whichdb correctly guesses module name 0047 # for databases opened with module mod. 0048 # Try with empty files first 0049 f = mod.open(_fname, 'c') 0050 f.close() 0051 self.assertEqual(name, whichdb.whichdb(_fname)) 0052 # Now add a key 0053 f = mod.open(_fname, 'w') 0054 f["1"] = "1" 0055 f.close() 0056 self.assertEqual(name, whichdb.whichdb(_fname)) 0057 setattr(WhichDBTestCase,"test_whichdb_%s" % name, test_whichdb_name) 0058 0059 def test_main(): 0060 try: 0061 test.test_support.run_unittest(WhichDBTestCase) 0062 finally: 0063 _delete_files() 0064 0065 if __name__ == "__main__": 0066 test_main() 0067
Generated by PyXR 0.9.4