3 from test
.test_support
import TESTFN
, run_unittest
, unlink
, import_module
4 gdbm
= import_module('gdbm')
9 class TestGdbm(unittest
.TestCase
):
15 if self
.g
is not None:
19 def test_key_methods(self
):
20 self
.g
= gdbm
.open(filename
, 'c')
21 self
.assertEqual(self
.g
.keys(), [])
23 self
.g
['12345678910'] = '019237410982340912840198242'
24 key_set
= set(self
.g
.keys())
25 self
.assertEqual(key_set
, frozenset(['a', '12345678910']))
26 self
.assertTrue(self
.g
.has_key('a'))
27 key
= self
.g
.firstkey()
29 self
.assertIn(key
, key_set
)
31 key
= self
.g
.nextkey(key
)
32 self
.assertRaises(KeyError, lambda: self
.g
['xxx'])
34 def test_error_conditions(self
):
35 # Try to open a non-existent database.
37 self
.assertRaises(gdbm
.error
, gdbm
.open, filename
, 'r')
38 # Try to access a closed database.
39 self
.g
= gdbm
.open(filename
, 'c')
41 self
.assertRaises(gdbm
.error
, lambda: self
.g
['a'])
42 # try pass an invalid open flag
43 self
.assertRaises(gdbm
.error
, lambda: gdbm
.open(filename
, 'rx').close())
46 # Test the flag parameter open() by trying all supported flag modes.
47 all
= set(gdbm
.open_flags
)
48 # Test standard flags (presumably "crwn").
49 modes
= all
- set('fsu')
51 self
.g
= gdbm
.open(filename
, mode
)
54 # Test additional flags (presumably "fsu").
55 flags
= all
- set('crwn')
58 self
.g
= gdbm
.open(filename
, mode
+ flag
)
61 def test_reorganize(self
):
62 self
.g
= gdbm
.open(filename
, 'c')
63 size0
= os
.path
.getsize(filename
)
65 self
.g
['x'] = 'x' * 10000
66 size1
= os
.path
.getsize(filename
)
67 self
.assertTrue(size0
< size1
)
70 # 'size' is supposed to be the same even after deleting an entry.
71 self
.assertEqual(os
.path
.getsize(filename
), size1
)
74 size2
= os
.path
.getsize(filename
)
75 self
.assertTrue(size1
> size2
>= size0
)
79 run_unittest(TestGdbm
)
81 if __name__
== '__main__':