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