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 | |
Ezio Melotti | f79493b | 2013-03-01 11:23:28 +0200 | [diff] [blame^] | 37 | class AnyDBMTestCase: |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 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): |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 122 | def test_whichdb(self): |
| 123 | for module in dbm_iterator(): |
| 124 | # Check whether whichdb correctly guesses module name |
| 125 | # for databases opened with "module" module. |
| 126 | # Try with empty files first |
| 127 | name = module.__name__ |
| 128 | if name == 'dbm.dumb': |
| 129 | continue # whichdb can't support dbm.dumb |
Senthil Kumaran | 725c2b9 | 2011-02-26 07:11:27 +0000 | [diff] [blame] | 130 | delete_files() |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 131 | f = module.open(_fname, 'c') |
| 132 | f.close() |
| 133 | self.assertEqual(name, dbm.whichdb(_fname)) |
| 134 | # Now add a key |
| 135 | f = module.open(_fname, 'w') |
| 136 | f[b"1"] = b"1" |
| 137 | # and test that we can find it |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 138 | self.assertIn(b"1", f) |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 139 | # and read it |
| 140 | self.assertTrue(f[b"1"] == b"1") |
| 141 | f.close() |
| 142 | self.assertEqual(name, dbm.whichdb(_fname)) |
| 143 | |
| 144 | def tearDown(self): |
| 145 | delete_files() |
| 146 | |
| 147 | def setUp(self): |
| 148 | delete_files() |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 149 | self.filename = test.support.TESTFN |
| 150 | self.d = dbm.open(self.filename, 'c') |
| 151 | self.d.close() |
| 152 | |
| 153 | def test_keys(self): |
| 154 | self.d = dbm.open(self.filename, 'c') |
| 155 | self.assertEqual(self.d.keys(), []) |
| 156 | a = [(b'a', b'b'), (b'12345678910', b'019237410982340912840198242')] |
| 157 | for k, v in a: |
| 158 | self.d[k] = v |
| 159 | self.assertEqual(sorted(self.d.keys()), sorted(k for (k, v) in a)) |
| 160 | for k, v in a: |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 161 | self.assertIn(k, self.d) |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 162 | self.assertEqual(self.d[k], v) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 163 | self.assertNotIn(b'xxx', self.d) |
Benjamin Peterson | 1bc15c2 | 2008-10-11 02:19:18 +0000 | [diff] [blame] | 164 | self.assertRaises(KeyError, lambda: self.d[b'xxx']) |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 165 | self.d.close() |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 166 | |
| 167 | |
Ezio Melotti | f79493b | 2013-03-01 11:23:28 +0200 | [diff] [blame^] | 168 | def load_tests(loader, tests, pattern): |
| 169 | classes = [] |
Georg Brandl | b17acad | 2008-05-28 08:43:17 +0000 | [diff] [blame] | 170 | for mod in dbm_iterator(): |
Ezio Melotti | f79493b | 2013-03-01 11:23:28 +0200 | [diff] [blame^] | 171 | classes.append(type("TestCase-" + mod.__name__, |
| 172 | (AnyDBMTestCase, unittest.TestCase), |
Georg Brandl | b17acad | 2008-05-28 08:43:17 +0000 | [diff] [blame] | 173 | {'module': mod})) |
Ezio Melotti | f79493b | 2013-03-01 11:23:28 +0200 | [diff] [blame^] | 174 | suites = [unittest.makeSuite(c) for c in classes] |
| 175 | |
| 176 | tests.addTests(suites) |
| 177 | return tests |
Georg Brandl | 7f986ac | 2008-05-26 10:30:20 +0000 | [diff] [blame] | 178 | |
| 179 | if __name__ == "__main__": |
Ezio Melotti | f79493b | 2013-03-01 11:23:28 +0200 | [diff] [blame^] | 180 | unittest.main() |