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

Python code coverage for Lib/test/test_dbm_ndbm.py

#countcontent
1n/afrom test import support
2n/asupport.import_module("dbm.ndbm") #skip if not supported
3n/aimport unittest
4n/aimport dbm.ndbm
5n/afrom dbm.ndbm import error
6n/a
7n/aclass DbmTestCase(unittest.TestCase):
8n/a
9n/a def setUp(self):
10n/a self.filename = support.TESTFN
11n/a self.d = dbm.ndbm.open(self.filename, 'c')
12n/a self.d.close()
13n/a
14n/a def tearDown(self):
15n/a for suffix in ['', '.pag', '.dir', '.db']:
16n/a support.unlink(self.filename + suffix)
17n/a
18n/a def test_keys(self):
19n/a self.d = dbm.ndbm.open(self.filename, 'c')
20n/a self.assertTrue(self.d.keys() == [])
21n/a self.d['a'] = 'b'
22n/a self.d[b'bytes'] = b'data'
23n/a self.d['12345678910'] = '019237410982340912840198242'
24n/a self.d.keys()
25n/a self.assertIn('a', self.d)
26n/a self.assertIn(b'a', self.d)
27n/a self.assertEqual(self.d[b'bytes'], b'data')
28n/a self.d.close()
29n/a
30n/a def test_modes(self):
31n/a for mode in ['r', 'rw', 'w', 'n']:
32n/a try:
33n/a self.d = dbm.ndbm.open(self.filename, mode)
34n/a self.d.close()
35n/a except error:
36n/a self.fail()
37n/a
38n/a def test_context_manager(self):
39n/a with dbm.ndbm.open(self.filename, 'c') as db:
40n/a db["ndbm context manager"] = "context manager"
41n/a
42n/a with dbm.ndbm.open(self.filename, 'r') as db:
43n/a self.assertEqual(list(db.keys()), [b"ndbm context manager"])
44n/a
45n/a with self.assertRaises(dbm.ndbm.error) as cm:
46n/a db.keys()
47n/a self.assertEqual(str(cm.exception),
48n/a "DBM object has already been closed")
49n/a
50n/a
51n/aif __name__ == '__main__':
52n/a unittest.main()