Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 1 | """Test script for the dbm.open function based on testdumbdbm.py""" |
| 2 | |
| 3 | import os |
| 4 | import unittest |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 5 | import glob |
| 6 | import test.support |
| 7 | |
R. David Murray | a21e4ca | 2009-03-31 23:16:50 +0000 | [diff] [blame] | 8 | # Skip tests if dbm module doesn't exist. |
| 9 | dbm = test.support.import_module('dbm') |
| 10 | |
Ezio Melotti | b08495b | 2013-07-07 13:15:08 +0200 | [diff] [blame] | 11 | try: |
| 12 | from dbm import ndbm |
| 13 | except ImportError: |
| 14 | ndbm = None |
| 15 | |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 16 | _fname = test.support.TESTFN |
| 17 | |
| 18 | # |
| 19 | # Iterates over every database module supported by dbm currently available, |
| 20 | # setting dbm to use each in turn, and yielding that module |
| 21 | # |
| 22 | def dbm_iterator(): |
Georg Brandl | b17acad | 2008-05-28 08:43:17 +0000 | [diff] [blame] | 23 | for name in dbm._names: |
| 24 | try: |
| 25 | mod = __import__(name, fromlist=['open']) |
| 26 | except ImportError: |
| 27 | continue |
| 28 | dbm._modules[name] = mod |
| 29 | yield mod |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 30 | |
| 31 | # |
| 32 | # Clean up all scratch databases we might have created during testing |
| 33 | # |
| 34 | def delete_files(): |
| 35 | # we don't know the precise name the underlying database uses |
| 36 | # so we use glob to locate all names |
| 37 | for f in glob.glob(_fname + "*"): |
| 38 | test.support.unlink(f) |
| 39 | |
| 40 | |
Ezio Melotti | f79493b | 2013-03-01 11:23:28 +0200 | [diff] [blame] | 41 | class AnyDBMTestCase: |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 42 | _dict = {'0': b'', |
| 43 | 'a': b'Python:', |
| 44 | 'b': b'Programming', |
| 45 | 'c': b'the', |
| 46 | 'd': b'way', |
| 47 | 'f': b'Guido', |
| 48 | 'g': b'intended', |
| 49 | } |
| 50 | |
Georg Brandl | b17acad | 2008-05-28 08:43:17 +0000 | [diff] [blame] | 51 | def init_db(self): |
| 52 | f = dbm.open(_fname, 'n') |
| 53 | for k in self._dict: |
| 54 | f[k.encode("ascii")] = self._dict[k] |
| 55 | f.close() |
| 56 | |
| 57 | def keys_helper(self, f): |
| 58 | keys = sorted(k.decode("ascii") for k in f.keys()) |
| 59 | dkeys = sorted(self._dict.keys()) |
| 60 | self.assertEqual(keys, dkeys) |
| 61 | return keys |
| 62 | |
| 63 | def test_error(self): |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 64 | self.assertTrue(issubclass(self.module.error, OSError)) |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 65 | |
Amaury Forgeot d'Arc | b5cf301 | 2008-09-25 22:27:43 +0000 | [diff] [blame] | 66 | def test_anydbm_not_existing(self): |
| 67 | self.assertRaises(dbm.error, dbm.open, _fname) |
| 68 | |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 69 | def test_anydbm_creation(self): |
| 70 | f = dbm.open(_fname, 'c') |
| 71 | self.assertEqual(list(f.keys()), []) |
| 72 | for key in self._dict: |
| 73 | f[key.encode("ascii")] = self._dict[key] |
| 74 | self.read_helper(f) |
| 75 | f.close() |
| 76 | |
briancurtin | 525c25d | 2011-03-14 16:03:54 -0400 | [diff] [blame] | 77 | def test_anydbm_creation_n_file_exists_with_invalid_contents(self): |
Victor Stinner | bf81622 | 2011-06-30 23:25:47 +0200 | [diff] [blame] | 78 | # create an empty file |
| 79 | test.support.create_empty_file(_fname) |
briancurtin | 525c25d | 2011-03-14 16:03:54 -0400 | [diff] [blame] | 80 | |
| 81 | f = dbm.open(_fname, 'n') |
| 82 | self.addCleanup(f.close) |
| 83 | self.assertEqual(len(f), 0) |
| 84 | |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 85 | def test_anydbm_modification(self): |
| 86 | self.init_db() |
| 87 | f = dbm.open(_fname, 'c') |
| 88 | self._dict['g'] = f[b'g'] = b"indented" |
| 89 | self.read_helper(f) |
| 90 | f.close() |
| 91 | |
| 92 | def test_anydbm_read(self): |
| 93 | self.init_db() |
| 94 | f = dbm.open(_fname, 'r') |
| 95 | self.read_helper(f) |
| 96 | f.close() |
| 97 | |
| 98 | def test_anydbm_keys(self): |
| 99 | self.init_db() |
| 100 | f = dbm.open(_fname, 'r') |
| 101 | keys = self.keys_helper(f) |
| 102 | f.close() |
| 103 | |
| 104 | def test_anydbm_access(self): |
| 105 | self.init_db() |
| 106 | f = dbm.open(_fname, 'r') |
| 107 | key = "a".encode("ascii") |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 108 | self.assertIn(key, f) |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 109 | assert(f[key] == b"Python:") |
| 110 | f.close() |
| 111 | |
| 112 | def read_helper(self, f): |
| 113 | keys = self.keys_helper(f) |
| 114 | for key in self._dict: |
| 115 | self.assertEqual(self._dict[key], f[key.encode("ascii")]) |
| 116 | |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 117 | def tearDown(self): |
| 118 | delete_files() |
| 119 | |
| 120 | def setUp(self): |
Georg Brandl | b17acad | 2008-05-28 08:43:17 +0000 | [diff] [blame] | 121 | dbm._defaultmod = self.module |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 122 | delete_files() |
| 123 | |
| 124 | |
| 125 | class WhichDBTestCase(unittest.TestCase): |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 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() |
Ezio Melotti | b08495b | 2013-07-07 13:15:08 +0200 | [diff] [blame] | 137 | self.assertEqual(name, self.dbm.whichdb(_fname)) |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 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() |
Ezio Melotti | b08495b | 2013-07-07 13:15:08 +0200 | [diff] [blame] | 146 | self.assertEqual(name, self.dbm.whichdb(_fname)) |
| 147 | |
| 148 | @unittest.skipUnless(ndbm, reason='Test requires ndbm') |
| 149 | def test_whichdb_ndbm(self): |
| 150 | # Issue 17198: check that ndbm which is referenced in whichdb is defined |
| 151 | db_file = '{}_ndbm.db'.format(_fname) |
| 152 | with open(db_file, 'w'): |
| 153 | self.addCleanup(test.support.unlink, db_file) |
| 154 | self.assertIsNone(self.dbm.whichdb(db_file[:-3])) |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 155 | |
| 156 | def tearDown(self): |
| 157 | delete_files() |
| 158 | |
| 159 | def setUp(self): |
| 160 | delete_files() |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 161 | self.filename = test.support.TESTFN |
| 162 | self.d = dbm.open(self.filename, 'c') |
| 163 | self.d.close() |
Ezio Melotti | b08495b | 2013-07-07 13:15:08 +0200 | [diff] [blame] | 164 | self.dbm = test.support.import_fresh_module('dbm') |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 165 | |
| 166 | def test_keys(self): |
| 167 | self.d = dbm.open(self.filename, 'c') |
| 168 | self.assertEqual(self.d.keys(), []) |
| 169 | a = [(b'a', b'b'), (b'12345678910', b'019237410982340912840198242')] |
| 170 | for k, v in a: |
| 171 | self.d[k] = v |
| 172 | self.assertEqual(sorted(self.d.keys()), sorted(k for (k, v) in a)) |
| 173 | for k, v in a: |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 174 | self.assertIn(k, self.d) |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 175 | self.assertEqual(self.d[k], v) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 176 | self.assertNotIn(b'xxx', self.d) |
Benjamin Peterson | 1bc15c2 | 2008-10-11 02:19:18 +0000 | [diff] [blame] | 177 | self.assertRaises(KeyError, lambda: self.d[b'xxx']) |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 178 | self.d.close() |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 179 | |
| 180 | |
Ezio Melotti | f79493b | 2013-03-01 11:23:28 +0200 | [diff] [blame] | 181 | def load_tests(loader, tests, pattern): |
| 182 | classes = [] |
Georg Brandl | b17acad | 2008-05-28 08:43:17 +0000 | [diff] [blame] | 183 | for mod in dbm_iterator(): |
Ezio Melotti | f79493b | 2013-03-01 11:23:28 +0200 | [diff] [blame] | 184 | classes.append(type("TestCase-" + mod.__name__, |
| 185 | (AnyDBMTestCase, unittest.TestCase), |
Georg Brandl | b17acad | 2008-05-28 08:43:17 +0000 | [diff] [blame] | 186 | {'module': mod})) |
Ezio Melotti | f79493b | 2013-03-01 11:23:28 +0200 | [diff] [blame] | 187 | suites = [unittest.makeSuite(c) for c in classes] |
| 188 | |
| 189 | tests.addTests(suites) |
| 190 | return tests |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 191 | |
| 192 | if __name__ == "__main__": |
Ezio Melotti | f79493b | 2013-03-01 11:23:28 +0200 | [diff] [blame] | 193 | unittest.main() |