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