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 |
| 13 | |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame^] | 14 | _fname = test.test_support.TESTFN |
Skip Montanaro | 13a5678 | 2002-08-02 17:10:10 +0000 | [diff] [blame] | 15 | |
| 16 | def _delete_files(): |
| 17 | # we don't know the precise name the underlying database uses |
| 18 | # so we use glob to locate all names |
| 19 | for f in glob.glob(_fname + "*"): |
| 20 | try: |
| 21 | os.unlink(f) |
| 22 | except OSError: |
| 23 | pass |
| 24 | |
| 25 | class WhichDBTestCase(unittest.TestCase): |
| 26 | # Actual test methods are added to namespace |
| 27 | # after class definition. |
| 28 | def __init__(self, *args): |
| 29 | unittest.TestCase.__init__(self, *args) |
| 30 | |
| 31 | def tearDown(self): |
| 32 | _delete_files() |
| 33 | |
| 34 | def setUp(self): |
| 35 | _delete_files() |
| 36 | |
| 37 | for name in anydbm._names: |
| 38 | # we define a new test method for each |
| 39 | # candidate database module. |
| 40 | try: |
| 41 | mod = __import__(name) |
| 42 | except ImportError: |
| 43 | continue |
Tim Peters | 469cdad | 2002-08-08 20:19:19 +0000 | [diff] [blame] | 44 | |
Skip Montanaro | 13a5678 | 2002-08-02 17:10:10 +0000 | [diff] [blame] | 45 | def test_whichdb_name(self,name=name,mod=mod): |
| 46 | """Check whether whichdb correctly guesses module name |
| 47 | for databases opened with module mod. |
| 48 | """ |
| 49 | f = mod.open(_fname, 'c') |
| 50 | f["1"] = "1" |
| 51 | f.close() |
| 52 | self.assertEqual(name, whichdb.whichdb(_fname)) |
| 53 | setattr(WhichDBTestCase,"test_whichdb_%s" % name, test_whichdb_name) |
| 54 | |
| 55 | def test_main(): |
| 56 | try: |
| 57 | test.test_support.run_unittest(WhichDBTestCase) |
| 58 | finally: |
| 59 | _delete_files() |
| 60 | |
| 61 | if __name__ == "__main__": |
| 62 | test_main() |