blob: e5757c9501a5cdeaf13dccfb73859384bd550ad1 [file] [log] [blame]
Roger E. Masse16f9fd21996-12-10 00:06:24 +00001#! /usr/bin/env python
2"""Test script for the dbm module
3 Roger E. Masse
4"""
Neal Norwitze7629c82002-11-02 18:25:08 +00005import os
6import random
Roger E. Masse16f9fd21996-12-10 00:06:24 +00007import dbm
8from dbm import error
Neal Norwitze7629c82002-11-02 18:25:08 +00009from test.test_support import verbose, verify, TestSkipped
Roger E. Massefab8ab81996-12-20 22:36:52 +000010
Neal Norwitze7629c82002-11-02 18:25:08 +000011# make filename unique to allow multiple concurrent tests
12# and to minimize the likelihood of a problem from an old file
13filename = '/tmp/delete_me_' + str(random.random())[-6:]
Roger E. Masse16f9fd21996-12-10 00:06:24 +000014
Neal Norwitze7629c82002-11-02 18:25:08 +000015def cleanup():
16 for suffix in ['', '.pag', '.dir', '.db']:
17 try:
18 os.unlink(filename + suffix)
19 except OSError, (errno, strerror):
20 # if we can't delete the file because of permissions,
21 # nothing will work, so skip the test
22 if errno == 1:
23 raise TestSkipped, 'unable to remove: ' + filename + suffix
Fred Drake57a4e902000-09-18 17:56:58 +000024
Neal Norwitze7629c82002-11-02 18:25:08 +000025def test_keys():
26 d = dbm.open(filename, 'c')
27 verify(d.keys() == [])
28 d['a'] = 'b'
29 d['12345678910'] = '019237410982340912840198242'
30 d.keys()
31 if d.has_key('a'):
32 if verbose:
33 print 'Test dbm keys: ', d.keys()
Roger E. Masse16f9fd21996-12-10 00:06:24 +000034
Neal Norwitze7629c82002-11-02 18:25:08 +000035 d.close()
36
37def test_modes():
38 d = dbm.open(filename, 'r')
39 d.close()
40 d = dbm.open(filename, 'rw')
41 d.close()
42 d = dbm.open(filename, 'w')
43 d.close()
44 d = dbm.open(filename, 'n')
45 d.close()
46
47cleanup()
Roger E. Masse16f9fd21996-12-10 00:06:24 +000048try:
Neal Norwitze7629c82002-11-02 18:25:08 +000049 test_keys()
50 test_modes()
Roger E. Masse16f9fd21996-12-10 00:06:24 +000051except:
Neal Norwitze7629c82002-11-02 18:25:08 +000052 cleanup()
53 raise
54
55cleanup()