PageRenderTime 314ms CodeModel.GetById 111ms app.highlight 4ms RepoModel.GetById 197ms app.codeStats 0ms

/Lib/test/test_whichdb.py

http://unladen-swallow.googlecode.com/
Python | 65 lines | 41 code | 10 blank | 14 comment | 5 complexity | c7729cd11206f224922023ae82c0b7e0 MD5 | raw file
 1#! /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 glob
12
13_fname = test.test_support.TESTFN
14
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:
40        mod = __import__(name)
41    except ImportError:
42        continue
43
44    def test_whichdb_name(self, name=name, mod=mod):
45        # Check whether whichdb correctly guesses module name
46        # for databases opened with module mod.
47        # Try with empty files first
48        f = mod.open(_fname, 'c')
49        f.close()
50        self.assertEqual(name, whichdb.whichdb(_fname))
51        # Now add a key
52        f = mod.open(_fname, 'w')
53        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()