1 | n/a | """Test script for the dbm.open function based on testdumbdbm.py""" |
---|
2 | n/a | |
---|
3 | n/a | import unittest |
---|
4 | n/a | import glob |
---|
5 | n/a | import test.support |
---|
6 | n/a | |
---|
7 | n/a | # Skip tests if dbm module doesn't exist. |
---|
8 | n/a | dbm = test.support.import_module('dbm') |
---|
9 | n/a | |
---|
10 | n/a | try: |
---|
11 | n/a | from dbm import ndbm |
---|
12 | n/a | except ImportError: |
---|
13 | n/a | ndbm = None |
---|
14 | n/a | |
---|
15 | n/a | _fname = test.support.TESTFN |
---|
16 | n/a | |
---|
17 | n/a | # |
---|
18 | n/a | # Iterates over every database module supported by dbm currently available, |
---|
19 | n/a | # setting dbm to use each in turn, and yielding that module |
---|
20 | n/a | # |
---|
21 | n/a | def dbm_iterator(): |
---|
22 | n/a | for name in dbm._names: |
---|
23 | n/a | try: |
---|
24 | n/a | mod = __import__(name, fromlist=['open']) |
---|
25 | n/a | except ImportError: |
---|
26 | n/a | continue |
---|
27 | n/a | dbm._modules[name] = mod |
---|
28 | n/a | yield mod |
---|
29 | n/a | |
---|
30 | n/a | # |
---|
31 | n/a | # Clean up all scratch databases we might have created during testing |
---|
32 | n/a | # |
---|
33 | n/a | def delete_files(): |
---|
34 | n/a | # we don't know the precise name the underlying database uses |
---|
35 | n/a | # so we use glob to locate all names |
---|
36 | n/a | for f in glob.glob(_fname + "*"): |
---|
37 | n/a | test.support.unlink(f) |
---|
38 | n/a | |
---|
39 | n/a | |
---|
40 | n/a | class AnyDBMTestCase: |
---|
41 | n/a | _dict = {'0': b'', |
---|
42 | n/a | 'a': b'Python:', |
---|
43 | n/a | 'b': b'Programming', |
---|
44 | n/a | 'c': b'the', |
---|
45 | n/a | 'd': b'way', |
---|
46 | n/a | 'f': b'Guido', |
---|
47 | n/a | 'g': b'intended', |
---|
48 | n/a | } |
---|
49 | n/a | |
---|
50 | n/a | def init_db(self): |
---|
51 | n/a | f = dbm.open(_fname, 'n') |
---|
52 | n/a | for k in self._dict: |
---|
53 | n/a | f[k.encode("ascii")] = self._dict[k] |
---|
54 | n/a | f.close() |
---|
55 | n/a | |
---|
56 | n/a | def keys_helper(self, f): |
---|
57 | n/a | keys = sorted(k.decode("ascii") for k in f.keys()) |
---|
58 | n/a | dkeys = sorted(self._dict.keys()) |
---|
59 | n/a | self.assertEqual(keys, dkeys) |
---|
60 | n/a | return keys |
---|
61 | n/a | |
---|
62 | n/a | def test_error(self): |
---|
63 | n/a | self.assertTrue(issubclass(self.module.error, OSError)) |
---|
64 | n/a | |
---|
65 | n/a | def test_anydbm_not_existing(self): |
---|
66 | n/a | self.assertRaises(dbm.error, dbm.open, _fname) |
---|
67 | n/a | |
---|
68 | n/a | def test_anydbm_creation(self): |
---|
69 | n/a | f = dbm.open(_fname, 'c') |
---|
70 | n/a | self.assertEqual(list(f.keys()), []) |
---|
71 | n/a | for key in self._dict: |
---|
72 | n/a | f[key.encode("ascii")] = self._dict[key] |
---|
73 | n/a | self.read_helper(f) |
---|
74 | n/a | f.close() |
---|
75 | n/a | |
---|
76 | n/a | def test_anydbm_creation_n_file_exists_with_invalid_contents(self): |
---|
77 | n/a | # create an empty file |
---|
78 | n/a | test.support.create_empty_file(_fname) |
---|
79 | n/a | |
---|
80 | n/a | f = dbm.open(_fname, 'n') |
---|
81 | n/a | self.addCleanup(f.close) |
---|
82 | n/a | self.assertEqual(len(f), 0) |
---|
83 | n/a | |
---|
84 | n/a | def test_anydbm_modification(self): |
---|
85 | n/a | self.init_db() |
---|
86 | n/a | f = dbm.open(_fname, 'c') |
---|
87 | n/a | self._dict['g'] = f[b'g'] = b"indented" |
---|
88 | n/a | self.read_helper(f) |
---|
89 | n/a | f.close() |
---|
90 | n/a | |
---|
91 | n/a | def test_anydbm_read(self): |
---|
92 | n/a | self.init_db() |
---|
93 | n/a | f = dbm.open(_fname, 'r') |
---|
94 | n/a | self.read_helper(f) |
---|
95 | n/a | f.close() |
---|
96 | n/a | |
---|
97 | n/a | def test_anydbm_keys(self): |
---|
98 | n/a | self.init_db() |
---|
99 | n/a | f = dbm.open(_fname, 'r') |
---|
100 | n/a | keys = self.keys_helper(f) |
---|
101 | n/a | f.close() |
---|
102 | n/a | |
---|
103 | n/a | def test_anydbm_access(self): |
---|
104 | n/a | self.init_db() |
---|
105 | n/a | f = dbm.open(_fname, 'r') |
---|
106 | n/a | key = "a".encode("ascii") |
---|
107 | n/a | self.assertIn(key, f) |
---|
108 | n/a | assert(f[key] == b"Python:") |
---|
109 | n/a | f.close() |
---|
110 | n/a | |
---|
111 | n/a | def read_helper(self, f): |
---|
112 | n/a | keys = self.keys_helper(f) |
---|
113 | n/a | for key in self._dict: |
---|
114 | n/a | self.assertEqual(self._dict[key], f[key.encode("ascii")]) |
---|
115 | n/a | |
---|
116 | n/a | def tearDown(self): |
---|
117 | n/a | delete_files() |
---|
118 | n/a | |
---|
119 | n/a | def setUp(self): |
---|
120 | n/a | dbm._defaultmod = self.module |
---|
121 | n/a | delete_files() |
---|
122 | n/a | |
---|
123 | n/a | |
---|
124 | n/a | class WhichDBTestCase(unittest.TestCase): |
---|
125 | n/a | def test_whichdb(self): |
---|
126 | n/a | for module in dbm_iterator(): |
---|
127 | n/a | # Check whether whichdb correctly guesses module name |
---|
128 | n/a | # for databases opened with "module" module. |
---|
129 | n/a | # Try with empty files first |
---|
130 | n/a | name = module.__name__ |
---|
131 | n/a | if name == 'dbm.dumb': |
---|
132 | n/a | continue # whichdb can't support dbm.dumb |
---|
133 | n/a | delete_files() |
---|
134 | n/a | f = module.open(_fname, 'c') |
---|
135 | n/a | f.close() |
---|
136 | n/a | self.assertEqual(name, self.dbm.whichdb(_fname)) |
---|
137 | n/a | # Now add a key |
---|
138 | n/a | f = module.open(_fname, 'w') |
---|
139 | n/a | f[b"1"] = b"1" |
---|
140 | n/a | # and test that we can find it |
---|
141 | n/a | self.assertIn(b"1", f) |
---|
142 | n/a | # and read it |
---|
143 | n/a | self.assertTrue(f[b"1"] == b"1") |
---|
144 | n/a | f.close() |
---|
145 | n/a | self.assertEqual(name, self.dbm.whichdb(_fname)) |
---|
146 | n/a | |
---|
147 | n/a | @unittest.skipUnless(ndbm, reason='Test requires ndbm') |
---|
148 | n/a | def test_whichdb_ndbm(self): |
---|
149 | n/a | # Issue 17198: check that ndbm which is referenced in whichdb is defined |
---|
150 | n/a | db_file = '{}_ndbm.db'.format(_fname) |
---|
151 | n/a | with open(db_file, 'w'): |
---|
152 | n/a | self.addCleanup(test.support.unlink, db_file) |
---|
153 | n/a | self.assertIsNone(self.dbm.whichdb(db_file[:-3])) |
---|
154 | n/a | |
---|
155 | n/a | def tearDown(self): |
---|
156 | n/a | delete_files() |
---|
157 | n/a | |
---|
158 | n/a | def setUp(self): |
---|
159 | n/a | delete_files() |
---|
160 | n/a | self.filename = test.support.TESTFN |
---|
161 | n/a | self.d = dbm.open(self.filename, 'c') |
---|
162 | n/a | self.d.close() |
---|
163 | n/a | self.dbm = test.support.import_fresh_module('dbm') |
---|
164 | n/a | |
---|
165 | n/a | def test_keys(self): |
---|
166 | n/a | self.d = dbm.open(self.filename, 'c') |
---|
167 | n/a | self.assertEqual(self.d.keys(), []) |
---|
168 | n/a | a = [(b'a', b'b'), (b'12345678910', b'019237410982340912840198242')] |
---|
169 | n/a | for k, v in a: |
---|
170 | n/a | self.d[k] = v |
---|
171 | n/a | self.assertEqual(sorted(self.d.keys()), sorted(k for (k, v) in a)) |
---|
172 | n/a | for k, v in a: |
---|
173 | n/a | self.assertIn(k, self.d) |
---|
174 | n/a | self.assertEqual(self.d[k], v) |
---|
175 | n/a | self.assertNotIn(b'xxx', self.d) |
---|
176 | n/a | self.assertRaises(KeyError, lambda: self.d[b'xxx']) |
---|
177 | n/a | self.d.close() |
---|
178 | n/a | |
---|
179 | n/a | |
---|
180 | n/a | def load_tests(loader, tests, pattern): |
---|
181 | n/a | classes = [] |
---|
182 | n/a | for mod in dbm_iterator(): |
---|
183 | n/a | classes.append(type("TestCase-" + mod.__name__, |
---|
184 | n/a | (AnyDBMTestCase, unittest.TestCase), |
---|
185 | n/a | {'module': mod})) |
---|
186 | n/a | suites = [unittest.makeSuite(c) for c in classes] |
---|
187 | n/a | |
---|
188 | n/a | tests.addTests(suites) |
---|
189 | n/a | return tests |
---|
190 | n/a | |
---|
191 | n/a | if __name__ == "__main__": |
---|
192 | n/a | unittest.main() |
---|