1996-12-09 20:06:24 -04:00
|
|
|
#! /usr/bin/env python
|
|
|
|
"""Test script for the dbm module
|
|
|
|
Roger E. Masse
|
|
|
|
"""
|
2002-11-02 14:25:08 -04:00
|
|
|
import os
|
1996-12-09 20:06:24 -04:00
|
|
|
import dbm
|
|
|
|
from dbm import error
|
2007-02-25 18:12:31 -04:00
|
|
|
from test.test_support import verbose, verify, TestSkipped, TESTFN
|
1996-12-20 18:36:52 -04:00
|
|
|
|
2002-11-02 14:25:08 -04:00
|
|
|
# make filename unique to allow multiple concurrent tests
|
|
|
|
# and to minimize the likelihood of a problem from an old file
|
2007-02-25 18:12:31 -04:00
|
|
|
filename = TESTFN
|
1996-12-09 20:06:24 -04:00
|
|
|
|
2002-11-02 14:25:08 -04:00
|
|
|
def cleanup():
|
|
|
|
for suffix in ['', '.pag', '.dir', '.db']:
|
|
|
|
try:
|
|
|
|
os.unlink(filename + suffix)
|
|
|
|
except OSError, (errno, strerror):
|
|
|
|
# if we can't delete the file because of permissions,
|
|
|
|
# nothing will work, so skip the test
|
|
|
|
if errno == 1:
|
|
|
|
raise TestSkipped, 'unable to remove: ' + filename + suffix
|
2000-09-18 14:56:58 -03:00
|
|
|
|
2002-11-02 14:25:08 -04:00
|
|
|
def test_keys():
|
|
|
|
d = dbm.open(filename, 'c')
|
|
|
|
verify(d.keys() == [])
|
|
|
|
d['a'] = 'b'
|
|
|
|
d['12345678910'] = '019237410982340912840198242'
|
|
|
|
d.keys()
|
|
|
|
if d.has_key('a'):
|
|
|
|
if verbose:
|
|
|
|
print 'Test dbm keys: ', d.keys()
|
1996-12-09 20:06:24 -04:00
|
|
|
|
2002-11-02 14:25:08 -04:00
|
|
|
d.close()
|
|
|
|
|
|
|
|
def test_modes():
|
|
|
|
d = dbm.open(filename, 'r')
|
|
|
|
d.close()
|
|
|
|
d = dbm.open(filename, 'rw')
|
|
|
|
d.close()
|
|
|
|
d = dbm.open(filename, 'w')
|
|
|
|
d.close()
|
|
|
|
d = dbm.open(filename, 'n')
|
|
|
|
d.close()
|
|
|
|
|
2008-03-03 00:19:29 -04:00
|
|
|
def test_main():
|
2002-11-02 14:25:08 -04:00
|
|
|
cleanup()
|
2008-03-03 00:19:29 -04:00
|
|
|
try:
|
|
|
|
test_keys()
|
|
|
|
test_modes()
|
|
|
|
except:
|
|
|
|
cleanup()
|
|
|
|
raise
|
2002-11-02 14:25:08 -04:00
|
|
|
|
2008-03-03 00:19:29 -04:00
|
|
|
cleanup()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|