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