ยปCore Development>Code coverage>Lib/test/test_dbm_dumb.py

Python code coverage for Lib/test/test_dbm_dumb.py

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