blob: 7746329899927b8acad5ddbc6674cf117321e440 [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
Neal Norwitz0337ef62007-08-25 18:00:36 +000031 test.test_support.unlink(_fname)
Gregory P. Smith178fefb2007-08-24 21:59:45 +000032 f = module.open(_fname, 'c')
33 f.close()
34 self.assertEqual(name, whichdb.whichdb(_fname))
35 # Now add a key
36 f = module.open(_fname, 'w')
37 f[b"1"] = b"1"
38 # and test that we can find it
Gregory P. Smithc3ee9502007-08-24 22:14:21 +000039 self.assertTrue(b"1" in f)
Gregory P. Smith178fefb2007-08-24 21:59:45 +000040 # and read it
Gregory P. Smithc3ee9502007-08-24 22:14:21 +000041 self.assertTrue(f[b"1"] == b"1")
Gregory P. Smith178fefb2007-08-24 21:59:45 +000042 f.close()
43 self.assertEqual(name, whichdb.whichdb(_fname))
44
Skip Montanaro13a56782002-08-02 17:10:10 +000045 def tearDown(self):
Gregory P. Smith178fefb2007-08-24 21:59:45 +000046 delete_files()
Skip Montanaro13a56782002-08-02 17:10:10 +000047
48 def setUp(self):
Gregory P. Smith178fefb2007-08-24 21:59:45 +000049 delete_files()
Skip Montanaro13a56782002-08-02 17:10:10 +000050
Skip Montanaro13a56782002-08-02 17:10:10 +000051
52def test_main():
53 try:
54 test.test_support.run_unittest(WhichDBTestCase)
55 finally:
Gregory P. Smith178fefb2007-08-24 21:59:45 +000056 delete_files()
Skip Montanaro13a56782002-08-02 17:10:10 +000057
58if __name__ == "__main__":
59 test_main()