source: python/trunk/Lib/test/test_whichdb.py

Last change on this file was 391, checked in by dmik, 11 years ago

python: Merge vendor 2.7.6 to trunk.

  • Property svn:eol-style set to native
File size: 1.8 KB
Line 
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 glob
11
12_fname = test.test_support.TESTFN
13
14# Silence Py3k warning
15anydbm = test.test_support.import_module('anydbm', deprecated=True)
16
17def _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:
42 # Silence Py3k warning
43 mod = test.test_support.import_module(name, deprecated=True)
44 except unittest.SkipTest:
45 continue
46
47 def test_whichdb_name(self, name=name, mod=mod):
48 # Check whether whichdb correctly guesses module name
49 # for databases opened with module mod.
50 # Try with empty files first
51 f = mod.open(_fname, 'c')
52 f.close()
53 self.assertEqual(name, whichdb.whichdb(_fname))
54 # Now add a key
55 f = mod.open(_fname, 'w')
56 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()
Note: See TracBrowser for help on using the repository browser.