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 |
Ezio Melotti | f79493b | 2013-03-01 11:23:28 +0200 | [diff] [blame] | 5 | from test.support import verbose, TESTFN, 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'])) |
Serhiy Storchaka | 7d6392c | 2013-10-25 00:06:52 +0300 | [diff] [blame] | 27 | self.assertIn('a', self.g) |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 28 | self.assertIn(b'a', self.g) |
Brett Cannon | 7317c1e | 2008-11-25 19:19:17 +0000 | [diff] [blame] | 29 | self.assertEqual(self.g[b'bytes'], b'data') |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 30 | key = self.g.firstkey() |
| 31 | while key: |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 32 | self.assertIn(key, key_set) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 33 | key_set.remove(key) |
| 34 | key = self.g.nextkey(key) |
| 35 | self.assertRaises(KeyError, lambda: self.g['xxx']) |
Georg Brandl | d9e833c | 2010-12-04 09:14:36 +0000 | [diff] [blame] | 36 | # get() and setdefault() work as in the dict interface |
| 37 | self.assertEqual(self.g.get(b'xxx', b'foo'), b'foo') |
| 38 | self.assertEqual(self.g.setdefault(b'xxx', b'foo'), b'foo') |
| 39 | self.assertEqual(self.g[b'xxx'], b'foo') |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 40 | |
| 41 | def test_error_conditions(self): |
| 42 | # Try to open a non-existent database. |
| 43 | unlink(filename) |
| 44 | self.assertRaises(gdbm.error, gdbm.open, filename, 'r') |
| 45 | # Try to access a closed database. |
| 46 | self.g = gdbm.open(filename, 'c') |
| 47 | self.g.close() |
| 48 | self.assertRaises(gdbm.error, lambda: self.g['a']) |
| 49 | # try pass an invalid open flag |
| 50 | self.assertRaises(gdbm.error, lambda: gdbm.open(filename, 'rx').close()) |
| 51 | |
| 52 | def test_flags(self): |
| 53 | # Test the flag parameter open() by trying all supported flag modes. |
| 54 | all = set(gdbm.open_flags) |
| 55 | # Test standard flags (presumably "crwn"). |
| 56 | modes = all - set('fsu') |
Georg Brandl | 16684eb | 2012-02-20 22:48:06 +0100 | [diff] [blame] | 57 | for mode in sorted(modes): # put "c" mode first |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 58 | self.g = gdbm.open(filename, mode) |
| 59 | self.g.close() |
| 60 | |
| 61 | # Test additional flags (presumably "fsu"). |
| 62 | flags = all - set('crwn') |
| 63 | for mode in modes: |
| 64 | for flag in flags: |
| 65 | self.g = gdbm.open(filename, mode + flag) |
| 66 | self.g.close() |
| 67 | |
| 68 | def test_reorganize(self): |
| 69 | self.g = gdbm.open(filename, 'c') |
| 70 | size0 = os.path.getsize(filename) |
| 71 | |
| 72 | self.g['x'] = 'x' * 10000 |
| 73 | size1 = os.path.getsize(filename) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 74 | self.assertTrue(size0 < size1) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 75 | |
| 76 | del self.g['x'] |
| 77 | # 'size' is supposed to be the same even after deleting an entry. |
| 78 | self.assertEqual(os.path.getsize(filename), size1) |
| 79 | |
| 80 | self.g.reorganize() |
| 81 | size2 = os.path.getsize(filename) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 82 | self.assertTrue(size1 > size2 >= size0) |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 83 | |
| 84 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 85 | if __name__ == '__main__': |
Ezio Melotti | f79493b | 2013-03-01 11:23:28 +0200 | [diff] [blame] | 86 | unittest.main() |