blob: d3eda84deb3b44bf459397e7b8471c70840163c8 [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
10import anydbm
11import tempfile
12import glob
Gregory P. Smith178fefb2007-08-24 21:59:45 +000013from test.test_anydbm import delete_files, dbm_iterator
Skip Montanaro13a56782002-08-02 17:10:10 +000014
Guido van Rossum3b0a3292002-08-09 16:38:32 +000015_fname = test.test_support.TESTFN
Skip Montanaro13a56782002-08-02 17:10:10 +000016
Skip Montanaro13a56782002-08-02 17:10:10 +000017class WhichDBTestCase(unittest.TestCase):
18 # Actual test methods are added to namespace
19 # after class definition.
20 def __init__(self, *args):
21 unittest.TestCase.__init__(self, *args)
22
Gregory P. Smith178fefb2007-08-24 21:59:45 +000023 def test_whichdb(self):
24 for module in dbm_iterator():
25 # Check whether whichdb correctly guesses module name
26 # for databases opened with "module" module.
27 # Try with empty files first
28 name = module.__name__
29 if name == 'dumbdbm':
30 continue # whichdb can't support dumbdbm
31 f = module.open(_fname, 'c')
32 f.close()
33 self.assertEqual(name, whichdb.whichdb(_fname))
34 # Now add a key
35 f = module.open(_fname, 'w')
36 f[b"1"] = b"1"
37 # and test that we can find it
38 assert b"1" in f
39 # and read it
40 assert f[b"1"] == b"1"
41 f.close()
42 self.assertEqual(name, whichdb.whichdb(_fname))
43
Skip Montanaro13a56782002-08-02 17:10:10 +000044 def tearDown(self):
Gregory P. Smith178fefb2007-08-24 21:59:45 +000045 delete_files()
Skip Montanaro13a56782002-08-02 17:10:10 +000046
47 def setUp(self):
Gregory P. Smith178fefb2007-08-24 21:59:45 +000048 delete_files()
Skip Montanaro13a56782002-08-02 17:10:10 +000049
Skip Montanaro13a56782002-08-02 17:10:10 +000050
51def test_main():
52 try:
53 test.test_support.run_unittest(WhichDBTestCase)
54 finally:
Gregory P. Smith178fefb2007-08-24 21:59:45 +000055 delete_files()
Skip Montanaro13a56782002-08-02 17:10:10 +000056
57if __name__ == "__main__":
58 test_main()