| 1 | n/a | #! /usr/bin/env python |
|---|
| 2 | n/a | """Test script for the whichdb module |
|---|
| 3 | n/a | based on test_anydbm.py |
|---|
| 4 | 1 | """ |
|---|
| 5 | n/a | |
|---|
| 6 | 1 | import os |
|---|
| 7 | 1 | import test.test_support |
|---|
| 8 | 1 | import unittest |
|---|
| 9 | 1 | import whichdb |
|---|
| 10 | 1 | import glob |
|---|
| 11 | n/a | |
|---|
| 12 | 1 | _fname = test.test_support.TESTFN |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | # Silence Py3k warning |
|---|
| 15 | 1 | anydbm = test.test_support.import_module('anydbm', deprecated=True) |
|---|
| 16 | n/a | |
|---|
| 17 | 1 | def _delete_files(): |
|---|
| 18 | n/a | # we don't know the precise name the underlying database uses |
|---|
| 19 | n/a | # so we use glob to locate all names |
|---|
| 20 | 16 | for f in glob.glob(_fname + "*"): |
|---|
| 21 | 7 | try: |
|---|
| 22 | 7 | os.unlink(f) |
|---|
| 23 | 0 | except OSError: |
|---|
| 24 | 0 | pass |
|---|
| 25 | n/a | |
|---|
| 26 | 2 | class WhichDBTestCase(unittest.TestCase): |
|---|
| 27 | n/a | # Actual test methods are added to namespace |
|---|
| 28 | n/a | # after class definition. |
|---|
| 29 | 1 | def __init__(self, *args): |
|---|
| 30 | 4 | unittest.TestCase.__init__(self, *args) |
|---|
| 31 | n/a | |
|---|
| 32 | 1 | def tearDown(self): |
|---|
| 33 | 4 | _delete_files() |
|---|
| 34 | n/a | |
|---|
| 35 | 1 | def setUp(self): |
|---|
| 36 | 4 | _delete_files() |
|---|
| 37 | n/a | |
|---|
| 38 | 5 | for name in anydbm._names: |
|---|
| 39 | n/a | # we define a new test method for each |
|---|
| 40 | n/a | # candidate database module. |
|---|
| 41 | 4 | try: |
|---|
| 42 | n/a | # Silence Py3k warning |
|---|
| 43 | 4 | mod = test.test_support.import_module(name, deprecated=True) |
|---|
| 44 | 0 | except unittest.SkipTest: |
|---|
| 45 | 0 | continue |
|---|
| 46 | n/a | |
|---|
| 47 | 4 | def test_whichdb_name(self, name=name, mod=mod): |
|---|
| 48 | n/a | # Check whether whichdb correctly guesses module name |
|---|
| 49 | n/a | # for databases opened with module mod. |
|---|
| 50 | n/a | # Try with empty files first |
|---|
| 51 | 4 | f = mod.open(_fname, 'c') |
|---|
| 52 | 4 | f.close() |
|---|
| 53 | 4 | self.assertEqual(name, whichdb.whichdb(_fname)) |
|---|
| 54 | n/a | # Now add a key |
|---|
| 55 | 4 | f = mod.open(_fname, 'w') |
|---|
| 56 | 4 | f["1"] = "1" |
|---|
| 57 | 4 | f.close() |
|---|
| 58 | 4 | self.assertEqual(name, whichdb.whichdb(_fname)) |
|---|
| 59 | 4 | setattr(WhichDBTestCase,"test_whichdb_%s" % name, test_whichdb_name) |
|---|
| 60 | n/a | |
|---|
| 61 | 1 | def test_main(): |
|---|
| 62 | 1 | try: |
|---|
| 63 | 1 | test.test_support.run_unittest(WhichDBTestCase) |
|---|
| 64 | n/a | finally: |
|---|
| 65 | 1 | _delete_files() |
|---|
| 66 | n/a | |
|---|
| 67 | 1 | if __name__ == "__main__": |
|---|
| 68 | 0 | test_main() |
|---|