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