summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_whichdb.py
blob: 2f1a3a2f092065b111ac4bd61abfa8591a90ede9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#! /usr/bin/env python
"""Test script for the whichdb module
   based on test_anydbm.py
"""

import os
import test.test_support
import unittest
import whichdb
import anydbm
import tempfile
import glob

_fname = test.test_support.TESTFN

def _delete_files():
    # we don't know the precise name the underlying database uses
    # so we use glob to locate all names
    for f in glob.glob(_fname + "*"):
        try:
            os.unlink(f)
        except OSError:
            pass

class WhichDBTestCase(unittest.TestCase):
    # Actual test methods are added to namespace
    # after class definition.
    def __init__(self, *args):
        unittest.TestCase.__init__(self, *args)

    def tearDown(self):
        _delete_files()

    def setUp(self):
        _delete_files()

for name in anydbm._names:
    # we define a new test method for each
    # candidate database module.
    try:
        mod = __import__(name)
    except ImportError:
        continue

    def test_whichdb_name(self,name=name,mod=mod):
        """Check whether whichdb correctly guesses module name
        for databases opened with module mod.
        """
        f = mod.open(_fname, 'c')
        f["1"] = "1"
        f.close()
        self.assertEqual(name, whichdb.whichdb(_fname))
    setattr(WhichDBTestCase,"test_whichdb_%s" % name, test_whichdb_name)

def test_main():
    try:
        test.test_support.run_unittest(WhichDBTestCase)
    finally:
        _delete_files()

if __name__ == "__main__":
    test_main()