Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 1 | import dbm.gnu as gdbm |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2 | import unittest |
| 3 | import os |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test.support import verbose, TESTFN, run_unittest, unlink |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 5 | |
Roger E. Masse | fab8ab8 | 1996-12-20 22:36:52 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | f35ad33 | 2006-08-25 23:40:32 +0000 | [diff] [blame] | 7 | filename = TESTFN |
Roger E. Masse | 62a017c | 1996-12-17 19:54:27 +0000 | [diff] [blame] | 8 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9 | class TestGdbm(unittest.TestCase): |
| 10 | def setUp(self): |
| 11 | self.g = None |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 12 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 13 | def tearDown(self): |
| 14 | if self.g is not None: |
| 15 | self.g.close() |
| 16 | unlink(filename) |
Roger E. Masse | 62a017c | 1996-12-17 19:54:27 +0000 | [diff] [blame] | 17 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 18 | def test_key_methods(self): |
| 19 | self.g = gdbm.open(filename, 'c') |
| 20 | self.assertEqual(self.g.keys(), []) |
| 21 | self.g['a'] = 'b' |
| 22 | self.g['12345678910'] = '019237410982340912840198242' |
| 23 | key_set = set(self.g.keys()) |
| 24 | self.assertEqual(key_set, set([b'a', b'12345678910'])) |
| 25 | self.assert_(b'a' in self.g) |
| 26 | key = self.g.firstkey() |
| 27 | while key: |
| 28 | self.assert_(key in key_set) |
| 29 | key_set.remove(key) |
| 30 | key = self.g.nextkey(key) |
| 31 | self.assertRaises(KeyError, lambda: self.g['xxx']) |
| 32 | |
| 33 | def test_error_conditions(self): |
| 34 | # Try to open a non-existent database. |
| 35 | unlink(filename) |
| 36 | self.assertRaises(gdbm.error, gdbm.open, filename, 'r') |
| 37 | # Try to access a closed database. |
| 38 | self.g = gdbm.open(filename, 'c') |
| 39 | self.g.close() |
| 40 | self.assertRaises(gdbm.error, lambda: self.g['a']) |
| 41 | # try pass an invalid open flag |
| 42 | self.assertRaises(gdbm.error, lambda: gdbm.open(filename, 'rx').close()) |
| 43 | |
| 44 | def test_flags(self): |
| 45 | # Test the flag parameter open() by trying all supported flag modes. |
| 46 | all = set(gdbm.open_flags) |
| 47 | # Test standard flags (presumably "crwn"). |
| 48 | modes = all - set('fsu') |
| 49 | for mode in modes: |
| 50 | self.g = gdbm.open(filename, mode) |
| 51 | self.g.close() |
| 52 | |
| 53 | # Test additional flags (presumably "fsu"). |
| 54 | flags = all - set('crwn') |
| 55 | for mode in modes: |
| 56 | for flag in flags: |
| 57 | self.g = gdbm.open(filename, mode + flag) |
| 58 | self.g.close() |
| 59 | |
| 60 | def test_reorganize(self): |
| 61 | self.g = gdbm.open(filename, 'c') |
| 62 | size0 = os.path.getsize(filename) |
| 63 | |
| 64 | self.g['x'] = 'x' * 10000 |
| 65 | size1 = os.path.getsize(filename) |
| 66 | self.assert_(size0 < size1) |
| 67 | |
| 68 | del self.g['x'] |
| 69 | # 'size' is supposed to be the same even after deleting an entry. |
| 70 | self.assertEqual(os.path.getsize(filename), size1) |
| 71 | |
| 72 | self.g.reorganize() |
| 73 | size2 = os.path.getsize(filename) |
| 74 | self.assert_(size1 > size2 >= size0) |
| 75 | |
| 76 | |
| 77 | def test_main(): |
| 78 | run_unittest(TestGdbm) |
| 79 | |
| 80 | if __name__ == '__main__': |
| 81 | test_main() |