blob: 1ce816f866fe2d9a21305c7fbc55e25bd15e0be0 [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
Senthil Kumarance8e33a2010-01-08 19:04:16 +000010import anydbm
Skip Montanaro13a56782002-08-02 17:10:10 +000011import glob
12
Guido van Rossum3b0a3292002-08-09 16:38:32 +000013_fname = test.test_support.TESTFN
Skip Montanaro13a56782002-08-02 17:10:10 +000014
15def _delete_files():
16 # we don't know the precise name the underlying database uses
17 # so we use glob to locate all names
18 for f in glob.glob(_fname + "*"):
19 try:
20 os.unlink(f)
21 except OSError:
22 pass
23
24class WhichDBTestCase(unittest.TestCase):
25 # Actual test methods are added to namespace
26 # after class definition.
27 def __init__(self, *args):
28 unittest.TestCase.__init__(self, *args)
29
30 def tearDown(self):
31 _delete_files()
32
33 def setUp(self):
34 _delete_files()
35
36for name in anydbm._names:
37 # we define a new test method for each
38 # candidate database module.
39 try:
Senthil Kumarance8e33a2010-01-08 19:04:16 +000040 mod = __import__(name)
41 except ImportError:
Skip Montanaro13a56782002-08-02 17:10:10 +000042 continue
Tim Peters469cdad2002-08-08 20:19:19 +000043
Guido van Rossumae469312002-08-22 20:22:16 +000044 def test_whichdb_name(self, name=name, mod=mod):
45 # Check whether whichdb correctly guesses module name
46 # for databases opened with module mod.
Martin v. Löwis17fb5072003-06-14 08:16:34 +000047 # Try with empty files first
Skip Montanaro13a56782002-08-02 17:10:10 +000048 f = mod.open(_fname, 'c')
Martin v. Löwis17fb5072003-06-14 08:16:34 +000049 f.close()
50 self.assertEqual(name, whichdb.whichdb(_fname))
51 # Now add a key
52 f = mod.open(_fname, 'w')
Skip Montanaro13a56782002-08-02 17:10:10 +000053 f["1"] = "1"
54 f.close()
55 self.assertEqual(name, whichdb.whichdb(_fname))
56 setattr(WhichDBTestCase,"test_whichdb_%s" % name, test_whichdb_name)
57
58def test_main():
59 try:
60 test.test_support.run_unittest(WhichDBTestCase)
61 finally:
62 _delete_files()
63
64if __name__ == "__main__":
65 test_main()