Move test_gdbm to use unittest.

Closes issue #1960. Thanks Giampaolo Rodola.
This commit is contained in:
Brett Cannon 2008-03-13 21:02:16 +00:00
parent 2e0f9f3dd9
commit 4a6e8d669a
3 changed files with 80 additions and 40 deletions

View File

@ -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()

View File

@ -559,6 +559,7 @@ Mark Roberts
Andy Robinson
Jim Robinson
Kevin Rodgers
Giampaolo Rodola
Mike Romberg
Case Roole
Timothy Roscoe

View File

@ -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.