blob: e63eb0fe769a38fc1503894de7a7d7c149e9e29b [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
Skip Montanaro13a56782002-08-02 17:10:10 +000011import glob
Gregory P. Smith178fefb2007-08-24 21:59:45 +000012from test.test_anydbm import delete_files, dbm_iterator
Skip Montanaro13a56782002-08-02 17:10:10 +000013
Guido van Rossum3b0a3292002-08-09 16:38:32 +000014_fname = test.test_support.TESTFN
Skip Montanaro13a56782002-08-02 17:10:10 +000015
Skip Montanaro13a56782002-08-02 17:10:10 +000016class WhichDBTestCase(unittest.TestCase):
17 # Actual test methods are added to namespace
18 # after class definition.
19 def __init__(self, *args):
20 unittest.TestCase.__init__(self, *args)
21
Gregory P. Smith178fefb2007-08-24 21:59:45 +000022 def test_whichdb(self):
23 for module in dbm_iterator():
24 # Check whether whichdb correctly guesses module name
25 # for databases opened with "module" module.
26 # Try with empty files first
27 name = module.__name__
28 if name == 'dumbdbm':
29 continue # whichdb can't support dumbdbm
Neal Norwitz0337ef62007-08-25 18:00:36 +000030 test.test_support.unlink(_fname)
Gregory P. Smith178fefb2007-08-24 21:59:45 +000031 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
Gregory P. Smithc3ee9502007-08-24 22:14:21 +000038 self.assertTrue(b"1" in f)
Gregory P. Smith178fefb2007-08-24 21:59:45 +000039 # and read it
Gregory P. Smithc3ee9502007-08-24 22:14:21 +000040 self.assertTrue(f[b"1"] == b"1")
Gregory P. Smith178fefb2007-08-24 21:59:45 +000041 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()