Skip Montanaro | 13a5678 | 2002-08-02 17:10:10 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | """Test script for the whichdb module |
| 3 | based on test_anydbm.py |
| 4 | """ |
| 5 | |
| 6 | import os |
| 7 | import test.test_support |
| 8 | import unittest |
| 9 | import whichdb |
| 10 | import anydbm |
| 11 | import tempfile |
| 12 | import glob |
Gregory P. Smith | 178fefb | 2007-08-24 21:59:45 +0000 | [diff] [blame] | 13 | from test.test_anydbm import delete_files, dbm_iterator |
Skip Montanaro | 13a5678 | 2002-08-02 17:10:10 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 15 | _fname = test.test_support.TESTFN |
Skip Montanaro | 13a5678 | 2002-08-02 17:10:10 +0000 | [diff] [blame] | 16 | |
Skip Montanaro | 13a5678 | 2002-08-02 17:10:10 +0000 | [diff] [blame] | 17 | class 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. Smith | 178fefb | 2007-08-24 21:59:45 +0000 | [diff] [blame] | 23 | 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 Norwitz | 0337ef6 | 2007-08-25 18:00:36 +0000 | [diff] [blame] | 31 | test.test_support.unlink(_fname) |
Gregory P. Smith | 178fefb | 2007-08-24 21:59:45 +0000 | [diff] [blame] | 32 | 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. Smith | c3ee950 | 2007-08-24 22:14:21 +0000 | [diff] [blame] | 39 | self.assertTrue(b"1" in f) |
Gregory P. Smith | 178fefb | 2007-08-24 21:59:45 +0000 | [diff] [blame] | 40 | # and read it |
Gregory P. Smith | c3ee950 | 2007-08-24 22:14:21 +0000 | [diff] [blame] | 41 | self.assertTrue(f[b"1"] == b"1") |
Gregory P. Smith | 178fefb | 2007-08-24 21:59:45 +0000 | [diff] [blame] | 42 | f.close() |
| 43 | self.assertEqual(name, whichdb.whichdb(_fname)) |
| 44 | |
Skip Montanaro | 13a5678 | 2002-08-02 17:10:10 +0000 | [diff] [blame] | 45 | def tearDown(self): |
Gregory P. Smith | 178fefb | 2007-08-24 21:59:45 +0000 | [diff] [blame] | 46 | delete_files() |
Skip Montanaro | 13a5678 | 2002-08-02 17:10:10 +0000 | [diff] [blame] | 47 | |
| 48 | def setUp(self): |
Gregory P. Smith | 178fefb | 2007-08-24 21:59:45 +0000 | [diff] [blame] | 49 | delete_files() |
Skip Montanaro | 13a5678 | 2002-08-02 17:10:10 +0000 | [diff] [blame] | 50 | |
Skip Montanaro | 13a5678 | 2002-08-02 17:10:10 +0000 | [diff] [blame] | 51 | |
| 52 | def test_main(): |
| 53 | try: |
| 54 | test.test_support.run_unittest(WhichDBTestCase) |
| 55 | finally: |
Gregory P. Smith | 178fefb | 2007-08-24 21:59:45 +0000 | [diff] [blame] | 56 | delete_files() |
Skip Montanaro | 13a5678 | 2002-08-02 17:10:10 +0000 | [diff] [blame] | 57 | |
| 58 | if __name__ == "__main__": |
| 59 | test_main() |