blob: 427e323f4943e638d116f16399bb7980ea39c77d [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
Skip Montanaro13a56782002-08-02 17:10:10 +000010import glob
11
Guido van Rossum3b0a3292002-08-09 16:38:32 +000012_fname = test.test_support.TESTFN
Skip Montanaro13a56782002-08-02 17:10:10 +000013
Ezio Melottia2d46532010-01-30 07:22:54 +000014# Silence Py3k warning
15anydbm = test.test_support.import_module('anydbm', deprecated=True)
16
Skip Montanaro13a56782002-08-02 17:10:10 +000017def _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
25
26class 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)
31
32 def tearDown(self):
33 _delete_files()
34
35 def setUp(self):
36 _delete_files()
37
38for name in anydbm._names:
39 # we define a new test method for each
40 # candidate database module.
41 try:
Ezio Melottia2d46532010-01-30 07:22:54 +000042 # Silence Py3k warning
43 mod = test.test_support.import_module(name, deprecated=True)
44 except unittest.SkipTest:
Skip Montanaro13a56782002-08-02 17:10:10 +000045 continue
Tim Peters469cdad2002-08-08 20:19:19 +000046
Guido van Rossumae469312002-08-22 20:22:16 +000047 def test_whichdb_name(self, name=name, mod=mod):
48 # Check whether whichdb correctly guesses module name
49 # for databases opened with module mod.
Martin v. Löwis17fb5072003-06-14 08:16:34 +000050 # Try with empty files first
Skip Montanaro13a56782002-08-02 17:10:10 +000051 f = mod.open(_fname, 'c')
Martin v. Löwis17fb5072003-06-14 08:16:34 +000052 f.close()
53 self.assertEqual(name, whichdb.whichdb(_fname))
54 # Now add a key
55 f = mod.open(_fname, 'w')
Skip Montanaro13a56782002-08-02 17:10:10 +000056 f["1"] = "1"
57 f.close()
58 self.assertEqual(name, whichdb.whichdb(_fname))
59 setattr(WhichDBTestCase,"test_whichdb_%s" % name, test_whichdb_name)
60
61def test_main():
62 try:
63 test.test_support.run_unittest(WhichDBTestCase)
64 finally:
65 _delete_files()
66
67if __name__ == "__main__":
68 test_main()