Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | """Test script for the dbm.open function based on testdumbdbm.py""" |
| 3 | |
| 4 | import os |
| 5 | import unittest |
| 6 | import dbm |
| 7 | import glob |
| 8 | import test.support |
| 9 | |
| 10 | _fname = test.support.TESTFN |
| 11 | |
| 12 | # |
| 13 | # Iterates over every database module supported by dbm currently available, |
| 14 | # setting dbm to use each in turn, and yielding that module |
| 15 | # |
| 16 | def dbm_iterator(): |
| 17 | old_default = dbm._defaultmod |
| 18 | for module in dbm._modules.values(): |
| 19 | dbm._defaultmod = module |
| 20 | yield module |
| 21 | dbm._defaultmod = old_default |
| 22 | |
| 23 | # |
| 24 | # Clean up all scratch databases we might have created during testing |
| 25 | # |
| 26 | def delete_files(): |
| 27 | # we don't know the precise name the underlying database uses |
| 28 | # so we use glob to locate all names |
| 29 | for f in glob.glob(_fname + "*"): |
| 30 | test.support.unlink(f) |
| 31 | |
| 32 | |
| 33 | class AnyDBMTestCase(unittest.TestCase): |
| 34 | _dict = {'0': b'', |
| 35 | 'a': b'Python:', |
| 36 | 'b': b'Programming', |
| 37 | 'c': b'the', |
| 38 | 'd': b'way', |
| 39 | 'f': b'Guido', |
| 40 | 'g': b'intended', |
| 41 | } |
| 42 | |
| 43 | def __init__(self, *args): |
| 44 | unittest.TestCase.__init__(self, *args) |
| 45 | |
| 46 | def test_anydbm_creation(self): |
| 47 | f = dbm.open(_fname, 'c') |
| 48 | self.assertEqual(list(f.keys()), []) |
| 49 | for key in self._dict: |
| 50 | f[key.encode("ascii")] = self._dict[key] |
| 51 | self.read_helper(f) |
| 52 | f.close() |
| 53 | |
| 54 | def test_anydbm_modification(self): |
| 55 | self.init_db() |
| 56 | f = dbm.open(_fname, 'c') |
| 57 | self._dict['g'] = f[b'g'] = b"indented" |
| 58 | self.read_helper(f) |
| 59 | f.close() |
| 60 | |
| 61 | def test_anydbm_read(self): |
| 62 | self.init_db() |
| 63 | f = dbm.open(_fname, 'r') |
| 64 | self.read_helper(f) |
| 65 | f.close() |
| 66 | |
| 67 | def test_anydbm_keys(self): |
| 68 | self.init_db() |
| 69 | f = dbm.open(_fname, 'r') |
| 70 | keys = self.keys_helper(f) |
| 71 | f.close() |
| 72 | |
| 73 | def test_anydbm_access(self): |
| 74 | self.init_db() |
| 75 | f = dbm.open(_fname, 'r') |
| 76 | key = "a".encode("ascii") |
| 77 | assert(key in f) |
| 78 | assert(f[key] == b"Python:") |
| 79 | f.close() |
| 80 | |
| 81 | def read_helper(self, f): |
| 82 | keys = self.keys_helper(f) |
| 83 | for key in self._dict: |
| 84 | self.assertEqual(self._dict[key], f[key.encode("ascii")]) |
| 85 | |
| 86 | def init_db(self): |
| 87 | f = dbm.open(_fname, 'n') |
| 88 | for k in self._dict: |
| 89 | f[k.encode("ascii")] = self._dict[k] |
| 90 | f.close() |
| 91 | |
| 92 | def keys_helper(self, f): |
| 93 | keys = sorted(k.decode("ascii") for k in f.keys()) |
| 94 | dkeys = sorted(self._dict.keys()) |
| 95 | self.assertEqual(keys, dkeys) |
| 96 | return keys |
| 97 | |
| 98 | def tearDown(self): |
| 99 | delete_files() |
| 100 | |
| 101 | def setUp(self): |
| 102 | delete_files() |
| 103 | |
| 104 | |
| 105 | class WhichDBTestCase(unittest.TestCase): |
| 106 | # Actual test methods are added to namespace after class definition. |
| 107 | def __init__(self, *args): |
| 108 | unittest.TestCase.__init__(self, *args) |
| 109 | |
| 110 | def test_whichdb(self): |
| 111 | for module in dbm_iterator(): |
| 112 | # Check whether whichdb correctly guesses module name |
| 113 | # for databases opened with "module" module. |
| 114 | # Try with empty files first |
| 115 | name = module.__name__ |
| 116 | if name == 'dbm.dumb': |
| 117 | continue # whichdb can't support dbm.dumb |
| 118 | test.support.unlink(_fname) |
| 119 | f = module.open(_fname, 'c') |
| 120 | f.close() |
| 121 | self.assertEqual(name, dbm.whichdb(_fname)) |
| 122 | # Now add a key |
| 123 | f = module.open(_fname, 'w') |
| 124 | f[b"1"] = b"1" |
| 125 | # and test that we can find it |
| 126 | self.assertTrue(b"1" in f) |
| 127 | # and read it |
| 128 | self.assertTrue(f[b"1"] == b"1") |
| 129 | f.close() |
| 130 | self.assertEqual(name, dbm.whichdb(_fname)) |
| 131 | |
| 132 | def tearDown(self): |
| 133 | delete_files() |
| 134 | |
| 135 | def setUp(self): |
| 136 | delete_files() |
| 137 | |
| 138 | |
| 139 | def test_main(): |
| 140 | try: |
| 141 | for module in dbm_iterator(): |
| 142 | test.support.run_unittest(AnyDBMTestCase, WhichDBTestCase) |
| 143 | finally: |
| 144 | delete_files() |
| 145 | |
| 146 | if __name__ == "__main__": |
| 147 | test_main() |