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

Python code coverage for Lib/test/test_dbm_gnu.py

#countcontent
1n/afrom test import support
2n/agdbm = support.import_module("dbm.gnu") #skip if not supported
3n/aimport unittest
4n/aimport os
5n/afrom test.support import TESTFN, unlink
6n/a
7n/a
8n/afilename = TESTFN
9n/a
10n/aclass TestGdbm(unittest.TestCase):
11n/a def setUp(self):
12n/a self.g = None
13n/a
14n/a def tearDown(self):
15n/a if self.g is not None:
16n/a self.g.close()
17n/a unlink(filename)
18n/a
19n/a def test_key_methods(self):
20n/a self.g = gdbm.open(filename, 'c')
21n/a self.assertEqual(self.g.keys(), [])
22n/a self.g['a'] = 'b'
23n/a self.g['12345678910'] = '019237410982340912840198242'
24n/a self.g[b'bytes'] = b'data'
25n/a key_set = set(self.g.keys())
26n/a self.assertEqual(key_set, set([b'a', b'bytes', b'12345678910']))
27n/a self.assertIn('a', self.g)
28n/a self.assertIn(b'a', self.g)
29n/a self.assertEqual(self.g[b'bytes'], b'data')
30n/a key = self.g.firstkey()
31n/a while key:
32n/a self.assertIn(key, key_set)
33n/a key_set.remove(key)
34n/a key = self.g.nextkey(key)
35n/a self.assertRaises(KeyError, lambda: self.g['xxx'])
36n/a # get() and setdefault() work as in the dict interface
37n/a self.assertEqual(self.g.get(b'xxx', b'foo'), b'foo')
38n/a self.assertEqual(self.g.setdefault(b'xxx', b'foo'), b'foo')
39n/a self.assertEqual(self.g[b'xxx'], b'foo')
40n/a
41n/a def test_error_conditions(self):
42n/a # Try to open a non-existent database.
43n/a unlink(filename)
44n/a self.assertRaises(gdbm.error, gdbm.open, filename, 'r')
45n/a # Try to access a closed database.
46n/a self.g = gdbm.open(filename, 'c')
47n/a self.g.close()
48n/a self.assertRaises(gdbm.error, lambda: self.g['a'])
49n/a # try pass an invalid open flag
50n/a self.assertRaises(gdbm.error, lambda: gdbm.open(filename, 'rx').close())
51n/a
52n/a def test_flags(self):
53n/a # Test the flag parameter open() by trying all supported flag modes.
54n/a all = set(gdbm.open_flags)
55n/a # Test standard flags (presumably "crwn").
56n/a modes = all - set('fsu')
57n/a for mode in sorted(modes): # put "c" mode first
58n/a self.g = gdbm.open(filename, mode)
59n/a self.g.close()
60n/a
61n/a # Test additional flags (presumably "fsu").
62n/a flags = all - set('crwn')
63n/a for mode in modes:
64n/a for flag in flags:
65n/a self.g = gdbm.open(filename, mode + flag)
66n/a self.g.close()
67n/a
68n/a def test_reorganize(self):
69n/a self.g = gdbm.open(filename, 'c')
70n/a size0 = os.path.getsize(filename)
71n/a
72n/a self.g['x'] = 'x' * 10000
73n/a size1 = os.path.getsize(filename)
74n/a self.assertTrue(size0 < size1)
75n/a
76n/a del self.g['x']
77n/a # 'size' is supposed to be the same even after deleting an entry.
78n/a self.assertEqual(os.path.getsize(filename), size1)
79n/a
80n/a self.g.reorganize()
81n/a size2 = os.path.getsize(filename)
82n/a self.assertTrue(size1 > size2 >= size0)
83n/a
84n/a def test_context_manager(self):
85n/a with gdbm.open(filename, 'c') as db:
86n/a db["gdbm context manager"] = "context manager"
87n/a
88n/a with gdbm.open(filename, 'r') as db:
89n/a self.assertEqual(list(db.keys()), [b"gdbm context manager"])
90n/a
91n/a with self.assertRaises(gdbm.error) as cm:
92n/a db.keys()
93n/a self.assertEqual(str(cm.exception),
94n/a "GDBM object has already been closed")
95n/a
96n/aif __name__ == '__main__':
97n/a unittest.main()