1 | n/a | """Test script for the dumbdbm module |
---|
2 | n/a | Original by Roger E. Masse |
---|
3 | n/a | """ |
---|
4 | n/a | |
---|
5 | n/a | import io |
---|
6 | n/a | import operator |
---|
7 | n/a | import os |
---|
8 | n/a | import stat |
---|
9 | n/a | import unittest |
---|
10 | n/a | import dbm.dumb as dumbdbm |
---|
11 | n/a | from test import support |
---|
12 | n/a | from functools import partial |
---|
13 | n/a | |
---|
14 | n/a | _fname = support.TESTFN |
---|
15 | n/a | |
---|
16 | n/a | def _delete_files(): |
---|
17 | n/a | for ext in [".dir", ".dat", ".bak"]: |
---|
18 | n/a | try: |
---|
19 | n/a | os.unlink(_fname + ext) |
---|
20 | n/a | except OSError: |
---|
21 | n/a | pass |
---|
22 | n/a | |
---|
23 | n/a | class DumbDBMTestCase(unittest.TestCase): |
---|
24 | n/a | _dict = {b'0': b'', |
---|
25 | n/a | b'a': b'Python:', |
---|
26 | n/a | b'b': b'Programming', |
---|
27 | n/a | b'c': b'the', |
---|
28 | n/a | b'd': b'way', |
---|
29 | n/a | b'f': b'Guido', |
---|
30 | n/a | b'g': b'intended', |
---|
31 | n/a | '\u00fc'.encode('utf-8') : b'!', |
---|
32 | n/a | } |
---|
33 | n/a | |
---|
34 | n/a | def test_dumbdbm_creation(self): |
---|
35 | n/a | f = dumbdbm.open(_fname, 'c') |
---|
36 | n/a | self.assertEqual(list(f.keys()), []) |
---|
37 | n/a | for key in self._dict: |
---|
38 | n/a | f[key] = self._dict[key] |
---|
39 | n/a | self.read_helper(f) |
---|
40 | n/a | f.close() |
---|
41 | n/a | |
---|
42 | n/a | @unittest.skipUnless(hasattr(os, 'umask'), 'test needs os.umask()') |
---|
43 | n/a | @unittest.skipUnless(hasattr(os, 'chmod'), 'test needs os.chmod()') |
---|
44 | n/a | def test_dumbdbm_creation_mode(self): |
---|
45 | n/a | try: |
---|
46 | n/a | old_umask = os.umask(0o002) |
---|
47 | n/a | f = dumbdbm.open(_fname, 'c', 0o637) |
---|
48 | n/a | f.close() |
---|
49 | n/a | finally: |
---|
50 | n/a | os.umask(old_umask) |
---|
51 | n/a | |
---|
52 | n/a | expected_mode = 0o635 |
---|
53 | n/a | if os.name != 'posix': |
---|
54 | n/a | # Windows only supports setting the read-only attribute. |
---|
55 | n/a | # This shouldn't fail, but doesn't work like Unix either. |
---|
56 | n/a | expected_mode = 0o666 |
---|
57 | n/a | |
---|
58 | n/a | import stat |
---|
59 | n/a | st = os.stat(_fname + '.dat') |
---|
60 | n/a | self.assertEqual(stat.S_IMODE(st.st_mode), expected_mode) |
---|
61 | n/a | st = os.stat(_fname + '.dir') |
---|
62 | n/a | self.assertEqual(stat.S_IMODE(st.st_mode), expected_mode) |
---|
63 | n/a | |
---|
64 | n/a | def test_close_twice(self): |
---|
65 | n/a | f = dumbdbm.open(_fname) |
---|
66 | n/a | f[b'a'] = b'b' |
---|
67 | n/a | self.assertEqual(f[b'a'], b'b') |
---|
68 | n/a | f.close() |
---|
69 | n/a | f.close() |
---|
70 | n/a | |
---|
71 | n/a | def test_dumbdbm_modification(self): |
---|
72 | n/a | self.init_db() |
---|
73 | n/a | f = dumbdbm.open(_fname, 'w') |
---|
74 | n/a | self._dict[b'g'] = f[b'g'] = b"indented" |
---|
75 | n/a | self.read_helper(f) |
---|
76 | n/a | f.close() |
---|
77 | n/a | |
---|
78 | n/a | def test_dumbdbm_read(self): |
---|
79 | n/a | self.init_db() |
---|
80 | n/a | f = dumbdbm.open(_fname, 'r') |
---|
81 | n/a | self.read_helper(f) |
---|
82 | n/a | with self.assertWarnsRegex(DeprecationWarning, |
---|
83 | n/a | 'The database is opened for reading only'): |
---|
84 | n/a | f[b'g'] = b'x' |
---|
85 | n/a | with self.assertWarnsRegex(DeprecationWarning, |
---|
86 | n/a | 'The database is opened for reading only'): |
---|
87 | n/a | del f[b'a'] |
---|
88 | n/a | f.close() |
---|
89 | n/a | |
---|
90 | n/a | def test_dumbdbm_keys(self): |
---|
91 | n/a | self.init_db() |
---|
92 | n/a | f = dumbdbm.open(_fname) |
---|
93 | n/a | keys = self.keys_helper(f) |
---|
94 | n/a | f.close() |
---|
95 | n/a | |
---|
96 | n/a | def test_write_contains(self): |
---|
97 | n/a | f = dumbdbm.open(_fname) |
---|
98 | n/a | f[b'1'] = b'hello' |
---|
99 | n/a | self.assertIn(b'1', f) |
---|
100 | n/a | f.close() |
---|
101 | n/a | |
---|
102 | n/a | def test_write_write_read(self): |
---|
103 | n/a | # test for bug #482460 |
---|
104 | n/a | f = dumbdbm.open(_fname) |
---|
105 | n/a | f[b'1'] = b'hello' |
---|
106 | n/a | f[b'1'] = b'hello2' |
---|
107 | n/a | f.close() |
---|
108 | n/a | f = dumbdbm.open(_fname) |
---|
109 | n/a | self.assertEqual(f[b'1'], b'hello2') |
---|
110 | n/a | f.close() |
---|
111 | n/a | |
---|
112 | n/a | def test_str_read(self): |
---|
113 | n/a | self.init_db() |
---|
114 | n/a | f = dumbdbm.open(_fname, 'r') |
---|
115 | n/a | self.assertEqual(f['\u00fc'], self._dict['\u00fc'.encode('utf-8')]) |
---|
116 | n/a | |
---|
117 | n/a | def test_str_write_contains(self): |
---|
118 | n/a | self.init_db() |
---|
119 | n/a | f = dumbdbm.open(_fname) |
---|
120 | n/a | f['\u00fc'] = b'!' |
---|
121 | n/a | f['1'] = 'a' |
---|
122 | n/a | f.close() |
---|
123 | n/a | f = dumbdbm.open(_fname, 'r') |
---|
124 | n/a | self.assertIn('\u00fc', f) |
---|
125 | n/a | self.assertEqual(f['\u00fc'.encode('utf-8')], |
---|
126 | n/a | self._dict['\u00fc'.encode('utf-8')]) |
---|
127 | n/a | self.assertEqual(f[b'1'], b'a') |
---|
128 | n/a | |
---|
129 | n/a | def test_line_endings(self): |
---|
130 | n/a | # test for bug #1172763: dumbdbm would die if the line endings |
---|
131 | n/a | # weren't what was expected. |
---|
132 | n/a | f = dumbdbm.open(_fname) |
---|
133 | n/a | f[b'1'] = b'hello' |
---|
134 | n/a | f[b'2'] = b'hello2' |
---|
135 | n/a | f.close() |
---|
136 | n/a | |
---|
137 | n/a | # Mangle the file by changing the line separator to Windows or Unix |
---|
138 | n/a | with io.open(_fname + '.dir', 'rb') as file: |
---|
139 | n/a | data = file.read() |
---|
140 | n/a | if os.linesep == '\n': |
---|
141 | n/a | data = data.replace(b'\n', b'\r\n') |
---|
142 | n/a | else: |
---|
143 | n/a | data = data.replace(b'\r\n', b'\n') |
---|
144 | n/a | with io.open(_fname + '.dir', 'wb') as file: |
---|
145 | n/a | file.write(data) |
---|
146 | n/a | |
---|
147 | n/a | f = dumbdbm.open(_fname) |
---|
148 | n/a | self.assertEqual(f[b'1'], b'hello') |
---|
149 | n/a | self.assertEqual(f[b'2'], b'hello2') |
---|
150 | n/a | |
---|
151 | n/a | |
---|
152 | n/a | def read_helper(self, f): |
---|
153 | n/a | keys = self.keys_helper(f) |
---|
154 | n/a | for key in self._dict: |
---|
155 | n/a | self.assertEqual(self._dict[key], f[key]) |
---|
156 | n/a | |
---|
157 | n/a | def init_db(self): |
---|
158 | n/a | f = dumbdbm.open(_fname, 'n') |
---|
159 | n/a | for k in self._dict: |
---|
160 | n/a | f[k] = self._dict[k] |
---|
161 | n/a | f.close() |
---|
162 | n/a | |
---|
163 | n/a | def keys_helper(self, f): |
---|
164 | n/a | keys = sorted(f.keys()) |
---|
165 | n/a | dkeys = sorted(self._dict.keys()) |
---|
166 | n/a | self.assertEqual(keys, dkeys) |
---|
167 | n/a | return keys |
---|
168 | n/a | |
---|
169 | n/a | # Perform randomized operations. This doesn't make assumptions about |
---|
170 | n/a | # what *might* fail. |
---|
171 | n/a | def test_random(self): |
---|
172 | n/a | import random |
---|
173 | n/a | d = {} # mirror the database |
---|
174 | n/a | for dummy in range(5): |
---|
175 | n/a | f = dumbdbm.open(_fname) |
---|
176 | n/a | for dummy in range(100): |
---|
177 | n/a | k = random.choice('abcdefghijklm') |
---|
178 | n/a | if random.random() < 0.2: |
---|
179 | n/a | if k in d: |
---|
180 | n/a | del d[k] |
---|
181 | n/a | del f[k] |
---|
182 | n/a | else: |
---|
183 | n/a | v = random.choice((b'a', b'b', b'c')) * random.randrange(10000) |
---|
184 | n/a | d[k] = v |
---|
185 | n/a | f[k] = v |
---|
186 | n/a | self.assertEqual(f[k], v) |
---|
187 | n/a | f.close() |
---|
188 | n/a | |
---|
189 | n/a | f = dumbdbm.open(_fname) |
---|
190 | n/a | expected = sorted((k.encode("latin-1"), v) for k, v in d.items()) |
---|
191 | n/a | got = sorted(f.items()) |
---|
192 | n/a | self.assertEqual(expected, got) |
---|
193 | n/a | f.close() |
---|
194 | n/a | |
---|
195 | n/a | def test_context_manager(self): |
---|
196 | n/a | with dumbdbm.open(_fname, 'c') as db: |
---|
197 | n/a | db["dumbdbm context manager"] = "context manager" |
---|
198 | n/a | |
---|
199 | n/a | with dumbdbm.open(_fname, 'r') as db: |
---|
200 | n/a | self.assertEqual(list(db.keys()), [b"dumbdbm context manager"]) |
---|
201 | n/a | |
---|
202 | n/a | with self.assertRaises(dumbdbm.error): |
---|
203 | n/a | db.keys() |
---|
204 | n/a | |
---|
205 | n/a | def test_check_closed(self): |
---|
206 | n/a | f = dumbdbm.open(_fname, 'c') |
---|
207 | n/a | f.close() |
---|
208 | n/a | |
---|
209 | n/a | for meth in (partial(operator.delitem, f), |
---|
210 | n/a | partial(operator.setitem, f, 'b'), |
---|
211 | n/a | partial(operator.getitem, f), |
---|
212 | n/a | partial(operator.contains, f)): |
---|
213 | n/a | with self.assertRaises(dumbdbm.error) as cm: |
---|
214 | n/a | meth('test') |
---|
215 | n/a | self.assertEqual(str(cm.exception), |
---|
216 | n/a | "DBM object has already been closed") |
---|
217 | n/a | |
---|
218 | n/a | for meth in (operator.methodcaller('keys'), |
---|
219 | n/a | operator.methodcaller('iterkeys'), |
---|
220 | n/a | operator.methodcaller('items'), |
---|
221 | n/a | len): |
---|
222 | n/a | with self.assertRaises(dumbdbm.error) as cm: |
---|
223 | n/a | meth(f) |
---|
224 | n/a | self.assertEqual(str(cm.exception), |
---|
225 | n/a | "DBM object has already been closed") |
---|
226 | n/a | |
---|
227 | n/a | def test_create_new(self): |
---|
228 | n/a | with dumbdbm.open(_fname, 'n') as f: |
---|
229 | n/a | for k in self._dict: |
---|
230 | n/a | f[k] = self._dict[k] |
---|
231 | n/a | |
---|
232 | n/a | with dumbdbm.open(_fname, 'n') as f: |
---|
233 | n/a | self.assertEqual(f.keys(), []) |
---|
234 | n/a | |
---|
235 | n/a | def test_eval(self): |
---|
236 | n/a | with open(_fname + '.dir', 'w') as stream: |
---|
237 | n/a | stream.write("str(print('Hacked!')), 0\n") |
---|
238 | n/a | with support.captured_stdout() as stdout: |
---|
239 | n/a | with self.assertRaises(ValueError): |
---|
240 | n/a | with dumbdbm.open(_fname) as f: |
---|
241 | n/a | pass |
---|
242 | n/a | self.assertEqual(stdout.getvalue(), '') |
---|
243 | n/a | |
---|
244 | n/a | def test_warn_on_ignored_flags(self): |
---|
245 | n/a | for value in ('r', 'w'): |
---|
246 | n/a | _delete_files() |
---|
247 | n/a | with self.assertWarnsRegex(DeprecationWarning, |
---|
248 | n/a | "The database file is missing, the " |
---|
249 | n/a | "semantics of the 'c' flag will " |
---|
250 | n/a | "be used."): |
---|
251 | n/a | f = dumbdbm.open(_fname, value) |
---|
252 | n/a | f.close() |
---|
253 | n/a | |
---|
254 | n/a | def test_missing_index(self): |
---|
255 | n/a | with dumbdbm.open(_fname, 'n') as f: |
---|
256 | n/a | pass |
---|
257 | n/a | os.unlink(_fname + '.dir') |
---|
258 | n/a | for value in ('r', 'w'): |
---|
259 | n/a | with self.assertWarnsRegex(DeprecationWarning, |
---|
260 | n/a | "The index file is missing, the " |
---|
261 | n/a | "semantics of the 'c' flag will " |
---|
262 | n/a | "be used."): |
---|
263 | n/a | f = dumbdbm.open(_fname, value) |
---|
264 | n/a | f.close() |
---|
265 | n/a | self.assertEqual(os.path.exists(_fname + '.dir'), value == 'w') |
---|
266 | n/a | self.assertFalse(os.path.exists(_fname + '.bak')) |
---|
267 | n/a | |
---|
268 | n/a | def test_invalid_flag(self): |
---|
269 | n/a | for flag in ('x', 'rf', None): |
---|
270 | n/a | with self.assertWarnsRegex(DeprecationWarning, |
---|
271 | n/a | "Flag must be one of " |
---|
272 | n/a | "'r', 'w', 'c', or 'n'"): |
---|
273 | n/a | f = dumbdbm.open(_fname, flag) |
---|
274 | n/a | f.close() |
---|
275 | n/a | |
---|
276 | n/a | @unittest.skipUnless(hasattr(os, 'chmod'), 'test needs os.chmod()') |
---|
277 | n/a | def test_readonly_files(self): |
---|
278 | n/a | with support.temp_dir() as dir: |
---|
279 | n/a | fname = os.path.join(dir, 'db') |
---|
280 | n/a | with dumbdbm.open(fname, 'n') as f: |
---|
281 | n/a | self.assertEqual(list(f.keys()), []) |
---|
282 | n/a | for key in self._dict: |
---|
283 | n/a | f[key] = self._dict[key] |
---|
284 | n/a | os.chmod(fname + ".dir", stat.S_IRUSR) |
---|
285 | n/a | os.chmod(fname + ".dat", stat.S_IRUSR) |
---|
286 | n/a | os.chmod(dir, stat.S_IRUSR|stat.S_IXUSR) |
---|
287 | n/a | with dumbdbm.open(fname, 'r') as f: |
---|
288 | n/a | self.assertEqual(sorted(f.keys()), sorted(self._dict)) |
---|
289 | n/a | f.close() # don't write |
---|
290 | n/a | |
---|
291 | n/a | def tearDown(self): |
---|
292 | n/a | _delete_files() |
---|
293 | n/a | |
---|
294 | n/a | def setUp(self): |
---|
295 | n/a | _delete_files() |
---|
296 | n/a | |
---|
297 | n/a | |
---|
298 | n/a | if __name__ == "__main__": |
---|
299 | n/a | unittest.main() |
---|