Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 2 | """Test script for the dbm.open function based on testdumbdbm.py""" |
| 3 | |
| 4 | import os |
| 5 | import unittest |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 6 | import glob |
| 7 | import test.support |
| 8 | |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 9 | # Skip tests if dbm module doesn't exist. |
| 10 | dbm = test.support.import_module('dbm') |
| 11 | |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 12 | _fname = test.support.TESTFN |
| 13 | |
| 14 | # |
| 15 | # Iterates over every database module supported by dbm currently available, |
| 16 | # setting dbm to use each in turn, and yielding that module |
| 17 | # |
| 18 | def dbm_iterator(): |
Georg Brandl | b17acad | 2008-05-28 08:43:17 +0000 | [diff] [blame] | 19 | for name in dbm._names: |
| 20 | try: |
| 21 | mod = __import__(name, fromlist=['open']) |
| 22 | except ImportError: |
| 23 | continue |
| 24 | dbm._modules[name] = mod |
| 25 | yield mod |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 26 | |
| 27 | # |
| 28 | # Clean up all scratch databases we might have created during testing |
| 29 | # |
| 30 | def delete_files(): |
| 31 | # we don't know the precise name the underlying database uses |
| 32 | # so we use glob to locate all names |
| 33 | for f in glob.glob(_fname + "*"): |
| 34 | test.support.unlink(f) |
| 35 | |
| 36 | |
| 37 | class AnyDBMTestCase(unittest.TestCase): |
| 38 | _dict = {'0': b'', |
| 39 | 'a': b'Python:', |
| 40 | 'b': b'Programming', |
| 41 | 'c': b'the', |
| 42 | 'd': b'way', |
| 43 | 'f': b'Guido', |
| 44 | 'g': b'intended', |
| 45 | } |
| 46 | |
Georg Brandl | b17acad | 2008-05-28 08:43:17 +0000 | [diff] [blame] | 47 | def init_db(self): |
| 48 | f = dbm.open(_fname, 'n') |
| 49 | for k in self._dict: |
| 50 | f[k.encode("ascii")] = self._dict[k] |
| 51 | f.close() |
| 52 | |
| 53 | def keys_helper(self, f): |
| 54 | keys = sorted(k.decode("ascii") for k in f.keys()) |
| 55 | dkeys = sorted(self._dict.keys()) |
| 56 | self.assertEqual(keys, dkeys) |
| 57 | return keys |
| 58 | |
| 59 | def test_error(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 60 | self.assertTrue(issubclass(self.module.error, IOError)) |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 61 | |
Amaury Forgeot d'Arc | b5cf301 | 2008-09-25 22:27:43 +0000 | [diff] [blame] | 62 | def test_anydbm_not_existing(self): |
| 63 | self.assertRaises(dbm.error, dbm.open, _fname) |
| 64 | |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 65 | def test_anydbm_creation(self): |
| 66 | f = dbm.open(_fname, 'c') |
| 67 | self.assertEqual(list(f.keys()), []) |
| 68 | for key in self._dict: |
| 69 | f[key.encode("ascii")] = self._dict[key] |
| 70 | self.read_helper(f) |
| 71 | f.close() |
| 72 | |
briancurtin | 525c25d | 2011-03-14 16:03:54 -0400 | [diff] [blame] | 73 | def test_anydbm_creation_n_file_exists_with_invalid_contents(self): |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 74 | # create an empty file |
| 75 | test.support.create_empty_file(_fname) |
briancurtin | 525c25d | 2011-03-14 16:03:54 -0400 | [diff] [blame] | 76 | |
| 77 | f = dbm.open(_fname, 'n') |
| 78 | self.addCleanup(f.close) |
| 79 | self.assertEqual(len(f), 0) |
| 80 | |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 81 | def test_anydbm_modification(self): |
| 82 | self.init_db() |
| 83 | f = dbm.open(_fname, 'c') |
| 84 | self._dict['g'] = f[b'g'] = b"indented" |
| 85 | self.read_helper(f) |
| 86 | f.close() |
| 87 | |
| 88 | def test_anydbm_read(self): |
| 89 | self.init_db() |
| 90 | f = dbm.open(_fname, 'r') |
| 91 | self.read_helper(f) |
| 92 | f.close() |
| 93 | |
| 94 | def test_anydbm_keys(self): |
| 95 | self.init_db() |
| 96 | f = dbm.open(_fname, 'r') |
| 97 | keys = self.keys_helper(f) |
| 98 | f.close() |
| 99 | |
| 100 | def test_anydbm_access(self): |
| 101 | self.init_db() |
| 102 | f = dbm.open(_fname, 'r') |
| 103 | key = "a".encode("ascii") |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 104 | self.assertIn(key, f) |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 105 | assert(f[key] == b"Python:") |
| 106 | f.close() |
| 107 | |
| 108 | def read_helper(self, f): |
| 109 | keys = self.keys_helper(f) |
| 110 | for key in self._dict: |
| 111 | self.assertEqual(self._dict[key], f[key.encode("ascii")]) |
| 112 | |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 113 | def tearDown(self): |
| 114 | delete_files() |
| 115 | |
| 116 | def setUp(self): |
Georg Brandl | b17acad | 2008-05-28 08:43:17 +0000 | [diff] [blame] | 117 | dbm._defaultmod = self.module |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 118 | delete_files() |
| 119 | |
| 120 | |
| 121 | class WhichDBTestCase(unittest.TestCase): |
| 122 | # Actual test methods are added to namespace after class definition. |
| 123 | def __init__(self, *args): |
| 124 | unittest.TestCase.__init__(self, *args) |
| 125 | |
| 126 | def test_whichdb(self): |
| 127 | for module in dbm_iterator(): |
| 128 | # Check whether whichdb correctly guesses module name |
| 129 | # for databases opened with "module" module. |
| 130 | # Try with empty files first |
| 131 | name = module.__name__ |
| 132 | if name == 'dbm.dumb': |
| 133 | continue # whichdb can't support dbm.dumb |
Senthil Kumaran | 725c2b9 | 2011-02-26 07:11:27 +0000 | [diff] [blame] | 134 | delete_files() |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 135 | f = module.open(_fname, 'c') |
| 136 | f.close() |
| 137 | self.assertEqual(name, dbm.whichdb(_fname)) |
| 138 | # Now add a key |
| 139 | f = module.open(_fname, 'w') |
| 140 | f[b"1"] = b"1" |
| 141 | # and test that we can find it |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 142 | self.assertIn(b"1", f) |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 143 | # and read it |
| 144 | self.assertTrue(f[b"1"] == b"1") |
| 145 | f.close() |
| 146 | self.assertEqual(name, dbm.whichdb(_fname)) |
| 147 | |
| 148 | def tearDown(self): |
| 149 | delete_files() |
| 150 | |
| 151 | def setUp(self): |
| 152 | delete_files() |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 153 | self.filename = test.support.TESTFN |
| 154 | self.d = dbm.open(self.filename, 'c') |
| 155 | self.d.close() |
| 156 | |
| 157 | def test_keys(self): |
| 158 | self.d = dbm.open(self.filename, 'c') |
| 159 | self.assertEqual(self.d.keys(), []) |
| 160 | a = [(b'a', b'b'), (b'12345678910', b'019237410982340912840198242')] |
| 161 | for k, v in a: |
| 162 | self.d[k] = v |
| 163 | self.assertEqual(sorted(self.d.keys()), sorted(k for (k, v) in a)) |
| 164 | for k, v in a: |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 165 | self.assertIn(k, self.d) |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 166 | self.assertEqual(self.d[k], v) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 167 | self.assertNotIn(b'xxx', self.d) |
Benjamin Peterson | 1bc15c2 | 2008-10-11 02:19:18 +0000 | [diff] [blame] | 168 | self.assertRaises(KeyError, lambda: self.d[b'xxx']) |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 169 | self.d.close() |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 170 | |
| 171 | |
| 172 | def test_main(): |
Georg Brandl | b17acad | 2008-05-28 08:43:17 +0000 | [diff] [blame] | 173 | classes = [WhichDBTestCase] |
| 174 | for mod in dbm_iterator(): |
| 175 | classes.append(type("TestCase-" + mod.__name__, (AnyDBMTestCase,), |
| 176 | {'module': mod})) |
| 177 | test.support.run_unittest(*classes) |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 178 | |
| 179 | if __name__ == "__main__": |
| 180 | test_main() |