blob: 91ab7f9b2cec1bc5d143d81e4ab26166f15c42f7 [file] [log] [blame]
Skip Montanaro13a56782002-08-02 17:10:10 +00001"""Test script for the whichdb module
2 based on test_anydbm.py
3"""
4
5import os
6import test.test_support
7import unittest
8import whichdb
Skip Montanaro13a56782002-08-02 17:10:10 +00009import glob
10
Guido van Rossum3b0a3292002-08-09 16:38:32 +000011_fname = test.test_support.TESTFN
Skip Montanaro13a56782002-08-02 17:10:10 +000012
Ezio Melottia2d46532010-01-30 07:22:54 +000013# Silence Py3k warning
14anydbm = test.test_support.import_module('anydbm', deprecated=True)
15
Skip Montanaro13a56782002-08-02 17:10:10 +000016def _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:
Ezio Melottia2d46532010-01-30 07:22:54 +000041 # Silence Py3k warning
42 mod = test.test_support.import_module(name, deprecated=True)
43 except unittest.SkipTest:
Skip Montanaro13a56782002-08-02 17:10:10 +000044 continue
Tim Peters469cdad2002-08-08 20:19:19 +000045
Guido van Rossumae469312002-08-22 20:22:16 +000046 def test_whichdb_name(self, name=name, mod=mod):
47 # Check whether whichdb correctly guesses module name
48 # for databases opened with module mod.
Martin v. Löwis17fb5072003-06-14 08:16:34 +000049 # Try with empty files first
Skip Montanaro13a56782002-08-02 17:10:10 +000050 f = mod.open(_fname, 'c')
Martin v. Löwis17fb5072003-06-14 08:16:34 +000051 f.close()
52 self.assertEqual(name, whichdb.whichdb(_fname))
53 # Now add a key
54 f = mod.open(_fname, 'w')
Skip Montanaro13a56782002-08-02 17:10:10 +000055 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()