summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_gdbm.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_gdbm.py')
-rwxr-xr-xLib/test/test_gdbm.py119
1 files changed, 77 insertions, 42 deletions
diff --git a/Lib/test/test_gdbm.py b/Lib/test/test_gdbm.py
index ee68277..17a7f63 100755
--- a/Lib/test/test_gdbm.py
+++ b/Lib/test/test_gdbm.py
@@ -1,46 +1,81 @@
-#! /usr/bin/env python
-"""Test script for the gdbm module
- Roger E. Masse
-"""
-
import gdbm
-from gdbm import error
-from test.test_support import verbose, verify, TestFailed, TESTFN
+import unittest
+import os
+from test.test_support import verbose, TESTFN, run_unittest, unlink
+
filename = TESTFN
-g = gdbm.open(filename, 'c')
-verify(g.keys() == [])
-g[b'a'] = b'b'
-g[b'12345678910'] = b'019237410982340912840198242'
-a = g.keys()
-if verbose:
- print('Test gdbm file keys: ', a)
-
-b'a' in g
-g.close()
-try:
- g['a']
-except error:
- pass
-else:
- raise TestFailed("expected gdbm.error accessing closed database")
-g = gdbm.open(filename, 'r')
-g.close()
-g = gdbm.open(filename, 'w')
-g.close()
-g = gdbm.open(filename, 'n')
-g.close()
-try:
- g = gdbm.open(filename, 'rx')
- g.close()
-except error:
- pass
-else:
- raise TestFailed("expected gdbm.error when passing invalid open flags")
-
-try:
- import os
- os.unlink(filename)
-except:
- pass
+class TestGdbm(unittest.TestCase):
+ def setUp(self):
+ self.g = None
+
+ def tearDown(self):
+ if self.g is not None:
+ self.g.close()
+ unlink(filename)
+
+ def test_key_methods(self):
+ self.g = gdbm.open(filename, 'c')
+ self.assertEqual(self.g.keys(), [])
+ self.g['a'] = 'b'
+ self.g['12345678910'] = '019237410982340912840198242'
+ key_set = set(self.g.keys())
+ self.assertEqual(key_set, set([b'a', b'12345678910']))
+ self.assert_(b'a' in self.g)
+ key = self.g.firstkey()
+ while key:
+ self.assert_(key in key_set)
+ key_set.remove(key)
+ key = self.g.nextkey(key)
+ self.assertRaises(KeyError, lambda: self.g['xxx'])
+
+ def test_error_conditions(self):
+ # Try to open a non-existent database.
+ unlink(filename)
+ self.assertRaises(gdbm.error, gdbm.open, filename, 'r')
+ # Try to access a closed database.
+ self.g = gdbm.open(filename, 'c')
+ self.g.close()
+ self.assertRaises(gdbm.error, lambda: self.g['a'])
+ # try pass an invalid open flag
+ self.assertRaises(gdbm.error, lambda: gdbm.open(filename, 'rx').close())
+
+ def test_flags(self):
+ # Test the flag parameter open() by trying all supported flag modes.
+ all = set(gdbm.open_flags)
+ # Test standard flags (presumably "crwn").
+ modes = all - set('fsu')
+ for mode in modes:
+ self.g = gdbm.open(filename, mode)
+ self.g.close()
+
+ # Test additional flags (presumably "fsu").
+ flags = all - set('crwn')
+ for mode in modes:
+ for flag in flags:
+ self.g = gdbm.open(filename, mode + flag)
+ self.g.close()
+
+ def test_reorganize(self):
+ self.g = gdbm.open(filename, 'c')
+ size0 = os.path.getsize(filename)
+
+ self.g['x'] = 'x' * 10000
+ size1 = os.path.getsize(filename)
+ self.assert_(size0 < size1)
+
+ del self.g['x']
+ # 'size' is supposed to be the same even after deleting an entry.
+ self.assertEqual(os.path.getsize(filename), size1)
+
+ self.g.reorganize()
+ size2 = os.path.getsize(filename)
+ self.assert_(size1 > size2 >= size0)
+
+
+def test_main():
+ run_unittest(TestGdbm)
+
+if __name__ == '__main__':
+ test_main()