blob: 000c9993657a3768f6d2d242c8696b4ea73197f5 [file] [log] [blame]
Skip Montanaro13a56782002-08-02 17:10:10 +00001#! /usr/bin/env python
2"""Test script for the whichdb module
3 based on test_anydbm.py
4"""
5
6import os
7import test.test_support
8import unittest
9import whichdb
10import anydbm
11import tempfile
12import glob
13
Guido van Rossum3b0a3292002-08-09 16:38:32 +000014_fname = test.test_support.TESTFN
Skip Montanaro13a56782002-08-02 17:10:10 +000015
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 mod = __import__(name)
42 except ImportError:
43 continue
Tim Peters469cdad2002-08-08 20:19:19 +000044
Guido van Rossumae469312002-08-22 20:22:16 +000045 def test_whichdb_name(self, name=name, mod=mod):
46 # Check whether whichdb correctly guesses module name
47 # for databases opened with module mod.
Skip Montanaro13a56782002-08-02 17:10:10 +000048 f = mod.open(_fname, 'c')
49 f["1"] = "1"
50 f.close()
51 self.assertEqual(name, whichdb.whichdb(_fname))
52 setattr(WhichDBTestCase,"test_whichdb_%s" % name, test_whichdb_name)
53
54def test_main():
55 try:
56 test.test_support.run_unittest(WhichDBTestCase)
57 finally:
58 _delete_files()
59
60if __name__ == "__main__":
61 test_main()