diff --git a/Lib/test/test_gdbm.py b/Lib/test/test_gdbm.py index 8e6f76f1702..bb49ffe2b20 100755 --- a/Lib/test/test_gdbm.py +++ b/Lib/test/test_gdbm.py @@ -1,46 +1,83 @@ -#! /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['a'] = 'b' -g['12345678910'] = '019237410982340912840198242' -a = g.keys() -if verbose: - print 'Test gdbm file keys: ', a +class TestGdbm(unittest.TestCase): -g.has_key('a') -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" + def setUp(self): + self.g = None -try: - import os - os.unlink(filename) -except: - pass + 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, frozenset(['a', '12345678910'])) + self.assert_(self.g.has_key('a')) + 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') + self.assertRaises(gdbm.error, gdbm.open, filename, 'w') + # 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() diff --git a/Misc/ACKS b/Misc/ACKS index c2cad5c1ecd..cce61522cf1 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -559,6 +559,7 @@ Mark Roberts Andy Robinson Jim Robinson Kevin Rodgers +Giampaolo Rodola Mike Romberg Case Roole Timothy Roscoe diff --git a/Misc/NEWS b/Misc/NEWS index 7743e0b7986..764b68e3298 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -50,6 +50,8 @@ Library Tests ----- +- Issue 1960: Convert test_gdbm to unittest. + - GHOP 294: Convert test_contains, test_crypt, and test_select to unittest. - GHOP 238: Convert test_tokenize to use doctest.