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

Python code coverage for Lib/test/test_dbm.py

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